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);
61 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
65 g_return_val_if_fail(window != NULL, NULL);
66 g_return_val_if_fail(rec != NULL, NULL);
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);
74 item->createtime = time(NULL);
77 MODULE_DATA_INIT(item);
78 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
82 static int process_get_new_id(void)
105 static PROCESS_REC *process_find_pid(int pid)
109 g_return_val_if_fail(pid > 0, NULL);
111 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
112 PROCESS_REC *rec = tmp->data;
121 static PROCESS_REC *process_find_id(int id, int verbose)
125 g_return_val_if_fail(id != -1, NULL);
127 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
128 PROCESS_REC *rec = tmp->data;
135 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
136 "Unknown process id: %d", id);
142 static PROCESS_REC *process_find(const char *name, int verbose)
146 g_return_val_if_fail(name != NULL, NULL);
148 if (*name == '%' && is_numeric(name+1, 0))
149 return process_find_id(atoi(name+1), verbose);
151 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
152 PROCESS_REC *rec = tmp->data;
154 if (rec->name != NULL && strcmp(rec->name, name) == 0)
159 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
160 "Unknown process name: %s", name);
166 static void process_destroy(PROCESS_REC *rec, int status)
168 processes = g_slist_remove(processes, rec);
170 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
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);
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);
182 g_free_not_null(rec->name);
183 g_free_not_null(rec->target);
188 static void processes_killall(int signum)
192 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
193 PROCESS_REC *rec = tmp->data;
195 kill(rec->pid, signum);
199 static int signal_name_to_id(const char *name)
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)
206 if (g_strcasecmp(name, "int") == 0)
208 if (g_strcasecmp(name, "term") == 0)
210 if (g_strcasecmp(name, "kill") == 0)
212 if (g_strcasecmp(name, "usr1") == 0)
214 if (g_strcasecmp(name, "usr2") == 0)
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,
224 GSList *list, *tmp, *next;
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);
232 for (tmp = list; tmp != NULL; tmp = next) {
233 char *option = tmp->data;
236 if (command_have_option(cmd, option))
237 list = g_slist_remove(list, option);
244 signame = list->data;
247 signum = is_numeric(signame, 0) ? atol(signame) :
248 signal_name_to_id(signame);
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);
263 static void exec_show_list(void)
267 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
268 PROCESS_REC *rec = tmp->data;
270 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
271 "%d (%s): %s", rec->id, rec->name, rec->args);
275 static void process_exec(PROCESS_REC *rec, const char *cmd)
277 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
289 if (rec->pid == -1) {
291 close(in[0]); close(in[1]);
292 close(out[0]); close(out[1]);
298 GIOChannel *outio = g_io_channel_unix_new(in[1]);
300 rec->in = g_io_channel_unix_new(out[0]);
301 rec->out = net_sendbuffer_create(outio, 0);
305 pidwait_add(rec->pid);
309 /* child process, try to clean up everything */
313 signal(SIGINT, SIG_IGN);
314 signal(SIGQUIT, SIG_DFL);
318 /* set stdin, stdout and stderr */
319 dup2(in[0], STDIN_FILENO);
320 dup2(out[1], STDOUT_FILENO);
321 dup2(out[1], STDERR_FILENO);
323 /* don't let child see our files */
324 for (n = 3; n < 256; n++)
328 execvp(shell_args[0], (char **) shell_args);
330 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
332 args = g_strsplit(cmd, " ", -1);
333 execvp(args[0], args);
335 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
341 static void sig_exec_input_reader(PROCESS_REC *rec)
343 char tmpbuf[512], *str;
344 unsigned int recvlen;
347 g_return_if_fail(rec != NULL);
350 err = g_io_channel_read(rec->in, tmpbuf,
351 sizeof(tmpbuf), &recvlen);
352 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
356 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
358 /* link to terminal closed? */
359 g_source_remove(rec->read_tag);
365 signal_emit_id(signal_exec_input, 2, rec, str);
366 if (recvlen > 0) recvlen = 0;
371 static void handle_exec(const char *args, GHashTable *optlist,
375 char *target, *level;
376 int notice, signum, interactive, target_nick, target_channel;
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);
393 if (g_hash_table_lookup(optlist, "in") != NULL) {
394 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
396 net_sendbuffer_send(rec->out, args, strlen(args));
397 net_sendbuffer_send(rec->out, "\n", 1);
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)
409 target_channel = target_nick = FALSE;
410 if (g_hash_table_lookup(optlist, "out") != NULL) {
411 /* redirect output to active channel/query */
413 cmd_return_error(CMDERR_NOT_JOINED);
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");
425 /* options that require process ID/name as argument */
427 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
428 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
429 "Unknown process name: %s", args);
432 if (g_hash_table_lookup(optlist, "close") != NULL) {
433 /* forcibly close the process */
434 process_destroy(rec, -1);
439 /* send a signal to process */
440 kill(rec->pid, signum);
444 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
446 /* do something to already existing process */
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;
456 name = g_hash_table_lookup(optlist, "name");
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;
468 if (rec->target_item != NULL)
469 exec_wi_destroy(rec->target_item);
473 exec_wi_create(active_win, rec);
479 /* starting a new process */
480 rec = g_new0(PROCESS_REC, 1);
482 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
484 process_exec(rec, args);
485 if (rec->pid == -1) {
486 /* pipe() or fork() failed */
488 cmd_return_error(CMDERR_ERRNO);
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"));
502 level = g_hash_table_lookup(optlist, "level");
503 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level);
505 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
506 (GInputFunction) sig_exec_input_reader,
508 processes = g_slist_append(processes, rec);
510 if (rec->target == NULL && interactive)
511 rec->target_item = exec_wi_create(active_win, rec);
513 signal_emit("exec new", 1, rec);
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)
527 g_return_if_fail(data != NULL);
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);
537 static void sig_pidwait(void *pid, void *statusp)
541 int status = GPOINTER_TO_INT(statusp);
543 rec = process_find_pid(GPOINTER_TO_INT(pid));
544 if (rec == NULL) return;
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);
552 if (WIFSIGNALED(status)) {
553 status = WTERMSIG(status);
554 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
555 "process %d (%s) terminated with signal %d (%s)",
557 status, g_strsignal(status));
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);
565 process_destroy(rec, status);
568 static void sig_exec_input(PROCESS_REC *rec, const char *text)
580 if (rec->target != NULL) {
581 item = window_item_find(NULL, rec->target);
582 server = item != NULL ? item->server :
583 active_win->active_server;
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);
591 } else if (rec->target_item != NULL) {
592 printtext(NULL, rec->target_item->name,
593 rec->level, "%s", text);
595 printtext_window(rec->target_win, rec->level, "%s", text);
599 static void sig_window_destroyed(WINDOW_REC *window)
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;
608 if (rec->target_win == window)
609 rec->target_win = active_win;
613 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
615 if (!IS_EXEC_WI(item)) return;
617 net_sendbuffer_send(item->process->out, data, strlen(data));
618 net_sendbuffer_send(item->process->out, "\n", 1);
622 void fe_exec_init(void)
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");
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);
634 void fe_exec_deinit(void)
636 if (processes != NULL) {
637 processes_killall(SIGTERM);
639 processes_killall(SIGKILL);
641 while (processes != NULL)
642 process_destroy(processes->data, -1);
645 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
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);