add status variable output to System() app (issue #5121)
[asterisk/asterisk.git] / apps / app_system.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Execute arbitrary system commands
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #include "asterisk.h"
21
22 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
23
24 #include "asterisk/lock.h"
25 #include "asterisk/file.h"
26 #include "asterisk/logger.h"
27 #include "asterisk/channel.h"
28 #include "asterisk/pbx.h"
29 #include "asterisk/module.h"
30 #include "asterisk/app.h"
31 #include "asterisk/options.h"
32
33 static char *tdesc = "Generic System() application";
34
35 static char *app = "System";
36
37 static char *app2 = "TrySystem";
38
39 static char *synopsis = "Execute a system command";
40
41 static char *synopsis2 = "Try executing a system command";
42
43 static char *chanvar = "SYSTEMSTATUS";
44
45 static char *descrip =
46 "  System(command): Executes a command  by  using  system(). Returns -1 on\n"
47 "failure to execute the specified command. \n"
48 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
49 "   FAILURE     Could not execute the specified command\n"
50 "   SUCCESS     Specified command successfully executed\n"
51 "\n"
52 "Old behaviour:\n"
53 "If  the command itself executes but is in error, and if there exists\n"
54 "a priority n + 101, where 'n' is the priority of the current instance,\n"
55 "then  the  channel  will  be  setup  to continue at that priority level.\n"
56 " Otherwise, System returns 0.\n";
57
58 static char *descrip2 =
59 "  TrySystem(command): Executes a command  by  using  system(). Returns 0\n"
60 "on any situation.\n"
61 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
62 "   FAILURE     Could not execute the specified command\n"
63 "   SUCCESS     Specified command successfully executed\n"
64 "   APPERROR    Specified command successfully executed, but returned error code\n"
65 "\n"
66 "Old behaviour:\nIf  the command itself executes but is in error, and if\n"
67 "there exists a priority n + 101, where 'n' is the priority of the current\n"
68 "instance, then  the  channel  will  be  setup  to continue at that\n"
69 "priority level.  Otherwise, System returns 0.\n";
70
71 STANDARD_LOCAL_USER;
72
73 LOCAL_USER_DECL;
74
75 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
76 {
77         int res=0;
78         struct localuser *u;
79         if (!data) {
80                 ast_log(LOG_WARNING, "System requires an argument(command)\n");
81                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
82                 return failmode;
83         }
84         LOCAL_USER_ADD(u);
85
86         /* Do our thing here */
87         res = ast_safe_system((char *)data);
88         if ((res < 0) && (errno != ECHILD)) {
89                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
90                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
91                 res = failmode;
92         } else if (res == 127) {
93                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
94                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
95                 res = failmode;
96         } else {
97                 if (res < 0) 
98                         res = 0;
99                 if (option_priority_jumping && res)
100                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
101
102                 if (res != 0)
103                         pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
104                 else
105                         pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
106                 res = 0;
107         } 
108
109         LOCAL_USER_REMOVE(u);
110         return res;
111 }
112
113 static int system_exec(struct ast_channel *chan, void *data)
114 {
115         return system_exec_helper(chan, data, -1);
116 }
117
118 static int trysystem_exec(struct ast_channel *chan, void *data)
119 {
120         return system_exec_helper(chan, data, 0);
121 }
122
123 int unload_module(void)
124 {
125         STANDARD_HANGUP_LOCALUSERS;
126         ast_unregister_application(app2);
127         return ast_unregister_application(app);
128 }
129
130 int load_module(void)
131 {
132         ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
133         return ast_register_application(app, system_exec, synopsis, descrip);
134 }
135
136 char *description(void)
137 {
138         return tdesc;
139 }
140
141 int usecount(void)
142 {
143         int res;
144         STANDARD_USECOUNT(res);
145         return res;
146 }
147
148 char *key()
149 {
150         return ASTERISK_GPL_KEY;
151 }