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