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 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
53 window_item_destroy((WI_ITEM_REC *) rec);
55 MODULE_DATA_DEINIT(rec);
60 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
64 g_return_val_if_fail(window != NULL, NULL);
65 g_return_val_if_fail(rec != NULL, NULL);
67 item = g_new0(EXEC_WI_REC, 1);
68 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
69 item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy;
70 item->name = rec->name != NULL ? g_strdup(rec->name) :
71 g_strdup_printf("%%%d", rec->id);
73 item->createtime = time(NULL);
76 MODULE_DATA_INIT(item);
77 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
81 static int process_get_new_id(void)
104 static PROCESS_REC *process_find_pid(int pid)
108 g_return_val_if_fail(pid > 0, NULL);
110 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
111 PROCESS_REC *rec = tmp->data;
120 static PROCESS_REC *process_find_id(int id, int verbose)
124 g_return_val_if_fail(id != -1, NULL);
126 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
127 PROCESS_REC *rec = tmp->data;
134 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
135 "Unknown process id: %d", id);
141 static PROCESS_REC *process_find(const char *name, int verbose)
145 g_return_val_if_fail(name != NULL, NULL);
147 if (*name == '%' && is_numeric(name+1, 0))
148 return process_find_id(atoi(name+1), verbose);
150 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
151 PROCESS_REC *rec = tmp->data;
153 if (rec->name != NULL && strcmp(rec->name, name) == 0)
158 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
159 "Unknown process name: %s", name);
165 static void process_destroy(PROCESS_REC *rec, int status)
167 processes = g_slist_remove(processes, rec);
169 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
171 if (rec->read_tag != -1)
172 g_source_remove(rec->read_tag);
173 if (rec->target_item != NULL)
174 exec_wi_destroy(rec->target_item);
176 line_split_free(rec->databuf);
177 g_io_channel_close(rec->in);
178 g_io_channel_unref(rec->in);
179 net_sendbuffer_destroy(rec->out, TRUE);
181 g_free_not_null(rec->name);
182 g_free_not_null(rec->target);
187 static void processes_killall(int signum)
191 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
192 PROCESS_REC *rec = tmp->data;
194 kill(rec->pid, signum);
198 static int signal_name_to_id(const char *name)
200 /* check only the few most common signals, too much job to check
201 them all. if we sometimes want more, procps-sources/proc/sig.c
202 would be useful for copypasting */
203 if (g_strcasecmp(name, "hup") == 0)
205 if (g_strcasecmp(name, "int") == 0)
207 if (g_strcasecmp(name, "term") == 0)
209 if (g_strcasecmp(name, "kill") == 0)
211 if (g_strcasecmp(name, "usr1") == 0)
213 if (g_strcasecmp(name, "usr2") == 0)
218 /* `optlist' should contain only one unknown key - the server tag.
219 returns NULL if there was unknown -option */
220 static int cmd_options_get_signal(const char *cmd,
223 GSList *list, *tmp, *next;
227 /* get all the options, then remove the known ones. there should
228 be only one left - the signal */
229 list = hashtable_get_keys(optlist);
231 for (tmp = list; tmp != NULL; tmp = next) {
232 char *option = tmp->data;
235 if (command_have_option(cmd, option))
236 list = g_slist_remove(list, option);
243 signame = list->data;
246 signum = is_numeric(signame, 0) ? atol(signame) :
247 signal_name_to_id(signame);
249 if (signum == -1 || list->next != NULL) {
250 /* unknown option (not a signal) */
251 signal_emit("error command", 2,
252 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
253 signum == -1 ? list->data : list->next->data);
262 static void exec_show_list(void)
266 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
267 PROCESS_REC *rec = tmp->data;
269 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
270 "%d (%s): %s", rec->id, rec->name, rec->args);
274 static void process_exec(PROCESS_REC *rec, const char *cmd)
276 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
288 if (rec->pid == -1) {
290 close(in[0]); close(in[1]);
291 close(out[0]); close(out[1]);
297 GIOChannel *outio = g_io_channel_unix_new(in[1]);
299 rec->in = g_io_channel_unix_new(out[0]);
300 rec->out = net_sendbuffer_create(outio, 0);
304 pidwait_add(rec->pid);
308 /* child process, try to clean up everything */
312 signal(SIGINT, SIG_IGN);
313 signal(SIGQUIT, SIG_DFL);
317 /* set stdin, stdout and stderr */
318 dup2(in[0], STDIN_FILENO);
319 dup2(out[1], STDOUT_FILENO);
320 dup2(out[1], STDERR_FILENO);
322 /* don't let child see our files */
323 for (n = 3; n < 256; n++)
327 execvp(shell_args[0], (char **) shell_args);
329 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
331 args = g_strsplit(cmd, " ", -1);
332 execvp(args[0], args);
334 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
340 static void sig_exec_input_reader(PROCESS_REC *rec)
342 char tmpbuf[512], *str;
343 unsigned int recvlen;
346 g_return_if_fail(rec != NULL);
349 err = g_io_channel_read(rec->in, tmpbuf,
350 sizeof(tmpbuf), &recvlen);
351 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
355 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
357 /* link to terminal closed? */
358 g_source_remove(rec->read_tag);
364 signal_emit_id(signal_exec_input, 2, rec, str);
365 if (recvlen > 0) recvlen = 0;
370 static void handle_exec(const char *args, GHashTable *optlist,
374 char *target, *level;
375 int notice, signum, interactive, target_nick, target_channel;
377 /* check that there's no unknown options. we allowed them
378 because signals can be used as options, but there should be
379 only one unknown option: the signal name/number. */
380 signum = cmd_options_get_signal("exec", optlist);
392 if (g_hash_table_lookup(optlist, "in") != NULL) {
393 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
395 net_sendbuffer_send(rec->out, args, strlen(args));
396 net_sendbuffer_send(rec->out, "\n", 1);
401 /* check if args is a process ID or name. if it's ID but not found,
402 complain about it and fail immediately */
403 rec = process_find(args, *args == '%');
404 if (*args == '%' && rec == NULL)
408 target_channel = target_nick = FALSE;
409 if (g_hash_table_lookup(optlist, "out") != NULL) {
410 /* redirect output to active channel/query */
412 cmd_return_error(CMDERR_NOT_JOINED);
414 target_channel = IS_CHANNEL(item);
415 target_nick = IS_QUERY(item);
416 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
417 /* redirect output to /msg <nick> */
418 target = g_hash_table_lookup(optlist, "msg");
419 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
420 target = g_hash_table_lookup(optlist, "notice");
424 /* options that require process ID/name as argument */
426 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
427 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
428 "Unknown process name: %s", args);
431 if (g_hash_table_lookup(optlist, "close") != NULL) {
432 /* forcibly close the process */
433 process_destroy(rec, -1);
438 /* send a signal to process */
439 kill(rec->pid, signum);
443 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
445 /* do something to already existing process */
448 if (target != NULL) {
449 /* redirect output to target */
450 g_free_and_null(rec->target);
451 rec->target = g_strdup(target);
452 rec->notice = notice;
455 name = g_hash_table_lookup(optlist, "name");
457 /* change window name */
458 g_free_not_null(rec->name);
459 rec->name = *name == '\0' ? NULL : g_strdup(name);
460 } else if (target == NULL &&
461 (rec->target_item == NULL || interactive)) {
462 /* no parameters given,
463 redirect output to the active window */
464 g_free_and_null(rec->target);
465 rec->target_win = active_win;
467 if (rec->target_item != NULL) {
468 exec_wi_destroy(rec->target_item);
469 rec->target_item = NULL;
474 exec_wi_create(active_win, rec);
480 /* starting a new process */
481 rec = g_new0(PROCESS_REC, 1);
483 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
485 process_exec(rec, args);
486 if (rec->pid == -1) {
487 /* pipe() or fork() failed */
489 cmd_return_error(CMDERR_ERRNO);
492 rec->id = process_get_new_id();
493 rec->target = g_strdup(target);
494 rec->target_win = active_win;
495 rec->target_channel = target_channel;
496 rec->target_nick = target_nick;
497 rec->args = g_strdup(args);
498 rec->notice = notice;
499 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
500 rec->quiet = g_hash_table_lookup(optlist, "quiet") != NULL;
501 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
503 level = g_hash_table_lookup(optlist, "level");
504 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level);
506 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
507 (GInputFunction) sig_exec_input_reader,
509 processes = g_slist_append(processes, rec);
511 if (rec->target == NULL && interactive)
512 rec->target_item = exec_wi_create(active_win, rec);
514 signal_emit("exec new", 1, rec);
517 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
518 [-name <name>] <cmd line>
519 EXEC -out | -window | -msg <target> | -notice <target> |
520 -close | -<signal> <id>
521 EXEC -in <id> <text to send to process> */
522 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
528 g_return_if_fail(data != NULL);
530 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
531 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
532 "exec", &optlist, &args)) {
533 handle_exec(args, optlist, item);
534 cmd_params_free(free_arg);
538 static void sig_pidwait(void *pid, void *statusp)
542 int status = GPOINTER_TO_INT(statusp);
544 rec = process_find_pid(GPOINTER_TO_INT(pid));
545 if (rec == NULL) return;
547 /* process exited - print the last line if
548 there wasn't a newline at end. */
549 if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0')
550 signal_emit_id(signal_exec_input, 2, rec, str);
553 if (WIFSIGNALED(status)) {
554 status = WTERMSIG(status);
555 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
556 "process %d (%s) terminated with signal %d (%s)",
558 status, g_strsignal(status));
560 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
561 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
562 "process %d (%s) terminated with return code %d",
563 rec->id, rec->args, status);
566 process_destroy(rec, status);
569 static void sig_exec_input(PROCESS_REC *rec, const char *text)
581 if (rec->target != NULL) {
582 item = window_item_find(NULL, rec->target);
583 server = item != NULL ? item->server :
584 active_win->active_server;
586 str = g_strconcat(rec->target_nick ? "-nick " :
587 rec->target_channel ? "-channel " : "",
588 rec->target, " ", text, NULL);
589 signal_emit(rec->notice ? "command notice" : "command msg",
590 3, str, server, item);
592 } else if (rec->target_item != NULL) {
593 printtext(NULL, rec->target_item->name,
594 rec->level, "%s", text);
596 printtext_window(rec->target_win, rec->level, "%s", text);
600 static void sig_window_destroyed(WINDOW_REC *window)
604 /* window is being closed, if there's any /exec targets for it,
605 change them to active window. */
606 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
607 PROCESS_REC *rec = tmp->data;
609 if (rec->target_win == window)
610 rec->target_win = active_win;
614 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
616 if (!IS_EXEC_WI(item)) return;
618 net_sendbuffer_send(item->process->out, data, strlen(data));
619 net_sendbuffer_send(item->process->out, "\n", 1);
623 void fe_exec_init(void)
625 command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
626 command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close +level quiet");
628 signal_exec_input = signal_get_uniq_id("exec input");
629 signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
630 signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
631 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
632 signal_add_first("send text", (SIGNAL_FUNC) event_text);
635 void fe_exec_deinit(void)
637 if (processes != NULL) {
638 processes_killall(SIGTERM);
640 processes_killall(SIGKILL);
642 while (processes != NULL)
643 process_destroy(processes->data, -1);
646 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
648 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
649 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
650 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
651 signal_remove("send text", (SIGNAL_FUNC) event_text);