From f798e9bb0bc667d07283f32d1d83b6223d375a03 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Thu, 13 Aug 2015 00:24:03 -0700 Subject: [PATCH] moved fd modifying stuff to log.c --- sway/commands.c | 31 ++++++++----------------------- sway/log.c | 12 ++++++++++++ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 7721c6fb..edf9db7a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include "stringop.h" #include "layout.h" @@ -20,7 +19,7 @@ struct modifier_key { uint32_t mod; }; -struct modifier_key modifiers[] = { +static struct modifier_key modifiers[] = { { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT }, { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS }, { XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL }, @@ -38,20 +37,22 @@ enum expected_args { }; static bool checkarg(int argc, char *name, enum expected_args type, int val) { - switch(type) { + switch (type) { case EXPECTED_MORE_THEN: if (argc > val) { return true; } sway_log(L_ERROR, "Invalid %s command." - "(expected more then %d arguments, got %d", name, val, argc); + "(expected more then %d argument%s, got %d", + name, val, (char*[2]){"s", ""}[argc==1], argc); break; case EXPECTED_LESS_THEN: if (argc < val) { return true; }; sway_log(L_ERROR, "Invalid %s command." - "(expected less then %d arguments, got %d", name, val, argc); + "(expected less then %d argument%s, got %d", + name, val, (char*[2]){"s", ""}[argc==1], argc); break; case EXPECTED_SAME_AS: if (argc == val) { @@ -116,25 +117,9 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { /* setup signal handler to cleanup dead proccesses */ /* TODO: replace this with a function that has constructor attribute? */ static bool cleanup = false; - if(cleanup == false) { + if (cleanup == false) { signal(SIGCHLD, cmd_exec_cleanup); cleanup = true; - /* Set it so filedescriptors are closed for executed programs */ - int flag_out = fcntl(STDOUT_FILENO, F_GETFD); - int flag_in = fcntl(STDIN_FILENO, F_GETFD); - int flag_err = fcntl(STDERR_FILENO, F_GETFD); - if (flag_out != -1) { - flag_out |= FD_CLOEXEC; - fcntl(STDOUT_FILENO, F_SETFD, flag_out); - } - if (flag_in != -1) { - flag_in |= FD_CLOEXEC; - fcntl(STDIN_FILENO, F_SETFD, flag_in); - } - if (flag_err != -1) { - flag_err |= FD_CLOEXEC; - fcntl(STDERR_FILENO, F_SETFD, flag_err); - } } if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { @@ -448,7 +433,7 @@ bool handle_command(struct sway_config *config, char *exec) { sway_log(L_ERROR, "Command failed: %s", cmd); } } - if(ptr) { + if (ptr) { free(cmd); } return exec_success; diff --git a/sway/log.c b/sway/log.c index 188461eb..b9048b34 100644 --- a/sway/log.c +++ b/sway/log.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include int colored = 1; int v = 0; @@ -15,6 +17,16 @@ const char *verbosity_colors[] = { void init_log(int verbosity) { v = verbosity; + /* set FD_CLOEXEC flag to prevent programs called with exec to write into + * logs */ + int i, flag; + int fd[] = { STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO }; + for (i = 0; i < 3; ++i) { + flag = fcntl(fd[i], F_GETFD); + if (flag != -1) { + fcntl(fd[i], F_SETFD, flag | FD_CLOEXEC); + } + } } void sway_log_colors(int mode) {