1
0
Fork 0

2019-02-04 22:01:42

tmp
LEdoian 6 years ago
parent 5a26e21e05
commit 9c33ef2dc1

@ -21,34 +21,41 @@ void async_kill(int sig) {
} }
void async_shutdown (int sig) { void async_shutdown (int sig) {
async_save(sig); syslog(cfg_log_facility | LOG_INFO, "Shutting down");
close(sockfd); if(close(sockfd) == -1) {
syslog(cfg_log_facility | LOG_ERR, "async: close failed: %m");
}
if(unlink(cfg_socket) == -1) { if(unlink(cfg_socket) == -1) {
syslog(cfg_log_facility | LOG_ERR, "async: unlink failed: %m"); syslog(cfg_log_facility | LOG_ERR, "async: unlink failed: %m");
} }
async_save(sig);
async_kill(sig); async_kill(sig);
return; return;
} }
void async_save(int sig) { void async_save(int sig) {
syslog(cfg_log_facility | LOG_INFO, "Saving everything");
task_save(cfg_task_file); task_save(cfg_task_file);
trie_save(cfg_trie_file); trie_save(cfg_trie_file);
return; return;
} }
void async_flush(int sig) { void async_flush(int sig) {
syslog(cfg_log_facility | LOG_INFO, "Flushing everything");
task_flush(); task_flush();
trie_flush(); trie_flush();
return; return;
} }
void async_load (int sig) { void async_load (int sig) {
syslog(cfg_log_facility | LOG_INFO, "Loading everything");
task_load(cfg_task_file); task_load(cfg_task_file);
trie_load(cfg_trie_file); trie_load(cfg_trie_file);
return; return;
} }
void async_restart (int sig) { void async_restart (int sig) {
syslog(cfg_log_facility | LOG_INFO, "Restarting");
async_save(sig); async_save(sig);
async_flush(sig); async_flush(sig);
async_load(sig); async_load(sig);

@ -28,8 +28,9 @@ int sockfd; //async.h has to be able to use it, and I would hate to write a gett
int main (void) { int main (void) {
openlog(cfg_log_ident, cfg_log_opt, cfg_log_facility); //I will probably close its fd a moment later, FIXME openlog(cfg_log_ident, cfg_log_opt, cfg_log_facility); //I will probably close its fd a moment later, FIXME
syslog(cfg_log_facility | LOG_INFO, "Starting");
// No arguments, if you want something, go change source, according to documentation. // No arguments, if you want something, go change source, according to documentation.
daemonize(0); // daemonize(0);
// Load saved state -- variable values and tasks // Load saved state -- variable values and tasks
async_load(0); async_load(0);
@ -43,8 +44,12 @@ int main (void) {
socklen_t addrsize = sizeof(struct sockaddr_un); socklen_t addrsize = sizeof(struct sockaddr_un);
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
memcpy(&addr.sun_path, cfg_socket, strlen(cfg_socket)+1); memcpy(&addr.sun_path, cfg_socket, strlen(cfg_socket)+1);
bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)); if(bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
listen(sockfd, cfg_sock_maxclients); syslog(cfg_log_facility | LOG_ERR, "daemon: bind failed: %m");
}
if(listen(sockfd, cfg_sock_maxclients) == -1) {
syslog(cfg_log_facility | LOG_ERR, "daemon: listen failed: %m");
}
// Everything prepared // Everything prepared
syslog(cfg_log_facility | LOG_INFO, "Started"); syslog(cfg_log_facility | LOG_INFO, "Started");
@ -52,8 +57,15 @@ int main (void) {
// Main loop: (everything else is handled by process() and signal handlers). // Main loop: (everything else is handled by process() and signal handlers).
while (true) { while (true) {
int fd = accept(sockfd, (struct sockaddr *) &addr, &addrsize); int fd = accept(sockfd, (struct sockaddr *) &addr, &addrsize);
if (fd == -1) {
syslog(cfg_log_facility | LOG_ERR, "daemon: accept failed: %m");
while(1);
break;
} else {
syslog(cfg_log_facility | LOG_INFO, "daemon: accepted new connection");
process(fd); process(fd);
} }
}
syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!"); syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!");
return 5; return 5;
@ -73,21 +85,29 @@ bool write_uint64(uint64_t num, int fd) {
} }
char *read_string(int fd) { char *read_string(int fd) {
char buf[1]; char *buf;
struct grow *tmp = grow_init(true); struct grow *tmp = grow_init(true);
do { do {
buf = malloc(1);
if(buf == NULL) {
syslog(cfg_log_facility | LOG_ERR, "cannot malloc memory for a letter: %m");
grow_drop(tmp);
return NULL;
}
read(fd, buf, 1); read(fd, buf, 1);
grow_push(buf, tmp); grow_push(buf, tmp);
} while (buf[0] != '\0'); } while (buf[0] != '\0');
char *str = malloc(tmp->elems * sizeof(char)); char *str = malloc(tmp->elems * sizeof(char));
if (str == NULL) { if (str == NULL) {
syslog(cfg_log_facility | LOG_ERR, "cannot malloc memory for string: %m"); syslog(cfg_log_facility | LOG_ERR, "cannot malloc memory for string: %m");
grow_drop(tmp);
return NULL; return NULL;
} }
for (uint64_t j = 0; j < tmp -> elems; j++) { for (uint64_t j = 0; j < tmp -> elems; j++) {
str[j] = *(char *)(tmp->arr[j]); str[j] = *(char *)(tmp->arr[j]);
} }
grow_drop(tmp); grow_drop(tmp);
syslog(cfg_log_facility | LOG_DEBUG, "daemon: read_string: returning '%s'", str);
return str; return str;
} }
@ -261,7 +281,8 @@ bool process(int fd) {
async_save(0); async_save(0);
return true; return true;
} else if (strcmp(cmd, "FLSH") == 0) { } else if (strcmp(cmd, "FLSH") == 0) {
char *safe = read_string(fd); char *safe = calloc(1+strlen(cfg_safe_string), sizeof(char));
read(fd, safe, strlen(cfg_safe_string));
if (strcmp(safe, cfg_safe_string) == 0) { if (strcmp(safe, cfg_safe_string) == 0) {
response(SUCCESS, fd); response(SUCCESS, fd);
async_flush(0); async_flush(0);
@ -271,7 +292,8 @@ bool process(int fd) {
return false; return false;
} }
} else if (strcmp(cmd, "LOAD") == 0) { } else if (strcmp(cmd, "LOAD") == 0) {
char *safe = read_string(fd); char *safe = calloc(1+strlen(cfg_safe_string), sizeof(char));
read(fd, safe, strlen(cfg_safe_string));
if (strcmp(safe, cfg_safe_string) == 0) { if (strcmp(safe, cfg_safe_string) == 0) {
response(SUCCESS, fd); response(SUCCESS, fd);
async_load(0); async_load(0);
@ -281,7 +303,8 @@ bool process(int fd) {
return false; return false;
} }
} else if (strcmp(cmd, "KILL") == 0) { } else if (strcmp(cmd, "KILL") == 0) {
char *safe = read_string(fd); char *safe = calloc(1+strlen(cfg_safe_string), sizeof(char));
read(fd, safe, strlen(cfg_safe_string));
if (strcmp(safe, cfg_safe_string) == 0) { if (strcmp(safe, cfg_safe_string) == 0) {
response(SUCCESS, fd); response(SUCCESS, fd);
async_kill(0); async_kill(0);

@ -268,7 +268,7 @@ struct task *task_read(int fd) {
bool task_load(const char fn[]) { bool task_load(const char fn[]) {
bool retval = true; bool retval = true;
int fd = open(fn, O_RDONLY); int fd = open(fn, O_RDWR);
if (fd == -1) { if (fd == -1) {
syslog(cfg_log_facility | LOG_WARNING, "task: could not load tasks from file: %m"); syslog(cfg_log_facility | LOG_WARNING, "task: could not load tasks from file: %m");
return false; return false;
@ -284,20 +284,25 @@ bool task_load(const char fn[]) {
bool task_save(const char fn[]) { bool task_save(const char fn[]) {
bool retval = true; bool retval = true;
int fd = open(fn, O_CREAT | O_WRONLY | O_TRUNC); int fd = open(fn, O_CREAT | O_RDWR | O_TRUNC, (mode_t) 0660);
if (fd == -1) { if (fd == -1) {
syslog(cfg_log_facility | LOG_WARNING, "trie: could not save tasks to file: %m"); syslog(cfg_log_facility | LOG_WARNING, "trie: could not save tasks to file: %m");
return false; return false;
} }
if (tasks != NULL) {
for (uint64_t i = 0; i < tasks -> elems; i++) { for (uint64_t i = 0; i < tasks -> elems; i++) {
if (tasks -> arr[i] == NULL) continue; if (tasks -> arr[i] == NULL) continue;
struct task t = *(struct task *)(tasks->arr[i]); struct task t = *(struct task *)(tasks->arr[i]);
retval &= task_write(t, fd); retval &= task_write(t, fd);
} }
}
close(fd); close(fd);
return retval; return retval;
} }
void task_flush(void) { void task_flush(void) {
if (tasks == NULL) {
return;
}
for (uint64_t i = 0; i < tasks->elems; i++) { for (uint64_t i = 0; i < tasks->elems; i++) {
free(((struct task *)(tasks->arr[i]))->formula); free(((struct task *)(tasks->arr[i]))->formula);
for (int j = 0; ((struct task *)(tasks->arr[i]))->argv[j] != NULL; j++) { for (int j = 0; ((struct task *)(tasks->arr[i]))->argv[j] != NULL; j++) {

Loading…
Cancel
Save