diff options
-rw-r--r-- | swaybar/main.c | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/swaybar/main.c b/swaybar/main.c index 0a25d0f5..27eebcaa 100644 --- a/swaybar/main.c +++ b/swaybar/main.c | |||
@@ -3,6 +3,7 @@ | |||
3 | #include <string.h> | 3 | #include <string.h> |
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | #include <stdbool.h> | 5 | #include <stdbool.h> |
6 | #include <unistd.h> | ||
6 | #include <stropts.h> | 7 | #include <stropts.h> |
7 | #include <json-c/json.h> | 8 | #include <json-c/json.h> |
8 | #include <sys/un.h> | 9 | #include <sys/un.h> |
@@ -47,6 +48,8 @@ struct workspace { | |||
47 | 48 | ||
48 | list_t *workspaces = NULL; | 49 | list_t *workspaces = NULL; |
49 | int socketfd; | 50 | int socketfd; |
51 | pid_t pid; | ||
52 | int pipefd[2]; | ||
50 | FILE *command; | 53 | FILE *command; |
51 | char *line, *output, *status_command; | 54 | char *line, *output, *status_command; |
52 | struct registry *registry; | 55 | struct registry *registry; |
@@ -90,6 +93,21 @@ void sway_terminate(void) { | |||
90 | if (registry) { | 93 | if (registry) { |
91 | registry_teardown(registry); | 94 | registry_teardown(registry); |
92 | } | 95 | } |
96 | |||
97 | if (command) { | ||
98 | fclose(command); | ||
99 | } | ||
100 | |||
101 | if (pid) { | ||
102 | // terminate status_command process | ||
103 | kill(pid, SIGTERM); | ||
104 | } | ||
105 | |||
106 | if (pipefd[0]) { | ||
107 | close(pipefd[0]); | ||
108 | } | ||
109 | |||
110 | free(line); | ||
93 | exit(EXIT_FAILURE); | 111 | exit(EXIT_FAILURE); |
94 | } | 112 | } |
95 | 113 | ||
@@ -417,7 +435,24 @@ int main(int argc, char **argv) { | |||
417 | bar_ipc_init(desired_output, bar_id); | 435 | bar_ipc_init(desired_output, bar_id); |
418 | 436 | ||
419 | if (status_command) { | 437 | if (status_command) { |
420 | command = popen(status_command, "r"); | 438 | pipe(pipefd); |
439 | pid = fork(); | ||
440 | if (pid == 0) { | ||
441 | close(pipefd[0]); | ||
442 | dup2(pipefd[1], STDOUT_FILENO); | ||
443 | close(pipefd[1]); | ||
444 | char *const cmd[] = { | ||
445 | "sh", | ||
446 | "-c", | ||
447 | status_command, | ||
448 | NULL, | ||
449 | }; | ||
450 | execvp(cmd[0], cmd); | ||
451 | return 0; | ||
452 | } | ||
453 | |||
454 | close(pipefd[1]); | ||
455 | command = fdopen(pipefd[0], "r"); | ||
421 | line = malloc(1024); | 456 | line = malloc(1024); |
422 | line[0] = '\0'; | 457 | line[0] = '\0'; |
423 | } | 458 | } |
@@ -443,6 +478,11 @@ int main(int argc, char **argv) { | |||
443 | 478 | ||
444 | window_teardown(window); | 479 | window_teardown(window); |
445 | registry_teardown(registry); | 480 | registry_teardown(registry); |
481 | fclose(command); | ||
482 | // terminate status_command process | ||
483 | kill(pid, SIGTERM); | ||
484 | close(pipefd[0]); | ||
485 | free(line); | ||
446 | 486 | ||
447 | return 0; | 487 | return 0; |
448 | } | 488 | } |