You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
C
66 lines
1.7 KiB
C
/*
|
|
* main.c: the daemon itself
|
|
*/
|
|
|
|
#include <syslog.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "config.h"
|
|
#include "trie.h"
|
|
#include "tasks.h"
|
|
|
|
void shutdown(int sigval);
|
|
|
|
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();
|
|
tasks_load();
|
|
|
|
// Set basic signal handlers
|
|
struct sigaction *term_sigh = (struct sigaction *) malloc(sizeof(struct sigaction));
|
|
term_sigh -> sa_handler = &shutdown;
|
|
term_sigh -> sa_mask = 0;
|
|
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 = 0;
|
|
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;
|
|
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, sizeof(struct sockaddr_un));
|
|
process(fd);
|
|
}
|
|
|
|
syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!");
|
|
return 5;
|
|
}
|
|
|
|
void shutdown (int signum) {
|
|
trie_save();
|
|
tasks_save();
|
|
close(sockfd);
|
|
unlink(cfg_socket);
|
|
}
|