/* * main.c: the daemon itself */ #include #include #include #include #include #include #include #include #include "config.h" #include "trie.h" #include "task.h" #include "daemonize.h" void term_handle(int sigval); bool process(int fd); static int sockfd; int main (void) { openlog(cfg_log_ident, cfg_log_opt, cfg_log_facility); //I will probably close its fd a moment later, FIXME // No arguments, if you want something, go change source, according to documentation. daemonize(0); // Load saved state -- variable values and tasks trie_load(cfg_trie_file); task_load(cfg_task_file); // Set basic signal handlers sigset_t empty; sigemptyset(&empty); // Shall not fail struct sigaction *term_sigh = (struct sigaction *) malloc(sizeof(struct sigaction)); term_sigh -> sa_handler = &term_handle; term_sigh -> sa_mask = empty; term_sigh -> sa_flags = 0; sigaction(SIGTERM, term_sigh, NULL); struct sigaction *chld_sigh = (struct sigaction *) malloc(sizeof(struct sigaction)); chld_sigh -> sa_handler = SIG_IGN; // We don't care for children chld_sigh -> sa_mask = empty; chld_sigh -> sa_flags = SA_NOCLDWAIT; sigaction(SIGCHLD, chld_sigh, NULL); // Setup a socket to listen on sockfd = socket(AF_UNIX, SOCK_STREAM, 0); struct sockaddr_un addr; socklen_t addrsize = sizeof(struct sockaddr_un); addr.sun_family = AF_UNIX; memcpy(&addr.sun_path, cfg_socket, strlen(cfg_socket)); bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)); listen(sockfd, cfg_sock_maxclients); // Everything prepared syslog(cfg_log_facility | LOG_INFO, "Started"); // Main loop: (everything else is handled by process() and signal handlers). while (true) { int fd = accept(sockfd, (struct sockaddr *) &addr, &addrsize); process(fd); } syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!"); return 5; } void term_handle (int signum) { trie_save(cfg_trie_file); task_save(cfg_task_file); close(sockfd); unlink(cfg_socket); exit(0); return; } bool process(int fd) {}