Redirect standard descriptors for hook/notify commands
Disconnect stdin, stdout and stderr when running an external hook or notification command. The previous solution of appending "<&- >&- 2>&-" to the shell command line does not work if the command includes pipes. Use shell_exec() in notify_launch_cmd() instead of a custom (and incomplete) reimplementation of that command. Partially addresses GitHub issue #326. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
5710a8bd7f
commit
193ad3415a
@ -671,7 +671,6 @@ struct nbar {
|
|||||||
char datefmt[BUFSIZ]; /* format for displaying date */
|
char datefmt[BUFSIZ]; /* format for displaying date */
|
||||||
char timefmt[BUFSIZ]; /* format for displaying time */
|
char timefmt[BUFSIZ]; /* format for displaying time */
|
||||||
char cmd[BUFSIZ]; /* notification command */
|
char cmd[BUFSIZ]; /* notification command */
|
||||||
const char *shell; /* user shell to launch notif. cmd */
|
|
||||||
unsigned notify_all; /* notify all appointments */
|
unsigned notify_all; /* notify all appointments */
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
};
|
};
|
||||||
|
13
src/hooks.c
13
src/hooks.c
@ -41,20 +41,18 @@
|
|||||||
|
|
||||||
int run_hook(const char *name)
|
int run_hook(const char *name)
|
||||||
{
|
{
|
||||||
char *hook_path = NULL, *hook_cmd = NULL, *mesg;
|
char *hook_path = NULL, *mesg;
|
||||||
|
int pid, pin, pout, perr, ret = -127;
|
||||||
char const *arg[2];
|
char const *arg[2];
|
||||||
int pid, ret = -127;
|
|
||||||
|
|
||||||
asprintf(&hook_path, "%s/%s", path_hooks, name);
|
asprintf(&hook_path, "%s/%s", path_hooks, name);
|
||||||
if (!io_file_exists(hook_path))
|
if (!io_file_exists(hook_path))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
arg[0] = hook_path;
|
||||||
asprintf(&hook_cmd, "%s <&- >&- 2>&-", hook_path);
|
|
||||||
arg[0] = hook_cmd;
|
|
||||||
arg[1] = NULL;
|
arg[1] = NULL;
|
||||||
|
|
||||||
if ((pid = shell_exec(NULL, NULL, NULL, *arg, arg))) {
|
if ((pid = shell_exec(&pin, &pout, &perr, *arg, arg))) {
|
||||||
ret = child_wait(NULL, NULL, NULL, pid);
|
ret = child_wait(&pin, &pout, &perr, pid);
|
||||||
if (ret > 0 && WIFEXITED(ret)) {
|
if (ret > 0 && WIFEXITED(ret)) {
|
||||||
asprintf(&mesg, "%s hook: exit status %d",
|
asprintf(&mesg, "%s hook: exit status %d",
|
||||||
name,
|
name,
|
||||||
@ -71,6 +69,5 @@ int run_hook(const char *name)
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
mem_free(hook_path);
|
mem_free(hook_path);
|
||||||
mem_free(hook_cmd);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
23
src/notify.c
23
src/notify.c
@ -137,9 +137,6 @@ void notify_init_vars(void)
|
|||||||
strncpy(nbar.cmd, cmd, BUFSIZ);
|
strncpy(nbar.cmd, cmd, BUFSIZ);
|
||||||
nbar.cmd[BUFSIZ - 1] = '\0';
|
nbar.cmd[BUFSIZ - 1] = '\0';
|
||||||
|
|
||||||
if ((nbar.shell = getenv("SHELL")) == NULL)
|
|
||||||
nbar.shell = "/bin/sh";
|
|
||||||
|
|
||||||
nbar.notify_all = 0;
|
nbar.notify_all = 0;
|
||||||
|
|
||||||
pthread_attr_init(&detached_thread_attr);
|
pthread_attr_init(&detached_thread_attr);
|
||||||
@ -216,26 +213,18 @@ void notify_reinit_bar(void)
|
|||||||
/* Launch user defined command as a notification. */
|
/* Launch user defined command as a notification. */
|
||||||
unsigned notify_launch_cmd(void)
|
unsigned notify_launch_cmd(void)
|
||||||
{
|
{
|
||||||
int pid;
|
char const *arg[2] = { nbar.cmd, NULL };
|
||||||
|
int pid, pin, pout, perr;
|
||||||
|
|
||||||
if (notify_app.state & APOINT_NOTIFIED)
|
if (notify_app.state & APOINT_NOTIFIED)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
notify_app.state |= APOINT_NOTIFIED;
|
notify_app.state |= APOINT_NOTIFIED;
|
||||||
|
|
||||||
pid = fork();
|
if ((pid = shell_exec(&pin, &pout, &perr, *arg, arg))) {
|
||||||
|
close(pin);
|
||||||
if (pid < 0) {
|
close(pout);
|
||||||
ERROR_MSG(_("error while launching command: could not fork"));
|
close(perr);
|
||||||
return 0;
|
|
||||||
} else if (pid == 0) {
|
|
||||||
/* Child: launch user defined command */
|
|
||||||
if (execlp(nbar.shell, nbar.shell, "-c", nbar.cmd, NULL) <
|
|
||||||
0) {
|
|
||||||
ERROR_MSG(_("error while launching command"));
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
_exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user