summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Taiyu <taiyu.len@gmail.com>2015-08-13 00:24:03 -0700
committerLibravatar Taiyu <taiyu.len@gmail.com>2015-08-13 00:24:03 -0700
commitf798e9bb0bc667d07283f32d1d83b6223d375a03 (patch)
tree962841ebe54827df9800a4550e9c2bbe105090e3
parentMerge branch 'master' of https://github.com/SirCmpwn/sway (diff)
downloadsway-f798e9bb0bc667d07283f32d1d83b6223d375a03.tar.gz
sway-f798e9bb0bc667d07283f32d1d83b6223d375a03.tar.zst
sway-f798e9bb0bc667d07283f32d1d83b6223d375a03.zip
moved fd modifying stuff to log.c
-rw-r--r--sway/commands.c31
-rw-r--r--sway/log.c12
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 @@
6#include <string.h> 6#include <string.h>
7#include <unistd.h> 7#include <unistd.h>
8#include <sys/wait.h> 8#include <sys/wait.h>
9#include <fcntl.h>
10#include <ctype.h> 9#include <ctype.h>
11#include "stringop.h" 10#include "stringop.h"
12#include "layout.h" 11#include "layout.h"
@@ -20,7 +19,7 @@ struct modifier_key {
20 uint32_t mod; 19 uint32_t mod;
21}; 20};
22 21
23struct modifier_key modifiers[] = { 22static struct modifier_key modifiers[] = {
24 { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT }, 23 { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
25 { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS }, 24 { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
26 { XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL }, 25 { XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
@@ -38,20 +37,22 @@ enum expected_args {
38}; 37};
39 38
40static bool checkarg(int argc, char *name, enum expected_args type, int val) { 39static bool checkarg(int argc, char *name, enum expected_args type, int val) {
41 switch(type) { 40 switch (type) {
42 case EXPECTED_MORE_THEN: 41 case EXPECTED_MORE_THEN:
43 if (argc > val) { 42 if (argc > val) {
44 return true; 43 return true;
45 } 44 }
46 sway_log(L_ERROR, "Invalid %s command." 45 sway_log(L_ERROR, "Invalid %s command."
47 "(expected more then %d arguments, got %d", name, val, argc); 46 "(expected more then %d argument%s, got %d",
47 name, val, (char*[2]){"s", ""}[argc==1], argc);
48 break; 48 break;
49 case EXPECTED_LESS_THEN: 49 case EXPECTED_LESS_THEN:
50 if (argc < val) { 50 if (argc < val) {
51 return true; 51 return true;
52 }; 52 };
53 sway_log(L_ERROR, "Invalid %s command." 53 sway_log(L_ERROR, "Invalid %s command."
54 "(expected less then %d arguments, got %d", name, val, argc); 54 "(expected less then %d argument%s, got %d",
55 name, val, (char*[2]){"s", ""}[argc==1], argc);
55 break; 56 break;
56 case EXPECTED_SAME_AS: 57 case EXPECTED_SAME_AS:
57 if (argc == val) { 58 if (argc == val) {
@@ -116,25 +117,9 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) {
116 /* setup signal handler to cleanup dead proccesses */ 117 /* setup signal handler to cleanup dead proccesses */
117 /* TODO: replace this with a function that has constructor attribute? */ 118 /* TODO: replace this with a function that has constructor attribute? */
118 static bool cleanup = false; 119 static bool cleanup = false;
119 if(cleanup == false) { 120 if (cleanup == false) {
120 signal(SIGCHLD, cmd_exec_cleanup); 121 signal(SIGCHLD, cmd_exec_cleanup);
121 cleanup = true; 122 cleanup = true;
122 /* Set it so filedescriptors are closed for executed programs */
123 int flag_out = fcntl(STDOUT_FILENO, F_GETFD);
124 int flag_in = fcntl(STDIN_FILENO, F_GETFD);
125 int flag_err = fcntl(STDERR_FILENO, F_GETFD);
126 if (flag_out != -1) {
127 flag_out |= FD_CLOEXEC;
128 fcntl(STDOUT_FILENO, F_SETFD, flag_out);
129 }
130 if (flag_in != -1) {
131 flag_in |= FD_CLOEXEC;
132 fcntl(STDIN_FILENO, F_SETFD, flag_in);
133 }
134 if (flag_err != -1) {
135 flag_err |= FD_CLOEXEC;
136 fcntl(STDERR_FILENO, F_SETFD, flag_err);
137 }
138 } 123 }
139 124
140 if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { 125 if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) {
@@ -448,7 +433,7 @@ bool handle_command(struct sway_config *config, char *exec) {
448 sway_log(L_ERROR, "Command failed: %s", cmd); 433 sway_log(L_ERROR, "Command failed: %s", cmd);
449 } 434 }
450 } 435 }
451 if(ptr) { 436 if (ptr) {
452 free(cmd); 437 free(cmd);
453 } 438 }
454 return exec_success; 439 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 @@
2#include <stdarg.h> 2#include <stdarg.h>
3#include <stdio.h> 3#include <stdio.h>
4#include <stdlib.h> 4#include <stdlib.h>
5#include <fcntl.h>
6#include <unistd.h>
5 7
6int colored = 1; 8int colored = 1;
7int v = 0; 9int v = 0;
@@ -15,6 +17,16 @@ const char *verbosity_colors[] = {
15 17
16void init_log(int verbosity) { 18void init_log(int verbosity) {
17 v = verbosity; 19 v = verbosity;
20 /* set FD_CLOEXEC flag to prevent programs called with exec to write into
21 * logs */
22 int i, flag;
23 int fd[] = { STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO };
24 for (i = 0; i < 3; ++i) {
25 flag = fcntl(fd[i], F_GETFD);
26 if (flag != -1) {
27 fcntl(fd[i], F_SETFD, flag | FD_CLOEXEC);
28 }
29 }
18} 30}
19 31
20void sway_log_colors(int mode) { 32void sway_log_colors(int mode) {