summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/pidwait.c3
-rw-r--r--src/fe-common/core/Makefile.am1
-rw-r--r--src/fe-common/core/fe-common-core.c5
-rw-r--r--src/fe-common/core/fe-core-commands.c42
-rw-r--r--src/fe-common/core/fe-exec.c579
5 files changed, 588 insertions, 42 deletions
diff --git a/src/core/pidwait.c b/src/core/pidwait.c
index 82a11c6c..f54aa34d 100644
--- a/src/core/pidwait.c
+++ b/src/core/pidwait.c
@@ -53,8 +53,9 @@ static int child_check(void)
next = tmp->next;
if (waitpid(pid, &status, WNOHANG) > 0) {
/* process terminated, remove from list */
+ signal_emit_id(signal_pidwait, 2, tmp->data,
+ GINT_TO_POINTER(status));
pids = g_slist_remove(pids, tmp->data);
- signal_emit_id(signal_pidwait, 1, tmp->data);
}
}
return 1;
diff --git a/src/fe-common/core/Makefile.am b/src/fe-common/core/Makefile.am
index bb341cf5..79aa49f4 100644
--- a/src/fe-common/core/Makefile.am
+++ b/src/fe-common/core/Makefile.am
@@ -14,6 +14,7 @@ libfe_common_core_a_SOURCES = \
fe-channels.c \
fe-common-core.c \
fe-core-commands.c \
+ fe-exec.c \
fe-expandos.c \
fe-ignore.c \
fe-ignore-messages.c \
diff --git a/src/fe-common/core/fe-common-core.c b/src/fe-common/core/fe-common-core.c
index 2b524dce..1547aa3a 100644
--- a/src/fe-common/core/fe-common-core.c
+++ b/src/fe-common/core/fe-common-core.c
@@ -47,6 +47,9 @@ void fe_channels_deinit(void);
void fe_core_log_init(void);
void fe_core_log_deinit(void);
+void fe_exec_init(void);
+void fe_exec_deinit(void);
+
void fe_expandos_init(void);
void fe_expandos_deinit(void);
@@ -109,6 +112,7 @@ void fe_common_core_init(void)
keyboard_init();
printtext_init();
formats_init();
+ fe_exec_init();
fe_expandos_init();
fe_ignore_init();
fe_log_init();
@@ -141,6 +145,7 @@ void fe_common_core_deinit(void)
keyboard_deinit();
printtext_deinit();
formats_deinit();
+ fe_exec_deinit();
fe_expandos_deinit();
fe_ignore_deinit();
fe_log_deinit();
diff --git a/src/fe-common/core/fe-core-commands.c b/src/fe-common/core/fe-core-commands.c
index a0fd03fb..fcf728f8 100644
--- a/src/fe-common/core/fe-core-commands.c
+++ b/src/fe-common/core/fe-core-commands.c
@@ -249,7 +249,7 @@ static void cmd_help(const char *data)
g_free(cmd);
}
-/* SYNTAX: ECHO [<text>] */
+/* SYNTAX: ECHO [-current] [-window <name>] [-level <level>] <text> */
static void cmd_echo(const char *data, void *server, WI_ITEM_REC *item)
{
WINDOW_REC *window;
@@ -326,44 +326,6 @@ static void cmd_cat(const char *data)
close(f);
}
-
-/* SYNTAX: EXEC <cmd line> */
-static void cmd_exec(const char *cmdline)
-{
-#ifndef WIN32
- char tmpbuf[512];
- char *foo;
- FILE *stream;
-
- stream = popen(cmdline, "r");
-
- if (!stream) {
- /* execution error of some kind */
- printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
- "Cannot open output stream.");
- return;
- }
-
- while (fgets(tmpbuf, sizeof(tmpbuf), stream)) {
- /* strip \n characters appended from fgets
- This is safer than using gets, though it is more work tbd
- */
- foo = tmpbuf;
- while (*foo != '\0') {
- if (*foo == '\n') {
- *foo = '\0';
- break;
- }
- foo++;
- }
-
- printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", tmpbuf);
- }
-
- pclose(stream);
-#endif
-}
-
/* SYNTAX: BEEP */
static void cmd_beep(void)
{
@@ -475,7 +437,6 @@ void fe_core_commands_init(void)
command_bind("echo", NULL, (SIGNAL_FUNC) cmd_echo);
command_bind("version", NULL, (SIGNAL_FUNC) cmd_version);
command_bind("cat", NULL, (SIGNAL_FUNC) cmd_cat);
- command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
command_bind("beep", NULL, (SIGNAL_FUNC) cmd_beep);
signal_add("send command", (SIGNAL_FUNC) event_command);
@@ -492,7 +453,6 @@ void fe_core_commands_deinit(void)
command_unbind("echo", (SIGNAL_FUNC) cmd_echo);
command_unbind("version", (SIGNAL_FUNC) cmd_version);
command_unbind("cat", (SIGNAL_FUNC) cmd_cat);
- command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
command_unbind("beep", (SIGNAL_FUNC) cmd_beep);
signal_remove("send command", (SIGNAL_FUNC) event_command);
diff --git a/src/fe-common/core/fe-exec.c b/src/fe-common/core/fe-exec.c
new file mode 100644
index 00000000..52d34cc2
--- /dev/null
+++ b/src/fe-common/core/fe-exec.c
@@ -0,0 +1,579 @@
+/*
+ fe-exec.c : irssi
+
+ Copyright (C) 2000 Timo Sirainen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include "module.h"
+#include "signals.h"
+#include "commands.h"
+#include "pidwait.h"
+#include "line-split.h"
+#include "net-sendbuffer.h"
+#include "misc.h"
+#include "levels.h"
+
+#include "printtext.h"
+#include "fe-windows.h"
+#include "window-items.h"
+
+#include <signal.h>
+#include <sys/wait.h>
+
+typedef struct {
+ int id;
+ char *name;
+ char *args;
+
+ int pid;
+ GIOChannel *in;
+ NET_SENDBUF_REC *out;
+ LINEBUF_REC *databuf;
+ int read_tag;
+
+ char *target; /* send text with /msg <target> ... */
+ WINDOW_REC *target_win; /* print text to this window */
+
+ int shell:1; /* start the program via /bin/sh */
+ int notice:1; /* send text with /notice, not /msg if target is set */
+ int silent:1; /* don't print "process exited with level xx" */
+} PROCESS_REC;
+
+static GSList *processes; /* processes, sorted by */
+static int signal_exec_input;
+
+static int process_get_new_id(void)
+{
+ PROCESS_REC *rec;
+ GSList *tmp;
+ int id;
+
+ id = 0;
+ tmp = processes;
+ while (tmp != NULL) {
+ rec = tmp->data;
+
+ if (id != rec->id) {
+ tmp = tmp->next;
+ continue;
+ }
+
+ id++;
+ tmp = processes;
+ }
+
+ return id;
+}
+
+static PROCESS_REC *process_find_pid(int pid)
+{
+ GSList *tmp;
+
+ g_return_val_if_fail(pid > 0, NULL);
+
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ if (rec->pid == pid)
+ return rec;
+ }
+
+ return NULL;
+}
+
+static PROCESS_REC *process_find_id(int id, int verbose)
+{
+ GSList *tmp;
+
+ g_return_val_if_fail(id != -1, NULL);
+
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ if (rec->id == id)
+ return rec;
+ }
+
+ if (verbose) {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
+ "Unknown process id: %d", id);
+ }
+
+ return NULL;
+}
+
+static PROCESS_REC *process_find(const char *name, int verbose)
+{
+ GSList *tmp;
+
+ g_return_val_if_fail(name != NULL, NULL);
+
+ if (*name == '%' && is_numeric(name+1, 0))
+ return process_find_id(atoi(name+1), verbose);
+
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ if (rec->name != NULL && strcmp(rec->name, name) == 0)
+ return rec;
+ }
+
+ if (verbose) {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
+ "Unknown process name: %s", name);
+ }
+
+ return NULL;
+}
+
+static void process_destroy(PROCESS_REC *rec)
+{
+ processes = g_slist_remove(processes, rec);
+
+ if (rec->read_tag != -1)
+ g_source_remove(rec->read_tag);
+
+ line_split_free(rec->databuf);
+ g_io_channel_close(rec->in);
+ g_io_channel_unref(rec->in);
+ net_sendbuffer_destroy(rec->out, TRUE);
+
+ g_free_not_null(rec->name);
+ g_free_not_null(rec->target);
+ g_free(rec->args);
+ g_free(rec);
+}
+
+static void processes_killall(int signum)
+{
+ GSList *tmp;
+
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ kill(rec->pid, signum);
+ }
+}
+
+static int signal_name_to_id(const char *name)
+{
+ /* check only the few most common signals, too much job to check
+ them all. if we sometimes want more, procps-sources/proc/sig.c
+ would be useful for copypasting */
+ if (g_strcasecmp(name, "hup") == 0)
+ return SIGHUP;
+ if (g_strcasecmp(name, "int") == 0)
+ return SIGINT;
+ if (g_strcasecmp(name, "term") == 0)
+ return SIGTERM;
+ if (g_strcasecmp(name, "kill") == 0)
+ return SIGKILL;
+ if (g_strcasecmp(name, "usr1") == 0)
+ return SIGUSR1;
+ if (g_strcasecmp(name, "usr2") == 0)
+ return SIGUSR2;
+ return -1;
+}
+
+/* `optlist' should contain only one unknown key - the server tag.
+ returns NULL if there was unknown -option */
+static int cmd_options_get_signal(const char *cmd,
+ GHashTable *optlist)
+{
+ GSList *list, *tmp, *next;
+ char *signame;
+ int signum;
+
+ /* get all the options, then remove the known ones. there should
+ be only one left - the signal */
+ list = hashtable_get_keys(optlist);
+ if (cmd != NULL) {
+ for (tmp = list; tmp != NULL; tmp = next) {
+ char *option = tmp->data;
+ next = tmp->next;
+
+ if (command_have_option(cmd, option))
+ list = g_slist_remove(list, option);
+ }
+ }
+
+ if (list == NULL)
+ return -1;
+
+ signame = list->data;
+ signum = -1;
+
+ signum = is_numeric(signame, 0) ? atol(signame) :
+ signal_name_to_id(signame);
+
+ if (signum == -1 || list->next != NULL) {
+ /* unknown option (not a signal) */
+ signal_emit("error command", 2,
+ GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
+ signum == -1 ? list->data : list->next->data);
+ signal_stop();
+ return -2;
+ }
+
+ g_slist_free(list);
+ return signum;
+}
+
+static void exec_show_list(void)
+{
+ GSList *tmp;
+
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
+ "%d (%s): %s", rec->id, rec->name, rec->args);
+ }
+}
+
+static void process_exec(PROCESS_REC *rec, const char *cmd)
+{
+ const char *shell_args[4] = { "/bin/sh", "-c", NULL, NULL };
+ char **args;
+ int in[2], out[2];
+ int n;
+
+ if (pipe(in) == -1)
+ return;
+ if (pipe(out) == -1)
+ return;
+
+ shell_args[2] = cmd;
+ rec->pid = fork();
+ if (rec->pid == -1) {
+ /* error */
+ close(in[0]); close(in[1]);
+ close(out[0]); close(out[1]);
+ return;
+ }
+
+ if (rec->pid != 0) {
+ /* parent process */
+ GIOChannel *outio = g_io_channel_unix_new(in[1]);
+
+ rec->in = g_io_channel_unix_new(out[0]);
+ rec->out = net_sendbuffer_create(outio, 0);
+
+ close(out[1]);
+ close(in[0]);
+ pidwait_add(rec->pid);
+ return;
+ }
+
+ /* child process, try to clean up everything */
+ setsid();
+ setuid(getuid());
+ setgid(getgid());
+ signal(SIGINT, SIG_IGN);
+ signal(SIGQUIT, SIG_DFL);
+
+ putenv("TERM=tty");
+
+ /* set stdin, stdout and stderr */
+ dup2(in[0], STDIN_FILENO);
+ dup2(out[1], STDOUT_FILENO);
+ dup2(out[1], STDERR_FILENO);
+
+ /* don't let child see our files */
+ for (n = 3; n < 256; n++)
+ close(n);
+
+ if (rec->shell) {
+ execvp(shell_args[0], (char **) shell_args);
+
+ fprintf(stderr, "Exec: /bin/sh: %s\n", g_strerror(errno));
+ } else {
+ args = g_strsplit(cmd, " ", -1);
+ execvp(args[0], args);
+
+ fprintf(stderr, "Exec: %s: %s\n", args[0], g_strerror(errno));
+ }
+
+ _exit(-1);
+}
+
+static void sig_exec_input_reader(PROCESS_REC *rec)
+{
+ char tmpbuf[512], *str;
+ int recvlen, err, ret;
+
+ g_return_if_fail(rec != NULL);
+
+ recvlen = 0;
+ err = g_io_channel_read(rec->in, tmpbuf,
+ sizeof(tmpbuf), &recvlen);
+ if (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
+ recvlen = -1;
+
+ do {
+ ret = line_split(tmpbuf, recvlen, &str,
+ (LINEBUF_REC **) &rec->databuf);
+ if (ret == -1) {
+ /* link to terminal closed? */
+ g_source_remove(rec->read_tag);
+ rec->read_tag = -1;
+ break;
+ }
+
+ if (ret > 0) {
+ signal_emit_id(signal_exec_input, 2, rec, str);
+ if (recvlen > 0) recvlen = 0;
+ }
+ } while (ret > 0);
+}
+
+static void handle_exec(const char *args, GHashTable *optlist,
+ WI_ITEM_REC *item)
+{
+ PROCESS_REC *rec;
+ char *target;
+ int notice, signum;
+
+ /* check that there's no unknown options. we allowed them
+ because signals can be used as options, but there should be
+ only one unknown option: the signal name/number. */
+ signum = cmd_options_get_signal("exec", optlist);
+ if (signum == -2)
+ return;
+
+ if (*args == '\0') {
+ exec_show_list();
+ return;
+ }
+
+ target = NULL;
+ notice = FALSE;
+
+ if (g_hash_table_lookup(optlist, "in") != NULL) {
+ rec = process_find(g_hash_table_lookup(optlist, "in"), TRUE);
+ if (rec != NULL) {
+ /* FIXME: use net_sendbuf instead */
+ net_sendbuffer_send(rec->out, args, strlen(args));
+ net_sendbuffer_send(rec->out, "\n", 1);
+ }
+ return;
+ }
+
+ /* check if args is a process ID or name. if it's ID but not found,
+ complain about it and fail immediately */
+ rec = process_find(args, *args == '%');
+ if (*args == '%' && rec == NULL)
+ return;
+
+ /* common options */
+ if (g_hash_table_lookup(optlist, "out") != NULL) {
+ /* redirect output to active channel/query */
+ if (item == NULL)
+ cmd_return_error(CMDERR_NOT_JOINED);
+ target = item->name;
+ } else if (g_hash_table_lookup(optlist, "msg") != NULL) {
+ /* redirect output to /msg <nick> */
+ target = g_hash_table_lookup(optlist, "msg");
+ } else if (g_hash_table_lookup(optlist, "notice") != NULL) {
+ target = g_hash_table_lookup(optlist, "notice");
+ notice = TRUE;
+ }
+
+ /* options that require process ID/name as argument */
+ if (rec == NULL &&
+ (signum != -1 || g_hash_table_lookup(optlist, "close") != NULL)) {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
+ "Unknown process name: %s", args);
+ return;
+ }
+ if (g_hash_table_lookup(optlist, "close") != NULL) {
+ /* forcibly close the process */
+ process_destroy(rec);
+ return;
+ }
+
+ if (signum != -1) {
+ /* send a signal to process */
+ kill(rec->pid, signum);
+ return;
+ }
+
+ if (*args == '%') {
+ /* do something to already existing process */
+ char *name;
+
+ if (target != NULL) {
+ /* redirect output to target */
+ g_free_and_null(rec->target);
+ rec->target = g_strdup(target);
+ rec->notice = notice;
+ }
+
+ name = g_hash_table_lookup(optlist, "name");
+ if (name != NULL) {
+ /* change window name */
+ g_free_not_null(rec->name);
+ rec->name = *name == '\0' ? NULL : g_strdup(name);
+ } else if (target == NULL) {
+ /* no parameters given,
+ redirect output to the active window */
+ g_free_and_null(rec->target);
+ rec->target_win = active_win;
+ }
+ return;
+ }
+
+ /* starting a new process */
+ rec = g_new0(PROCESS_REC, 1);
+ rec->pid = -1;
+ rec->shell = g_hash_table_lookup(optlist, "nosh") == NULL;
+
+ process_exec(rec, args);
+ if (rec->pid == -1) {
+ /* pipe() or fork() failed */
+ g_free(rec);
+ cmd_return_error(CMDERR_ERRNO);
+ }
+
+ rec->id = process_get_new_id();
+ rec->target = g_strdup(target);
+ rec->target_win = active_win;
+ rec->args = g_strdup(args);
+ rec->notice = notice;
+ rec->silent = g_hash_table_lookup(optlist, "-") != NULL;
+ rec->name = g_strdup(g_hash_table_lookup(optlist, "name"));
+
+ rec->read_tag = g_input_add(rec->in, G_INPUT_READ,
+ (GInputFunction) sig_exec_input_reader,
+ rec);
+
+ processes = g_slist_append(processes, rec);
+}
+
+/* SYNTAX: EXEC [-] [-nosh] [-out | -msg <target> | -notice <target>]
+ [-name <name>] <cmd line>
+ EXEC -out | -window | -msg <target> | -notice <target> |
+ -close | -<signal> <id>
+ EXEC -in <id> <text to send to process> */
+static void cmd_exec(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
+{
+ GHashTable *optlist;
+ char *args;
+ void *free_arg;
+
+ g_return_if_fail(data != NULL);
+
+ if (cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
+ PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
+ "exec", &optlist, &args)) {
+ handle_exec(args, optlist, item);
+ cmd_params_free(free_arg);
+ }
+}
+
+static void sig_pidwait(void *pid, void *statusp)
+{
+ PROCESS_REC *rec;
+ int status = GPOINTER_TO_INT(statusp);
+
+ rec = process_find_pid(GPOINTER_TO_INT(pid));
+ if (rec == NULL) return;
+
+ /* process exited */
+ if (!rec->silent) {
+ if (WIFSIGNALED(status)) {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
+ "process %d (%s) terminated with signal %d (%s)",
+ rec->id, rec->args,
+ WTERMSIG(status),
+ g_strsignal(WTERMSIG(status)));
+ } else {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
+ "process %d (%s) terminated with return code %d",
+ rec->id, rec->args,
+ WIFEXITED(status) ? WEXITSTATUS(status) : -1);
+ }
+ }
+ process_destroy(rec);
+}
+
+static void sig_exec_input(PROCESS_REC *rec, const char *text)
+{
+ WI_ITEM_REC *item;
+ SERVER_REC *server;
+ char *str;
+
+ item = NULL;
+ server = NULL;
+
+ if (rec->target != NULL) {
+ item = window_item_find(NULL, rec->target);
+ server = item != NULL ? item->server :
+ active_win->active_server;
+
+ str = g_strconcat(rec->target, " ", text, NULL);
+ signal_emit(rec->notice ? "command notice" : "command msg",
+ 3, str, server, item);
+ g_free(str);
+ } else {
+ printtext_window(rec->target_win, MSGLEVEL_CLIENTCRAP,
+ "%s", text);
+ }
+}
+
+static void sig_window_destroyed(WINDOW_REC *window)
+{
+ GSList *tmp;
+
+ /* window is being closed, if there's any /exec targets for it,
+ change them to active window. */
+ for (tmp = processes; tmp != NULL; tmp = tmp->next) {
+ PROCESS_REC *rec = tmp->data;
+
+ if (rec->target_win == window)
+ rec->target_win = active_win;
+ }
+}
+
+void fe_exec_init(void)
+{
+ command_bind("exec", NULL, (SIGNAL_FUNC) cmd_exec);
+ command_set_options("exec", "!- nosh +name out +msg +notice +in window close");
+
+ signal_exec_input = signal_get_uniq_id("exec input");
+ signal_add("pidwait", (SIGNAL_FUNC) sig_pidwait);
+ signal_add("exec input", (SIGNAL_FUNC) sig_exec_input);
+ signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
+}
+
+void fe_exec_deinit(void)
+{
+ if (processes != NULL) {
+ processes_killall(SIGTERM);
+ sleep(1);
+ processes_killall(SIGKILL);
+
+ while (processes != NULL)
+ process_destroy(processes->data);
+ }
+
+ command_unbind("exec", (SIGNAL_FUNC) cmd_exec);
+
+ signal_remove("pidwait", (SIGNAL_FUNC) sig_pidwait);
+ signal_remove("exec input", (SIGNAL_FUNC) sig_exec_input);
+ signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
+}