4 Copyright (C) 2000 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
25 #include "line-split.h"
26 #include "net-sendbuffer.h"
30 #include "printtext.h"
32 #include "fe-windows.h"
33 #include "window-items.h"
38 static GSList *processes;
39 static int signal_exec_input;
41 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
45 g_return_val_if_fail(window != NULL, NULL);
46 g_return_val_if_fail(rec != NULL, NULL);
48 item = g_new0(EXEC_WI_REC, 1);
49 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
50 item->name = rec->name != NULL ?
51 g_strdup_printf("%%%s", rec->name) :
52 g_strdup_printf("%%%d", rec->id);
54 item->window = window;
55 item->createtime = time(NULL);
58 MODULE_DATA_INIT(item);
59 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
63 static void exec_wi_destroy(EXEC_WI_REC *rec)
65 g_return_if_fail(rec != NULL);
67 if (rec->destroying) return;
68 rec->destroying = TRUE;
70 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
71 window_item_destroy((WI_ITEM_REC *) rec);
73 MODULE_DATA_DEINIT(rec);
78 static int process_get_new_id(void)
101 static PROCESS_REC *process_find_pid(int pid)
105 g_return_val_if_fail(pid > 0, NULL);
107 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
108 PROCESS_REC *rec = tmp->data;
117 static PROCESS_REC *process_find_id(int id, int verbose)
121 g_return_val_if_fail(id != -1, NULL);
123 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
124 PROCESS_REC *rec = tmp->data;
131 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
132 "Unknown process id: %d", id);
138 static PROCESS_REC *process_find(const char *name, int verbose)
142 g_return_val_if_fail(name != NULL, NULL);
144 if (*name == '%' && is_numeric(name+1, 0))
145 return process_find_id(atoi(name+1), verbose);
147 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
148 PROCESS_REC *rec = tmp->data;
150 if (rec->name != NULL && strcmp(rec->name, name) == 0)
155 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
156 "Unknown process name: %s", name);
162 static void process_destroy(PROCESS_REC *rec, int status)
164 processes = g_slist_remove(processes, rec);
166 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
168 if (rec->read_tag != -1)
169 g_source_remove(rec->read_tag);
170 if (rec->target_item != NULL)
171 exec_wi_destroy(rec->target_item);
173 line_split_free(rec->databuf);
174 g_io_channel_close(rec->in);
175 g_io_channel_unref(rec->in);
176 net_sendbuffer_destroy(rec->out, TRUE);
178 g_free_not_null(rec->name);
179 g_free_not_null(rec->target);
184 static void processes_killall(int signum)
188 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
189 PROCESS_REC *rec = tmp->data;
191 kill(rec->pid, signum);
195 static int signal_name_to_id(const char *name)
197 /* check only the few most common signals, too much job to check
198 them all. if we sometimes want more, procps-sources/proc/sig.c
199 would be useful for copypasting */
200 if (g_strcasecmp(name, "hup") == 0)
202 if (g_strcasecmp(name, "int") == 0)
204 if (g_strcasecmp(name, "term") == 0)
206 if (g_strcasecmp(name, "kill") == 0)
208 if (g_strcasecmp(name, "usr1") == 0)
210 if (g_strcasecmp(name, "usr2") == 0)
215 /* `optlist' should contain only one unknown key - the server tag.
216 returns NULL if there was unknown -option */
217 static int cmd_options_get_signal(const char *cmd,
220 GSList *list, *tmp, *next;
224 /* get all the options, then remove the known ones. there should
225 be only one left - the signal */
226 list = hashtable_get_keys(optlist);
228 for (tmp = list; tmp != NULL; tmp = next) {
229 char *option = tmp->data;
232 if (command_have_option(cmd, option))
233 list = g_slist_remove(list, option);
240 signame = list->data;
243 signum = is_numeric(signame, 0) ? atol(signame) :
244 signal_name_to_id(signame);
246 if (signum == -1 || list->next != NULL) {
247 /* unknown option (not a signal) */
248 signal_emit("error command", 2,
249 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
250 signum == -1 ? list->data : list->next->data);
259 static void exec_show_list(void)
263 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
264 PROCESS_REC *rec = tmp->data;
266 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
267 "%d (%s): %s", rec->id, rec->name, rec->args);
271 static void process_exec(PROCESS_REC *rec, const char *cmd)
273 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
285 if (rec->pid == -1) {
287 close(in[0]); close(in[1]);
288 close(out[0]); close(out[1]);
294 GIOChannel *outio = g_io_channel_unix_new(in[1]);
296 rec->in = g_io_channel_unix_new(out[0]);
297 rec->out = net_sendbuffer_create(outio, 0);
301 pidwait_add(rec->pid);
305 /* child process, try to clean up everything */
309 signal(SIGINT, SIG_IGN);
310 signal(SIGQUIT, SIG_DFL);
314 /* set stdin, stdout and stderr */
315 dup2(in[0], STDIN_FILENO);
316 dup2(out[1], STDOUT_FILENO);
317 dup2(out[1], STDERR_FILENO);
319 /* don't let child see our files */
320 for (n = 3; n < 256; n++)
324 execvp(shell_args[0], (char **) shell_args);
326 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
328 args = g_strsplit(cmd, " ", -1);
329 execvp(args[0], args);
331 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
337 static void sig_exec_input_reader(PROCESS_REC *rec)
339 char tmpbuf[512], *str;
340 unsigned int recvlen;
343 g_return_if_fail(rec != NULL);
346 err = g_io_channel_read(rec->in, tmpbuf,
347 sizeof(tmpbuf), &recvlen);
348 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
352 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
354 /* link to terminal closed? */
355 g_source_remove(rec->read_tag);
361 signal_emit_id(signal_exec_input, 2, rec, str);
362 if (recvlen > 0) recvlen = 0;
367 static void handle_exec(const char *args, GHashTable *optlist,
372 int notice, signum, interactive;
374 /* check that there's no unknown options. we allowed them
375 because signals can be used as options, but there should be
376 only one unknown option: the signal name/number. */
377 signum = cmd_options_get_signal("exec", optlist);
389 if (g_hash_table_lookup(optlist, "in") != NULL) {
390 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
392 net_sendbuffer_send(rec->out, args, strlen(args));
393 net_sendbuffer_send(rec->out, "\n", 1);
398 /* check if args is a process ID or name. if it's ID but not found,
399 complain about it and fail immediately */
400 rec = process_find(args, *args == '%');
401 if (*args == '%' && rec == NULL)
405 if (g_hash_table_lookup(optlist, "out") != NULL) {
406 /* redirect output to active channel/query */
408 cmd_return_error(CMDERR_NOT_JOINED);
410 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
411 /* redirect output to /msg <nick> */
412 target = g_hash_table_lookup(optlist, "msg");
413 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
414 target = g_hash_table_lookup(optlist, "notice");
418 /* options that require process ID/name as argument */
420 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
421 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
422 "Unknown process name: %s", args);
425 if (g_hash_table_lookup(optlist, "close") != NULL) {
426 /* forcibly close the process */
427 process_destroy(rec, -1);
432 /* send a signal to process */
433 kill(rec->pid, signum);
437 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
439 /* do something to already existing process */
442 if (target != NULL) {
443 /* redirect output to target */
444 g_free_and_null(rec->target);
445 rec->target = g_strdup(target);
446 rec->notice = notice;
449 name = g_hash_table_lookup(optlist, "name");
451 /* change window name */
452 g_free_not_null(rec->name);
453 rec->name = *name == '\0' ? NULL : g_strdup(name);
454 } else if (target == NULL &&
455 (rec->target_item == NULL || interactive)) {
456 /* no parameters given,
457 redirect output to the active window */
458 g_free_and_null(rec->target);
459 rec->target_win = active_win;
461 if (rec->target_item != NULL) {
462 exec_wi_destroy(rec->target_item);
463 rec->target_item = NULL;
468 exec_wi_create(active_win, rec);
474 /* starting a new process */
475 rec = g_new0(PROCESS_REC, 1);
477 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
479 process_exec(rec, args);
480 if (rec->pid == -1) {
481 /* pipe() or fork() failed */
483 cmd_return_error(CMDERR_ERRNO);
486 rec->id = process_get_new_id();
487 rec->target = g_strdup(target);
488 rec->target_win = active_win;
489 rec->args = g_strdup(args);
490 rec->notice = notice;
491 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
492 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
494 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
495 (GInputFunction) sig_exec_input_reader,
497 processes = g_slist_append(processes, rec);
499 if (rec->target == NULL && interactive)
500 rec->target_item = exec_wi_create(active_win, rec);
502 signal_emit("exec new", 1, rec);
505 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
506 [-name <name>] <cmd line>
507 EXEC -out | -window | -msg <target> | -notice <target> |
508 -close | -<signal> <id>
509 EXEC -in <id> <text to send to process> */
510 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
516 g_return_if_fail(data != NULL);
518 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
519 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
520 "exec", &optlist, &args)) {
521 handle_exec(args, optlist, item);
522 cmd_params_free(free_arg);
526 static void sig_pidwait(void *pid, void *statusp)
529 int status = GPOINTER_TO_INT(statusp);
531 rec = process_find_pid(GPOINTER_TO_INT(pid));
532 if (rec == NULL) return;
536 if (WIFSIGNALED(status)) {
537 status = WTERMSIG(status);
538 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
539 "process %d (%s) terminated with signal %d (%s)",
541 status, g_strsignal(status));
543 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
544 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
545 "process %d (%s) terminated with return code %d",
546 rec->id, rec->args, status);
549 process_destroy(rec, status);
552 static void sig_exec_input(PROCESS_REC *rec, const char *text)
561 if (rec->target != NULL) {
562 item = window_item_find(NULL, rec->target);
563 server = item != NULL ? item->server :
564 active_win->active_server;
566 str = g_strconcat(rec->target, " ", text, NULL);
567 signal_emit(rec->notice ? "command notice" : "command msg",
568 3, str, server, item);
570 } else if (rec->target_item != NULL) {
571 printtext(NULL, rec->target_item->name, MSGLEVEL_CLIENTCRAP,
574 printtext_window(rec->target_win, MSGLEVEL_CLIENTCRAP,
579 static void sig_window_destroyed(WINDOW_REC *window)
583 /* window is being closed, if there's any /exec targets for it,
584 change them to active window. */
585 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
586 PROCESS_REC *rec = tmp->data;
588 if (rec->target_win == window)
589 rec->target_win = active_win;
593 static void sig_window_item_destroyed(WINDOW_REC *window, EXEC_WI_REC *item)
595 if (IS_EXEC_WI(item)) {
596 item->process->target_item = NULL;
597 exec_wi_destroy(item);
601 static void event_text(const char *data, SERVER_REC *server, EXEC_WI_REC *item)
603 if (!IS_EXEC_WI(item)) return;
605 net_sendbuffer_send(item->process->out, data, strlen(data));
606 net_sendbuffer_send(item->process->out, "\n", 1);
610 void fe_exec_init(void)
612 command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
613 command_set_options("exec", "!- interactive nosh +name out +msg +notice +in window close");
615 signal_exec_input = signal_get_uniq_id("exec input");
616 signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
617 signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
618 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
619 signal_add("window item destroy", (SIGNAL_FUNC) sig_window_item_destroyed);
620 signal_add_first("send text", (SIGNAL_FUNC) event_text);
623 void fe_exec_deinit(void)
625 if (processes != NULL) {
626 processes_killall(SIGTERM);
628 processes_killall(SIGKILL);
630 while (processes != NULL)
631 process_destroy(processes->data, -1);
634 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
636 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
637 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
638 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
639 signal_remove("window item destroy", (SIGNAL_FUNC) sig_window_item_destroyed);
640 signal_remove("send text", (SIGNAL_FUNC) event_text);