static void try___time(int dofork) { void * a0 = randptr(); void * a1 = randptr(); int result, pid, status; char buf[128]; snprintf(buf, sizeof(buf), "__time(%p, %p)", (a0), (a1)); printf("%-47s", buf); pid = dofork ? fork() : 0; if (pid<0) { err(1, "fork"); } if (pid>0) { waitpid(pid, &status, 0); return; } result = __time(a0, a1); printf(" result %d, errno %d\n", result, errno); if (dofork) { exit(0); } }
static void check_timing(void) { time_t secs; unsigned long nsecs; if (__time(&secs, &nsecs)>0) { timing = 1; warnx("Timing enabled."); } }
/* * Run the whole workload. */ static void runit(unsigned numthinkers, unsigned numgrinders, unsigned numponggroups, unsigned ponggroupsize) { pid_t pids[numponggroups + 2]; time_t startsecs; unsigned long startnsecs; char buf[32]; unsigned i; tprintf("Running with %u thinkers, %u grinders, and %u pong groups " "of size %u each.\n", numthinkers, numgrinders, numponggroups, ponggroupsize); usem_init(&startsem, STARTSEM); createresultsfile(); forkem(numthinkers, nop, think, nop, 0, &pids[0]); forkem(numgrinders, nop, grind, nop, 1, &pids[1]); for (i=0; i<numponggroups; i++) { forkem(ponggroupsize, pong_prep, pong, pong_cleanup, i+2, &pids[i+2]); } usem_open(&startsem); tprintf("Forking done; starting the workload.\n"); __time(&startsecs, &startnsecs); Vn(&startsem, numthinkers + numgrinders + numponggroups * ponggroupsize); waitall(pids, numponggroups + 2); usem_close(&startsem); usem_cleanup(&startsem); openresultsfile(O_RDONLY); tprintf("--- Timings ---\n"); if (numthinkers > 0) { calcresult(0, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Thinkers: %s\n", buf); } if (numgrinders > 0) { calcresult(1, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Grinders: %s\n", buf); } for (i=0; i<numponggroups; i++) { calcresult(i+2, startsecs, startnsecs, buf, sizeof(buf)); tprintf("Pong group %u: %s\n", i, buf); } closeresultsfile(); destroyresultsfile(); }
static inline void router_send_traceback_message (uint32_t victim_addr, uint16_t victim_port, uint8_t forward, uint8_t *buf, uint16_t buflen, uint8_t num_ips) { struct sockaddr_in srvaddr; struct in_addr addr; static uint8_t started = TRUE; DEBUG_VAR(char, str2[INET_ADDRSTRLEN]); char str[INET_ADDRSTRLEN]; int rc; /* Fill server details */ memset(&srvaddr, 0, sizeof(srvaddr)); srvaddr.sin_family = AF_INET; srvaddr.sin_port = htons(victim_port); srvaddr.sin_addr.s_addr = htonl(victim_addr); rc = sendto(trback_sockfd, buf, buflen, 0, (struct sockaddr *)&srvaddr, sizeof(struct sockaddr_in)); if (rc < 0) { printf("Error sending traceback msg to %s. Reason: %s\n", inet_ntop(AF_INET, &srvaddr.sin_addr, str, sizeof(str)), strerror(errno)); return; } addr.s_addr = htonl(input.local_addr); (void)addr; DEBUG_LOG("Traceback [%c / %u] [%s => %s]\n", (forward ? 'F' : 'I'), num_ips, inet_ntop(AF_INET, &srvaddr.sin_addr, str, sizeof(str)), inet_ntop(AF_INET, &addr, str2, sizeof(str2))); if (!forward && started) { started = FALSE; fprintf(input.loghdl, "%.6f startedMarking %s\n", __time(), "Traceback message"); } return; }
/* * Do a task group: fork the processes, then wait for them. */ static void runtaskgroup(unsigned count, void (*prep)(unsigned, unsigned), void (*task)(unsigned, unsigned), void (*cleanup)(unsigned, unsigned), unsigned groupid) { pid_t mypids[count]; unsigned i; unsigned failures = 0; time_t secs; unsigned long nsecs; prep(groupid, count); for (i=0; i<count; i++) { mypids[i] = fork(); if (mypids[i] < 0) { err(1, "fork"); } if (mypids[i] == 0) { /* child (of second fork) */ task(groupid, i); exit(0); } /* parent (of second fork) - continue */ } /* * now wait for the task to finish */ for (i=0; i<count; i++) { failures += dowait(mypids[i]); } /* * Store the end time. */ __time(&secs, &nsecs); openresultsfile(O_WRONLY); putresult(groupid, secs, nsecs); closeresultsfile(); cleanup(groupid, count); exit(failures ? 1 : 0); }
int main() { /* test __time */ time_t seconds; __time(&seconds, NULL); int hour = ((int)seconds % 86600) / 3600; int minute = ((int)seconds % 3600) / 60; seconds = seconds % 60; printf("The __time is: %d:%d:%d\n", hour, minute, (int)seconds); /* test libc time */ time(&seconds); hour = ((int)seconds % 86600) / 3600; minute = ((int)seconds % 3600) / 60; seconds = seconds % 60; printf("The libc time is: %d:%d:%d\n", hour, minute, (int)seconds); return 0; /* avoid compiler warnings */ }
time_t time (time_t* timer) { return __time(timer); }
static int router_wait_for_notifications (uint16_t port) { struct sockaddr_in lcladdr; struct sockaddr_in cliaddr; DEBUG_VAR(char, str2[INET_ADDRSTRLEN]); int cli_sockfd; int sockfd, rc, yes=1; char str[INET_ADDRSTRLEN]; char msg[MAX_BUF_LEN]; /* Create socket for service */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0){ perror("Error creating socket. Reason"); return sockfd; } /* Set socket options */ rc = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if (rc < 0){ perror("Error setting socket options. Reason"); return rc; } /* Fill bind details */ memset(&lcladdr, 0, sizeof(lcladdr)); lcladdr.sin_family = AF_INET; lcladdr.sin_port = htons(port); lcladdr.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind to local addr-port */ rc = bind(sockfd, (struct sockaddr *)&lcladdr, sizeof(struct sockaddr)); if (rc < 0){ perror("Error binding addr-port. Reason"); return rc; } /* Set backlog queue length */ rc = listen(sockfd, 10); if (rc < 0) { perror("Error setting backlog. Reason"); return rc; } /* Wait for TCP connections */ for (;;) { socklen_t len = sizeof(struct sockaddr_in); cli_sockfd = accept(sockfd, (struct sockaddr *)&cliaddr, &len); if (cli_sockfd < 0) { perror("Error accepting connection. Reason"); break; } /* Read from client socket */ rc = read(cli_sockfd, msg, sizeof(msg)); if (rc < 0) { perror("Error reading from client socket. Reason"); return rc; } msg[rc] = '\0'; /* Truncate message */ /* Fetch local addr/port from kernel */ len = sizeof(struct sockaddr_in); rc = getsockname(cli_sockfd, (struct sockaddr *)&lcladdr, &len); if (rc < 0) { perror("Error fetching socket name. Reason"); return rc; } close(cli_sockfd); break; } DEBUG_LOG("Control signal [%s => %s]\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), inet_ntop(AF_INET, &lcladdr.sin_addr, str2, sizeof(str2))); /* Log got-marking */ fprintf(input.loghdl, "%.6f gotMarking %s %s\n", __time(), inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), msg); /* * Bind traceback socket to the address on * which control signal was received. This * same address is supposed to be used while * sending traceback messages */ lcladdr.sin_port = ntohs(0); input.local_addr = ntohl(lcladdr.sin_addr.s_addr); rc = bind(trback_sockfd, (struct sockaddr *)&lcladdr, sizeof(struct sockaddr_in)); if (rc < 0) { perror("Error binding traceback socket. Reason"); return rc; } /* DDOS detected. Start scanning packets */ router_start_scanning_packets(ntohl(lcladdr.sin_addr.s_addr), ntohl(cliaddr.sin_addr.s_addr), input.udp_port); return 0; }
/* * docommand * tokenizes the command line using strtok. if there aren't any commands, * simply returns. checks to see if it's a builtin, running it if it is. * otherwise, it's a standard command. check for the '&', try to background * the job if possible, otherwise just run it and wait on it. */ static int docommand(char *buf) { char *args[NARG_MAX + 1]; int nargs, i; char *s; pid_t pid; int status; int bg=0; time_t startsecs, endsecs; unsigned long startnsecs, endnsecs; nargs = 0; for (s = strtok(buf, " \t\r\n"); s; s = strtok(NULL, " \t\r\n")) { if (nargs >= NARG_MAX) { printf("%s: Too many arguments " "(exceeds system limit)\n", args[0]); return 1; } args[nargs++] = s; } args[nargs] = NULL; if (nargs==0) { /* empty line */ return 0; } for (i=0; builtins[i].name; i++) { if (!strcmp(builtins[i].name, args[0])) { return builtins[i].func(nargs, args); } } /* Not a builtin; run it */ if (nargs > 0 && !strcmp(args[nargs-1], "&")) { /* background */ if (!can_bg()) { printf("%s: Too many background jobs; wait for " "some to finish before starting more\n", args[0]); return -1; } nargs--; args[nargs] = NULL; bg = 1; } if (timing) { __time(&startsecs, &startnsecs); } pid = fork(); switch (pid) { case -1: /* error */ warn("fork"); return 255; case 0: /* child */ execv(args[0], args); warn("%s", args[0]); /* * Use _exit() instead of exit() in the child * process to avoid calling atexit() functions, * which would cause hostcompat (if present) to * reset the tty state and mess up our input * handling. */ _exit(1); default: break; } /* parent */ if (bg) { /* background this command */ remember_bg(pid); printf("[%d] %s ... &\n", pid, args[0]); return 0; } if (waitpid(pid, &status, 0) < 0) { warn("waitpid"); status = -1; } if (timing) { __time(&endsecs, &endnsecs); if (endnsecs < startnsecs) { endnsecs += 1000000000; endsecs--; } endnsecs -= startnsecs; endsecs -= startsecs; warnx("subprocess time: %lu.%09lu seconds", (unsigned long) endsecs, (unsigned long) endnsecs); } return status; }
time_t time(time_t *t) { return __time(t, NULL); }