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"
35 #include "printtext.h"
37 #include "fe-windows.h"
38 #include "window-items.h"
44 static int signal_exec_input;
46 static void exec_wi_destroy(EXEC_WI_REC *rec)
48 g_return_if_fail(rec != NULL);
50 if (rec->destroying) return;
51 rec->destroying = TRUE;
53 rec->process->target_item = NULL;
54 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
55 window_item_destroy((WI_ITEM_REC *) rec);
57 MODULE_DATA_DEINIT(rec);
58 g_free(rec->visible_name);
62 static const char *exec_get_target(WI_ITEM_REC *item)
64 return ((EXEC_WI_REC *) item)->visible_name;
67 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
71 g_return_val_if_fail(window != NULL, NULL);
72 g_return_val_if_fail(rec != NULL, NULL);
74 item = g_new0(EXEC_WI_REC, 1);
75 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
76 item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy;
77 item->get_target = exec_get_target;
78 item->visible_name = rec->name != NULL ? g_strdup(rec->name) :
79 g_strdup_printf("%%%d", rec->id);
81 item->createtime = time(NULL);
84 MODULE_DATA_INIT(item);
85 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
89 static int process_get_new_id(void)
112 static PROCESS_REC *process_find_pid(int pid)
116 g_return_val_if_fail(pid > 0, NULL);
118 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
119 PROCESS_REC *rec = tmp->data;
128 static PROCESS_REC *process_find_id(int id, int verbose)
132 g_return_val_if_fail(id != -1, NULL);
134 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
135 PROCESS_REC *rec = tmp->data;
142 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
143 "Unknown process id: %d", id);
149 static PROCESS_REC *process_find(const char *name, int verbose)
153 g_return_val_if_fail(name != NULL, NULL);
155 if (*name == '%' && is_numeric(name+1, 0))
156 return process_find_id(atoi(name+1), verbose);
158 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
159 PROCESS_REC *rec = tmp->data;
161 if (rec->name != NULL && strcmp(rec->name, name) == 0)
166 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
167 "Unknown process name: %s", name);
173 static void process_destroy(PROCESS_REC *rec, int status)
175 processes = g_slist_remove(processes, rec);
177 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
179 if (rec->read_tag != -1)
180 g_source_remove(rec->read_tag);
181 if (rec->target_item != NULL)
182 exec_wi_destroy(rec->target_item);
184 line_split_free(rec->databuf);
185 g_io_channel_close(rec->in);
186 g_io_channel_unref(rec->in);
187 net_sendbuffer_destroy(rec->out, TRUE);
189 g_free_not_null(rec->name);
190 g_free_not_null(rec->target);
191 g_free_not_null(rec->target_server);
196 static void processes_killall(int signum)
200 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
201 PROCESS_REC *rec = tmp->data;
203 kill(rec->pid, signum);
207 static int signal_name_to_id(const char *name)
209 /* check only the few most common signals, too much job to check
210 them all. if we sometimes want more, procps-sources/proc/sig.c
211 would be useful for copypasting */
212 if (g_strcasecmp(name, "hup") == 0)
214 if (g_strcasecmp(name, "int") == 0)
216 if (g_strcasecmp(name, "term") == 0)
218 if (g_strcasecmp(name, "kill") == 0)
220 if (g_strcasecmp(name, "usr1") == 0)
222 if (g_strcasecmp(name, "usr2") == 0)
227 /* `optlist' should contain only one unknown key - the server tag.
228 returns NULL if there was unknown -option */
229 static int cmd_options_get_signal(const char *cmd,
232 GSList *list, *tmp, *next;
236 /* get all the options, then remove the known ones. there should
237 be only one left - the signal */
238 list = hashtable_get_keys(optlist);
240 for (tmp = list; tmp != NULL; tmp = next) {
241 char *option = tmp->data;
244 if (command_have_option(cmd, option))
245 list = g_slist_remove(list, option);
252 signame = list->data;
255 signum = is_numeric(signame, 0) ? atol(signame) :
256 signal_name_to_id(signame);
258 if (signum == -1 || list->next != NULL) {
259 /* unknown option (not a signal) */
260 signal_emit("error command", 2,
261 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
262 signum == -1 ? list->data : list->next->data);
271 static void exec_show_list(void)
275 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
276 PROCESS_REC *rec = tmp->data;
278 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
279 "%d (%s): %s", rec->id, rec->name, rec->args);
283 static void process_exec(PROCESS_REC *rec, const char *cmd)
285 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
297 if (rec->pid == -1) {
299 close(in[0]); close(in[1]);
300 close(out[0]); close(out[1]);
306 GIOChannel *outio = g_io_channel_unix_new(in[1]);
308 rec->in = g_io_channel_unix_new(out[0]);
309 rec->out = net_sendbuffer_create(outio, 0);
313 pidwait_add(rec->pid);
317 /* child process, try to clean up everything */
321 signal(SIGINT, SIG_IGN);
322 signal(SIGQUIT, SIG_DFL);
326 /* set stdin, stdout and stderr */
327 dup2(in[0], STDIN_FILENO);
328 dup2(out[1], STDOUT_FILENO);
329 dup2(out[1], STDERR_FILENO);
331 /* don't let child see our files */
332 for (n = 3; n < 256; n++)
336 execvp(shell_args[0], (char **) shell_args);
338 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
340 args = g_strsplit(cmd, " ", -1);
341 execvp(args[0], args);
343 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
349 static void sig_exec_input_reader(PROCESS_REC *rec)
351 char tmpbuf[512], *str;
355 g_return_if_fail(rec != NULL);
358 err = g_io_channel_read(rec->in, tmpbuf,
359 sizeof(tmpbuf), &recvlen);
360 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
364 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
366 /* link to terminal closed? */
367 g_source_remove(rec->read_tag);
373 signal_emit_id(signal_exec_input, 2, rec, str);
374 if (recvlen > 0) recvlen = 0;
379 static void handle_exec(const char *args, GHashTable *optlist,
380 SERVER_REC *server, WI_ITEM_REC *item)
383 SERVER_REC *target_server;
384 char *target, *level;
385 int notice, signum, interactive, target_nick, target_channel;
387 /* check that there's no unknown options. we allowed them
388 because signals can be used as options, but there should be
389 only one unknown option: the signal name/number. */
390 signum = cmd_options_get_signal("exec", optlist);
400 target_server = NULL;
403 if (g_hash_table_lookup(optlist, "in") != NULL) {
404 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
406 net_sendbuffer_send(rec->out, args, strlen(args));
407 net_sendbuffer_send(rec->out, "\n", 1);
412 /* check if args is a process ID or name. if it's ID but not found,
413 complain about it and fail immediately */
414 rec = process_find(args, *args == '%');
415 if (*args == '%' && rec == NULL)
419 target_channel = target_nick = FALSE;
420 if (g_hash_table_lookup(optlist, "out") != NULL) {
421 /* redirect output to active channel/query */
423 cmd_return_error(CMDERR_NOT_JOINED);
424 target = (char *) window_item_get_target(item);
425 target_server = item->server;
426 target_channel = IS_CHANNEL(item);
427 target_nick = IS_QUERY(item);
428 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
429 /* redirect output to /msg <nick> */
430 target = g_hash_table_lookup(optlist, "msg");
431 target_server = server;
432 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
433 target = g_hash_table_lookup(optlist, "notice");
434 target_server = server;
438 /* options that require process ID/name as argument */
440 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
441 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
442 "Unknown process name: %s", args);
445 if (g_hash_table_lookup(optlist, "close") != NULL) {
446 /* forcibly close the process */
447 process_destroy(rec, -1);
452 /* send a signal to process */
453 kill(rec->pid, signum);
457 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
459 /* do something to already existing process */
462 if (target != NULL) {
463 /* redirect output to target */
464 g_free_and_null(rec->target);
465 rec->target = g_strdup(target);
466 rec->target_server = target_server == NULL ? NULL :
467 g_strdup(target_server->tag);
468 rec->notice = notice;
471 name = g_hash_table_lookup(optlist, "name");
473 /* change window name */
474 g_free_not_null(rec->name);
475 rec->name = *name == '\0' ? NULL : g_strdup(name);
476 } else if (target == NULL &&
477 (rec->target_item == NULL || interactive)) {
478 /* no parameters given,
479 redirect output to the active window */
480 g_free_and_null(rec->target);
481 rec->target_win = active_win;
483 if (rec->target_item != NULL)
484 exec_wi_destroy(rec->target_item);
488 exec_wi_create(active_win, rec);
494 /* starting a new process */
495 rec = g_new0(PROCESS_REC, 1);
497 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
499 process_exec(rec, args);
500 if (rec->pid == -1) {
501 /* pipe() or fork() failed */
503 cmd_return_error(CMDERR_ERRNO);
506 rec->id = process_get_new_id();
507 rec->target = g_strdup(target);
508 rec->target_server = target_server == NULL ? NULL :
509 g_strdup(target_server->tag);
510 rec->target_win = active_win;
511 rec->target_channel = target_channel;
512 rec->target_nick = target_nick;
513 rec->args = g_strdup(args);
514 rec->notice = notice;
515 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
516 rec->quiet = g_hash_table_lookup(optlist, "quiet") != NULL;
517 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
519 level = g_hash_table_lookup(optlist, "level");
520 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level);
522 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
523 (GInputFunction) sig_exec_input_reader,
525 processes = g_slist_append(processes, rec);
527 if (rec->target == NULL && interactive)
528 rec->target_item = exec_wi_create(active_win, rec);
530 signal_emit("exec new", 1, rec);
533 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
534 [-name <name>] <cmd line>
535 EXEC -out | -window | -msg <target> | -notice <target> |
536 -close | -<signal> <id>
537 EXEC -in <id> <text to send to process> */
538 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
544 g_return_if_fail(data != NULL);
546 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
547 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
548 "exec", &optlist, &args)) {
549 handle_exec(args, optlist, server, item);
550 cmd_params_free(free_arg);
554 static void sig_pidwait(void *pid, void *statusp)
558 int status = GPOINTER_TO_INT(statusp);
560 rec = process_find_pid(GPOINTER_TO_INT(pid));
561 if (rec == NULL) return;
563 /* process exited - print the last line if
564 there wasn't a newline at end. */
565 if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0')
566 signal_emit_id(signal_exec_input, 2, rec, str);
569 if (WIFSIGNALED(status)) {
570 status = WTERMSIG(status);
571 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
572 "process %d (%s) terminated with signal %d (%s)",
574 status, g_strsignal(status));
576 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
577 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
578 "process %d (%s) terminated with return code %d",
579 rec->id, rec->args, status);
582 process_destroy(rec, status);
585 static void sig_exec_input(PROCESS_REC *rec, const char *text)
597 if (rec->target != NULL) {
598 if (rec->target_server != NULL) {
599 server = server_find_tag(rec->target_server);
600 if (server == NULL) {
601 /* disconnected - target is lost */
606 item = window_item_find(NULL, rec->target);
607 server = item != NULL ? item->server :
608 active_win->active_server;
611 str = g_strconcat(rec->target_nick ? "-nick " :
612 rec->target_channel ? "-channel " : "",
613 rec->target, " ", text, NULL);
614 signal_emit(rec->notice ? "command notice" : "command msg",
615 3, str, server, item);
617 } else if (rec->target_item != NULL) {
618 printtext(NULL, rec->target_item->visible_name,
619 rec->level, "%s", text);
621 printtext_window(rec->target_win, rec->level, "%s", text);
625 static void sig_window_destroyed(WINDOW_REC *window)
629 /* window is being closed, if there's any /exec targets for it,
630 change them to active window. */
631 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
632 PROCESS_REC *rec = tmp->data;
634 if (rec->target_win == window)
635 rec->target_win = active_win;
639 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
641 if (!IS_EXEC_WI(item))
644 net_sendbuffer_send(item->process->out, data, strlen(data));
645 net_sendbuffer_send(item->process->out, "\n", 1);
649 void fe_exec_init(void)
651 command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
652 command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet");
654 signal_exec_input = signal_get_uniq_id("exec input");
655 signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
656 signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
657 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
658 signal_add_first("send text", (SIGNAL_FUNC) event_text);
661 void fe_exec_deinit(void)
663 if (processes != NULL) {
664 processes_killall(SIGTERM);
666 processes_killall(SIGKILL);
668 while (processes != NULL)
669 process_destroy(processes->data, -1);
672 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
674 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
675 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
676 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
677 signal_remove("send text", (SIGNAL_FUNC) event_text);