Skip to content

Commit

Permalink
Terminating applications on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Feb 23, 2022
1 parent ef85d23 commit f344030
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
19 changes: 17 additions & 2 deletions src/desktop-application.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,21 @@ struct dapplication *select_application(struct category_option *co) {
return NULL;
}

void shutdown_all_daemons(void) {
struct plist *pl;
while ((pl = plist_peek())) {
if (kill(pl->pid, SIGTERM) == -1) {
plist_pop();
continue;
}
plist_wait(pl, 1000);
if (!pl->status_changed) {
kill(pl->pid, SIGKILL);
}
plist_pop();
}
}

void shutdown_daemon(struct category_option *co) {
struct plist *pl;
pl = plist_search(NULL, co->name);
Expand All @@ -415,9 +430,9 @@ void shutdown_daemon(struct category_option *co) {
return;
plist_wait(pl, 1000);
if (!pl->status_changed) {
if (kill(pl->pid, SIGKILL) == -1)
return;
kill(pl->pid, SIGKILL);
}
plist_remove(pl->pid);
}

void shutdown_optionals(struct category_option *co) {
Expand Down
1 change: 1 addition & 0 deletions src/desktop-application.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void load_applications_from_dir(const char *dir);
int print_application(struct dapplication *a);
int print_applications(void);
struct dapplication *select_application(struct category_option *co);
void shutdown_all_daemons(void);
void shutdown_daemon(struct category_option *co);
void shutdown_optionals(struct category_option *co);
void startup_daemon(struct category_option *co);
Expand Down
4 changes: 4 additions & 0 deletions src/pademelon-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ static void setup_signals(void) {
status = sigaction(SIGINT, &sigaction_sigint_handler, NULL);
if (status == -1)
die("Unable to install signal handler");
status = sigaction(SIGTERM, &sigaction_sigint_handler, NULL);
if (status == -1)
die("Unable to install signal handler");

/* handle SIGUSR1 */
status = sigfillset(&sigaction_sigusr1_handler.sa_mask); // @TODO do I have to block anything here?
Expand Down Expand Up @@ -293,6 +296,7 @@ int main(int argc, char *argv[]) {
startup_daemons();
loop();

shutdown_all_daemons();
#ifdef X11
x11_deinit();
#endif /* X11 */
Expand Down
26 changes: 19 additions & 7 deletions src/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,33 @@ void plist_free(void) {
plist_remove(plist_head->pid);
}

struct plist *plist_peek(void) {
return plist_head;
}

struct plist *plist_pop(void) {
struct plist *pl_remove = NULL;
pl_remove = plist_head;
if (plist_head)
plist_head = plist_head->next;
return pl_remove;
}

void plist_remove(pid_t pid) {
struct plist *pl, *pl_delete = NULL;
struct plist *pl, *pl_remove = NULL;

block_signal(SIGCHLD);

/* check if head matches */
if (plist_head->pid == pid) {
pl_delete = plist_head;
plist_head = pl_delete->next;
pl_remove = plist_head;
plist_head = pl_remove->next;
} else {
/* search for the entry before pid */
for (pl = plist_head; pl && pl->next; pl = pl->next) {
if (pl->next->pid == pid) {
pl_delete = pl->next;
pl->next = pl_delete->next;
pl_remove = pl->next;
pl->next = pl_remove->next;
break;
}
}
Expand All @@ -144,7 +156,7 @@ void plist_remove(pid_t pid) {
unblock_signal(SIGCHLD);

/* free item (NULL anyway if not found) */
free(pl_delete);
free(pl_remove);
}

struct plist *plist_search(char *id_name, char *category) {
Expand Down Expand Up @@ -190,7 +202,7 @@ void plist_sigchld_handler(int signal) {

void plist_wait(struct plist *pl, long timeout_milli) {
int status;
struct timespec ts = { .tv_sec = 0, .tv_nsec = timeout_milli + 1000 };
struct timespec ts = { .tv_sec = 0, .tv_nsec = timeout_milli * 1000 };
while (!pl->status_changed) {
status = nanosleep(&ts, &ts);
if (status == 0 /* time elapsed */
Expand Down
2 changes: 2 additions & 0 deletions src/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct plist *plist_add(pid_t pid, void *content);
void plist_free(void);
struct plist *plist_get(pid_t pid);
struct plist *plist_next_event(struct plist *from);
struct plist *plist_peek(void);
struct plist *plist_pop(void);
void plist_remove(pid_t pid);
struct plist *plist_search(char *id_name, char *category);
void plist_wait(struct plist *pl, long timeout_milli);
Expand Down

0 comments on commit f344030

Please sign in to comment.