X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=powersensordaemon%2Fautosample.c;h=b79e1b9dcb37db6e200bcf045ed7dccf0cdb975e;hb=c62c39411cd07099da6ca5998e98b3123d15095c;hp=24edfc67d314768d30115dd777e99374d66299d4;hpb=ed84246e82295c574998450115a98565814ac2b2;p=svn42.git diff --git a/powersensordaemon/autosample.c b/powersensordaemon/autosample.c index 24edfc6..b79e1b9 100644 --- a/powersensordaemon/autosample.c +++ b/powersensordaemon/autosample.c @@ -24,6 +24,8 @@ #include "options.h" #include "log.h" +#include +#include #include #include #include @@ -32,6 +34,40 @@ #include "autosample.h" +int send_sample_cmd(int fd, const char* device_name) +{ + if(!device_name) + return -1; + + char* buf; + int len = asprintf(&buf, "sample %s\n", device_name); + if(len <= 0) + return len; + int offset = 0; + int ret; + for(;;) { + ret = write(fd, &buf[offset], len - offset); + if(ret < 0) { + if(errno != EINTR) { + free(buf); + return ret; + } + + ret = 0; + } + + offset += ret; + if(offset+1 >= len) + break; + } + free(buf); + + if(ret > 0) + return 0; + + return ret; +} + int start_autosample_process(options_t* opt) { int pipefd[2]; @@ -51,21 +87,83 @@ int start_autosample_process(options_t* opt) } if (cpid == 0) { - close(pipefd[1]); - return autosample_process(opt, pipefd[0]); + close(pipefd[0]); + return autosample_process(opt, pipefd[1]); } - close(pipefd[0]); - return pipefd[1]; + close(pipefd[1]); + return pipefd[0]; } int autosample_process(options_t *opt, int pipefd) { log_printf(NOTICE, "autosample process just started"); - sleep(5); + int device_num = key_value_storage_length(&opt->autosampledevs_); + if(device_num <= 0) { + log_printf(WARNING, "autosample no devices to sample, exiting"); + return 0; + } + + autosample_device_t* devices = malloc(sizeof(autosample_device_t)*device_num); + if(!devices) { + log_printf(WARNING, "autosample memory error, exiting"); + return -3; + } + + int i = 0; + string_list_element_t* k = opt->autosampledevs_.keys_.first_; + string_list_element_t* v = opt->autosampledevs_.values_.first_; + while(k && v) { + devices[i].delay_ = atoi(v->string_); + devices[i].cnt_ = 0; + devices[i].device_name_ = k->string_; + k = k->next_; + v = v->next_; + } + + int sig_fd = signal_init(); + if(sig_fd < 0) + return -3; + + fd_set readfds; + struct timeval timeout; + int return_value = 0; + while(!return_value) { + FD_SET(sig_fd, &readfds); + timeout.tv_sec = 0; + timeout.tv_usec = 1000000; + int ret = select(sig_fd+1, &readfds, NULL, NULL, &timeout); + if(ret == -1 && errno != EINTR) { + log_printf(ERROR, "autosample process select returned with error: %s", strerror(errno)); + return_value = -3; + break; + } + if(ret == -1) + continue; + if(!ret) { + int i; + for(i = 0; i < device_num; i++) { + devices[i].cnt_++; + if(devices[i].cnt_ >= devices[i].delay_) { + log_printf(DEBUG, "autosample send sample command for '%s'", devices[i].device_name_); + send_sample_cmd(pipefd, devices[i].device_name_); + devices[i].cnt_ = 0; + } + } + } + + if(FD_ISSET(sig_fd, &readfds)) { + if(signal_handle()) { + return_value = -2; + break; + } + } + } - return 0; + signal_stop(); + free(devices); + return return_value; }