static char *parseString(char **pInOut) { char *p = *pInOut; char *q = strBuf; while(*p == ' ') ++p; if (*p != '"') everror("String has no opening quotation mark: '%s'", p); ++p; while(1) { if (q - strBuf >= MAX_STRING_LENGTH) { everror("String length exceeds %d", MAX_STRING_LENGTH); } if (*p == '\\') { /* escaped character */ ++p; if (*p == '\0') everror("Closing NUL byte escaped by backslash."); *q++ = *p++; } else if (*p == '"') { /* end of string */ *q = '\0'; ++p; *pInOut = p; return strBuf; } else if (*p == '\0') everror("Unexpected closing NUL byte."); else *q++ = *p++; } }
void close_all(global_resources_struct *global_resources) { if (event_del(global_resources->int_signal_event)) everror("event_del"); event_free(global_resources->int_signal_event); if (event_del(global_resources->server_event)) everror("event_del"); event_free(global_resources->server_event); free_all_clients_events(global_resources->bases.base, client_handler_events_filter, client_handler_destruct); free_all_clients_events(global_resources->bases.base, transfer_events_filter, transfer_destruct); evdns_base_free(global_resources->bases.dns_base, 0); event_base_free(global_resources->bases.base); if (close(global_resources->server_sockfd)) perror("close"); }
int main(int argc, char *argv[]) { char *filename; FILE *input; assert(CHECKCONV(ulongest_t, Word)); assert(CHECKCONV(ulongest_t, Addr)); assert(CHECKCONV(ulongest_t, void *)); assert(CHECKCONV(ulongest_t, EventCode)); filename = parseArgs(argc, argv); if (!filename) { filename = getenv(TELEMETRY_FILENAME_ENVAR); if(!filename) filename = DEFAULT_TELEMETRY_FILENAME; } if (strcmp(filename, "-") == 0) input = stdin; else { input = fopen(filename, "rb"); if (input == NULL) everror("unable to open \"%s\"\n", filename); } readLog(input); return EXIT_SUCCESS; }
static void recordIntern(char *p) { ulongest_t stringId; char *string; char *copy; size_t len; Res res; stringId = parseHex(&p); string = parseString(&p); len = strlen(string); copy = malloc(len+1); if (copy == NULL) everror("Couldn't allocate space for a string."); (void)strcpy(copy, string); res = TableDefine(internTable, (Word)stringId, (void *)copy); if (res != ResOK) everror("Couldn't create an intern mapping."); }
static void createTables(void) { Res res; /* MPS intern IDs are serials from zero up, so we can use -1 * and -2 as specials. */ res = TableCreate(&internTable, (size_t)1<<4, tableAlloc, tableFree, NULL, (Word)-1, (Word)-2); if (res != ResOK) everror("Couldn't make intern table."); /* We assume that 0 and 1 are invalid as Addrs. */ res = TableCreate(&labelTable, (size_t)1<<7, tableAlloc, tableFree, NULL, 0, 1); if (res != ResOK) everror("Couldn't make label table."); }
static void recordLabel(char *p) { ulongest_t address; ulongest_t *stringIdP; Res res; address = parseHex(&p); if (address > (Word)-1) { printf("label address too large!"); return; } stringIdP = malloc(sizeof(ulongest_t)); if (stringIdP == NULL) everror("Can't allocate space for a string's ID"); *stringIdP = parseHex(&p); res = TableDefine(labelTable, (Word)address, (void *)stringIdP); if (res != ResOK) everror("Couldn't create an intern mapping."); }
static double parseDouble(char **pInOut) { double val; int i, l; char *p = *pInOut; i = sscanf(p, "%lg%n", &val, &l); if (i != 1) everror("Couldn't read a float from '%s'", p); *pInOut = p + l; return val; }
static ulongest_t parseHex(char **pInOut) { ulongest_t val; int i, l; char *p = *pInOut; i = sscanf(p, "%" SCNXLONGEST "%n", &val, &l); if (i != 1) everror("Couldn't read a hex number from '%s'", p); *pInOut = p + l; return val; }
void signal_cb(evutil_socket_t signum, short ev_flag, void *arg) { struct event_base *base = (struct event_base *)arg; assert(ev_flag == EV_SIGNAL); (void)ev_flag; switch(signum) { case INTERRUPT_SIGNAL: { if (event_base_loopbreak(base)) {everror("event_base_loopbreak"); return;} break; } default: assert(0); } }
void free_all_clients_events(struct event_base *base, events_filter_type filter, event_cb_arg_destruct_type destruct) { enum_clients_events_cb_arg_struct arg; stack_new(&arg.events_stack); arg.events_filter = filter; while (event_base_foreach_event(base, enum_clients_events_cb, &arg) > 0); while (!stack_is_empty(&arg.events_stack)) { struct event *event = stack_pop(&arg.events_stack); assert(event != NULL); if (event_del(event)) everror("event_del"); destruct(event); event_free(event); } }
int main(int argc, char *argv[]) { FILE *input; parseArgs(argc, argv); if (!logFileName) { input = stdin; logFileName = "<stdin>"; } else { input = fopen(logFileName, "r"); if (input == NULL) everror("unable to open %s", logFileName); } createTables(); readLog(input); (void)fclose(input); return 0; }
static void usageError(void) { usage(); everror("Bad usage"); }
static void readLog(FILE *input) { int i; for (i=0; i <= EventCodeMAX; ++i) eventName[i] = NULL; EVENT_LIST(EVENT_SET_NAME, X); while (TRUE) { /* loop for each event */ char line[MAX_LOG_LINE_LENGTH]; char *p, *q; ulongest_t clock; int code; ulongest_t val_hex; double val_float; const char *val_string; p = fgets(line, MAX_LOG_LINE_LENGTH, input); if (!p) { if (feof(input)) break; else everror("Couldn't read line from input."); } clock = parseHex(&p); code = (int)parseHex(&p); if (eventName[code]) printf("%0*" PRIXLONGEST " %04X %-19s ", hexWordWidth, clock, code, eventName[code]); else printf("%0*" PRIXLONGEST " %04X %-19s ", hexWordWidth, clock, code, "[Unknown]"); q = p; /* for a few particular codes, we do local processing. */ if (code == EventInternCode) { recordIntern(q); } else if (code == EventLabelCode) { recordLabel(q); } else if (code == EventEventInitCode) { ulongest_t major, median, minor, maxCode, maxNameLen, wordWidth, clocksPerSec; major = parseHex(&q); /* EVENT_VERSION_MAJOR */ median = parseHex(&q); /* EVENT_VERSION_MEDIAN */ minor = parseHex(&q); /* EVENT_VERSION_MINOR */ maxCode = parseHex(&q); /* EventCodeMAX */ maxNameLen = parseHex(&q); /* EventNameMAX */ wordWidth = parseHex(&q); /* MPS_WORD_WIDTH */ clocksPerSec = parseHex(&q); /* mps_clocks_per_sec() */ UNUSED(clocksPerSec); UNUSED(maxNameLen); if ((major != EVENT_VERSION_MAJOR) || (median != EVENT_VERSION_MEDIAN) || (minor != EVENT_VERSION_MINOR)) { fprintf(stderr, "Event log version does not match: %d.%d.%d vs %d.%d.%d\n", (int)major, (int)median, (int)minor, EVENT_VERSION_MAJOR, EVENT_VERSION_MEDIAN, EVENT_VERSION_MINOR); } if (maxCode > EventCodeMAX) { fprintf(stderr, "Event log may contain unknown events with codes from %d to %d\n", EventCodeMAX+1, (int)maxCode); } if (wordWidth > MPS_WORD_WIDTH) { int newHexWordWidth = (int)((wordWidth + 3) / 4); if (newHexWordWidth > hexWordWidth) { fprintf(stderr, "Event log word width is greater than on current platform;" "previous values may be printed too narrowly.\n"); } hexWordWidth = newHexWordWidth; } if (wordWidth > sizeof(ulongest_t) * CHAR_BIT) { everror("Event log word width %d is too wide for the current platform.", (int)wordWidth); } } switch(code) { EVENT_LIST(EVENT_PROCESS, X); default: printf("Unknown event."); } putchar('\n'); } }
static void readLog(FILE *stream) { for(;;) { /* loop for each event */ EventUnion eventUnion; Event event = &eventUnion; EventCode code; Res res; Bool eof = FALSE; /* suppress warnings about uninitialized use */ /* Read and parse event. */ res = eventRead(&eof, event, stream); if (res == ResFAIL) everror("Truncated log"); else if (res == ResIO) everror("I/O error reading log"); else if (res != ResOK) everror("Unknown error reading log"); if (eof) break; eventTime = event->any.clock; code = event->any.code; /* Special handling for some events, prior to text output */ switch(code) { case EventEventInitCode: if ((event->EventInit.f0 != EVENT_VERSION_MAJOR) || (event->EventInit.f1 != EVENT_VERSION_MEDIAN) || (event->EventInit.f2 != EVENT_VERSION_MINOR)) evwarn("Event log version does not match: %d.%d.%d vs %d.%d.%d", event->EventInit.f0, event->EventInit.f1, event->EventInit.f2, EVENT_VERSION_MAJOR, EVENT_VERSION_MEDIAN, EVENT_VERSION_MINOR); if (event->EventInit.f3 > EventCodeMAX) evwarn("Event log may contain unknown events with codes from %d to %d", EventCodeMAX+1, event->EventInit.f3); if (event->EventInit.f5 != MPS_WORD_WIDTH) /* This probably can't happen; other things will break * before we get here */ evwarn("Event log has incompatible word width: %d instead of %d", event->EventInit.f5, MPS_WORD_WIDTH); break; } EVENT_CLOCK_PRINT(stdout, eventTime); printf(" %4X", (unsigned)code); switch (code) { #define EVENT_PARAM_PRINT(name, index, sort, ident) \ printParam##sort(event->name.f##index); #define EVENT_PRINT(X, name, code, always, kind) \ case code: \ EVENT_##name##_PARAMS(EVENT_PARAM_PRINT, name) \ break; EVENT_LIST(EVENT_PRINT, X) default: evwarn("Unknown event code %d", code); } putchar('\n'); fflush(stdout); } /* while(!feof(input)) */ }