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