summaryrefslogtreecommitdiffstats
path: root/swaybar/main.c
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-18 03:02:35 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-18 03:15:49 +0100
commit529ee83ef63d69f3cc045f2e9cc52c7ec2fe1334 (patch)
treeff7041a0c7765c525e6444aca6b1fafcdbbd15a8 /swaybar/main.c
parentMerge pull request #352 from progandy/workspace-numbers (diff)
downloadsway-529ee83ef63d69f3cc045f2e9cc52c7ec2fe1334.tar.gz
sway-529ee83ef63d69f3cc045f2e9cc52c7ec2fe1334.tar.zst
sway-529ee83ef63d69f3cc045f2e9cc52c7ec2fe1334.zip
swaybar: terminate status_command process
Fix #346 Send SIGTERM to the `status_command` process before swaybar exits.
Diffstat (limited to 'swaybar/main.c')
-rw-r--r--swaybar/main.c42
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
48list_t *workspaces = NULL; 49list_t *workspaces = NULL;
49int socketfd; 50int socketfd;
51pid_t pid;
52int pipefd[2];
50FILE *command; 53FILE *command;
51char *line, *output, *status_command; 54char *line, *output, *status_command;
52struct registry *registry; 55struct 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}