int main(int unused_argc, char **unused_argv) { struct timeval tv; fd_set read_fds; fd_set except_fds; int fd; int fd2; (void) unlink(FIFO_PATH); printf("Create fifo %s...\n", FIFO_PATH); if (mkfifo(FIFO_PATH, 0600) < 0) perrorexit("mkfifo"); printf("Open fifo %s, read-only mode...\n", FIFO_PATH); if ((fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK, 0)) < 0) perrorcleanup("open"); printf("Write one byte to the fifo, then close it...\n"); if ((fd2 = open(FIFO_PATH, O_WRONLY, 0)) < 0) perrorcleanup("open fifo O_WRONLY"); if (write(fd2, "", 1) < 1) perrorcleanup("write one byte to fifo"); if (close(fd2) < 0) perrorcleanup("close fifo"); printf("Selecting the fifo for readability...\n"); for (;;) { FD_ZERO(&read_fds); FD_SET(fd, &read_fds); FD_ZERO(&except_fds); FD_SET(fd, &except_fds); tv.tv_sec = 1; tv.tv_usec = 0; switch (select(fd + 1, &read_fds, (fd_set *) 0, &except_fds, &tv)) { case -1: perrorexit("select"); default: if (FD_ISSET(fd, &except_fds)) { printf("Exceptional fifo condition! You are not normal!\n"); readable_event(fd); } else if (FD_ISSET(fd, &read_fds)) { printf("Readable fifo condition\n"); readable_event(fd); } break; case 0: printf("The fifo is not readable. You're normal.\n"); cleanup(); exit(0); break; } } }
static void cleanup(void) { printf("Removing fifo %s...\n", FIFO_PATH); if (unlink(FIFO_PATH)) perrorexit("unlink"); printf("Done.\n"); }
int main(int argc, char *argv[]) { int time_to_run = 0; pid_t pid; pid_t child_pid; int status; progname = argv[0]; /* * Parse JCL. */ while (--argc && *++argv && **argv == '-') if ((kill_signal = atoi(*argv + 1)) <= 0) usage(); if (argc < 2 || (time_to_run = atoi(argv[0])) <= 0) usage(); commandname = argv[1]; /* * Run the command and its watchdog in a separate process group so that * both can be killed off with one signal. */ setsid(); switch (child_pid = fork()) { case -1: /* error */ perrorexit("timeout: fork"); case 00: /* run controlled command */ execvp(argv[1], argv + 1); perrorexit(argv[1]); default: /* become watchdog */ (void) signal(SIGHUP, terminate); (void) signal(SIGINT, terminate); (void) signal(SIGQUIT, terminate); (void) signal(SIGTERM, terminate); (void) signal(SIGALRM, terminate); alarm(time_to_run); while ((pid = wait(&status)) != -1 && pid != child_pid) /* void */ ; return (pid == child_pid ? status : -1); } return 0; }
int inotifyUTMP(void*) { int inotifyFd, wd; char buf[BUF_LEN] __attribute__ ((aligned(8))); ssize_t numRead; char *p; struct inotify_event *event; struct passwd *pw; uid_t uid; uid_t NO_UID = -1; utmp *utmpbuf; int utmphandle; errno = 0; uid = geteuid (); pw = (uid == NO_UID && errno ? NULL : getpwuid (uid)); if (!pw) perrorexit("cannot find user ID"); utmphandle = mmapUTMP(0, &utmpbuf); inotifyFd = inotify_init(); /* Create inotify instance */ if (inotifyFd == -1) perrorexit("inotify_init"); wd = inotify_add_watch(inotifyFd, UTMP_FILE, IN_MODIFY); if (wd == -1) perrorexit("inotify_add_watch"); ReflushPTS(utmpbuf, utmphandle, pw); // first start for (;;) { /* Read events forever */ numRead = read(inotifyFd, buf, BUF_LEN); if (numRead == 0) perror("read() from inotify fd returned 0!"); if (numRead == -1) perrorexit("read"); for (p = buf; p < buf + numRead; ) { event = (struct inotify_event *) p; if (event->mask & IN_MODIFY) { ReflushPTS(utmpbuf, utmphandle, pw); // printf("IN_MODIFY\n"); } p += sizeof(struct inotify_event) + event->len; } } exit(EXIT_SUCCESS); }
int mmapUTMP(int utmpfile, utmp **utmp_buf) { union assignAddress utmpStart; if (!utmpfile) utmpfile = open(UTMP_FILE, O_RDONLY); utmpStart.addressVoid = mmap (0, NORMAL_PAGE_SIZE*100, PROT_READ, MAP_SHARED, utmpfile, 0); if (utmpStart.addressVoid == MAP_FAILED) perrorexit("Error in mmap file"); *utmp_buf = static_cast<utmp*>(utmpStart.addressVoid); return utmpfile; }
char* getStack(void) { static union assignAddress totalStackStart = {STACK_START}; union assignAddress nowstack, realstack; nowstack.addressLong = __sync_fetch_and_add(&(totalStackStart.addressLong), STACK_SIZE); realstack.addressVoid = mmap (nowstack.addressVoid, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED | MAP_GROWSDOWN, -1, 0); if (realstack.addressVoid == MAP_FAILED ) { printf("error in mmap%llx, %llx\n", nowstack.addressLong, realstack.addressLong); perrorexit("adsf"); return 0; } return realstack.addressChar; }