EXPORTED int mappedfile_unlock(struct mappedfile *mf) { struct timeval endtime; double timediff; int r; /* make this safe to call multiple times */ if (!mf) return 0; if (mf->lock_status == MF_UNLOCKED) return 0; assert(mf->fd != -1); assert(!mf->dirty); r = lock_unlock(mf->fd, mf->fname); if (r < 0) { syslog(LOG_ERR, "IOERROR: lock_unlock %s: %m", mf->fname); return r; } mf->lock_status = MF_UNLOCKED; gettimeofday(&endtime, 0); timediff = timesub(&mf->starttime, &endtime); if (timediff > 1.0) { syslog(LOG_NOTICE, "mappedfile: longlock %s for %0.1f seconds", mf->fname, timediff); } return 0; }
static struct tm * gmtsub(const time_t * const timep, const long offset, struct tm * const tmp) { struct tm * result; if (!gmt_is_set) { _MUTEX_LOCK(&gmt_mutex); if (!gmt_is_set) { gmtload(gmtptr); gmt_is_set = TRUE; } _MUTEX_UNLOCK(&gmt_mutex); } result = timesub(timep, offset, gmtptr, tmp); #ifdef TM_ZONE /* ** Could get fancy here and deliver something such as ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero, ** but this is no time for a treasure hunt. */ if (offset != 0) tmp->TM_ZONE = wildabbr; else tmp->TM_ZONE = gmtptr->chars; #endif /* defined TM_ZONE */ return result; }
void timer::print() { std::list<split>::iterator s; float delta, fromstart; s=splits.begin(); split &first = *s; split *last = &(*s); s++; std::cout << title.c_str() << "\n"; std::cout << std::setw(10) << "split" << std::setw(10) << "last" << std::setw(10) << "total\n"; for (; s!=splits.end(); ++s) { fromstart = timesub(first.when, s->when); delta = timesub(last->when, s->when); last=&(*s); std::cout << std::setw(10) << s->label.c_str() << " " << std::setw(10) << std::setprecision(6) << delta << std::setw(10) << std::setprecision(6) << fromstart << "\n"; } }
static void tztime(const time_t *const timep, const char *tzstring, struct tm *const tmp) { struct state *tzptr, *sp; const time_t t = *timep; register int i; register const struct ttinfo *ttisp; if (tzstring == NULL) tzstring = gmt; tzptr = (struct state *) malloc(sizeof(struct state)); sp = tzptr; if (tzptr != NULL) { memset(tzptr, 0, sizeof(struct state)); (void) tzparse(tzstring, tzptr, FALSE); if (sp->timecnt == 0 || t < sp->ats[0]) { i = 0; while (sp->ttis[i].tt_isdst) if (++i >= sp->typecnt) { i = 0; break; } } else { for (i = 1; i < sp->timecnt; ++i) if (t < sp->ats[i]) break; i = sp->types[i - 1]; // DST begin or DST end } ttisp = &sp->ttis[i]; /* To get (wrong) behavior that's compatible with System V Release 2.0 you'd replace the statement below with t += ttisp->tt_gmtoff; timesub(&t, 0L, sp, tmp); */ if (tmp != NULL) { /* Just a check not to assert */ timesub(&t, ttisp->tt_gmtoff, sp, tmp); tmp->tm_isdst = ttisp->tt_isdst; #if defined(HAVE_STRUCT_TM_TM_ZONE) tmp->tm_zone = &sp->chars[ttisp->tt_abbrind]; #endif } free(tzptr); } }
times_t curclock(void) { GTimeVal end_time; if(!clock_running) { g_fprintf(stderr,_("curclock botch\n")); exit(1); } g_get_current_time(&end_time); return timesub(end_time,start_time); }
/*ARGSUSED*/ static void localsub( const time_t * const timep, const long offset, struct tm * const tmp ) { register struct state * sp; register const struct ttinfo * ttisp; register int i; const time_t t = *timep; long useless; useless = offset; /* Removes a warning */ sp = lclptr; if (sp->timecnt == 0 || t < sp->ats[0]) { i = 0; while (sp->ttis[i].tt_isdst) { if (++i >= sp->typecnt) { i = 0; break; } } } else { for (i = 1; i < sp->timecnt; ++i) { if (t < sp->ats[i]) break; } i = sp->types[i - 1]; } ttisp = &sp->ttis[i]; /* ** To get (wrong) behavior that's compatible with System V Release 2.0 ** you'd replace the statement below with ** t += ttisp->tt_gmtoff; ** timesub(&t, 0L, sp, tmp); */ timesub(&t, ttisp->tt_gmtoff, sp, tmp); tmp->tm_isdst = ttisp->tt_isdst; tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; #ifdef TM_ZONE tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; #endif /* defined TM_ZONE */ }
/* * gmtsub is to gmtime as localsub is to localtime. */ static struct pg_tm * gmtsub(const pg_time_t *timep, long offset, struct pg_tm * tmp) { struct pg_tm *result; if (!gmt_is_set) { gmt_is_set = TRUE; gmtload(gmtptr); } result = timesub(timep, offset, gmtptr, tmp); /* * Could get fancy here and deliver something such as "UTC+xxxx" or * "UTC-xxxx" if offset is non-zero, but this is no time for a treasure * hunt. */ if (offset != 0) tmp->tm_zone = wildabbr; else tmp->tm_zone = gmtptr->chars; return result; }
/* * The easy way to behave "as if no library function calls" localtime * is to not call it--so we drop its guts into "localsub", which can be * freely called. (And no, the PANS doesn't require the above behavior-- * but it *is* desirable.) * * The unused offset argument is for the benefit of mktime variants. */ static struct pg_tm * localsub(const pg_time_t *timep, long offset, struct pg_tm * tmp, const pg_tz *tz) { const struct state *sp; const struct ttinfo *ttisp; int i; struct pg_tm *result; const pg_time_t t = *timep; sp = &tz->state; if ((sp->goback && t < sp->ats[0]) || (sp->goahead && t > sp->ats[sp->timecnt - 1])) { pg_time_t newt = t; pg_time_t seconds; pg_time_t tcycles; int64 icycles; if (t < sp->ats[0]) seconds = sp->ats[0] - t; else seconds = t - sp->ats[sp->timecnt - 1]; --seconds; tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR; ++tcycles; icycles = tcycles; if (tcycles - icycles >= 1 || icycles - tcycles >= 1) return NULL; seconds = icycles; seconds *= YEARSPERREPEAT; seconds *= AVGSECSPERYEAR; if (t < sp->ats[0]) newt += seconds; else newt -= seconds; if (newt < sp->ats[0] || newt > sp->ats[sp->timecnt - 1]) return NULL; /* "cannot happen" */ result = localsub(&newt, offset, tmp, tz); if (result == tmp) { pg_time_t newy; newy = tmp->tm_year; if (t < sp->ats[0]) newy -= icycles * YEARSPERREPEAT; else newy += icycles * YEARSPERREPEAT; tmp->tm_year = newy; if (tmp->tm_year != newy) return NULL; } return result; } if (sp->timecnt == 0 || t < sp->ats[0]) { i = 0; while (sp->ttis[i].tt_isdst) if (++i >= sp->typecnt) { i = 0; break; } } else { int lo = 1; int hi = sp->timecnt; while (lo < hi) { int mid = (lo + hi) >> 1; if (t < sp->ats[mid]) hi = mid; else lo = mid + 1; } i = (int) sp->types[lo - 1]; } ttisp = &sp->ttis[i]; result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); tmp->tm_isdst = ttisp->tt_isdst; tmp->tm_zone = &sp->chars[ttisp->tt_abbrind]; return result; }
int main(int argc, char **argv) { int opt; pid_t pid; char *alt_config = NULL; time_t runattime = 0; int upgrade = 0; if ((geteuid()) == 0 && (become_cyrus(/*is_master*/0) != 0)) { fatal("must run as the Cyrus user", EC_USAGE); } while ((opt = getopt(argc, argv, "C:dt:U")) != EOF) { switch (opt) { case 'C': /* alt config file */ alt_config = optarg; break; case 'd': /* don't fork. debugging mode */ debugmode = 1; break; case 't': /* run a single scan at this time */ runattime = atoi(optarg); break; case 'U': upgrade = 1; break; default: fprintf(stderr, "invalid argument\n"); exit(EC_USAGE); break; } } cyrus_init(alt_config, "calalarmd", 0, 0); mboxlist_init(0); mboxlist_open(NULL); quotadb_init(0); quotadb_open(NULL); annotatemore_open(); caldav_init(); mboxevent_init(); if (upgrade) { caldav_alarm_upgrade(); shut_down(0); } if (runattime) { caldav_alarm_process(runattime); shut_down(0); } signals_set_shutdown(shut_down); signals_add_handlers(0); /* fork unless we were given the -d option or we're running as a daemon */ if (debugmode == 0 && !getenv("CYRUS_ISDAEMON")) { pid = fork(); if (pid == -1) { perror("fork"); exit(1); } if (pid != 0) { /* parent */ exit(0); } } /* child */ for (;;) { struct timeval start, end; double totaltime; int tosleep; signals_poll(); gettimeofday(&start, 0); caldav_alarm_process(0); gettimeofday(&end, 0); signals_poll(); totaltime = timesub(&start, &end); tosleep = 10 - (int) (totaltime + 0.5); /* round to nearest int */ if (tosleep > 0) sleep(tosleep); } /* NOTREACHED */ shut_down(1); }
static int handle_input(void) { static struct timeval pressed; static int long_press; struct timeval now, diff; unsigned long key; int rc; serial_poll(); if (pressed.tv_sec && pressed.tv_usec) { gettimeofday(&now, NULL); timesub(&diff, &now, &pressed); /* LONG_PRESS? */ if (diff.tv_sec >= 1) { switch (visual) { case VIEW_SATS: visual = VIEW_MAP; break; case VIEW_MAP: visual = VIEW_ROUTE; break; case VIEW_ROUTE: visual = VIEW_SATS; break; } /* allow for cycling by keeping the button pressed */ pressed.tv_sec = now.tv_sec; pressed.tv_usec = now.tv_usec; long_press = 1; do_refresh = 1; return 0; } } rc = empeg_getkey(&key); if (rc != 1) return rc; switch(key) { case IR_TOP_BUTTON_PRESSED: if (!load_route && !menu) return 1; /* done, wait to be selected again in the hijack menu */ load_route = 0; menu = 0; do_refresh = 1; break; case IR_BOTTOM_BUTTON_PRESSED: gettimeofday(&pressed, NULL); break; case IR_BOTTOM_BUTTON_RELEASED: pressed.tv_sec = pressed.tv_usec = 0; if (long_press) { long_press = 0; break; } if (load_route) { route_load(); load_route = 0; menu = 0; } else if (menu) { switch(menu_pos) { case 0: load_route = routes_init(); break; case 1: switch (visual) { case VIEW_SATS: visual = VIEW_MAP; break; case VIEW_MAP: visual = VIEW_ROUTE; break; case VIEW_ROUTE: visual = VIEW_SATS; break; } break; case 2: if (++show_popups == 3) show_popups = 0; break; case 3: show_metric = 1 - show_metric; break; case 4: show_gpscoords = 1 - show_gpscoords; break; case 5: show_time = 1 - show_time; break; case 6: show_track = 1 - show_track; break; case 7: if (++coord_format == 3) coord_format = 0; break; } menu = 0; lastmenu = 3; lastmenu_pos = menu_pos; } else menu = 1; do_refresh = 1; break; case IR_LEFT_BUTTON_PRESSED: if (load_route) route_select(0); else if (menu) { if (--menu_pos < 0) menu_pos = MENU_ENTRIES - 1; } else { switch (visual) { case VIEW_SATS: break; case VIEW_MAP: draw_zoom(0); break; case VIEW_ROUTE: route_skipwp(-1); break; } } do_refresh = 1; break; case IR_RIGHT_BUTTON_PRESSED: if (load_route) route_select(1); else if (menu) { if (++menu_pos >= MENU_ENTRIES) menu_pos = 0; } else { switch (visual) { case VIEW_SATS: break; case VIEW_MAP: draw_zoom(1); break; case VIEW_ROUTE: route_skipwp(1); break; } } do_refresh = 1; break; case IR_KNOB_LEFT: case IR_KNOB_RIGHT: route_skipwp(key == IR_KNOB_LEFT ? -1 : 1); route_locate(); do_refresh = 1; break; case IR_KNOB_PRESSED: default: break; } return 0; }
/* Link up the TaperSource with the Device, including retries etc. */ static void run_device_output(taper_state_t * taper_state, dump_info_t * dump_info) { GValue val; guint file_number; dump_info->current_part = 1; dump_info->total_time.tv_sec = 0; dump_info->total_time.tv_usec = 0; dump_info->total_bytes = 0; for (;;) { GTimeVal start_time, end_time, run_time; StreamingRequirement streaming_mode; queue_result_flags queue_result; CountingConsumerData consumer_data; dumpfile_t *this_header; size_t max_memory; this_header = munge_headers(dump_info); if (this_header == NULL) { char * qdiskname = quote_string(dump_info->diskname); char * errstr = taper_source_get_errmsg(dump_info->source); if (!errstr) errstr = "Failed reading dump header."; errstr = quote_string(errstr); putresult(FAILED, "%s INPUT-ERROR TAPE-GOOD %s \"\"\n", dump_info->handle, errstr); log_add(L_FAIL, "%s %s %s %d %s", dump_info->hostname, qdiskname, dump_info->timestamp, dump_info->level, errstr); amfree(qdiskname); amfree(errstr); return; } if (!find_and_label_new_tape(taper_state, dump_info)) { bail_no_volume(dump_info, taper_state->last_errmsg); dumpfile_free(this_header); return; } while (!device_start_file(taper_state->device, this_header)) { /* Close the device. */ device_finish(taper_state->device); g_object_unref(taper_state->device); taper_state->device = NULL; if (!find_and_label_new_tape(taper_state, dump_info)) { bail_no_volume(dump_info, taper_state->last_errmsg); dumpfile_free(this_header); return; } } dumpfile_free(this_header); bzero(&val, sizeof(val)); if (!device_property_get(taper_state->device, PROPERTY_STREAMING, &val) || !G_VALUE_HOLDS(&val, STREAMING_REQUIREMENT_TYPE)) { g_fprintf(stderr, "taper: Couldn't get streaming type!\n"); streaming_mode = STREAMING_REQUIREMENT_REQUIRED; } else { streaming_mode = g_value_get_enum(&val); } file_number = taper_state->device->file; consumer_data.next_consumer = device_write_consumer; consumer_data.next_consumer_data = taper_state->device; consumer_data.bytes_written = 0; g_get_current_time(&start_time); if (getconf_seen(CNF_DEVICE_OUTPUT_BUFFER_SIZE)) { max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE); if (getconf_seen(CNF_TAPEBUFS)) { g_fprintf(stderr, "Configuration directives 'device_output_buffer_size' " "and \n" "'tapebufs' are incompatible; using former.\n"); } } else if (getconf_seen(CNF_TAPEBUFS)) { max_memory = getconf_int(CNF_TAPEBUFS) * taper_state->device->block_size; } else { /* Use default. */ max_memory = getconf_size(CNF_DEVICE_OUTPUT_BUFFER_SIZE); } queue_result = do_consumer_producer_queue_full (taper_source_producer, dump_info->source, counting_consumer, &consumer_data, taper_state->device->block_size, max_memory, streaming_mode); g_get_current_time(&end_time); run_time = timesub(end_time, start_time); /* The device_write_consumer leaves the file open, so close it now. */ if (!device_finish_file(taper_state->device)) { queue_result = queue_result | QUEUE_CONSUMER_ERROR; } if (!finish_part_attempt(taper_state, dump_info, queue_result, run_time, consumer_data.bytes_written)) { break; } } }
int main(int argc, char *argv[]) { unsigned long flags = 0; int fd, size, r; char *buffer; struct timeval starttime, firsttime, nowtime, tm, t0, t1; pkt_asm_list_t list; int first = 1; argument_read(&argc, argv, args); if (ifname == NULL) error_exit("Unknown interface.\n"); if (complete) flags |= PKT_SEND_FLAG_COMPLETE; if (interval) flags |= PKT_SEND_FLAG_INTERVAL; fd = pkthandler.open_send(ifname, flags); buffer = malloc(bufsize); if (buffer == NULL) error_exit("Out of memory.\n"); while (!terminated) { list = pkt_asm_list_create(); size = pkt_text_read(stdin, buffer, bufsize, NULL, NULL, &tm, list); if (size < 0) break; if (size == bufsize) error_exit("Out of buffer.\n"); pkt_assemble_ethernet(list, buffer, size); list = pkt_asm_list_destroy(list); if (pkt_asm_list_filter_args(NULL, argc, argv) == 0) { list = pkt_asm_list_create(); pkt_disasm_ethernet(list, buffer, size); r = pkt_asm_list_filter_args(list, argc, argv); list = pkt_asm_list_destroy(list); if (r >= 0) { if (filrev) r = !r; if (r == 0) continue; } } list = pkt_asm_list_create(); pkt_asm_list_read_args(list, argc, argv); pkt_assemble_ethernet(list, buffer, size); list = pkt_asm_list_destroy(list); if ((flags & PKT_SEND_FLAG_INTERVAL) && (tm.tv_sec || tm.tv_usec)) { if (first) { firsttime.tv_sec = tm.tv_sec; firsttime.tv_usec = tm.tv_usec; gettimeofday(&starttime, NULL); first = 0; } else { gettimeofday(&nowtime, NULL); if (timesub(&t0, &tm, &firsttime) == 0) if (timesub(&t1, &nowtime, &starttime) == 0) if (timesub(&t0, &t0, &t1) == 0) select(0, NULL, NULL, NULL, &t0); if (justbefore) { firsttime.tv_sec = tm.tv_sec; firsttime.tv_usec = tm.tv_usec; starttime.tv_sec = nowtime.tv_sec; starttime.tv_usec = nowtime.tv_usec; } } } signal(SIGINT , sigint_handler); signal(SIGTERM, sigint_handler); pkthandler.send(fd, buffer, size); signal(SIGINT , SIG_DFL); signal(SIGTERM, SIG_DFL); if (waitusec > 0) usleep(waitusec); } free(buffer); close(fd); return 0; }
/*ARGSUSED*/ static struct tm * localsub(const time_t * const timep, const long offset __unused, struct tm * const tmp) { struct state * sp; const struct ttinfo * ttisp; int i; struct tm * result; const time_t t = *timep; sp = lclptr; if ((sp->goback && t < sp->ats[0]) || (sp->goahead && t > sp->ats[sp->timecnt - 1])) { time_t newt = t; time_t seconds; time_t tcycles; int_fast64_t icycles; if (t < sp->ats[0]) seconds = sp->ats[0] - t; else seconds = t - sp->ats[sp->timecnt - 1]; --seconds; tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR; ++tcycles; icycles = tcycles; if (tcycles - icycles >= 1 || icycles - tcycles >= 1) return NULL; seconds = icycles; seconds *= YEARSPERREPEAT; seconds *= AVGSECSPERYEAR; if (t < sp->ats[0]) newt += seconds; else newt -= seconds; if (newt < sp->ats[0] || newt > sp->ats[sp->timecnt - 1]) return NULL; /* "cannot happen" */ result = localsub(&newt, offset, tmp); if (result == tmp) { time_t newy; newy = tmp->tm_year; if (t < sp->ats[0]) newy -= icycles * YEARSPERREPEAT; else newy += icycles * YEARSPERREPEAT; tmp->tm_year = newy; if (tmp->tm_year != newy) return NULL; } return result; } if (sp->timecnt == 0 || t < sp->ats[0]) { i = 0; while (sp->ttis[i].tt_isdst) if (++i >= sp->typecnt) { i = 0; break; } } else { int lo = 1; int hi = sp->timecnt; while (lo < hi) { int mid = (lo + hi) >> 1; if (t < sp->ats[mid]) hi = mid; else lo = mid + 1; } i = (int) sp->types[lo - 1]; } ttisp = &sp->ttis[i]; /* ** To get (wrong) behavior that's compatible with System V Release 2.0 ** you'd replace the statement below with ** t += ttisp->tt_gmtoff; ** timesub(&t, 0L, sp, tmp); */ result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); tmp->tm_isdst = ttisp->tt_isdst; tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; #ifdef TM_ZONE tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; #endif /* defined TM_ZONE */ return result; }