aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-17 21:45:07 -0600
committerLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-18 15:25:37 -0600
commitcee26500a86ccc6b508ae927136beadc4279de14 (patch)
treee2b42b472e799f6b5a79c0174fe249205ff52035 /sway/config.c
parentMerge pull request #1173 from JerziKaminsky/security_resolve_symlink (diff)
downloadsway-cee26500a86ccc6b508ae927136beadc4279de14.tar.gz
sway-cee26500a86ccc6b508ae927136beadc4279de14.tar.zst
sway-cee26500a86ccc6b508ae927136beadc4279de14.zip
Prevent sway from duplicating on a failed fork
Also remove a useless `sway_log` and replace it with a pipe
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c34
1 files changed, 32 insertions, 2 deletions
diff --git a/sway/config.c b/sway/config.c
index c8432a2a..ae09d4f8 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -912,8 +912,16 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
912} 912}
913 913
914static void invoke_swaybar(struct bar_config *bar) { 914static void invoke_swaybar(struct bar_config *bar) {
915 // Pipe to communicate errors
916 int filedes[2];
917 if (pipe(filedes) == -1) {
918 sway_log(L_ERROR, "Pipe setup failed! Cannot fork into bar");
919 return;
920 }
921
915 bar->pid = fork(); 922 bar->pid = fork();
916 if (bar->pid == 0) { 923 if (bar->pid == 0) {
924 close(filedes[0]);
917 if (!bar->swaybar_command) { 925 if (!bar->swaybar_command) {
918 char *const cmd[] = { 926 char *const cmd[] = {
919 "swaybar", 927 "swaybar",
@@ -922,14 +930,20 @@ static void invoke_swaybar(struct bar_config *bar) {
922 NULL, 930 NULL,
923 }; 931 };
924 932
933 close(filedes[1]);
925 execvp(cmd[0], cmd); 934 execvp(cmd[0], cmd);
935 _exit(EXIT_SUCCESS);
926 } else { 936 } else {
927 // run custom swaybar 937 // run custom swaybar
928 int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; 938 int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5;
929 char *command = malloc(len * sizeof(char)); 939 char *command = malloc(len * sizeof(char));
930 if (!command) { 940 if (!command) {
931 sway_log(L_ERROR, "Unable to allocate swaybar command string"); 941 const char msg[] = "Unable to allocate swaybar command string";
932 return; 942 int len = sizeof(msg);
943 write(filedes[1], &len, sizeof(int));
944 write(filedes[1], msg, len);
945 close(filedes[1]);
946 _exit(EXIT_FAILURE);
933 } 947 }
934 snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); 948 snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id);
935 949
@@ -940,10 +954,26 @@ static void invoke_swaybar(struct bar_config *bar) {
940 NULL, 954 NULL,
941 }; 955 };
942 956
957 close(filedes[1]);
943 execvp(cmd[0], cmd); 958 execvp(cmd[0], cmd);
944 free(command); 959 free(command);
960 _exit(EXIT_SUCCESS);
961 }
962 }
963 close(filedes[0]);
964 int len;
965 if(read(filedes[1], &len, sizeof(int)) == sizeof(int)) {
966 char *buf = malloc(len);
967 if(!buf) {
968 sway_log(L_ERROR, "Cannot allocate error string");
969 return;
970 }
971 if(read(filedes[1], buf, len)) {
972 sway_log(L_ERROR, "%s", buf);
945 } 973 }
974 free(buf);
946 } 975 }
976 close(filedes[1]);
947} 977}
948 978
949static void terminate_swaybar(pid_t pid) { 979static void terminate_swaybar(pid_t pid) {