/* see what the status of the UPS is and handle any changes */ static void pollups(utype_t *ups) { char status[SMALLBUF]; /* try a reconnect here */ if (!flag_isset(ups->status, ST_CONNECTED)) if (try_connect(ups) != 1) return; if (upscli_ssl(&ups->conn) == 1) upsdebugx(2, "%s: %s [SSL]", __func__, ups->sys); else upsdebugx(2, "%s: %s", __func__, ups->sys); set_alarm(); if (get_var(ups, "status", status, sizeof(status)) == 0) { clear_alarm(); parse_status(ups, status); return; } /* fallthrough: no communications */ clear_alarm(); /* try to make some of these a little friendlier */ switch (upscli_upserror(&ups->conn)) { case UPSCLI_ERR_UNKNOWNUPS: upslogx(LOG_ERR, "Poll UPS [%s] failed - [%s] " "does not exist on server %s", ups->sys, ups->upsname, ups->hostname); break; default: upslogx(LOG_ERR, "Poll UPS [%s] failed - %s", ups->sys, upscli_strerror(&ups->conn)); break; } /* throw COMMBAD or NOCOMM as conditions may warrant */ ups_is_gone(ups); /* if upsclient lost the connection, clean up things on our side */ if (upscli_fd(&ups->conn) == -1) { drop_connection(ups); return; } }
EAPI int elm_main(int argc, char **argv) { int i; int activate = 0; int repeat = 0; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-activate")) activate = 1; } load_alarm(); if (activate) { clear_alarm(); alm.job = 0; if (alm.on) { if (alm.repeat) set_alarm(); else alm.on = 0; } save_alarm(); create_alarm_win(); alarm_sound(); } else create_main_win(); elm_run(); elm_shutdown(); return 0; }
// press ok to save time and set up alarm static void on_button_activate(void *data, Evas_Object *obj, void *event_info) { clear_alarm(); if (alm.on) set_alarm(); save_alarm(); elm_exit(); }
void timeit(char *where, size_t size) { int sum = 0; char *end = where + size; size_t n; size_t s; size_t range; size_t incr = 1024 * 1024; ssize_t stride; size_t pagesize = getpagesize(); if (size < 1024*1024 - 16*1024) { fprintf(stderr, "Bad size\n"); return; } range = 1024 * 1024; incr = 1024 * 1024; touchRange(where, range, pagesize); for (range += incr; range <= size; range += incr) { n = range / pagesize; set_alarm(n * TOO_LONG); touchRange(where + range - incr, incr, pagesize); clear_alarm(); set_alarm(n * TOO_LONG); start(0); touchRange(where, range, pagesize); sum = stop(0, 0); clear_alarm(); if ((sum / n) > TOO_LONG || alarm_triggered) { size = range - incr; break; } for (s = 8 * 1024 * 1024; s <= range; s *= 2) ; incr = s / 8; if (range < size && size < range + incr) { incr = size - range; } fprintf(stderr, "%dMB OK\r", range/(1024*1024)); } fprintf(stderr, "\n"); printf("%d\n", (size>>20)); }
static void on_set_button_activate(void *data, Evas_Object *obj, void *event_info) { if (!alarm_win) return; evas_object_del(alarm_win); alarm_win = NULL; alarm_sound_end(); clear_alarm(); alm.on = 1; save_alarm(); create_main_win(); }
/* deal with the contents of STATUS or ups.status for this ups */ static void parse_status(utype_t *ups, char *status) { char *statword, *ptr; clear_alarm(); upsdebugx(2, "%s: [%s]", __func__, status); /* empty response is the same as a dead ups */ if (!strcmp(status, "")) { ups_is_gone(ups); return; } ups_is_alive(ups); /* clear these out early if they disappear */ if (!strstr(status, "LB")) clearflag(&ups->status, ST_LOWBATT); if (!strstr(status, "FSD")) clearflag(&ups->status, ST_FSD); statword = status; /* split up the status words and parse each one separately */ while (statword != NULL) { ptr = strchr(statword, ' '); if (ptr) *ptr++ = '\0'; upsdebugx(3, "parsing: [%s]", statword); if (!strcasecmp(statword, "OL")) ups_on_line(ups); if (!strcasecmp(statword, "OB")) ups_on_batt(ups); if (!strcasecmp(statword, "LB")) ups_low_batt(ups); if (!strcasecmp(statword, "RB")) upsreplbatt(ups); /* do it last to override any possible OL */ if (!strcasecmp(statword, "FSD")) ups_fsd(ups); update_crittimer(ups); statword = ptr; } }
static void slavesync(void) { utype_t *ups; char temp[SMALLBUF]; time_t start, now; int maxlogins, logins; time(&start); for (;;) { maxlogins = 0; for (ups = firstups; ups != NULL; ups = ups->next) { /* only check login count on our master(s) */ if (!flag_isset(ups->status, ST_MASTER)) continue; set_alarm(); if (get_var(ups, "numlogins", temp, sizeof(temp)) >= 0) { logins = strtol(temp, (char **)NULL, 10); if (logins > maxlogins) maxlogins = logins; } clear_alarm(); } /* if no UPS has more than 1 login (us), then slaves are gone */ if (maxlogins <= 1) return; /* after HOSTSYNC seconds, assume slaves are stuck and bail */ time(&now); if ((now - start) > hostsync) { upslogx(LOG_INFO, "Host sync timer expired, forcing shutdown"); return; } usleep(250000); } }
void SIFCService::update_sets() { // Check whether the file exists. struct stat s; TRC_DEBUG("stat(%s) returns %d", _configuration.c_str(), stat(_configuration.c_str(), &s)); if ((stat(_configuration.c_str(), &s) != 0) && (errno == ENOENT)) { TRC_STATUS("No shared iFCs configuration (file %s does not exist)", _configuration.c_str()); CL_SPROUT_SIFC_FILE_MISSING.log(); set_alarm(); return; } TRC_STATUS("Loading shared iFCs configuration from %s", _configuration.c_str()); // Read from the file std::ifstream fs(_configuration.c_str()); std::string sifc_str((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>()); if (sifc_str == "") { TRC_ERROR("Failed to read shared iFCs configuration data from %s", _configuration.c_str()); CL_SPROUT_SIFC_FILE_EMPTY.log(); set_alarm(); return; } // Now parse the document rapidxml::xml_document<>* root = new rapidxml::xml_document<>; try { root->parse<0>(root->allocate_string(sifc_str.c_str())); } catch (rapidxml::parse_error& err) { TRC_ERROR("Failed to parse the shared iFCs configuration data:\n %s\n %s", sifc_str.c_str(), err.what()); CL_SPROUT_SIFC_FILE_INVALID_XML.log(); set_alarm(); delete root; root = NULL; return; } if (!root->first_node(SIFCService::SHARED_IFCS_SETS)) { TRC_ERROR("Invalid shared iFCs configuration file - missing SharedIFCsSets block"); CL_SPROUT_SIFC_FILE_MISSING_SHARED_IFCS_SETS.log(); set_alarm(); delete root; root = NULL; return; } // At this point, we're definitely going to override the iFCs we've got. // Update our map, taking a lock while we do so. boost::lock_guard<boost::shared_mutex> write_lock(_sets_rw_lock); _shared_ifc_sets.clear(); bool any_errors = false; rapidxml::xml_node<>* sets = root->first_node(SIFCService::SHARED_IFCS_SETS); rapidxml::xml_node<>* set = NULL; for (set = sets->first_node(SIFCService::SHARED_IFCS_SET); set != NULL; set = set->next_sibling(SIFCService::SHARED_IFCS_SET)) { rapidxml::xml_node<>* set_id_node = set->first_node(SIFCService::SET_ID); if (!set_id_node) { TRC_ERROR("Invalid shared iFC block - missing SetID. Skipping this entry"); CL_SPROUT_SIFC_FILE_MISSING_SET_ID.log(); any_errors = true; continue; } std::string set_id_str = std::string(set_id_node->value()); Utils::trim(set_id_str); int32_t set_id = std::atoi(set_id_str.c_str()); if (set_id_str != std::to_string(set_id)) { TRC_ERROR("Invalid shared iFC block - SetID (%s) isn't an int. Skipping this entry", set_id_str.c_str()); CL_SPROUT_SIFC_FILE_INVALID_SET_ID.log(set_id_str.c_str()); any_errors = true; continue; } if (_shared_ifc_sets.count(set_id) != 0) { TRC_ERROR("Invalid shared iFC block - SetID (%d) is repeated. Skipping this entry", set_id); CL_SPROUT_SIFC_FILE_REPEATED_SET_ID.log(set_id_str.c_str()); any_errors = true; continue; } std::vector<std::pair<int32_t, std::string>> ifc_set; for (rapidxml::xml_node<>* ifc = set->first_node(RegDataXMLUtils::IFC); ifc != NULL; ifc = ifc->next_sibling(RegDataXMLUtils::IFC)) { int32_t priority = 0; rapidxml::xml_node<>* priority_node = ifc->first_node(RegDataXMLUtils::PRIORITY); if (priority_node) { std::string priority_str = std::string(priority_node->value()); Utils::trim(priority_str); priority = std::atoi(priority_str.c_str()); if (priority_str != std::to_string(priority)) { TRC_ERROR("Invalid shared iFC block - Priority (%s) isn't an int. Skipping this entry", priority_str.c_str()); CL_SPROUT_SIFC_FILE_INVALID_PRIORITY.log(priority_str.c_str()); any_errors = true; continue; } } // Creating the iFC always passes; we don't validate the iFC any further // at this stage. We've validated this against a schema before allowing // any upload though. std::string ifc_str; rapidxml::print(std::back_inserter(ifc_str), *ifc, 0); ifc_set.push_back(std::make_pair(priority, ifc_str)); } TRC_STATUS("Adding %lu iFCs for ID %d", ifc_set.size(), set_id); _shared_ifc_sets.insert(std::make_pair(set_id, ifc_set)); } if (any_errors) { set_alarm(); } else { clear_alarm(); } delete root; root = NULL; }
void find_address (struct sockaddr_storage *address, struct sockaddr_storage *anywhere, int *port, char *hostname, int timespan) { /* Locate the specified NTP server and return its Internet address and port number. */ int family, rval; struct addrinfo hints; struct addrinfo *res; res = NULL; memset(address, 0, sizeof(struct sockaddr_storage)); memset(anywhere, 0, sizeof(struct sockaddr_storage)); if (setjmp(jump_buffer)) fatal(0,"unable to set up access to NTP server %s",hostname); errno = 0; if (signal(SIGALRM,jump_handler) == SIG_ERR) fatal(1,"unable to set up signal handler",NULL); alarm((unsigned int)timespan); /* Look up the Internet name or IP number. */ memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; hints.ai_family = pref_family; rval = getaddrinfo(hostname, "ntp", &hints, &res); if (rval != 0) fatal(0, "getaddrinfo(hostname, ntp) failed with %s", gai_strerror(rval)); /* Now clear the timer and check the result. */ clear_alarm(); /* There can be more than one address in the list, but for now only use the first. */ memcpy(address, res->ai_addr, res->ai_addrlen); family = res->ai_family; freeaddrinfo(res); switch(family) { case AF_INET: hints.ai_family = AF_INET; hints.ai_flags = AI_PASSIVE; rval = getaddrinfo(NULL, "ntp", &hints, &res); if (rval != 0) fatal(0, "getaddrinfo(NULL, ntp) failed with %s", gai_strerror(rval)); memcpy(anywhere, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); break; case AF_INET6: hints.ai_family = AF_INET6; hints.ai_flags = AI_PASSIVE; rval = getaddrinfo(NULL, "ntp", &hints, &res); if (rval != 0) fatal(0, "getaddrinfo(NULL, ntp, INET6, AI_PASSIVE) failed with %s", gai_strerror(rval)); memcpy(anywhere, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); break; } }
void find_address (struct in_addr *address, struct in_addr *anywhere, int *port, char *hostname, int timespan) { /* Locate the specified NTP server and return its Internet address and port number. */ unsigned long ipaddr; struct in_addr nowhere[1]; struct hostent *host; struct servent *service; /* Set up the reserved Internet addresses, attempting not to assume that addresses are 32 bits. */ local_to_address(nowhere,INADDR_LOOPBACK); local_to_address(anywhere,INADDR_ANY); /* Check the address, if any. This assumes that the DNS is reliable, or is at least checked by someone else. But it doesn't assume that it is accessible, so it needs to set up a timeout. */ if (hostname == NULL) *address = *anywhere; else { if (setjmp(jump_buffer)) fatal(0,"unable to set up access to NTP server %s",hostname); errno = 0; if (signal(SIGALRM,jump_handler) == SIG_ERR) fatal(1,"unable to set up signal handler",NULL); alarm((unsigned int)timespan); /* Look up the Internet name or IP number. */ if (! isdigit(hostname[0])) { errno = 0; host = gethostbyname(hostname); } else { if ((ipaddr = inet_addr(hostname)) == (unsigned long)-1) fatal(0,"invalid IP number %s",hostname); network_to_address(address,ipaddr); errno = 0; host = gethostbyaddr((void *)address,sizeof(struct in_addr), AF_INET); } /* Now clear the timer and check the result. */ clear_alarm(); if (host == NULL) fatal(1,"unable to locate IP address/number",NULL); if (host->h_length != sizeof(struct in_addr)) fatal(0,"the address does not seem to be an Internet one",NULL); *address = *((struct in_addr **)host->h_addr_list)[0]; if (memcmp(address,nowhere,sizeof(struct in_addr)) == 0 || memcmp(address,anywhere,sizeof(struct in_addr)) == 0) fatal(0,"reserved IP numbers cannot be used",NULL); if (verbose) fprintf(stderr, "%s: using NTP server %s (%s)\n", argv0,host->h_name,inet_ntoa(*address)); } /* Find out the port number (usually from /etc/services), and leave it in network format. This is assumed not to be obtained from a network service! Note that a port number is not assumed to be 16 bits. */ if ((service = getservbyname("ntp","udp")) != NULL) { *port = service->s_port; if (verbose > 2) fprintf(stderr,"Using port %d for NTP\n",port_to_integer(*port)); } else { *port = NTP_PORT; if (verbose) fprintf(stderr, "%s: assuming port %d for NTP - check /etc/services\n", argv0,port_to_integer(*port)); } }
static THREAD_FUNC TapeWriteThread(void *dummy) { int status; size_t blen, remain, want, put, nrec; char *ptr; OLD_MSGQ_MSG *obuf; static char *fid = "TapeWriteThread"; blen = Params->bfact * IDA_BUFSIZ; /* IDA10 OK */ MUTEX_LOCK(&mp); ioerr = 0; MUTEX_UNLOCK(&mp); while (1) { obuf = msgq_get(&Q->obuf, OLD_MSGQ_WAITFOREVER); if (!msgq_chkmsg2(fid, obuf)) { util_log(1, "%s: corrupt message received", fid); ispd_die(MY_MOD_ID + 1); } nrec = *((size_t *) obuf->data); util_log(1, "dumping %ld records to %s", nrec, Params->odev); ptr = obuf->data + sizeof(size_t); remain = nrec * IDA_BUFSIZ; /* IDA10 OK */ while (remain > 0) { want = remain > blen ? blen : remain; nrec = want / IDA_BUFSIZ; /* IDA10 OK */ do { lock_device(); put = mtio_write(tp, ptr, want); if (put == want) { MUTEX_LOCK(&Status->lock); Status->output.nrec += nrec; status = Status->output.state; MUTEX_UNLOCK(&Status->lock); ptr += put; if (ioerr) { clear_alarm(ISP_ALARM_IOERR); ioerr = 0; } } else { if (put != 0) { if (++ioerr == 1) { set_alarm(ISP_ALARM_IOERR); util_log(1, "%s: %s", Params->odev, syserrmsg(errno) ); } MUTEX_LOCK(&Status->lock); ++Status->output.err; MUTEX_UNLOCK(&Status->lock); eject_tape(0); } } release_device(); if (put != want) { if (shutting_down()) { complete_shutdown(); THREAD_EXIT(0); } else { sleep(5); } } } while (put != want); remain -= put; } util_log(1, "tape dump completed OK"); if (shutting_down()) { complete_shutdown(); THREAD_EXIT(0); } MUTEX_LOCK(&mp); if (eject_flag) eject_tape(0); eject_flag = 0; MUTEX_UNLOCK(&mp); msgq_put(&Heap->obuf, obuf); } }
static THREAD_FUNC TapeWatchThread(void *dummy) { int boot_flag; static struct mtget info; static char *fid = "TapeWatchThread"; boot_flag = 1; util_log(2, "TAPE_WATCH thread started, tid = %d", THREAD_SELF()); set_alarm(ISP_ALARM_OFFLINE); while (1) { MUTEX_LOCK(&mp); /* if we were offline, see if a tape is now present */ if (prev_state == MTIO_OFFLINE) { if ((tp = mtio_open(Params->odev, "w")) != null) { if (mtio_status(tp, &info) != -1) { crnt_state = MTIO_ONLINE; clear_alarm(ISP_ALARM_OFFLINE); if (ioerr) { clear_alarm(ISP_ALARM_IOERR); ioerr = 0; } check_tape(boot_flag); } else { mtio_close(tp); tp = (TAPE *) NULL; } } if (tp == null && shutting_down()) { util_log(1, "shutting down w/o final flush (no tape)"); complete_shutdown(); } /* if we were on-line, make sure we still are that way */ /* THIS CODE DOES NOT WORK AND I DON'T KNOW HOW TO MAKE IT WORK! */ } else if (mtio_nop(tp) < 0 || mtio_status(tp, &info) < 0) { mtio_close(tp); tp = (TAPE *) tp; set_alarm(ISP_ALARM_OFFLINE); crnt_state = MTIO_OFFLINE; } if (crnt_state != prev_state) { MUTEX_LOCK(&Status->lock); if (crnt_state == MTIO_ONLINE) { Status->output.state = ISP_OUTPUT_ONLINE; Status->output.err = 0; Status->output.file = info.mt_fileno + 1; Status->output.nrec = 0; } else { Status->output.state = ISP_OUTPUT_OFFLINE; } MUTEX_UNLOCK(&Status->lock); } prev_state = crnt_state; boot_flag = 0; if (pending && (crnt_state == MTIO_ONLINE)) { pending = 0; SEM_POST(&sp); } MUTEX_UNLOCK(&mp); sleep(10); } }