4 Copyright (C) 2000-2001 Timo Sirainen
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "line-split.h"
28 #include "net-sendbuffer.h"
36 #include "printtext.h"
38 #include "fe-windows.h"
39 #include "window-items.h"
45 static int signal_exec_input;
47 static void exec_wi_destroy(EXEC_WI_REC *rec)
49 g_return_if_fail(rec != NULL);
51 if (rec->destroying) return;
52 rec->destroying = TRUE;
54 rec->process->target_item = NULL;
55 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
56 window_item_destroy((WI_ITEM_REC *) rec);
58 MODULE_DATA_DEINIT(rec);
59 g_free(rec->visible_name);
63 static const char *exec_get_target(WI_ITEM_REC *item)
65 return ((EXEC_WI_REC *) item)->visible_name;
68 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
72 g_return_val_if_fail(window != NULL, NULL);
73 g_return_val_if_fail(rec != NULL, NULL);
75 item = g_new0(EXEC_WI_REC, 1);
76 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
77 item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy;
78 item->get_target = exec_get_target;
79 item->visible_name = rec->name != NULL ? g_strdup(rec->name) :
80 g_strdup_printf("%%%d", rec->id);
82 item->createtime = time(NULL);
85 MODULE_DATA_INIT(item);
86 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
90 static int process_get_new_id(void)
113 static PROCESS_REC *process_find_pid(int pid)
117 g_return_val_if_fail(pid > 0, NULL);
119 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
120 PROCESS_REC *rec = tmp->data;
129 static PROCESS_REC *process_find_id(int id, int verbose)
133 g_return_val_if_fail(id != -1, NULL);
135 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
136 PROCESS_REC *rec = tmp->data;
143 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
144 "Unknown process id: %d", id);
150 static PROCESS_REC *process_find(const char *name, int verbose)
154 g_return_val_if_fail(name != NULL, NULL);
156 if (*name == '%' && is_numeric(name+1, 0))
157 return process_find_id(atoi(name+1), verbose);
159 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
160 PROCESS_REC *rec = tmp->data;
162 if (rec->name != NULL && strcmp(rec->name, name) == 0)
167 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
168 "Unknown process name: %s", name);
174 static void process_destroy(PROCESS_REC *rec, int status)
176 processes = g_slist_remove(processes, rec);
178 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
180 if (rec->read_tag != -1)
181 g_source_remove(rec->read_tag);
182 if (rec->target_item != NULL)
183 exec_wi_destroy(rec->target_item);
185 line_split_free(rec->databuf);
186 g_io_channel_close(rec->in);
187 g_io_channel_unref(rec->in);
188 net_sendbuffer_destroy(rec->out, TRUE);
190 g_free_not_null(rec->name);
191 g_free_not_null(rec->target);
192 g_free_not_null(rec->target_server);
197 static void processes_killall(int signum)
202 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
203 PROCESS_REC *rec = tmp->data;
205 kill_ret = kill(-rec->pid, signum);
207 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
208 "Error sending signal %d to pid %d: %s",
209 signum, rec->pid, g_strerror(errno));
213 static int signal_name_to_id(const char *name)
215 /* check only the few most common signals, too much job to check
216 them all. if we sometimes want more, procps-sources/proc/sig.c
217 would be useful for copypasting */
218 if (g_ascii_strcasecmp(name, "hup") == 0)
220 if (g_ascii_strcasecmp(name, "int") == 0)
222 if (g_ascii_strcasecmp(name, "term") == 0)
224 if (g_ascii_strcasecmp(name, "kill") == 0)
226 if (g_ascii_strcasecmp(name, "usr1") == 0)
228 if (g_ascii_strcasecmp(name, "usr2") == 0)
233 /* `optlist' should contain only one unknown key - the server tag.
234 returns NULL if there was unknown -option */
235 static int cmd_options_get_signal(const char *cmd,
238 GSList *list, *tmp, *next;
242 /* get all the options, then remove the known ones. there should
243 be only one left - the signal */
244 list = hashtable_get_keys(optlist);
246 for (tmp = list; tmp != NULL; tmp = next) {
247 char *option = tmp->data;
250 if (command_have_option(cmd, option))
251 list = g_slist_remove(list, option);
258 signame = list->data;
261 signum = is_numeric(signame, 0) ? atol(signame) :
262 signal_name_to_id(signame);
264 if (signum == -1 || list->next != NULL) {
265 /* unknown option (not a signal) */
266 signal_emit("error command", 2,
267 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
268 signum == -1 ? list->data : list->next->data);
277 static void exec_show_list(void)
281 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
282 PROCESS_REC *rec = tmp->data;
284 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
285 "%d (%s): %s", rec->id, rec->name, rec->args);
289 static void process_exec(PROCESS_REC *rec, const char *cmd)
291 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
303 if (rec->pid == -1) {
305 close(in[0]); close(in[1]);
306 close(out[0]); close(out[1]);
312 GIOChannel *outio = g_io_channel_new(in[1]);
314 rec->in = g_io_channel_new(out[0]);
315 rec->out = net_sendbuffer_create(outio, 0);
319 pidwait_add(rec->pid);
323 /* child process, try to clean up everything */
327 signal(SIGINT, SIG_IGN);
328 signal(SIGQUIT, SIG_DFL);
332 /* set stdin, stdout and stderr */
333 dup2(in[0], STDIN_FILENO);
334 dup2(out[1], STDOUT_FILENO);
335 dup2(out[1], STDERR_FILENO);
337 /* don't let child see our files */
338 for (n = 3; n < 256; n++)
342 execvp(shell_args[0], (char **) shell_args);
344 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
346 args = g_strsplit(cmd, " ", -1);
347 execvp(args[0], args);
349 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
355 static void sig_exec_input_reader(PROCESS_REC *rec)
357 char tmpbuf[512], *str;
361 g_return_if_fail(rec != NULL);
363 recvlen = net_receive(rec->in, tmpbuf, sizeof(tmpbuf));
365 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
367 /* link to terminal closed? */
368 g_source_remove(rec->read_tag);
374 signal_emit_id(signal_exec_input, 2, rec, str);
375 if (recvlen > 0) recvlen = 0;
380 static void handle_exec(const char *args, GHashTable *optlist,
381 SERVER_REC *server, WI_ITEM_REC *item)
384 SERVER_REC *target_server;
385 char *target, *level;
386 int notice, signum, interactive, target_nick, target_channel, kill_ret;
388 /* check that there's no unknown options. we allowed them
389 because signals can be used as options, but there should be
390 only one unknown option: the signal name/number. */
391 signum = cmd_options_get_signal("exec", optlist);
401 target_server = NULL;
404 if (g_hash_table_lookup(optlist, "in") != NULL) {
405 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
407 net_sendbuffer_send(rec->out, args, strlen(args));
408 net_sendbuffer_send(rec->out, "\n", 1);
413 /* check if args is a process ID or name. if it's ID but not found,
414 complain about it and fail immediately */
415 rec = process_find(args, *args == '%');
416 if (*args == '%' && rec == NULL)
420 target_channel = target_nick = FALSE;
421 if (g_hash_table_lookup(optlist, "out") != NULL) {
422 /* redirect output to active channel/query */
424 cmd_return_error(CMDERR_NOT_JOINED);
425 target = (char *) window_item_get_target(item);
426 target_server = item->server;
427 target_channel = IS_CHANNEL(item);
428 target_nick = IS_QUERY(item);
429 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
430 /* redirect output to /msg <nick> */
431 target = g_hash_table_lookup(optlist, "msg");
432 target_server = server;
433 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
434 target = g_hash_table_lookup(optlist, "notice");
435 target_server = server;
439 /* options that require process ID/name as argument */
441 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
442 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
443 "Unknown process name: %s", args);
446 if (g_hash_table_lookup(optlist, "close") != NULL) {
447 /* forcibly close the process */
448 process_destroy(rec, -1);
453 /* send a signal to process group */
454 kill_ret = kill(-rec->pid, signum);
456 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
457 "Error sending signal %d to pid %d: %s",
458 signum, rec->pid, g_strerror(errno));
462 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
464 /* do something to already existing process */
467 if (target != NULL) {
468 /* redirect output to target */
469 g_free_and_null(rec->target);
470 rec->target = g_strdup(target);
471 rec->target_server = target_server == NULL ? NULL :
472 g_strdup(target_server->tag);
473 rec->notice = notice;
476 name = g_hash_table_lookup(optlist, "name");
478 /* change window name */
479 g_free_not_null(rec->name);
480 rec->name = *name == '\0' ? NULL : g_strdup(name);
481 } else if (target == NULL &&
482 (rec->target_item == NULL || interactive)) {
483 /* no parameters given,
484 redirect output to the active window */
485 g_free_and_null(rec->target);
486 rec->target_win = active_win;
488 if (rec->target_item != NULL)
489 exec_wi_destroy(rec->target_item);
493 exec_wi_create(active_win, rec);
499 /* starting a new process */
500 rec = g_new0(PROCESS_REC, 1);
502 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
504 process_exec(rec, args);
505 if (rec->pid == -1) {
506 /* pipe() or fork() failed */
508 cmd_return_error(CMDERR_ERRNO);
511 rec->id = process_get_new_id();
512 rec->target = g_strdup(target);
513 rec->target_server = target_server == NULL ? NULL :
514 g_strdup(target_server->tag);
515 rec->target_win = active_win;
516 rec->target_channel = target_channel;
517 rec->target_nick = target_nick;
518 rec->args = g_strdup(args);
519 rec->notice = notice;
520 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
521 rec->quiet = g_hash_table_lookup(optlist, "quiet") != NULL;
522 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
524 level = g_hash_table_lookup(optlist, "level");
525 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level, NULL);
527 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
528 (GInputFunction) sig_exec_input_reader,
530 processes = g_slist_append(processes, rec);
532 if (rec->target == NULL && interactive)
533 rec->target_item = exec_wi_create(active_win, rec);
535 signal_emit("exec new", 1, rec);
538 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
539 [-name <name>] <cmd line>
540 EXEC -out | -window | -msg <target> | -notice <target> |
541 -close | -<signal> %<id>
542 EXEC -in %<id> <text to send to process> */
543 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
549 g_return_if_fail(data != NULL);
551 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
552 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
553 "exec", &optlist, &args)) {
554 handle_exec(args, optlist, server, item);
555 cmd_params_free(free_arg);
559 static void sig_pidwait(void *pid, void *statusp)
563 int status = GPOINTER_TO_INT(statusp);
565 rec = process_find_pid(GPOINTER_TO_INT(pid));
566 if (rec == NULL) return;
568 /* process exited - print the last line if
569 there wasn't a newline at end. */
570 if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0')
571 signal_emit_id(signal_exec_input, 2, rec, str);
574 if (WIFSIGNALED(status)) {
575 status = WTERMSIG(status);
576 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
577 "process %d (%s) terminated with signal %d (%s)",
579 status, g_strsignal(status));
581 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
582 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
583 "process %d (%s) terminated with return code %d",
584 rec->id, rec->args, status);
587 process_destroy(rec, status);
590 static void sig_exec_input(PROCESS_REC *rec, const char *text)
602 if (rec->target != NULL) {
603 if (rec->target_server != NULL) {
604 server = server_find_tag(rec->target_server);
605 if (server == NULL) {
606 /* disconnected - target is lost */
611 item = window_item_find(NULL, rec->target);
612 server = item != NULL ? item->server :
613 active_win->active_server;
616 str = g_strconcat(rec->target_nick ? "-nick " :
617 rec->target_channel ? "-channel " : "",
618 rec->target, " ", text, NULL);
619 signal_emit(rec->notice ? "command notice" : "command msg",
620 3, str, server, item);
622 } else if (rec->target_item != NULL) {
623 printtext(NULL, rec->target_item->visible_name,
624 rec->level, "%s", text);
626 printtext_window(rec->target_win, rec->level, "%s", text);
630 static void sig_window_destroyed(WINDOW_REC *window)
634 /* window is being closed, if there's any /exec targets for it,
635 change them to active window. */
636 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
637 PROCESS_REC *rec = tmp->data;
639 if (rec->target_win == window)
640 rec->target_win = active_win;
644 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
646 if (!IS_EXEC_WI(item))
649 net_sendbuffer_send(item->process->out, data, strlen(data));
650 net_sendbuffer_send(item->process->out, "\n", 1);
654 void fe_exec_init(void)
656 command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
657 command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet");
659 signal_exec_input = signal_get_uniq_id("exec input");
660 signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
661 signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
662 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
663 signal_add_first("send text", (SIGNAL_FUNC) event_text);
666 void fe_exec_deinit(void)
668 if (processes != NULL) {
669 processes_killall(SIGTERM);
671 processes_killall(SIGKILL);
673 while (processes != NULL)
674 process_destroy(processes->data, -1);
677 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
679 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
680 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
681 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
682 signal_remove("send text", (SIGNAL_FUNC) event_text);