style, safer fork

This commit is contained in:
taiyu 2015-09-15 06:56:51 -07:00
parent a18caf6706
commit a04f645d7c

View file

@ -179,7 +179,7 @@ static enum cmd_status cmd_bindsym(int argc, char **argv) {
} }
static enum cmd_status cmd_exec_always(int argc, char **argv) { static enum cmd_status cmd_exec_always(int argc, char **argv) {
if (!config->active) return CMD_DEFER;; if (!config->active) return CMD_DEFER;
if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) { if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -190,27 +190,21 @@ static enum cmd_status cmd_exec_always(int argc, char **argv) {
free(tmp); free(tmp);
char *args[] = {"sh", "-c", cmd, 0 }; char *args[] = {"sh", "-c", cmd, 0 };
sway_log(L_DEBUG, "Executing %s", cmd);
pid_t pid = vfork(); pid_t pid;
/* Failed to fork */ if ((pid = vfork()) == 0) {
if (pid < 0) { execv("/bin/sh", args);
sway_log(L_ERROR, "exec command failed, sway did not fork"); _exit(-1);
} else if (pid < 0) {
sway_log(L_ERROR, "exec command failed, sway could not fork");
return CMD_FAILURE; return CMD_FAILURE;
} }
/* Child process */
if (pid == 0) {
sway_log(L_DEBUG, "Executing %s", cmd);
execv("/bin/sh", args);
/* Execv doesnt return unless failure */
sway_log(L_ERROR, "execv failde to return");
_exit(-1);
}
/* Parent */
return CMD_SUCCESS; return CMD_SUCCESS;
} }
static enum cmd_status cmd_exec(int argc, char **argv) { static enum cmd_status cmd_exec(int argc, char **argv) {
if (!config->active) return CMD_DEFER;; if (!config->active) return CMD_DEFER;
if (config->reloading) { if (config->reloading) {
char *args = join_args(argv, argc); char *args = join_args(argv, argc);
@ -228,8 +222,8 @@ static void kill_views(swayc_t *container, void *data) {
} }
static enum cmd_status cmd_exit(int argc, char **argv) { static enum cmd_status cmd_exit(int argc, char **argv) {
if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0) if (config->reading) return CMD_INVALID;
|| config->reading || !config->active) { if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
// Close all views // Close all views
@ -239,8 +233,8 @@ static enum cmd_status cmd_exit(int argc, char **argv) {
} }
static enum cmd_status cmd_floating(int argc, char **argv) { static enum cmd_status cmd_floating(int argc, char **argv) {
if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1) if (config->reading) return CMD_INVALID;
|| config->reading || !config->active) { if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -301,8 +295,8 @@ static enum cmd_status cmd_floating(int argc, char **argv) {
} }
static enum cmd_status cmd_floating_mod(int argc, char **argv) { static enum cmd_status cmd_floating_mod(int argc, char **argv) {
if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1) if (!config->reading) return CMD_INVALID;
|| !config->reading) { if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
int i, j; int i, j;
@ -326,10 +320,10 @@ static enum cmd_status cmd_floating_mod(int argc, char **argv) {
} }
static enum cmd_status cmd_focus(int argc, char **argv) { static enum cmd_status cmd_focus(int argc, char **argv) {
if (config->reading) return CMD_INVALID;
static int floating_toggled_index = 0; static int floating_toggled_index = 0;
static int tiled_toggled_index = 0; static int tiled_toggled_index = 0;
if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1) if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) {
|| config->reading || !config->active) {
return CMD_FAILURE; return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "left") == 0) { if (strcasecmp(argv[0], "left") == 0) {
@ -384,6 +378,7 @@ static enum cmd_status cmd_focus(int argc, char **argv) {
} }
static enum cmd_status cmd_focus_follows_mouse(int argc, char **argv) { static enum cmd_status cmd_focus_follows_mouse(int argc, char **argv) {
if (!config->reading) return CMD_INVALID;
if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -393,7 +388,7 @@ static enum cmd_status cmd_focus_follows_mouse(int argc, char **argv) {
} }
static void hide_view_in_scratchpad(swayc_t *sp_view) { static void hide_view_in_scratchpad(swayc_t *sp_view) {
if(sp_view == NULL) { if (sp_view == NULL) {
return; return;
} }
@ -416,7 +411,7 @@ static enum cmd_status cmd_mode(int argc, char **argv) {
int mode_len = strlen(mode_name); int mode_len = strlen(mode_name);
bool mode_make = mode_name[mode_len-1] == '{'; bool mode_make = mode_name[mode_len-1] == '{';
if (mode_make) { if (mode_make) {
if (!config->reading) return CMD_FAILURE;; if (!config->reading) return CMD_INVALID;
// Trim trailing spaces // Trim trailing spaces
do { do {
mode_name[--mode_len] = 0; mode_name[--mode_len] = 0;
@ -454,7 +449,7 @@ static enum cmd_status cmd_mode(int argc, char **argv) {
} }
static enum cmd_status cmd_move(int argc, char **argv) { static enum cmd_status cmd_move(int argc, char **argv) {
if (config->reading) return CMD_FAILURE;; if (config->reading) return CMD_FAILURE;
if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -525,7 +520,7 @@ static enum cmd_status cmd_move(int argc, char **argv) {
} }
static enum cmd_status cmd_orientation(int argc, char **argv) { static enum cmd_status cmd_orientation(int argc, char **argv) {
if (!config->reading) return CMD_FAILURE;; if (!config->reading) return CMD_FAILURE;
if (!checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -543,7 +538,7 @@ static enum cmd_status cmd_orientation(int argc, char **argv) {
} }
static enum cmd_status cmd_output(int argc, char **argv) { static enum cmd_status cmd_output(int argc, char **argv) {
if (!config->reading) return CMD_FAILURE;; if (!config->reading) return CMD_FAILURE;
if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) {
return CMD_FAILURE; return CMD_FAILURE;
} }
@ -994,7 +989,7 @@ static enum cmd_status cmd_log_colors(int argc, char **argv) {
} }
if (strcasecmp(argv[0], "no") == 0) { if (strcasecmp(argv[0], "no") == 0) {
sway_log_colors(0); sway_log_colors(0);
} else if(strcasecmp(argv[0], "yes") == 0) { } else if (strcasecmp(argv[0], "yes") == 0) {
sway_log_colors(1); sway_log_colors(1);
} else { } else {
sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);