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"
31 #include "printtext.h"
33 #include "fe-windows.h"
34 #include "window-items.h"
40 static int signal_exec_input;
42 static void exec_wi_destroy(EXEC_WI_REC *rec)
44 g_return_if_fail(rec != NULL);
46 if (rec->destroying) return;
47 rec->destroying = TRUE;
49 if (window_item_window((WI_ITEM_REC *) rec) != NULL)
50 window_item_destroy((WI_ITEM_REC *) rec);
52 MODULE_DATA_DEINIT(rec);
57 static EXEC_WI_REC *exec_wi_create(WINDOW_REC *window, PROCESS_REC *rec)
61 g_return_val_if_fail(window != NULL, NULL);
62 g_return_val_if_fail(rec != NULL, NULL);
64 item = g_new0(EXEC_WI_REC, 1);
65 item->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "EXEC");
66 item->destroy = (void (*) (WI_ITEM_REC *)) exec_wi_destroy;
67 item->name = rec->name != NULL ?
68 g_strdup_printf("%%%s", rec->name) :
69 g_strdup_printf("%%%d", rec->id);
71 item->createtime = time(NULL);
74 MODULE_DATA_INIT(item);
75 window_item_add(window, (WI_ITEM_REC *) item, FALSE);
79 static int process_get_new_id(void)
102 static PROCESS_REC *process_find_pid(int pid)
106 g_return_val_if_fail(pid > 0, NULL);
108 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
109 PROCESS_REC *rec = tmp->data;
118 static PROCESS_REC *process_find_id(int id, int verbose)
122 g_return_val_if_fail(id != -1, NULL);
124 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
125 PROCESS_REC *rec = tmp->data;
132 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
133 "Unknown process id: %d", id);
139 static PROCESS_REC *process_find(const char *name, int verbose)
143 g_return_val_if_fail(name != NULL, NULL);
145 if (*name == '%' && is_numeric(name+1, 0))
146 return process_find_id(atoi(name+1), verbose);
148 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
149 PROCESS_REC *rec = tmp->data;
151 if (rec->name != NULL && strcmp(rec->name, name) == 0)
156 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
157 "Unknown process name: %s", name);
163 static void process_destroy(PROCESS_REC *rec, int status)
165 processes = g_slist_remove(processes, rec);
167 signal_emit("exec remove", 2, rec, GINT_TO_POINTER(status));
169 if (rec->read_tag != -1)
170 g_source_remove(rec->read_tag);
171 if (rec->target_item != NULL)
172 exec_wi_destroy(rec->target_item);
174 line_split_free(rec->databuf);
175 g_io_channel_close(rec->in);
176 g_io_channel_unref(rec->in);
177 net_sendbuffer_destroy(rec->out, TRUE);
179 g_free_not_null(rec->name);
180 g_free_not_null(rec->target);
185 static void processes_killall(int signum)
189 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
190 PROCESS_REC *rec = tmp->data;
192 kill(rec->pid, signum);
196 static int signal_name_to_id(const char *name)
198 /* check only the few most common signals, too much job to check
199 them all. if we sometimes want more, procps-sources/proc/sig.c
200 would be useful for copypasting */
201 if (g_strcasecmp(name, "hup") == 0)
203 if (g_strcasecmp(name, "int") == 0)
205 if (g_strcasecmp(name, "term") == 0)
207 if (g_strcasecmp(name, "kill") == 0)
209 if (g_strcasecmp(name, "usr1") == 0)
211 if (g_strcasecmp(name, "usr2") == 0)
216 /* `optlist' should contain only one unknown key - the server tag.
217 returns NULL if there was unknown -option */
218 static int cmd_options_get_signal(const char *cmd,
221 GSList *list, *tmp, *next;
225 /* get all the options, then remove the known ones. there should
226 be only one left - the signal */
227 list = hashtable_get_keys(optlist);
229 for (tmp = list; tmp != NULL; tmp = next) {
230 char *option = tmp->data;
233 if (command_have_option(cmd, option))
234 list = g_slist_remove(list, option);
241 signame = list->data;
244 signum = is_numeric(signame, 0) ? atol(signame) :
245 signal_name_to_id(signame);
247 if (signum == -1 || list->next != NULL) {
248 /* unknown option (not a signal) */
249 signal_emit("error command", 2,
250 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
251 signum == -1 ? list->data : list->next->data);
260 static void exec_show_list(void)
264 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
265 PROCESS_REC *rec = tmp->data;
267 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
268 "%d (%s): %s", rec->id, rec->name, rec->args);
272 static void process_exec(PROCESS_REC *rec, const char *cmd)
274 const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
286 if (rec->pid == -1) {
288 close(in[0]); close(in[1]);
289 close(out[0]); close(out[1]);
295 GIOChannel *outio = g_io_channel_unix_new(in[1]);
297 rec->in = g_io_channel_unix_new(out[0]);
298 rec->out = net_sendbuffer_create(outio, 0);
302 pidwait_add(rec->pid);
306 /* child process, try to clean up everything */
310 signal(SIGINT, SIG_IGN);
311 signal(SIGQUIT, SIG_DFL);
315 /* set stdin, stdout and stderr */
316 dup2(in[0], STDIN_FILENO);
317 dup2(out[1], STDOUT_FILENO);
318 dup2(out[1], STDERR_FILENO);
320 /* don't let child see our files */
321 for (n = 3; n < 256; n++)
325 execvp(shell_args[0], (char **) shell_args);
327 fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
329 args = g_strsplit(cmd, " ", -1);
330 execvp(args[0], args);
332 fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
338 static void sig_exec_input_reader(PROCESS_REC *rec)
340 char tmpbuf[512], *str;
341 unsigned int recvlen;
344 g_return_if_fail(rec != NULL);
347 err = g_io_channel_read(rec->in, tmpbuf,
348 sizeof(tmpbuf), &recvlen);
349 if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
353 ret = line_split(tmpbuf, recvlen, &str, &rec->databuf);
355 /* link to terminal closed? */
356 g_source_remove(rec->read_tag);
362 signal_emit_id(signal_exec_input, 2, rec, str);
363 if (recvlen > 0) recvlen = 0;
368 static void handle_exec(const char *args, GHashTable *optlist,
372 char *target, *level;
373 int notice, signum, interactive;
375 /* check that there's no unknown options. we allowed them
376 because signals can be used as options, but there should be
377 only one unknown option: the signal name/number. */
378 signum = cmd_options_get_signal("exec", optlist);
390 if (g_hash_table_lookup(optlist, "in") != NULL) {
391 rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
393 net_sendbuffer_send(rec->out, args, strlen(args));
394 net_sendbuffer_send(rec->out, "\n", 1);
399 /* check if args is a process ID or name. if it's ID but not found,
400 complain about it and fail immediately */
401 rec = process_find(args, *args == '%');
402 if (*args == '%' && rec == NULL)
406 if (g_hash_table_lookup(optlist, "out") != NULL) {
407 /* redirect output to active channel/query */
409 cmd_return_error(CMDERR_NOT_JOINED);
411 } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
412 /* redirect output to /msg <nick> */
413 target = g_hash_table_lookup(optlist, "msg");
414 } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
415 target = g_hash_table_lookup(optlist, "notice");
419 /* options that require process ID/name as argument */
421 (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
422 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
423 "Unknown process name: %s", args);
426 if (g_hash_table_lookup(optlist, "close") != NULL) {
427 /* forcibly close the process */
428 process_destroy(rec, -1);
433 /* send a signal to process */
434 kill(rec->pid, signum);
438 interactive = g_hash_table_lookup(optlist, "interactive") != NULL;
440 /* do something to already existing process */
443 if (target != NULL) {
444 /* redirect output to target */
445 g_free_and_null(rec->target);
446 rec->target = g_strdup(target);
447 rec->notice = notice;
450 name = g_hash_table_lookup(optlist, "name");
452 /* change window name */
453 g_free_not_null(rec->name);
454 rec->name = *name == '\0' ? NULL : g_strdup(name);
455 } else if (target == NULL &&
456 (rec->target_item == NULL || interactive)) {
457 /* no parameters given,
458 redirect output to the active window */
459 g_free_and_null(rec->target);
460 rec->target_win = active_win;
462 if (rec->target_item != NULL) {
463 exec_wi_destroy(rec->target_item);
464 rec->target_item = NULL;
469 exec_wi_create(active_win, rec);
475 /* starting a new process */
476 rec = g_new0(PROCESS_REC, 1);
478 rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
480 process_exec(rec, args);
481 if (rec->pid == -1) {
482 /* pipe() or fork() failed */
484 cmd_return_error(CMDERR_ERRNO);
487 rec->id = process_get_new_id();
488 rec->target = g_strdup(target);
489 rec->target_win = active_win;
490 rec->args = g_strdup(args);
491 rec->notice = notice;
492 rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
493 rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
495 level = g_hash_table_lookup(optlist, "level");
496 rec->level = level == NULL ? MSGLEVEL_CLIENTCRAP : level2bits(level);
498 rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
499 (GInputFunction) sig_exec_input_reader,
501 processes = g_slist_append(processes, rec);
503 if (rec->target == NULL && interactive)
504 rec->target_item = exec_wi_create(active_win, rec);
506 signal_emit("exec new", 1, rec);
509 /* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
510 [-name <name>] <cmd line>
511 EXEC -out | -window | -msg <target> | -notice <target> |
512 -close | -<signal> <id>
513 EXEC -in <id> <text to send to process> */
514 static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
520 g_return_if_fail(data != NULL);
522 if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
523 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
524 "exec", &optlist, &args)) {
525 handle_exec(args, optlist, item);
526 cmd_params_free(free_arg);
530 static void sig_pidwait(void *pid, void *statusp)
534 int status = GPOINTER_TO_INT(statusp);
536 rec = process_find_pid(GPOINTER_TO_INT(pid));
537 if (rec == NULL) return;
539 /* process exited - print the last line if
540 there wasn't a newline at end. */
541 if (line_split("\n", 1, &str, &rec->databuf) > 0 && *str != '\0')
542 signal_emit_id(signal_exec_input, 2, rec, str);
545 if (WIFSIGNALED(status)) {
546 status = WTERMSIG(status);
547 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
548 "process %d (%s) terminated with signal %d (%s)",
550 status, g_strsignal(status));
552 status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
553 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
554 "process %d (%s) terminated with return code %d",
555 rec->id, rec->args, status);
558 process_destroy(rec, status);
561 static void sig_exec_input(PROCESS_REC *rec, const char *text)
570 if (rec->target != NULL) {
571 item = window_item_find(NULL, rec->target);
572 server = item != NULL ? item->server :
573 active_win->active_server;
575 str = g_strconcat(rec->target, " ", text, NULL);
576 signal_emit(rec->notice ? "command notice" : "command msg",
577 3, str, server, item);
579 } else if (rec->target_item != NULL) {
580 printtext(NULL, rec->target_item->name,
581 rec->level, "%s", text);
583 printtext_window(rec->target_win, rec->level, "%s", text);
587 static void sig_window_destroyed(WINDOW_REC *window)
591 /* window is being closed, if there's any /exec targets for it,
592 change them to active window. */
593 for (tmp = processes; tmp != NULL; tmp = tmp->next) {
594 PROCESS_REC *rec = tmp->data;
596 if (rec->target_win == window)
597 rec->target_win = active_win;
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 +level");
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_first("send text", (SIGNAL_FUNC) event_text);
622 void fe_exec_deinit(void)
624 if (processes != NULL) {
625 processes_killall(SIGTERM);
627 processes_killall(SIGKILL);
629 while (processes != NULL)
630 process_destroy(processes->data, -1);
633 command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
635 signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
636 signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
637 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
638 signal_remove("send text", (SIGNAL_FUNC) event_text);