X-Git-Url: https://git.realraum.at/?p=svn42.git;a=blobdiff_plain;f=powersensordaemon%2Fpowersensordaemon.c;h=f32b5718088d92d2bb4d94063e00497892ef9055;hp=99dbf74154d13248adee2f853d5db301ae3c5c0e;hb=ed84246e82295c574998450115a98565814ac2b2;hpb=efb533dcb175bf15fd003a635d77b6ff15756b6f diff --git a/powersensordaemon/powersensordaemon.c b/powersensordaemon/powersensordaemon.c index 99dbf74..f32b571 100644 --- a/powersensordaemon/powersensordaemon.c +++ b/powersensordaemon/powersensordaemon.c @@ -40,6 +40,8 @@ #include "daemon.h" +#include "autosample.h" + int init_command_socket(const char* path) { int fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -81,12 +83,22 @@ int send_command(int tty_fd, cmd_t* cmd) char c; switch(cmd->cmd) { - case POWER: { - c = 'A'; // get command from powerids[cmd->param] + case POWER_ON: { + if(!cmd->param) + return 0; + c = toupper(cmd->param[0]); + break; + } + case POWER_OFF: { + if(!cmd->param) + return 0; + c = tolower(cmd->param[0]); break; } case SAMPLE: { - c = 'T'; // get command from sampledevs[cmd->param] + if(!cmd->param) + return 0; + c = cmd->param[0]; break; } default: return 0; @@ -147,7 +159,7 @@ int send_response(int fd, const char* response) log_printf(DEBUG, "sent %s to %d additional listeners", TYPE_NAME,listener_cnt); -int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) +int process_cmd(char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { log_printf(DEBUG, "processing command from %d", fd); @@ -155,8 +167,14 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) return -1; cmd_id_t cmd_id; - if(!strncmp(cmd, "power", 5)) - cmd_id = POWER; + if(!strncmp(cmd, "power on", 8)) { + cmd_id = POWER_ON; + cmd[5] = '_'; + } + else if(!strncmp(cmd, "power off", 9)) { + cmd_id = POWER_OFF; + cmd[5] = '_'; + } else if(!strncmp(cmd, "sample", 6)) cmd_id = SAMPLE; else if(!strncmp(cmd, "log", 3)) @@ -169,10 +187,28 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) return 0; } char* param = strchr(cmd, ' '); - if(param) + if(param) param++; - if(cmd_id == POWER || cmd_id == SAMPLE) { + if(cmd_id == POWER_ON || cmd_id == POWER_OFF) { + char* orig_param = param; + param = key_value_storage_find(&opt->powerids_, param); + if(!param) { + send_response(fd, "Error: invalid power id"); + log_printf(WARNING, "invalid power id '%s' in command from %d", orig_param, fd); + } + } + + if(cmd_id == SAMPLE) { + char* orig_param = param; + param = key_value_storage_find(&opt->sampledevs_, param); + if(!param) { + send_response(fd, "Error: invalid sample device"); + log_printf(WARNING, "invalid sample device '%s' in command from %d", orig_param, fd); + } + } + + if(cmd_id == POWER_ON || cmd_id == POWER_OFF || cmd_id == SAMPLE) { char* resp; asprintf(&resp, "Request: %s", cmd); if(resp) { @@ -185,7 +221,8 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) } switch(cmd_id) { - case POWER: + case POWER_ON: + case POWER_OFF: case SAMPLE: { int ret = cmd_push(cmd_q, fd, cmd_id, param); if(ret) @@ -248,21 +285,21 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) return 0; } -int nonblock_recvline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* client_lst) +int nonblock_readline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* client_lst, options_t* opt) { int ret = 0; for(;;) { - ret = recv(fd, &buffer->buf[buffer->offset], 1, 0); - if(!ret) + ret = read(fd, &buffer->buf[buffer->offset], 1); + if(!ret || (ret == -1 && errno == EBADF)) return 2; - if(ret == -1 && errno == EAGAIN) + if(ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 0; else if(ret < 0) break; if(buffer->buf[buffer->offset] == '\n') { buffer->buf[buffer->offset] = 0; - ret = process_cmd(buffer->buf, fd, cmd_q, client_lst); + ret = process_cmd(buffer->buf, fd, cmd_q, client_lst, opt); buffer->offset = 0; break; } @@ -349,7 +386,7 @@ int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* clie return ret; } -int main_loop(int tty_fd, int cmd_listen_fd) +int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt) { log_printf(NOTICE, "entering main loop"); @@ -358,11 +395,15 @@ int main_loop(int tty_fd, int cmd_listen_fd) FD_SET(tty_fd, &readfds); FD_SET(cmd_listen_fd, &readfds); int max_fd = tty_fd > cmd_listen_fd ? tty_fd : cmd_listen_fd; + FD_SET(autosample_fd, &readfds); + max_fd = (max_fd < autosample_fd) ? autosample_fd : max_fd; cmd_t* cmd_q = NULL; client_t* client_lst = NULL; read_buffer_t tty_buffer; tty_buffer.offset = 0; + read_buffer_t autosample_buffer; + autosample_buffer.offset = 0; int sig_fd = signal_init(); if(sig_fd < 0) @@ -421,10 +462,22 @@ int main_loop(int tty_fd, int cmd_listen_fd) client_add(&client_lst, new_fd); } + if(FD_ISSET(autosample_fd, &tmpfds)) { + return_value = nonblock_readline(&autosample_buffer, autosample_fd, &cmd_q, client_lst, opt); + if(return_value == 2) { + log_printf(WARNING, "autosample process has crashed, removing pipe to it"); + FD_CLR(autosample_fd, &readfds); + return_value = 0; + continue; + } + if(return_value) + break; + } + client_t* lst = client_lst; while(lst) { if(FD_ISSET(lst->fd, &tmpfds)) { - return_value = nonblock_recvline(&(lst->buffer), lst->fd, &cmd_q, client_lst); + return_value = nonblock_readline(&(lst->buffer), lst->fd, &cmd_q, client_lst, opt); if(return_value == 2) { log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd); client_t* deletee = lst; @@ -550,7 +603,11 @@ int main(int argc, char* argv[]) } } log_printf(NOTICE, "just started..."); - options_parse_post(&opt); + if(options_parse_post(&opt)) { + options_clear(&opt); + log_close(); + exit(-1); + } priv_info_t priv; if(opt.username_) @@ -592,6 +649,29 @@ int main(int argc, char* argv[]) fprintf(pid_file, "%d", pid); fclose(pid_file); } + + int autosample_fd = -1; +// if(opt.led_filename_) { + log_printf(NOTICE, "starting autosample process"); + autosample_fd = start_autosample_process(&opt); + if(autosample_fd == -1) { + options_clear(&opt); + log_close(); + exit(1); + } + else if(autosample_fd <= 0) { + if(!autosample_fd) + log_printf(NOTICE, "autosample process normal shutdown"); + else if(autosample_fd == -2) + log_printf(NOTICE, "autosample shutdown after signal"); + else + log_printf(NOTICE, "autosample shutdown after error"); + + options_clear(&opt); + log_close(); + exit(1); + } +// } int cmd_listen_fd = init_command_socket(opt.command_sock_); if(cmd_listen_fd < 0) { @@ -610,7 +690,7 @@ int main(int argc, char* argv[]) if(ret) ret = 2; else - ret = main_loop(tty_fd, cmd_listen_fd); + ret = main_loop(tty_fd, cmd_listen_fd, autosample_fd, &opt); } if(ret == 2) {