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