File: | fe-common/core/fe-exec.c |
Location: | line 254, column 2 |
Description: | Value stored to 'signum' is never read |
1 | /* |
2 | fe-exec.c : irssi |
3 | |
4 | Copyright (C) 2000-2001 Timo Sirainen |
5 | |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License along |
17 | with this program; if not, write to the Free Software Foundation, Inc., |
18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | */ |
20 | |
21 | #include "module.h" |
22 | #include "modules.h" |
23 | #include "signals.h" |
24 | #include "commands.h" |
25 | #include "pidwait.h" |
26 | #include "line-split.h" |
27 | #include "network.h" |
28 | #include "net-sendbuffer.h" |
29 | #include "misc.h" |
30 | #include "levels.h" |
31 | |
32 | #include "servers.h" |
33 | #include "channels.h" |
34 | #include "queries.h" |
35 | |
36 | #include "printtext.h" |
37 | #include "fe-exec.h" |
38 | #include "fe-windows.h" |
39 | #include "window-items.h" |
40 | |
41 | #include <signal.h> |
42 | #include <sys/wait.h> |
43 | |
44 | GSList *processes; |
45 | static int signal_exec_input; |
46 | |
47 | static void exec_wi_destroy(EXEC_WI_REC *rec) |
48 | { |
49 | g_return_if_fail(rec != NULL)do{ if (rec != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "rec != NULL"); return; } ; }while (0); |
50 | |
51 | if (rec->destroying) return; |
52 | rec->destroying = TRUE(!(0)); |
53 | |
54 | rec->process->target_item = NULL((void *)0); |
55 | if (window_item_window((WI_ITEM_REC *) rec)((WINDOW_REC *) ((WI_ITEM_REC *) ((WI_ITEM_REC *) rec))->window ) != NULL((void *)0)) |
56 | window_item_destroy((WI_ITEM_REC *) rec); |
57 | |
58 | MODULE_DATA_DEINIT(rec)g_hash_table_destroy((rec)->module_data); |
59 | g_free(rec->visible_name); |
60 | g_free(rec); |
61 | } |
62 | |
63 | static const char *exec_get_target(WI_ITEM_REC *item) |
64 | { |
65 | return ((EXEC_WI_REC *) item)->visible_name; |
66 | } |
67 | |
68 | static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec) |
69 | { |
70 | EXEC_WI_REC *item; |
71 | |
72 | g_return_val_if_fail(window != NULL, NULL)do{ if (window != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "window != NULL"); return (((void *)0)); }; }while (0); |
73 | g_return_val_if_fail(rec != NULL, NULL)do{ if (rec != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "rec != NULL"); return ( ((void *)0)); }; }while (0); |
74 | |
75 | item = g_new0(EXEC_WI_REC, 1)((EXEC_WI_REC *) g_malloc0 (((gsize) sizeof (EXEC_WI_REC)) * ( (gsize) (1)))); |
76 | item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC"); |
77 | item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy; |
78 | item->get_target = exec_get_target; |
79 | item->visible_name = rec->name != NULL((void *)0) ? g_strdup(rec->name) : |
80 | g_strdup_printf("%%%d", rec->id); |
81 | |
82 | item->createtime = time(NULL((void *)0)); |
83 | item->process = rec; |
84 | |
85 | MODULE_DATA_INIT(item)(item)->module_data = g_hash_table_new(g_str_hash, g_str_equal ); |
86 | window_item_add(window, (WI_ITEM_REC *) item, FALSE(0)); |
87 | return item; |
88 | } |
89 | |
90 | static int process_get_new_id(void) |
91 | { |
92 | PROCESS_REC *rec; |
93 | GSList *tmp; |
94 | int id; |
95 | |
96 | id = 0; |
97 | tmp = processes; |
98 | while (tmp != NULL((void *)0)) { |
99 | rec = tmp->data; |
100 | |
101 | if (id != rec->id) { |
102 | tmp = tmp->next; |
103 | continue; |
104 | } |
105 | |
106 | id++; |
107 | tmp = processes; |
108 | } |
109 | |
110 | return id; |
111 | } |
112 | |
113 | static PROCESS_REC *process_find_pid(int pid) |
114 | { |
115 | GSList *tmp; |
116 | |
117 | g_return_val_if_fail(pid > 0, NULL)do{ if (pid > 0) { } else { g_return_if_fail_warning (((gchar *) 0), __PRETTY_FUNCTION__, "pid > 0"); return (((void *)0 )); }; }while (0); |
118 | |
119 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
120 | PROCESS_REC *rec = tmp->data; |
121 | |
122 | if (rec->pid == pid) |
123 | return rec; |
124 | } |
125 | |
126 | return NULL((void *)0); |
127 | } |
128 | |
129 | static PROCESS_REC *process_find_id(int id, int verbose) |
130 | { |
131 | GSList *tmp; |
132 | |
133 | g_return_val_if_fail(id != -1, NULL)do{ if (id != -1) { } else { g_return_if_fail_warning (((gchar *) 0), __PRETTY_FUNCTION__, "id != -1"); return (((void *)0)) ; }; }while (0); |
134 | |
135 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
136 | PROCESS_REC *rec = tmp->data; |
137 | |
138 | if (rec->id == id) |
139 | return rec; |
140 | } |
141 | |
142 | if (verbose) { |
143 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTERROR, |
144 | "Unknown process id: %d", id); |
145 | } |
146 | |
147 | return NULL((void *)0); |
148 | } |
149 | |
150 | static PROCESS_REC *process_find(const char *name, int verbose) |
151 | { |
152 | GSList *tmp; |
153 | |
154 | g_return_val_if_fail(name != NULL, NULL)do{ if (name != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "name != NULL"); return ( ((void *)0)); }; }while (0); |
155 | |
156 | if (*name == '%' && is_numeric(name+1, 0)) |
157 | return process_find_id(atoi(name+1), verbose); |
158 | |
159 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
160 | PROCESS_REC *rec = tmp->data; |
161 | |
162 | if (rec->name != NULL((void *)0) && strcmp(rec->name, name) == 0) |
163 | return rec; |
164 | } |
165 | |
166 | if (verbose) { |
167 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTERROR, |
168 | "Unknown process name: %s", name); |
169 | } |
170 | |
171 | return NULL((void *)0); |
172 | } |
173 | |
174 | static void process_destroy(PROCESS_REC *rec, int status) |
175 | { |
176 | processes = g_slist_remove(processes, rec); |
177 | |
178 | signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status)((gpointer) (status))); |
179 | |
180 | if (rec->read_tag != -1) |
181 | g_source_remove(rec->read_tag); |
182 | if (rec->target_item != NULL((void *)0)) |
183 | exec_wi_destroy(rec->target_item); |
184 | |
185 | line_split_free(rec->databuf); |
186 | g_io_channel_close(rec->in); |
187 | g_io_channel_unref(rec->in); |
188 | net_sendbuffer_destroy(rec->out, TRUE(!(0))); |
189 | |
190 | g_free_not_null(rec->name)g_free(rec->name); |
191 | g_free_not_null(rec->target)g_free(rec->target); |
192 | g_free_not_null(rec->target_server)g_free(rec->target_server); |
193 | g_free(rec->args); |
194 | g_free(rec); |
195 | } |
196 | |
197 | static void processes_killall(int signum) |
198 | { |
199 | GSList *tmp; |
200 | |
201 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
202 | PROCESS_REC *rec = tmp->data; |
203 | |
204 | kill(rec->pid, signum); |
205 | } |
206 | } |
207 | |
208 | static int signal_name_to_id(const char *name) |
209 | { |
210 | /* check only the few most common signals, too much job to check |
211 | them all. if we sometimes want more, procps-sources/proc/sig.c |
212 | would be useful for copypasting */ |
213 | if (g_ascii_strcasecmp(name, "hup") == 0) |
214 | return SIGHUP1; |
215 | if (g_ascii_strcasecmp(name, "int") == 0) |
216 | return SIGINT2; |
217 | if (g_ascii_strcasecmp(name, "term") == 0) |
218 | return SIGTERM15; |
219 | if (g_ascii_strcasecmp(name, "kill") == 0) |
220 | return SIGKILL9; |
221 | if (g_ascii_strcasecmp(name, "usr1") == 0) |
222 | return SIGUSR130; |
223 | if (g_ascii_strcasecmp(name, "usr2") == 0) |
224 | return SIGUSR231; |
225 | return -1; |
226 | } |
227 | |
228 | /* `optlist' should contain only one unknown key - the server tag. |
229 | returns NULL if there was unknown -option */ |
230 | static int cmd_options_get_signal(const char *cmd, |
231 | GHashTable *optlist) |
232 | { |
233 | GSList *list, *tmp, *next; |
234 | char *signame; |
235 | int signum; |
236 | |
237 | /* get all the options, then remove the known ones. there should |
238 | be only one left - the signal */ |
239 | list = hashtable_get_keys(optlist); |
240 | if (cmd != NULL((void *)0)) { |
241 | for (tmp = list; tmp != NULL((void *)0); tmp = next) { |
242 | char *option = tmp->data; |
243 | next = tmp->next; |
244 | |
245 | if (command_have_option(cmd, option)) |
246 | list = g_slist_remove(list, option); |
247 | } |
248 | } |
249 | |
250 | if (list == NULL((void *)0)) |
251 | return -1; |
252 | |
253 | signame = list->data; |
254 | signum = -1; |
Value stored to 'signum' is never read | |
255 | |
256 | signum = is_numeric(signame, 0) ? atol(signame) : |
257 | signal_name_to_id(signame); |
258 | |
259 | if (signum == -1 || list->next != NULL((void *)0)) { |
260 | /* unknown option (not a signal) */ |
261 | signal_emit("error command", 2, |
262 | GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN)((gpointer) (CMDERR_OPTION_UNKNOWN)), |
263 | signum == -1 ? list->data : list->next->data); |
264 | signal_stop(); |
265 | return -2; |
266 | } |
267 | |
268 | g_slist_free(list); |
269 | return signum; |
270 | } |
271 | |
272 | static void exec_show_list(void) |
273 | { |
274 | GSList *tmp; |
275 | |
276 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
277 | PROCESS_REC *rec = tmp->data; |
278 | |
279 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTCRAP, |
280 | "%d (%s): %s", rec->id, rec->name, rec->args); |
281 | } |
282 | } |
283 | |
284 | static void process_exec(PROCESS_REC *rec, const char *cmd) |
285 | { |
286 | const char *shell_args[4] = { "/bin/sh", "-c", NULL((void *)0), NULL((void *)0) }; |
287 | char **args; |
288 | int in[2], out[2]; |
289 | int n; |
290 | |
291 | if (pipe(in) == -1) |
292 | return; |
293 | if (pipe(out) == -1) |
294 | return; |
295 | |
296 | shell_args[2] = cmd; |
297 | rec->pid = fork(); |
298 | if (rec->pid == -1) { |
299 | /* error */ |
300 | close(in[0]); close(in[1]); |
301 | close(out[0]); close(out[1]); |
302 | return; |
303 | } |
304 | |
305 | if (rec->pid != 0) { |
306 | /* parent process */ |
307 | GIOChannel *outio = g_io_channel_unix_new(in[1]); |
308 | |
309 | rec->in = g_io_channel_unix_new(out[0]); |
310 | rec->out = net_sendbuffer_create(outio, 0); |
311 | |
312 | close(out[1]); |
313 | close(in[0]); |
314 | pidwait_add(rec->pid); |
315 | return; |
316 | } |
317 | |
318 | /* child process, try to clean up everything */ |
319 | setsid(); |
320 | setuid(getuid()); |
321 | setgid(getgid()); |
322 | signal(SIGINT2, SIG_IGN((__sighandler_t *)1)); |
323 | signal(SIGQUIT3, SIG_DFL((__sighandler_t *)0)); |
324 | |
325 | putenv("TERM=tty"); |
326 | |
327 | /* set stdin, stdout and stderr */ |
328 | dup2(in[0], STDIN_FILENO0); |
329 | dup2(out[1], STDOUT_FILENO1); |
330 | dup2(out[1], STDERR_FILENO2); |
331 | |
332 | /* don't let child see our files */ |
333 | for (n = 3; n < 256; n++) |
334 | close(n); |
335 | |
336 | if (rec->shell) { |
337 | execvp(shell_args[0], (char **) shell_args); |
338 | |
339 | fprintf(stderr__stderrp, "Exec: /bin/sh: %s\n", g_strerror(errno(* __error()))); |
340 | } else { |
341 | args = g_strsplit(cmd, " ", -1); |
342 | execvp(args[0], args); |
343 | |
344 | fprintf(stderr__stderrp, "Exec: %s: %s\n", args[0], g_strerror(errno(* __error()))); |
345 | } |
346 | |
347 | _exit(-1); |
348 | } |
349 | |
350 | static void sig_exec_input_reader(PROCESS_REC *rec) |
351 | { |
352 | char tmpbuf[512], *str; |
353 | int recvlen; |
354 | int ret; |
355 | |
356 | g_return_if_fail(rec != NULL)do{ if (rec != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "rec != NULL"); return; } ; }while (0); |
357 | |
358 | recvlen = net_receive(rec->in, tmpbuf, sizeof(tmpbuf)); |
359 | do { |
360 | ret = line_split(tmpbuf, recvlen, &str, &rec->databuf); |
361 | if (ret == -1) { |
362 | /* link to terminal closed? */ |
363 | g_source_remove(rec->read_tag); |
364 | rec->read_tag = -1; |
365 | break; |
366 | } |
367 | |
368 | if (ret > 0) { |
369 | signal_emit_id(signal_exec_input, 2, rec, str); |
370 | if (recvlen > 0) recvlen = 0; |
371 | } |
372 | } while (ret > 0); |
373 | } |
374 | |
375 | static void handle_exec(const char *args, GHashTable *optlist, |
376 | SERVER_REC *server, WI_ITEM_REC *item) |
377 | { |
378 | PROCESS_REC *rec; |
379 | SERVER_REC *target_server; |
380 | char *target, *level; |
381 | int notice, signum, interactive, target_nick, target_channel; |
382 | |
383 | /* check that there's no unknown options. we allowed them |
384 | because signals can be used as options, but there should be |
385 | only one unknown option: the signal name/number. */ |
386 | signum = cmd_options_get_signal("exec", optlist); |
387 | if (signum == -2) |
388 | return; |
389 | |
390 | if (*args == '\0') { |
391 | exec_show_list(); |
392 | return; |
393 | } |
394 | |
395 | target = NULL((void *)0); |
396 | target_server = NULL((void *)0); |
397 | notice = FALSE(0); |
398 | |
399 | if (g_hash_table_lookup(optlist, "in") != NULL((void *)0)) { |
400 | rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE(!(0))); |
401 | if (rec != NULL((void *)0)) { |
402 | net_sendbuffer_send(rec->out, args, strlen(args)); |
403 | net_sendbuffer_send(rec->out, "\n", 1); |
404 | } |
405 | return; |
406 | } |
407 | |
408 | /* check if args is a process ID or name. if it's ID but not found, |
409 | complain about it and fail immediately */ |
410 | rec = process_find(args, *args == '%'); |
411 | if (*args == '%' && rec == NULL((void *)0)) |
412 | return; |
413 | |
414 | /* common options */ |
415 | target_channel = target_nick = FALSE(0); |
416 | if (g_hash_table_lookup(optlist, "out") != NULL((void *)0)) { |
417 | /* redirect output to active channel/query */ |
418 | if (item == NULL((void *)0)) |
419 | cmd_return_error(CMDERR_NOT_JOINED)do { signal_emit("error command", 1, ((gpointer) (CMDERR_NOT_JOINED ))); signal_stop(); return; } while (0); |
420 | target = (char *) window_item_get_target(item)((item)->get_target(item)); |
421 | target_server = item->server; |
422 | target_channel = IS_CHANNEL(item)(((CHANNEL_REC *) module_check_cast_module(item, __builtin_offsetof (CHANNEL_REC, type), "WINDOW ITEM TYPE", "CHANNEL")) ? (!(0)) : (0)); |
423 | target_nick = IS_QUERY(item)(((QUERY_REC *) module_check_cast_module(item, __builtin_offsetof (QUERY_REC, type), "WINDOW ITEM TYPE", "QUERY")) ? (!(0)) : ( 0)); |
424 | } else if (g_hash_table_lookup(optlist, "msg") != NULL((void *)0)) { |
425 | /* redirect output to /msg <nick> */ |
426 | target = g_hash_table_lookup(optlist, "msg"); |
427 | target_server = server; |
428 | } else if (g_hash_table_lookup(optlist, "notice") != NULL((void *)0)) { |
429 | target = g_hash_table_lookup(optlist, "notice"); |
430 | target_server = server; |
431 | notice = TRUE(!(0)); |
432 | } |
433 | |
434 | /* options that require process ID/name as argument */ |
435 | if (rec == NULL((void *)0) && |
436 | (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL((void *)0))) { |
437 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTERROR, |
438 | "Unknown process name: %s", args); |
439 | return; |
440 | } |
441 | if (g_hash_table_lookup(optlist, "close") != NULL((void *)0)) { |
442 | /* forcibly close the process */ |
443 | process_destroy(rec, -1); |
444 | return; |
445 | } |
446 | |
447 | if (signum != -1) { |
448 | /* send a signal to process */ |
449 | kill(rec->pid, signum); |
450 | return; |
451 | } |
452 | |
453 | interactive = g_hash_table_lookup(optlist, "interactive") != NULL((void *)0); |
454 | if (*args == '%') { |
455 | /* do something to already existing process */ |
456 | char *name; |
457 | |
458 | if (target != NULL((void *)0)) { |
459 | /* redirect output to target */ |
460 | g_free_and_null(rec->target)do { if (rec->target) { g_free(rec->target); (rec->target ) = ((void *)0); } } while (0); |
461 | rec->target = g_strdup(target); |
462 | rec->target_server = target_server == NULL((void *)0) ? NULL((void *)0) : |
463 | g_strdup(target_server->tag); |
464 | rec->notice = notice; |
465 | } |
466 | |
467 | name = g_hash_table_lookup(optlist, "name"); |
468 | if (name != NULL((void *)0)) { |
469 | /* change window name */ |
470 | g_free_not_null(rec->name)g_free(rec->name); |
471 | rec->name = *name == '\0' ? NULL((void *)0) : g_strdup(name); |
472 | } else if (target == NULL((void *)0) && |
473 | (rec->target_item == NULL((void *)0) || interactive)) { |
474 | /* no parameters given, |
475 | redirect output to the active window */ |
476 | g_free_and_null(rec->target)do { if (rec->target) { g_free(rec->target); (rec->target ) = ((void *)0); } } while (0); |
477 | rec->target_win = active_win; |
478 | |
479 | if (rec->target_item != NULL((void *)0)) |
480 | exec_wi_destroy(rec->target_item); |
481 | |
482 | if (interactive) { |
483 | rec->target_item = |
484 | exec_wi_create(active_win, rec); |
485 | } |
486 | } |
487 | return; |
488 | } |
489 | |
490 | /* starting a new process */ |
491 | rec = g_new0(PROCESS_REC, 1)((PROCESS_REC *) g_malloc0 (((gsize) sizeof (PROCESS_REC)) * ( (gsize) (1)))); |
492 | rec->pid = -1; |
493 | rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL((void *)0); |
494 | |
495 | process_exec(rec, args); |
496 | if (rec->pid == -1) { |
497 | /* pipe() or fork() failed */ |
498 | g_free(rec); |
499 | cmd_return_error(CMDERR_ERRNO)do { signal_emit("error command", 1, ((gpointer) (CMDERR_ERRNO ))); signal_stop(); return; } while (0); |
500 | } |
501 | |
502 | rec->id = process_get_new_id(); |
503 | rec->target = g_strdup(target); |
504 | rec->target_server = target_server == NULL((void *)0) ? NULL((void *)0) : |
505 | g_strdup(target_server->tag); |
506 | rec->target_win = active_win; |
507 | rec->target_channel = target_channel; |
508 | rec->target_nick = target_nick; |
509 | rec->args = g_strdup(args); |
510 | rec->notice = notice; |
511 | rec->silent = g_hash_table_lookup(optlist, "-") != NULL((void *)0); |
512 | rec->quiet = g_hash_table_lookup(optlist, "quiet") != NULL((void *)0); |
513 | rec->name = g_strdup(g_hash_table_lookup(optlist, "name")); |
514 | |
515 | level = g_hash_table_lookup(optlist, "level"); |
516 | rec->level = level == NULL((void *)0) ? MSGLEVEL_CLIENTCRAP : level2bits(level, NULL((void *)0)); |
517 | |
518 | rec->read_tag = g_input_add(rec->in, G_INPUT_READ(1 << 0), |
519 | (GInputFunction) sig_exec_input_reader, |
520 | rec); |
521 | processes = g_slist_append(processes, rec); |
522 | |
523 | if (rec->target == NULL((void *)0) && interactive) |
524 | rec->target_item = exec_wi_create(active_win, rec); |
525 | |
526 | signal_emit("exec new", 1, rec); |
527 | } |
528 | |
529 | /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>] |
530 | [-name <name>] <cmd line> |
531 | EXEC -out | -window | -msg <target> | -notice <target> | |
532 | -close | -<signal> <id> |
533 | EXEC -in <id> <text to send to process> */ |
534 | static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item) |
535 | { |
536 | GHashTable *optlist; |
537 | char *args; |
538 | void *free_arg; |
539 | |
540 | g_return_if_fail(data != NULL)do{ if (data != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "data != NULL"); return; }; }while (0); |
541 | |
542 | if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS0x00004000 | |
543 | PARAM_FLAG_UNKNOWN_OPTIONS0x00008000 | PARAM_FLAG_GETREST0x00002000, |
544 | "exec", &optlist, &args)) { |
545 | handle_exec(args, optlist, server, item); |
546 | cmd_params_free(free_arg); |
547 | } |
548 | } |
549 | |
550 | static void sig_pidwait(void *pid, void *statusp) |
551 | { |
552 | PROCESS_REC *rec; |
553 | char *str; |
554 | int status = GPOINTER_TO_INT(statusp)((gint) (statusp)); |
555 | |
556 | rec = process_find_pid(GPOINTER_TO_INT(pid)((gint) (pid))); |
557 | if (rec == NULL((void *)0)) return; |
558 | |
559 | /* process exited - print the last line if |
560 | there wasn't a newline at end. */ |
561 | if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0') |
562 | signal_emit_id(signal_exec_input, 2, rec, str); |
563 | |
564 | if (!rec->silent) { |
565 | if (WIFSIGNALED(status)(((status) & 0177) != 0177 && ((status) & 0177 ) != 0)) { |
566 | status = WTERMSIG(status)(((status) & 0177)); |
567 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTNOTICE, |
568 | "process %d (%s) terminated with signal %d (%s)", |
569 | rec->id, rec->args, |
570 | status, g_strsignal(status)); |
571 | } else { |
572 | status = WIFEXITED(status)(((status) & 0177) == 0) ? WEXITSTATUS(status)((status) >> 8) : -1; |
573 | printtext(NULL((void *)0), NULL((void *)0), MSGLEVEL_CLIENTNOTICE, |
574 | "process %d (%s) terminated with return code %d", |
575 | rec->id, rec->args, status); |
576 | } |
577 | } |
578 | process_destroy(rec, status); |
579 | } |
580 | |
581 | static void sig_exec_input(PROCESS_REC *rec, const char *text) |
582 | { |
583 | WI_ITEM_REC *item; |
584 | SERVER_REC *server; |
585 | char *str; |
586 | |
587 | if (rec->quiet) |
588 | return; |
589 | |
590 | item = NULL((void *)0); |
591 | server = NULL((void *)0); |
592 | |
593 | if (rec->target != NULL((void *)0)) { |
594 | if (rec->target_server != NULL((void *)0)) { |
595 | server = server_find_tag(rec->target_server); |
596 | if (server == NULL((void *)0)) { |
597 | /* disconnected - target is lost */ |
598 | return; |
599 | } |
600 | item = NULL((void *)0); |
601 | } else { |
602 | item = window_item_find(NULL((void *)0), rec->target); |
603 | server = item != NULL((void *)0) ? item->server : |
604 | active_win->active_server; |
605 | } |
606 | |
607 | str = g_strconcat(rec->target_nick ? "-nick " : |
608 | rec->target_channel ? "-channel " : "", |
609 | rec->target, " ", text, NULL((void *)0)); |
610 | signal_emit(rec->notice ? "command notice" : "command msg", |
611 | 3, str, server, item); |
612 | g_free(str); |
613 | } else if (rec->target_item != NULL((void *)0)) { |
614 | printtext(NULL((void *)0), rec->target_item->visible_name, |
615 | rec->level, "%s", text); |
616 | } else { |
617 | printtext_window(rec->target_win, rec->level, "%s", text); |
618 | } |
619 | } |
620 | |
621 | static void sig_window_destroyed(WINDOW_REC *window) |
622 | { |
623 | GSList *tmp; |
624 | |
625 | /* window is being closed, if there's any /exec targets for it, |
626 | change them to active window. */ |
627 | for (tmp = processes; tmp != NULL((void *)0); tmp = tmp->next) { |
628 | PROCESS_REC *rec = tmp->data; |
629 | |
630 | if (rec->target_win == window) |
631 | rec->target_win = active_win; |
632 | } |
633 | } |
634 | |
635 | static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item) |
636 | { |
637 | if (!IS_EXEC_WI(item)(((EXEC_WI_REC *) module_check_cast_module(item, __builtin_offsetof (EXEC_WI_REC, type), "WINDOW ITEM TYPE", "EXEC")) ? (!(0)) : ( 0))) |
638 | return; |
639 | |
640 | net_sendbuffer_send(item->process->out, data, strlen(data)); |
641 | net_sendbuffer_send(item->process->out, "\n", 1); |
642 | signal_stop(); |
643 | } |
644 | |
645 | void fe_exec_init(void) |
646 | { |
647 | command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec)command_bind_full("fe-common/core", 0, "exec", -1, ((void *)0 ), (SIGNAL_FUNC) cmd_exec, ((void *)0)); |
648 | command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet")command_set_options_module("fe-common/core", "exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet" ); |
649 | |
650 | signal_exec_input = signal_get_uniq_id("exec input")module_get_uniq_id_str("signals", "exec input"); |
651 | signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait)signal_add_full("fe-common/core", 0, ("pidwait"), (SIGNAL_FUNC ) ((SIGNAL_FUNC) sig_pidwait), ((void *)0)); |
652 | signal_add("exec input", (SIGNAL_FUNC) sig_exec_input)signal_add_full("fe-common/core", 0, ("exec input"), (SIGNAL_FUNC ) ((SIGNAL_FUNC) sig_exec_input), ((void *)0)); |
653 | signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed)signal_add_full("fe-common/core", 0, ("window destroyed"), (SIGNAL_FUNC ) ((SIGNAL_FUNC) sig_window_destroyed), ((void *)0)); |
654 | signal_add_first("send text", (SIGNAL_FUNC) event_text)signal_add_full("fe-common/core", -100, ("send text"), (SIGNAL_FUNC ) ((SIGNAL_FUNC) event_text), ((void *)0)); |
655 | } |
656 | |
657 | void fe_exec_deinit(void) |
658 | { |
659 | if (processes != NULL((void *)0)) { |
660 | processes_killall(SIGTERM15); |
661 | sleep(1); |
662 | processes_killall(SIGKILL9); |
663 | |
664 | while (processes != NULL((void *)0)) |
665 | process_destroy(processes->data, -1); |
666 | } |
667 | |
668 | command_unbind("exec", (SIGNAL_FUNC) cmd_exec)command_unbind_full("exec", (SIGNAL_FUNC) cmd_exec, ((void *) 0)); |
669 | |
670 | signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait)signal_remove_full(("pidwait"), (SIGNAL_FUNC) ((SIGNAL_FUNC) sig_pidwait ), ((void *)0)); |
671 | signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input)signal_remove_full(("exec input"), (SIGNAL_FUNC) ((SIGNAL_FUNC ) sig_exec_input), ((void *)0)); |
672 | signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed)signal_remove_full(("window destroyed"), (SIGNAL_FUNC) ((SIGNAL_FUNC ) sig_window_destroyed), ((void *)0)); |
673 | signal_remove("send text", (SIGNAL_FUNC) event_text)signal_remove_full(("send text"), (SIGNAL_FUNC) ((SIGNAL_FUNC ) event_text), ((void *)0)); |
674 | } |