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