commit 2e1efec7bbdebe1a293af303972ac0562ec5ce69 from: Caleb Stein date: Tue Aug 11 23:30:09 2026 UTC move mail to its own process commit - 0386ec3071e4e205bd9cfb7730d7bdc27f0f0637 commit + 2e1efec7bbdebe1a293af303972ac0562ec5ce69 blob - 10c5e40f61e9c931a9a6eafc895afc0263f056f4 blob + a35938e3106115a6f9911eafbfd21c8be054d771 --- include/statbar.h +++ include/statbar.h @@ -47,11 +47,10 @@ extern pid_t start_volume_process(int *pipe_fd); extern pid_t start_timer_process(int *pipe_fd, int timerval_fd); /* Mail */ -extern char mail_string[7]; +#define MAIL_STRING_SIZE 8 -extern void get_mail(void); +extern pid_t start_mail_process(int *pipe_fd); extern char **get_append_mail_path_ptr(void); -extern void close_mail(void); /* Network */ #define NETWORK_STRING_SIZE 8 blob - a12a3dc312ce17805a89431f02c513fdbdc46287 blob + 859dc469236d9ba61b8f7d10afba5fec59ce1ef6 --- modules/mail.c +++ modules/mail.c @@ -3,14 +3,16 @@ #include #include +#include +#include #include "statbar.h" #define MAX_MAILDIRS 8 -char mail_string[7]; - static char *mail_path[MAX_MAILDIRS]; +static sig_atomic_t should_quit = 0; +static sig_atomic_t reload = 0; char ** get_append_mail_path_ptr(void) @@ -25,8 +27,8 @@ get_append_mail_path_ptr(void) return NULL; } -void -get_mail(void) +static void +get_mail(int fd) { DIR *dir; struct dirent *dp; @@ -56,10 +58,10 @@ get_mail(void) if (new_mail || ++i == MAX_MAILDIRS) break; } - (void)snprintf(mail_string, 7, "%s ", new_mail ? MAIL : ""); + (void)dprintf(fd, "%s ", new_mail ? MAIL : ""); } -void +static void close_mail(void) { int i = 0; @@ -72,3 +74,71 @@ close_mail(void) } } +static void +signal_handler(int sig) +{ + if (sig == SIGTERM || sig == SIGINT) should_quit = 1; + if (sig == SIGUSR1) reload = 1; +} + +pid_t +start_mail_process(int *pipe_fd) +{ + pid_t pid; + int mail_pipe[2]; + + if (pipe(mail_pipe) == -1) + { + perror("pipe"); + + return -1; + } + *pipe_fd = mail_pipe[0]; + + pid = fork(); + if (pid) + { + if (pid == -1) (void)close(mail_pipe[0]); + (void)close(mail_pipe[1]); + + return pid; + } + + (void)close(mail_pipe[0]); + setproctitle("mail"); + (void)puts("Mail process started"); + + (void)signal(SIGTERM, signal_handler); + (void)signal(SIGINT, signal_handler); + (void)signal(SIGUSR1, signal_handler); + + if (pledge("stdio rpath", NULL) == -1) + { + perror("pledge"); + (void)close(mail_pipe[0]); + (void)close(mail_pipe[1]); + + _exit(1); + } + + get_mail(mail_pipe[1]); + + while(!should_quit) + { + if (reload) + { + reload = 0; + + get_mail(mail_pipe[1]); + } + (void)pause(); + + if (getppid() == 1) break; + } + (void)puts("Closing mail process"); + (void)close(mail_pipe[1]); + close_mail(); + + _exit(0); +} + blob - 2ff440e2976c78a1329a3bd7ee7accf034a7dbd6 blob + 6e727bb37bc5ddc79682c0d32ab9e2d4a0b4176c --- src/main.c +++ src/main.c @@ -13,12 +13,9 @@ #include "statbar.h" -#define NFDS 6 +#define NFDS 7 static volatile sig_atomic_t should_quit = 0; -static int renormalize_clock = 0; -static int reload_mail = 0; -static int reload_battery = 0; void sig_handler(int sig) @@ -55,7 +52,6 @@ install_signal_handlers(void) int main(void) { - bool dirty = true; int output_fd; int clock_fd; int batt_fd; @@ -63,13 +59,14 @@ main(void) int timer_fd; int timerval_fd; int network_fd; + int mail_fd; pid_t clock_pid; pid_t batt_pid; pid_t vol_pid; pid_t timer_pid; pid_t network_pid; + pid_t mail_pid; unsigned char cmd; - bool network_open = false; struct pollfd pfd[NFDS]; struct pollfd *cmd_pfd = &pfd[0]; struct pollfd *clock_pfd = &pfd[1]; @@ -77,11 +74,16 @@ main(void) struct pollfd *vol_pfd = &pfd[3]; struct pollfd *timer_pfd = &pfd[4]; struct pollfd *network_pfd = &pfd[5]; + struct pollfd *mail_pfd = &pfd[6]; char clock_string[CLOCK_STRING_SIZE + 1]; char battery_string[BATTERY_STRING_SIZE + 1]; char volume_string[VOLUME_STRING_SIZE + 1]; char timer_string[TIMER_STRING_SIZE + 1]; char network_string[NETWORK_STRING_SIZE + 1]; + char mail_string[MAIL_STRING_SIZE + 1]; + int renormalize_clock = 0; + int reload_mail = 0; + int reload_battery = 0; int i; ssize_t n; @@ -112,6 +114,7 @@ main(void) vol_pid = start_volume_process(&vol_fd); timer_pid = start_timer_process(&timer_fd, timerval_fd); network_pid = start_network_process(&network_fd); + mail_pid = start_mail_process(&mail_fd); cmd_pfd->fd = open_command_interface(); cmd_pfd->events = POLLIN; @@ -140,7 +143,11 @@ main(void) network_pfd->fd = network_fd; network_pfd->events = POLLIN; } - get_mail(); + if (mail_pid > 0) + { + mail_pfd->fd = mail_fd; + mail_pfd->events = POLLIN; + } if (pledge("stdio rpath cpath proc", NULL) == -1) { @@ -197,8 +204,11 @@ main(void) n = read(network_pfd->fd, network_string, NETWORK_STRING_SIZE); if (n > 0) network_string[n] = '\0'; } - - dirty = true; + if (mail_pfd->revents & POLLIN) + { + n = read(mail_pfd->fd, mail_string, MAIL_STRING_SIZE); + if (n > 0) mail_string[n] = '\0'; + } } if (renormalize_clock) @@ -211,8 +221,7 @@ main(void) if (reload_mail) { reload_mail = 0; - get_mail(); - dirty = true; + (void)kill(mail_pid, SIGUSR1); } if (reload_battery) @@ -221,17 +230,13 @@ main(void) (void)kill(batt_pid, SIGUSR1); } - if (dirty) - { - (void)dprintf(output_fd, "%s | %s | %s | %s | %s%s\n", - clock_string, - battery_string, - volume_string, - timer_string, - network_string, - mail_string); - dirty = false; - } + (void)dprintf(output_fd, "%s | %s | %s | %s | %s%s\n", + clock_string, + battery_string, + volume_string, + timer_string, + network_string, + mail_string); } cleanup: @@ -241,7 +246,7 @@ cleanup: if (vol_pid > 0) (void)kill(vol_pid, SIGTERM); if (timer_pid > 0) (void)kill(timer_pid, SIGTERM); if (network_pid > 0) (void)kill(network_pid, SIGTERM); - close_mail(); + if (mail_pid > 0) (void)kill(mail_pid, SIGTERM); close_fifos(); return 0;