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