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.
80 lines
2.1 KiB
C
80 lines
2.1 KiB
C
7 years ago
|
/*
|
||
|
* main.c: the daemon itself
|
||
|
*/
|
||
|
|
||
7 years ago
|
#include <unistd.h>
|
||
7 years ago
|
#include <syslog.h>
|
||
7 years ago
|
#include <stdlib.h>
|
||
7 years ago
|
#include <string.h>
|
||
|
#include <stdbool.h>
|
||
7 years ago
|
#include <sys/socket.h>
|
||
|
#include <sys/un.h>
|
||
|
#include <signal.h>
|
||
7 years ago
|
|
||
|
#include "config.h"
|
||
|
#include "trie.h"
|
||
7 years ago
|
#include "task.h"
|
||
|
#include "daemonize.h"
|
||
7 years ago
|
|
||
7 years ago
|
void term_handle(int sigval);
|
||
|
bool process(int fd);
|
||
7 years ago
|
|
||
|
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
|
||
7 years ago
|
trie_load(cfg_trie_file);
|
||
|
task_load(cfg_task_file);
|
||
7 years ago
|
|
||
|
// Set basic signal handlers
|
||
7 years ago
|
sigset_t empty;
|
||
|
sigemptyset(&empty); // Shall not fail
|
||
7 years ago
|
struct sigaction *term_sigh = (struct sigaction *) malloc(sizeof(struct sigaction));
|
||
7 years ago
|
term_sigh -> sa_handler = &term_handle;
|
||
|
term_sigh -> sa_mask = empty;
|
||
7 years ago
|
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
|
||
7 years ago
|
chld_sigh -> sa_mask = empty;
|
||
7 years ago
|
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;
|
||
7 years ago
|
socklen_t addrsize = sizeof(struct sockaddr_un);
|
||
7 years ago
|
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) {
|
||
7 years ago
|
int fd = accept(sockfd, (struct sockaddr *) &addr, &addrsize);
|
||
7 years ago
|
process(fd);
|
||
|
}
|
||
|
|
||
|
syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!");
|
||
|
return 5;
|
||
|
}
|
||
|
|
||
7 years ago
|
void term_handle (int signum) {
|
||
|
trie_save(cfg_trie_file);
|
||
|
task_save(cfg_task_file);
|
||
7 years ago
|
close(sockfd);
|
||
|
unlink(cfg_socket);
|
||
7 years ago
|
exit(0);
|
||
|
return;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
bool process(int fd) {}
|