|
|
|
@ -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) {
|
|
|
|
|
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.
|
|
|
|
|
daemonize(0);
|
|
|
|
|
// daemonize(0);
|
|
|
|
|
|
|
|
|
|
// Load saved state -- variable values and tasks
|
|
|
|
|
async_load(0);
|
|
|
|
@ -43,8 +44,12 @@ int main (void) {
|
|
|
|
|
socklen_t addrsize = sizeof(struct sockaddr_un);
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
memcpy(&addr.sun_path, cfg_socket, strlen(cfg_socket)+1);
|
|
|
|
|
bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
|
|
|
|
|
listen(sockfd, cfg_sock_maxclients);
|
|
|
|
|
if(bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
|
|
|
|
|
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
|
|
|
|
|
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).
|
|
|
|
|
while (true) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syslog(cfg_log_facility | LOG_CRIT, "Escaped infinite loop!");
|
|
|
|
|
return 5;
|
|
|
|
@ -73,21 +85,29 @@ bool write_uint64(uint64_t num, int fd) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *read_string(int fd) {
|
|
|
|
|
char buf[1];
|
|
|
|
|
char *buf;
|
|
|
|
|
struct grow *tmp = grow_init(true);
|
|
|
|
|
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);
|
|
|
|
|
grow_push(buf, tmp);
|
|
|
|
|
} while (buf[0] != '\0');
|
|
|
|
|
char *str = malloc(tmp->elems * sizeof(char));
|
|
|
|
|
if (str == NULL) {
|
|
|
|
|
syslog(cfg_log_facility | LOG_ERR, "cannot malloc memory for string: %m");
|
|
|
|
|
grow_drop(tmp);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
for (uint64_t j = 0; j < tmp -> elems; j++) {
|
|
|
|
|
str[j] = *(char *)(tmp->arr[j]);
|
|
|
|
|
}
|
|
|
|
|
grow_drop(tmp);
|
|
|
|
|
syslog(cfg_log_facility | LOG_DEBUG, "daemon: read_string: returning '%s'", str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -261,7 +281,8 @@ bool process(int fd) {
|
|
|
|
|
async_save(0);
|
|
|
|
|
return true;
|
|
|
|
|
} 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) {
|
|
|
|
|
response(SUCCESS, fd);
|
|
|
|
|
async_flush(0);
|
|
|
|
@ -271,7 +292,8 @@ bool process(int fd) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} 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) {
|
|
|
|
|
response(SUCCESS, fd);
|
|
|
|
|
async_load(0);
|
|
|
|
@ -281,7 +303,8 @@ bool process(int fd) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} 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) {
|
|
|
|
|
response(SUCCESS, fd);
|
|
|
|
|
async_kill(0);
|
|
|
|
|