+ 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_;
+ i++;
+ }
+
+ int sig_fd = signal_init();
+ if(sig_fd < 0)
+ return -1;
+
+ fd_set readfds, tmpfds;
+ FD_ZERO(&readfds);
+ FD_SET(readfd, &readfds);
+ FD_SET(sig_fd, &readfds);
+ int max_fd = (readfd < sig_fd) ? sig_fd : readfd;
+
+ struct timeval timeout;
+ int return_value = 0;
+ unsigned char sample_enabled = 0;
+ while(!return_value) {
+ memcpy(&tmpfds, &readfds, sizeof(tmpfds));
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+ int ret = select(max_fd+1, &tmpfds, 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_) {
+ if(sample_enabled) {
+ log_printf(DEBUG, "autosample send sample command for '%s'", devices[i].device_name_);
+ send_sample_cmd(writefd, devices[i].device_name_);
+ }
+ devices[i].cnt_ = 0;
+ }
+ }
+ }
+
+ if(FD_ISSET(readfd, &tmpfds)) {
+ int ret;
+ do {
+ ret = read(readfd, &sample_enabled, 1);
+ } while(!ret || (ret == -1 && errno == EINTR));
+ log_printf(NOTICE, "autosample %s", sample_enabled == 0 ? "disabled" : "enabled");
+ }
+
+ if(FD_ISSET(sig_fd, &tmpfds)) {
+ if(signal_handle()) {
+ return_value = 1;
+ break;
+ }
+ }
+ }
+
+ signal_stop();
+ free(devices);
+ return return_value;