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
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
26 #include "line-split.h"
27 #include "net-sendbuffer.h"
34 #include "printtext.h"
36 #include "fe-windows.h"
37 #include "window-items.h"
43 static int signal_exec_input;
45 static void exec_wi_destroy(EXEC_WI_REC *rec)
47 g_return_if_fail(rec != NULL);
49 if (rec->destroying) return;
50 rec->destroying = TRUE;
52 rec->process->target_item = NULL;
53 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
54 window_item_destroy((WI_ITEM_REC *) rec);
56 MODULE_DATA_DEINIT(rec);
57 g_free(rec->visible_name);
61 static const char *exec_get_target(WI_ITEM_REC *item)
63 return ((EXEC_WI_REC *) item)->visible_name;
66 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
70 g_return_val_if_fail(window != NULL, NULL);
71 g_return_val_if_fail(rec != NULL, NULL);
73 item = g_new0(EXEC_WI_REC, 1);
74 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
75 item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy;
76 item->get_target = exec_get_target;
77 item->visible_name = rec->name != NULL ? g_strdup(rec->name) :
78 g_strdup_printf("%%%d", rec->id);
80 item->createtime = time(NULL);
83 MODULE_DATA_INIT(item);
84 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
88 static int process_get_new_id(void)
111 static PROCESS_REC *process_find_pid(int pid)
115 g_return_val_if_fail(pid > 0, NULL);
117 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
118 PROCESS_REC *rec = tmp->data;
127 static PROCESS_REC *process_find_id(int id, int verbose)
131 g_return_val_if_fail(id != -1, NULL);
133 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
134 PROCESS_REC *rec = tmp->data;
141 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
142 "Unknown process id: %d", id);
148 static PROCESS_REC *process_find(const char *name, int verbose)
152 g_return_val_if_fail(name != NULL, NULL);
154 if (*name == '%' && is_numeric(name+1, 0))
155 return process_find_id(atoi(name+1), verbose);
157 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
158 PROCESS_REC *rec = tmp->data;
160 if (rec->name != NULL && strcmp(rec->name, name) == 0)
165 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
166 "Unknown process name: %s", name);
172 static void process_destroy(PROCESS_REC *rec, int status)
174 processes = g_slist_remove(processes, rec);
176 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
178 if (rec->read_tag != -1)
179 g_source_remove(rec->read_tag);
180 if (rec->target_item != NULL)
181 exec_wi_destroy(rec->target_item);
183 line_split_free(rec->databuf);
184 g_io_channel_close(rec->in);
185 g_io_channel_unref(rec->in);
186 net_sendbuffer_destroy(rec->out, TRUE);
188 g_free_not_null(rec->name);
189 g_free_not_null(rec->target);
194 static void processes_killall(int signum)
198 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
199 PROCESS_REC *rec = tmp->data;
201 kill(rec->pid, signum);
205 static int signal_name_to_id(const char *name)
207 /* check only the few most common signals, too much job to check
208 them all. if we sometimes want more, procps-sources/proc/sig.c
209 would be useful for copypasting */
210 if (g_strcasecmp(name, "hup") == 0)
212 if (g_strcasecmp(name, "int") == 0)
214 if (g_strcasecmp(name, "term") == 0)
216 if (g_strcasecmp(name, "kill") == 0)
218 if (g_strcasecmp(name, "usr1") == 0)
220 if (g_strcasecmp(name, "usr2") == 0)
225 /* `optlist' should contain only one unknown key - the server tag.
226 returns NULL if there was unknown -option */
227 static int cmd_options_get_signal(const char *cmd,
230 GSList *list, *tmp, *next;
234 /* get all the options, then remove the known ones. there should
235 be only one left - the signal */
236 list = hashtable_get_keys(optlist);
238 for (tmp = list; tmp != NULL; tmp = next) {
239 char *option = tmp->data;
242 if (command_have_option(cmd, option))
243 list = g_slist_remove(list, option);
250 signame = list->data;
253 signum = is_numeric(signame, 0) ? atol(signame) :
254 signal_name_to_id(signame);
256 if (signum == -1 || list->next != NULL) {
257 /* unknown option (not a signal) */
258 signal_emit("error command", 2,
259 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
260 signum == -1 ? list->data : list->next->data);
269 static void exec_show_list(void)
273 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
274 PROCESS_REC *rec = tmp->data;
276 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
277 "%d (%s): %s", rec->id, rec->name, rec->args);
281 static void process_exec(PROCESS_REC *rec, const char *cmd)
283 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
295 if (rec->pid == -1) {
297 close(in[0]); close(in[1]);
298 close(out[0]); close(out[1]);
304 GIOChannel *outio = g_io_channel_unix_new(in[1]);
306 rec->in = g_io_channel_unix_new(out[0]);
307 rec->out = net_sendbuffer_create(outio, 0);
311 pidwait_add(rec->pid);
315 /* child process, try to clean up everything */
319 signal(SIGINT, SIG_IGN);
320 signal(SIGQUIT, SIG_DFL);
324 /* set stdin, stdout and stderr */
325 dup2(in[0], STDIN_FILENO);
326 dup2(out[1], STDOUT_FILENO);
327 dup2(out[1], STDERR_FILENO);
329 /* don't let child see our files */
330 for (n = 3; n < 256; n++)
334 execvp(shell_args[0], (char **) shell_args);
336 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
338 args = g_strsplit(cmd, " ", -1);
339 execvp(args[0], args);
341 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
347 static void sig_exec_input_reader(PROCESS_REC *rec)
349 char tmpbuf[512], *str;
350 unsigned int recvlen;
353 g_return_if_fail(rec != NULL);
356 err = g_io_channel_read(rec->in, tmpbuf,
357 sizeof(tmpbuf), &recvlen);
358 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
362 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
364 /* link to terminal closed? */
365 g_source_remove(rec->read_tag);
371 signal_emit_id(signal_exec_input, 2, rec, str);
372 if (recvlen > 0) recvlen = 0;
377 static void handle_exec(const char *args, GHashTable *optlist,
381 char *target, *level;
382 int notice, signum, interactive, target_nick, target_channel;
384 /* check that there's no unknown options. we allowed them
385 because signals can be used as options, but there should be
386 only one unknown option: the signal name/number. */
387 signum = cmd_options_get_signal("exec", optlist);
399 if (g_hash_table_lookup(optlist, "in") != NULL) {
400 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
402 net_sendbuffer_send(rec->out, args, strlen(args));
403 net_sendbuffer_send(rec->out, "\n", 1);
408 /* check if args is a process ID or name. if it's ID but not found,
409 complain about it and fail immediately */
410 rec = process_find(args, *args == '%');
411 if (*args == '%' && rec == NULL)
415 target_channel = target_nick = FALSE;
416 if (g_hash_table_lookup(optlist, "out") != NULL) {
417 /* redirect output to active channel/query */
419 cmd_return_error(CMDERR_NOT_JOINED);
420 target = (char *) window_item_get_target(item);
421 target_channel = IS_CHANNEL(item);
422 target_nick = IS_QUERY(item);
423 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
424 /* redirect output to /msg <nick> */
425 target = g_hash_table_lookup(optlist, "msg");
426 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
427 target = g_hash_table_lookup(optlist, "notice");
431 /* options that require process ID/name as argument */
433 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
434 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
435 "Unknown process name: %s", args);
438 if (g_hash_table_lookup(optlist, "close") != NULL) {
439 /* forcibly close the process */
440 process_destroy(rec, -1);
445 /* send a signal to process */
446 kill(rec->pid, signum);
450 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
452 /* do something to already existing process */
455 if (target != NULL) {
456 /* redirect output to target */
457 g_free_and_null(rec->target);
458 rec->target = g_strdup(target);
459 rec->notice = notice;
462 name = g_hash_table_lookup(optlist, "name");
464 /* change window name */
465 g_free_not_null(rec->name);
466 rec->name = *name == '\0' ? NULL : g_strdup(name);
467 } else if (target == NULL &&
468 (rec->target_item == NULL || interactive)) {
469 /* no parameters given,
470 redirect output to the active window */
471 g_free_and_null(rec->target);
472 rec->target_win = active_win;
474 if (rec->target_item != NULL)
475 exec_wi_destroy(rec->target_item);
479 exec_wi_create(active_win, rec);
485 /* starting a new process */
486 rec = g_new0(PROCESS_REC, 1);
488 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
490 process_exec(rec, args);
491 if (rec->pid == -1) {
492 /* pipe() or fork() failed */
494 cmd_return_error(CMDERR_ERRNO);
497 rec->id = process_get_new_id();
498 rec->target = g_strdup(target);
499 rec->target_win = active_win;
500 rec->target_channel = target_channel;
501 rec->target_nick = target_nick;
502 rec->args = g_strdup(args);
503 rec->notice = notice;
504 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
505 rec->quiet = g_hash_table_lookup(optlist, "quiet") != NULL;
506 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
508 level = g_hash_table_lookup(optlist, "level");
509 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level);
511 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
512 (GInputFunction) sig_exec_input_reader,
514 processes = g_slist_append(processes, rec);
516 if (rec->target == NULL && interactive)
517 rec->target_item = exec_wi_create(active_win, rec);
519 signal_emit("exec new", 1, rec);
522 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
523 [-name <name>] <cmd line>
524 EXEC -out | -window | -msg <target> | -notice <target> |
525 -close | -<signal> <id>
526 EXEC -in <id> <text to send to process> */
527 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
533 g_return_if_fail(data != NULL);
535 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
536 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
537 "exec", &optlist, &args)) {
538 handle_exec(args, optlist, item);
539 cmd_params_free(free_arg);
543 static void sig_pidwait(void *pid, void *statusp)
547 int status = GPOINTER_TO_INT(statusp);
549 rec = process_find_pid(GPOINTER_TO_INT(pid));
550 if (rec == NULL) return;
552 /* process exited - print the last line if
553 there wasn't a newline at end. */
554 if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0')
555 signal_emit_id(signal_exec_input, 2, rec, str);
558 if (WIFSIGNALED(status)) {
559 status = WTERMSIG(status);
560 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
561 "process %d (%s) terminated with signal %d (%s)",
563 status, g_strsignal(status));
565 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
566 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
567 "process %d (%s) terminated with return code %d",
568 rec->id, rec->args, status);
571 process_destroy(rec, status);
574 static void sig_exec_input(PROCESS_REC *rec, const char *text)
586 if (rec->target != NULL) {
587 item = window_item_find(NULL, rec->target);
588 server = item != NULL ? item->server :
589 active_win->active_server;
591 str = g_strconcat(rec->target_nick ? "-nick " :
592 rec->target_channel ? "-channel " : "",
593 rec->target, " ", text, NULL);
594 signal_emit(rec->notice ? "command notice" : "command msg",
595 3, str, server, item);
597 } else if (rec->target_item != NULL) {
598 printtext(NULL, rec->target_item->visible_name,
599 rec->level, "%s", text);
601 printtext_window(rec->target_win, rec->level, "%s", text);
605 static void sig_window_destroyed(WINDOW_REC *window)
609 /* window is being closed, if there's any /exec targets for it,
610 change them to active window. */
611 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
612 PROCESS_REC *rec = tmp->data;
614 if (rec->target_win == window)
615 rec->target_win = active_win;
619 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
621 if (!IS_EXEC_WI(item))
624 net_sendbuffer_send(item->process->out, data, strlen(data));
625 net_sendbuffer_send(item->process->out, "\n", 1);
629 void fe_exec_init(void)
631 command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
632 command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet");
634 signal_exec_input = signal_get_uniq_id("exec input");
635 signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
636 signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
637 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
638 signal_add_first("send text", (SIGNAL_FUNC) event_text);
641 void fe_exec_deinit(void)
643 if (processes != NULL) {
644 processes_killall(SIGTERM);
646 processes_killall(SIGKILL);
648 while (processes != NULL)
649 process_destroy(processes->data, -1);
652 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
654 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
655 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
656 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
657 signal_remove("send text", (SIGNAL_FUNC) event_text);