moved fd modifying stuff to log.c

This commit is contained in:
Taiyu 2015-08-13 00:24:03 -07:00
parent c9d1eb1e02
commit f798e9bb0b
2 changed files with 20 additions and 23 deletions

View file

@ -6,7 +6,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include "stringop.h" #include "stringop.h"
#include "layout.h" #include "layout.h"
@ -20,7 +19,7 @@ struct modifier_key {
uint32_t mod; uint32_t mod;
}; };
struct modifier_key modifiers[] = { static struct modifier_key modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT }, { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS }, { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL }, { 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) { static bool checkarg(int argc, char *name, enum expected_args type, int val) {
switch(type) { switch (type) {
case EXPECTED_MORE_THEN: case EXPECTED_MORE_THEN:
if (argc > val) { if (argc > val) {
return true; return true;
} }
sway_log(L_ERROR, "Invalid %s command." 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; break;
case EXPECTED_LESS_THEN: case EXPECTED_LESS_THEN:
if (argc < val) { if (argc < val) {
return true; return true;
}; };
sway_log(L_ERROR, "Invalid %s command." 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; break;
case EXPECTED_SAME_AS: case EXPECTED_SAME_AS:
if (argc == val) { 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 */ /* setup signal handler to cleanup dead proccesses */
/* TODO: replace this with a function that has constructor attribute? */ /* TODO: replace this with a function that has constructor attribute? */
static bool cleanup = false; static bool cleanup = false;
if(cleanup == false) { if (cleanup == false) {
signal(SIGCHLD, cmd_exec_cleanup); signal(SIGCHLD, cmd_exec_cleanup);
cleanup = true; 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) { 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); sway_log(L_ERROR, "Command failed: %s", cmd);
} }
} }
if(ptr) { if (ptr) {
free(cmd); free(cmd);
} }
return exec_success; return exec_success;

View file

@ -2,6 +2,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int colored = 1; int colored = 1;
int v = 0; int v = 0;
@ -15,6 +17,16 @@ const char *verbosity_colors[] = {
void init_log(int verbosity) { void init_log(int verbosity) {
v = 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) { void sway_log_colors(int mode) {