static uint64_t ibping(ib_portid_t *portid, int quiet) { char data[IB_VENDOR_RANGE2_DATA_SIZE] = {0}; ib_vendor_call_t call; uint64_t start, rtt; DEBUG("Ping.."); start = getcurrenttime(); call.method = IB_MAD_METHOD_GET; call.mgmt_class = IB_VENDOR_OPENIB_PING_CLASS; call.attrid = 0; call.mod = 0; call.oui = IB_OPENIB_OUI; call.timeout = 0; memset(&call.rmpp, 0, sizeof call.rmpp); if (!ib_vendor_call(data, portid, &call)) return ~0llu; rtt = getcurrenttime() - start; if (!last_host[0]) memcpy(last_host, data, sizeof last_host); if (!quiet) printf("Pong from %s (%s): time %" PRIu64 ".%03" PRIu64 " ms\n", data, portid2str(portid), rtt/1000, rtt%1000); return rtt; }
static int hdhomerun_control_recv(struct hdhomerun_control_sock_t *cs, uint8_t *buffer, uint8_t *limit) { uint64_t timeout = getcurrenttime() + 1000; uint8_t *ptr = buffer; while (getcurrenttime() < timeout) { int length = hdhomerun_control_recv_sock(cs, ptr, limit); if (length < 0) { return -1; } if (length == 0) { continue; } ptr += length; if (buffer + HDHOMERUN_MIN_PEEK_LENGTH > limit) { continue; } length = (int)hdhomerun_peek_packet_length(buffer); if (buffer + length > limit) { continue; } return length; } return -1; }
void sleepuntil(uint64_t nanoseconds, unsigned slop) { int64_t delta = nanoseconds - getcurrenttime(); while(delta > slop) { struct timespec ts = { delta / 1000000000, delta % 1000000000 }; nanosleep(&ts, NULL); delta = nanoseconds - getcurrenttime(); } }
void timingwaitframe(struct frametimingstate *state) { state->framecounter++; uint64_t targettime = state->starttime + state->framecounter * state->timeperframe; if(targettime < getcurrenttime() - state->resetthreshold) { // we've overshot and are running too slow, reset timing state->framecounter = 0; state->starttime = getcurrenttime(); } else { // things are going fine, sleep until the target time sleepuntil(targettime, state->slop); } }
void msleep_minimum(uint64_t ms) { uint64_t stop_time = getcurrenttime() + ms; while (1) { uint64_t current_time = getcurrenttime(); if (current_time >= stop_time) { return; } msleep_approx(stop_time - current_time); } }
uint32_t random_get32(void) { FILE *fp = fopen("/dev/urandom", "rb"); if (!fp) { return (uint32_t)getcurrenttime(); } uint32_t Result; if (fread(&Result, 4, 1, fp) != 1) { Result = (uint32_t)getcurrenttime(); } fclose(fp); return Result; }
uint32_t random_get32(void) { pthread_once(&random_get32_once, random_get32_init); if (!random_get32_fp) { return (uint32_t)getcurrenttime(); } uint32_t Result; if (fread(&Result, 4, 1, random_get32_fp) != 1) { return (uint32_t)getcurrenttime(); } return Result; }
bool_t hdhomerun_sock_send(hdhomerun_sock_t sock, const void *data, size_t length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; const uint8_t *ptr = (uint8_t *)data; while (1) { int ret = send(sock, (char *)ptr, (int)length, 0); if (ret >= (int)length) { return TRUE; } if (ret > 0) { ptr += ret; length -= ret; } if (WSAGetLastError() != WSAEWOULDBLOCK) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) { return FALSE; } } }
static bool_t hdhomerun_sock_wait_for_event(hdhomerun_sock_t sock, long event_type, uint64_t stop_time) { uint64_t current_time = getcurrenttime(); if (current_time >= stop_time) { return FALSE; } WSAEVENT wsa_event = WSACreateEvent(); if (wsa_event == WSA_INVALID_EVENT) { return FALSE; } if (WSAEventSelect(sock, wsa_event, event_type) == SOCKET_ERROR) { WSACloseEvent(wsa_event); return FALSE; } DWORD ret = WaitForSingleObjectEx(wsa_event, (DWORD)(stop_time - current_time), FALSE); WSACloseEvent(wsa_event); if (ret != WAIT_OBJECT_0) { return FALSE; } return TRUE; }
/* * function circle roate send opbuf to librdkafka queue , * if the five time all failed it will exit , at the * same time will write some error info to local file * and check librdkafka queue data if it not empty then * will write queuedata file * */ void producer(rd_kafka_t * *rks, char *topic, int partitions, int tag, char *opbuf, int len, int rkcount) { int failnum = 0; int s = 1; while (s) { s = rotate_send_toqueue(rks, topic, partitions, RD_KAFKA_OP_F_FREE, opbuf, len, rkcount); check_queuedata_size(rks, rkcount, g_monitor_qusizelogpath); if (s == 1) { sleep(1); if (++failnum == 5) { char timebuf[50] = { 0 }; strcpy(timebuf, getcurrenttime()); timebuf[strlen(timebuf) - 1] = '\0'; fprintf(stderr, "%s all broker down \n", timebuf); char buf[]="all broker down"; save_error(g_logsavelocal_tag, LOG_INFO, buf); save_snddata_tofile(opbuf); save_queuedata_tofile(rks, rkcount); exit(7); } } } }
/* * function write sendkafka log info to local * file the path will depend on usr configure * default /var/log/sendkafka */ void save_error_tolocal(char *errlogpath, char *errinfo) { if (NULL != errinfo) { int len = strlen(errinfo)+1; char *perrinfo = calloc(1,len+50); strcpy(perrinfo,getcurrenttime()); perrinfo[strlen(perrinfo)-1]='|'; strncat(perrinfo,errinfo,len+50-1); rotate_logs(errlogpath); int fd = open(errlogpath, O_WRONLY | O_APPEND | O_CREAT, 0666); if (fd == -1) { char buf[1080] = { 0 }; sprintf(buf, "%d line open %s fail...", __LINE__ - 4,errlogpath); perror(buf); free(perrinfo); perrinfo=NULL; exit(4); } write(fd, perrinfo, strlen(perrinfo)); close(fd); free(perrinfo); perrinfo=NULL; } }
/**************************************************************************** * @usage : This function creates a file name based on timestamp. * * @arg1 : pointer to file name * @arg2 : image type * @return : void * *************************************************************************/ void get_image_filename(char *name, int val) { DATE_TIME current; char path[50] = {0}; char cmd[100] = {0}; struct passwd *pwd; getcurrenttime(¤t); pwd = getpwuid(getuid()); if(pwd) { strcpy(path,pwd->pw_dir); } snprintf(cmd,100,"mkdir -p %s/Pictures/svs",path); if(system(cmd) < 0) { printf("Fail to create 'svs' directory\n"); } if(val == 0) { snprintf(name,100,"%s/Pictures/svs/jpeg/IMG_%d%d%d_%d%d%d.jpg", path, current.year, current.mon, current.day, current.hour, current.min, current.sec); strcat(cmd,"/jpeg"); if(system(cmd) < 0) { printf("Fail to create 'records/images/jpeg' directory\n"); } } else if(val == 1) { snprintf(name,100,"%s/Pictures/svs/raw/IMG_%d%d%d_%d%d%d.raw", path, current.year, current.mon, current.day, current.hour, current.min, current.sec); strcat(cmd,"/raw"); if(system(cmd) < 0) { printf("Fail to create 'Pictures/svs/raw' directory\n"); } } }
static void locator_updatecache(enum locator_servicetype_t svc, char *key, char *resp) { RbtIterator handle; cacheitm_t *newitm; if (!havecache[svc]) return; handle = rbtFind(locatorcache[svc], key); if (handle == rbtEnd(locatorcache[svc])) { newitm = (cacheitm_t *)calloc(1, sizeof(cacheitm_t)); newitm->key = strdup(key); newitm->resp = strdup(resp); if (rbtInsert(locatorcache[svc], newitm->key, newitm) != RBT_STATUS_OK) { xfree(newitm->key); xfree(newitm->resp); xfree(newitm); } } else { newitm = (cacheitm_t *)gettreeitem(locatorcache[svc], handle); if (newitm->resp) xfree(newitm->resp); newitm->resp = strdup(resp); newitm->tstamp = getcurrenttime(NULL); } }
void timinginitializestate(struct frametimingstate *state, uint64_t timeperframe, unsigned slop, unsigned resetthreshold) { state->starttime = getcurrenttime(); state->framecounter = 0; state->timeperframe = timeperframe; state->slop = slop; state->resetthreshold = resetthreshold; }
bool_t hdhomerun_sock_recvfrom(hdhomerun_sock_t sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; while (1) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); int sockaddr_size = sizeof(sock_addr); int ret = recvfrom(sock, (char *)data, (int)(*length), 0, (struct sockaddr *)&sock_addr, &sockaddr_size); if (ret > 0) { *remote_addr = ntohl(sock_addr.sin_addr.s_addr); *remote_port = ntohs(sock_addr.sin_port); *length = ret; return TRUE; } if (WSAGetLastError() != WSAEWOULDBLOCK) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) { return FALSE; } } }
bool_t hdhomerun_sock_send(hdhomerun_sock_t sock, const void *data, size_t length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; const uint8_t *ptr = (const uint8_t *)data; while (1) { int ret = send(sock, ptr, length, 0); if (ret >= (int)length) { return TRUE; } if (ret > 0) { ptr += ret; length -= ret; } if (errno == EINPROGRESS) { errno = EWOULDBLOCK; } if (errno != EWOULDBLOCK) { return FALSE; } if (!hdhomerun_sock_wait_for_write_event(sock, stop_time)) { return FALSE; } } }
/* Send lock held by caller */ static bool_t hdhomerun_debug_output_message_sock(struct hdhomerun_debug_t *dbg, struct hdhomerun_debug_message_t *message) { if (dbg->sock == HDHOMERUN_SOCK_INVALID) { uint64_t current_time = getcurrenttime(); if (current_time < dbg->connect_delay) { return FALSE; } dbg->connect_delay = current_time + HDHOMERUN_DEBUG_CONNECT_RETRY_TIME; dbg->sock = hdhomerun_sock_create_tcp(); if (dbg->sock == HDHOMERUN_SOCK_INVALID) { return FALSE; } uint32_t remote_addr = hdhomerun_sock_getaddrinfo_addr(dbg->sock, HDHOMERUN_DEBUG_HOST); if (remote_addr == 0) { hdhomerun_debug_close_internal(dbg); return FALSE; } if (!hdhomerun_sock_connect(dbg->sock, remote_addr, HDHOMERUN_DEBUG_PORT, HDHOMERUN_DEBUG_CONNECT_TIMEOUT)) { hdhomerun_debug_close_internal(dbg); return FALSE; } } size_t length = strlen(message->buffer); if (!hdhomerun_sock_send(dbg->sock, message->buffer, length, HDHOMERUN_DEBUG_SEND_TIMEOUT)) { hdhomerun_debug_close_internal(dbg); return FALSE; } return TRUE; }
bool_t hdhomerun_sock_sendto(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; const uint8_t *ptr = (uint8_t *)data; while (1) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = htonl(remote_addr); sock_addr.sin_port = htons(remote_port); int ret = sendto(sock, (char *)ptr, (int)length, 0, (struct sockaddr *)&sock_addr, sizeof(sock_addr)); if (ret >= (int)length) { return TRUE; } if (ret > 0) { ptr += ret; length -= ret; } if (WSAGetLastError() != WSAEWOULDBLOCK) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) { return FALSE; } } }
void delete_old_acks(void) { DIR *xymonacks; struct dirent *d; struct stat st; time_t now = getcurrenttime(NULL); char fn[PATH_MAX]; xymonacks = opendir(xgetenv("XYMONACKDIR")); if (!xymonacks) { errprintf("No XYMONACKDIR! Cannot cd to directory %s\n", xgetenv("XYMONACKDIR")); return; } chdir(xgetenv("XYMONACKDIR")); while ((d = readdir(xymonacks))) { strcpy(fn, d->d_name); if (strncmp(fn, "ack.", 4) == 0) { stat(fn, &st); if (S_ISREG(st.st_mode) && (st.st_mtime < now)) { unlink(fn); } } } closedir(xymonacks); }
void cleandir(char *dirname) { DIR *dir; struct dirent *d; struct stat st; char fn[PATH_MAX]; time_t killtime = getcurrenttime(NULL)-86400; dir = opendir(dirname); if (dir == NULL) return; while ((d = readdir(dir))) { if (d->d_name[0] != '.') { sprintf(fn, "%s/%s", dirname, d->d_name); if ((stat(fn, &st) == 0) && (st.st_mtime < killtime)) { if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { dbgprintf("rm %s\n", fn); unlink(fn); } else if (S_ISDIR(st.st_mode)) { dbgprintf("Cleaning directory %s\n", fn); cleandir(fn); dbgprintf("rmdir %s\n", fn); rmdir(fn); } else { /* Ignore file */ }; } } } }
void format_rrdtime(char *t, char **tday, char **thm) { int year, month, day, hour, min,sec; int n; time_t now = getcurrenttime(NULL); struct tm *nowtm = localtime(&now); if (t == NULL) return; /* Input is YYYY/MM/DD@HH:MM:SS or YYYYMMDD or MMDD */ n = sscanf(t, "%d/%d/%d@%d:%d:%d", &year, &month, &day, &hour, &min, &sec); switch (n) { case 6: break; /* Got all */ case 5: sec = 0; break; case 4: min = sec = 0; break; case 3: hour = min = sec = 0; break; default: break; } hour = min = sec = 0; n = sscanf(t, "%d/%d/%d", &year, &month, &day); switch (n) { case 3: break; /* Got all */ case 2: day = month; month = year; year = nowtm->tm_year + 1900; default: break; } if (year < 100) year += 2000; *tday = (char *)malloc(10); sprintf(*tday, "%4d%02d%02d", year, month, day); *thm = (char *)malloc(20); sprintf(*thm, "%02d:%02d:%02d", hour, min, sec); }
static void delete_old_cards(char *dirname) { DIR *xymoncards; struct dirent *d; struct stat st; time_t now = getcurrenttime(NULL); char fn[PATH_MAX]; xymoncards = opendir(dirname); if (!xymoncards) { errprintf("Cannot read directory %s\n", dirname); return; } if (chdir(dirname) == -1) { return; } while ((d = readdir(xymoncards))) { strcpy(fn, d->d_name); stat(fn, &st); if ((fn[0] != '.') && S_ISREG(st.st_mode) && (st.st_mtime < (now-3600))) { unlink(fn); } } closedir(xymoncards); }
static void locator_updatecache(enum locator_servicetype_t svc, char *key, char *resp) { xtreePos_t handle; cacheitm_t *newitm; if (!havecache[svc]) return; handle = xtreeFind(locatorcache[svc], key); if (handle == xtreeEnd(locatorcache[svc])) { newitm = (cacheitm_t *)calloc(1, sizeof(cacheitm_t)); newitm->key = strdup(key); newitm->resp = strdup(resp); if (xtreeAdd(locatorcache[svc], newitm->key, newitm) != XTREE_STATUS_OK) { xfree(newitm->key); xfree(newitm->resp); xfree(newitm); } } else { newitm = (cacheitm_t *)xtreeData(locatorcache[svc], handle); if (newitm->resp) xfree(newitm->resp); newitm->resp = strdup(resp); newitm->tstamp = getcurrenttime(NULL); } }
bool_t hdhomerun_sock_recvfrom(hdhomerun_sock_t sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; while (1) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); socklen_t sockaddr_size = sizeof(sock_addr); int ret = recvfrom(sock, data, *length, 0, (struct sockaddr *)&sock_addr, &sockaddr_size); if (ret < 0) { if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, POLLIN, stop_time)) { return FALSE; } continue; } if (ret == 0) { return FALSE; } *remote_addr = ntohl(sock_addr.sin_addr.s_addr); *remote_port = ntohs(sock_addr.sin_port); *length = ret; return TRUE; } }
bool_t hdhomerun_sock_sendto(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; const uint8_t *ptr = (const uint8_t *)data; while (1) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = htonl(remote_addr); sock_addr.sin_port = htons(remote_port); int ret = sendto(sock, ptr, length, 0, (struct sockaddr *)&sock_addr, sizeof(sock_addr)); if (ret <= 0) { if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, POLLOUT, stop_time)) { return FALSE; } continue; } if (ret < (int)length) { ptr += ret; length -= ret; continue; } return TRUE; } }
bool_t hdhomerun_sock_send(hdhomerun_sock_t sock, const void *data, size_t length, uint64_t timeout) { uint64_t stop_time = getcurrenttime() + timeout; const uint8_t *ptr = (const uint8_t *)data; while (1) { int ret = send(sock, ptr, length, MSG_NOSIGNAL); if (ret <= 0) { if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINPROGRESS)) { return FALSE; } if (!hdhomerun_sock_wait_for_event(sock, POLLOUT, stop_time)) { return FALSE; } continue; } if (ret < (int)length) { ptr += ret; length -= ret; continue; } return TRUE; } }
static int timecheck(time_t starttime, time_t endtime, char *nktime) { time_t now = getcurrenttime(NULL); if (starttime && (now < starttime)) return 0; if (endtime && (now > endtime)) return 0; if ((nktime == NULL) || within_sla(NULL, nktime, 0)) return 1; /* FIXME */ return 0; }
time_t next_alert(activealerts_t *alert) { time_t now = getcurrenttime(NULL); int first = 1; int found = 0; time_t nexttime = now+(30*86400); /* 30 days from now */ recip_t *recip; repeat_t *rpt; time_t r_next = -1; stoprulefound = 0; while (!stoprulefound && ((recip = next_recipient(alert, &first, NULL, &r_next)) != NULL)) { found = 1; /* * This runs in the parent xymond_alert proces, so we must create * a repeat-record here - or all alerts will get repeated every minute. */ rpt = find_repeatinfo(alert, recip, 1); if (rpt) { if (rpt->nextalert <= now) rpt->nextalert = (now + recip->interval); if (rpt->nextalert < nexttime) nexttime = rpt->nextalert; } else if (r_next != -1) { if (r_next < nexttime) nexttime = r_next; } else { /* * This can happen, e.g. if we get an alert, but the minimum * DURATION has not been met. * This simply means we dropped the alert -for now - for some * reason, so it should be retried again right away. Put in a * 1 minute delay to prevent run-away alerts from flooding us. */ if ((now + 60) < nexttime) nexttime = now + 60; } } if (r_next != -1) { /* * Waiting for a minimum duration to trigger */ if (r_next < nexttime) nexttime = r_next; } else if (!found) { /* * There IS a potential recipient (or we would not be here). * And it's not a DURATION waiting to happen. * Probably waiting for a TIME restriction to trigger, so try * again soon. */ nexttime = now + 60; } return nexttime; }
void parse_query(void) { int day, mon, year, hour, min, sec; struct tm tmbuf; cgidata_t *cwalk; day = mon = year = hour = min = sec = -1; cwalk = cgidata; while (cwalk) { /* * cwalk->name points to the name of the setting. * cwalk->value points to the value (may be an empty string). */ if (strcasecmp(cwalk->name, "day") == 0) { day = atoi(cwalk->value); } else if (strcasecmp(cwalk->name, "mon") == 0) { char *errptr; mon = strtol(cwalk->value, &errptr, 10) - 1; if (errptr == cwalk->value) { for (mon=0; (monthnames[mon] && strcmp(cwalk->value, monthnames[mon])); mon++) ; if (mon >= 12) mon = -1; } } else if (strcasecmp(cwalk->name, "yr") == 0) { year = atoi(cwalk->value); } else if (strcasecmp(cwalk->name, "hour") == 0) { hour = atoi(cwalk->value); } else if (strcasecmp(cwalk->name, "min") == 0) { min = atoi(cwalk->value); } else if (strcasecmp(cwalk->name, "sec") == 0) { sec = atoi(cwalk->value); } cwalk = cwalk->next; } memset(&tmbuf, 0, sizeof(tmbuf)); tmbuf.tm_mday = day; tmbuf.tm_mon = mon; tmbuf.tm_year = year - 1900; tmbuf.tm_hour = hour; tmbuf.tm_min = min; tmbuf.tm_sec = sec; tmbuf.tm_isdst = -1; /* Important! Or we mishandle DST periods */ starttime = mktime(&tmbuf); if ((starttime == -1) || (starttime > getcurrenttime(NULL))) errormsg("Invalid parameters"); }
void init_timestamp(void) { time_t now; if (timestamp == NULL) timestamp = (char *)malloc(30); now = getcurrenttime(NULL); strcpy(timestamp, ctime(&now)); timestamp[strlen(timestamp)-1] = '\0'; }