/* syslogtest.c * * By Jonathan Marks * j-marks@uiuc.edu * * Compiling: * Solaris: cc syslogtest.c -o syslogtest -lxnet * AIX: cc syslogtest.c -o syslogtest */ #include #include #include #include #include #include #include #include #include #include #include #define LISTEN_PORT 10000 int f_no_seqno = 0; /* Set to disable printing sequence numbers in msgs */ struct facility_def { char *symbol; int value; } facility_table[] = { {"AUTH", LOG_AUTH}, {"DAEMON", LOG_DAEMON}, {"KERN", LOG_KERN}, {"LPR", LOG_LPR}, {"MAIL", LOG_MAIL}, {"NEWS", LOG_NEWS}, {"UUCP", LOG_UUCP}, {"USER", LOG_USER}, {"LOCAL0", LOG_LOCAL0}, {"LOCAL1", LOG_LOCAL1}, {"LOCAL2", LOG_LOCAL2}, {"LOCAL3", LOG_LOCAL3}, {"LOCAL4", LOG_LOCAL4}, {"LOCAL5", LOG_LOCAL5}, {"LOCAL6", LOG_LOCAL6}, {"LOCAL7", LOG_LOCAL7}, {NULL, 0} }; struct level_def { char *symbol; int value; } level_table[] = { {"ALERT", LOG_ALERT}, {"CRIT", LOG_CRIT}, {"DEBUG", LOG_DEBUG}, {"EMERG", LOG_EMERG}, {"ERR", LOG_ERR}, {"INFO", LOG_INFO}, {"NOTICE", LOG_NOTICE}, {"WARNING", LOG_WARNING}, {NULL, 0} }; struct running_time { long int sec; long int msec; long int usec; }; void printFacilities(void) { int col; struct facility_def *facilityDef; for (facilityDef = facility_table, col = 0; facilityDef->symbol; col = (col + 1) % 4, facilityDef++) fprintf(stderr, "%15s%s", facilityDef->symbol, (col == 3 ? "\n" : "")); if (col != 3) fprintf(stderr, "\n"); return; } void printLevels(void) { int col; struct level_def *levelDef; for (levelDef = level_table, col = 0; levelDef->symbol; col = (col + 1) % 4, levelDef++) fprintf(stderr, "%15s%s", levelDef->symbol, (col == 3 ? "\n" : "")); if (col != 3) fprintf(stderr, "\n"); return; } void printUsage(void) { fprintf(stderr, "usage: syslogtest facility level repetitions sleep [s]\n\n"); fprintf(stderr, "\trepetitions\t\tNumber of messages to send.\n"); fprintf(stderr, "\tsleep\t\tTime to wait between sending messages.\n"); fprintf(stderr, "\ts\t\tBecome a daemon (for use with 'master' client)\n\n"); fprintf(stderr, "FACILITIES:\n"); printFacilities(); fprintf(stderr, "LEVELS:\n"); printLevels(); return; } int priorityFromFacilityLevel(const char *facility, const char *level) { int pri = -1; int lev = -1; struct facility_def *fd_current = facility_table; struct level_def *ld_current = level_table; while (fd_current->symbol) { if (!strcmp(fd_current->symbol, facility)) { pri = fd_current->value; break; } fd_current++; } if (pri < 0) return -1; while (ld_current->symbol) { if (!strcmp(ld_current->symbol, level)) { lev = ld_current->value; break; } ld_current++; } if (lev < -1) return -1; pri |= lev; return pri; } int promptForPriority(void) { char s_fac[100]; char s_lev[100]; printf("Enter facility: "); fgets(s_fac, sizeof(s_fac), stdin); s_fac[strlen(s_fac) - 1] = '\0'; printf("Enter level: "); fgets(s_lev, sizeof(s_lev), stdin); s_lev[strlen(s_lev) - 1] = '\0'; return priorityFromFacilityLevel(s_fac, s_lev); } int promptForRepetitions(void) { char s_inp[100]; printf("Enter number of repetitions: "); fgets(s_inp, sizeof(s_inp), stdin); return atoi(s_inp); } struct running_time computeRunningTime(struct timeval *tv_start, struct timeval *tv_end) { long int total_usec; struct running_time rt; total_usec = tv_end->tv_sec; total_usec -= tv_start->tv_sec; total_usec *= 1000000; total_usec -= tv_start->tv_usec; total_usec += tv_end->tv_usec; rt.sec = total_usec / 1000000; rt.usec = total_usec - (rt.sec * 1000000); rt.msec = rt.usec / 1000; rt.usec = rt.usec - (rt.msec * 1000); return rt; } int waitForGoSignal(int fd_client) { char buf[10]; if (read(fd_client, &buf, sizeof(buf)) <= 0) return 0; if (tolower(buf[0]) == 'g' && tolower(buf[1]) == 'o') return 1; if (!strncasecmp(buf, "stop", 4)) return 2; return 0; } void runTest(int pri, int i_nReps, int sleep) { int i; char report[1024]; struct timeval tv_start, tv_end; struct running_time rt; gettimeofday(&tv_start, NULL); for (i = 0; i < i_nReps; i++) { if (f_no_seqno) syslog(pri, "TEST MESSAGE", i, i); else syslog(pri, "(%d) TEST MESSAGE (%d)", i, i); if (sleep) usleep(sleep); } gettimeofday(&tv_end, NULL); /* Report results */ rt = computeRunningTime(&tv_start, &tv_end); snprintf(report, sizeof(report), "REPS: %d Time: %ld s %ld ms %ld us", i_nReps, rt.sec, rt.msec, rt.usec); printf("[%d]: %s\n", (int) getpid(), report); syslog(pri, report); } void err(char *errstr) { int pri = LOG_ERR | LOG_USER; fputs(errstr, stderr); syslog(pri, errstr); return; } static char *basename(char *str) { char *end = str + strlen(str); while(end > str && *end != '/') end--; if (*end == '/') end++; return end; } int main(int argc, char *argv[]) { int i; int pri = -1; int i_nReps = 0; int sleep = 0; int optval = 1; int sync = 0; /* Synchronized operation */ int fd_listen = -1, fd_client = -1; struct sockaddr_in sa_listen, sa_client; /* printf("BASENAME: %s\n", basename(argv[0])); */ if (!strcmp(basename(argv[0]), "lasttest")) f_no_seqno = 1; for (i=1; i 3) i_nReps = atoi(argv[3]); else i_nReps = promptForRepetitions(); if (argc > 4) sleep = atoi(argv[4]); if (argc > 5) if (!strcmp(argv[5], "s")) sync = 1; openlog("TEST", LOG_PID, pri); if (i_nReps < 1) { err("No repetitions indicated- nothing to do."); printUsage(); return 1; } if (sync) { if ((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0) { char *s_err = "Couldn't get socket."; fprintf(stderr, "%s\n", s_err); pri = LOG_ERR; syslog(pri, s_err); return 1; } setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); bzero(&sa_listen, sizeof(sa_listen)); sa_listen.sin_family = AF_INET; sa_listen.sin_port = htons(LISTEN_PORT); sa_listen.sin_addr.s_addr = INADDR_ANY; if (bind(fd_listen, (struct sockaddr*) &sa_listen, sizeof(sa_listen)) < 0) { err("Couldn't bind listen port."); close(fd_listen); return 1; } if (listen(fd_listen, 5) < 0) { err("Can't listen on listening socket."); close(fd_listen); return 1; } } if (!sync) { runTest(pri, i_nReps, sleep); } else { int quit = 1; while(quit != 2) { optval = sizeof(sa_client); bzero(&sa_client, sizeof(sa_client)); if ((fd_client = accept(fd_listen, (struct sockaddr*) &sa_client, (size_t*) &optval)) < 0) { err("Can't accept connection."); close(fd_listen); return 1; } while((quit = waitForGoSignal(fd_client)) == 1) { runTest(pri, i_nReps, sleep); if (fork()) exit(0); /* To change PID for next test. */ } close(fd_client); } close(fd_listen); } closelog(); return 0; }