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