DF-2803 / harness.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | /* * DF-2803 / DF-2804 harness: udev(4) event-queue exerciser (root). * * Modes: * stall <sec> - open /dev/udev, issue UDEVPROP "getdevs" (this *initiates* * the reader: marker inserted at queue head), then sleep with * the fd held open, then close WITHOUT reading anything. * read <sec> - initiate, then blocking-read events for <sec> seconds, * print count + first event (control: drains the queue). * probe - initiate, close immediately, no churn (control). * * The getdevs ioctl protocol (kernel side prop_dictionary_copyout_ioctl) * mmaps the reply into userspace and returns it via the plistref; we munmap it. */ #include <sys/types.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <sys/udev.h> #include <libprop/proplib.h> #include <errno.h> #include <fcntl.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #define PAGE_ROUND(l) (((l) + 4095L) & ~4095L) static int udev_initiate(int fd) { prop_dictionary_t d; struct plistref pr; char *xml; int rc = -1; d = prop_dictionary_create(); if (d == NULL) return -1; if (prop_dictionary_set_cstring(d, "command", "getdevs") == false) { prop_object_release(d); return -1; } xml = prop_dictionary_externalize(d); prop_object_release(d); if (xml == NULL) { fprintf(stderr, "harness: externalize failed\n"); return -1; } memset(&pr, 0, sizeof(pr)); pr.pref_plist = xml; pr.pref_len = strlen(xml) + 1; if (ioctl(fd, UDEVPROP, &pr) < 0) { perror("harness: ioctl(UDEVPROP, getdevs)"); goto out; } /* * Success: kernel replaced pr with an mmap'd reply buffer. * munmap it; we don't need the device list itself. */ if (pr.pref_plist != NULL) munmap(pr.pref_plist, PAGE_ROUND(pr.pref_len)); rc = 0; out: free(xml); return rc; } static void onalarm(int sig __unused) { /* just EINTR the blocking read */ } int main(int argc, char **argv) { char buf[65536]; int fd, sec, n, count = 0; long long bytes = 0; if (argc < 2) { fprintf(stderr, "usage: %s stall <sec>|read <sec>|probe\n", argv[0]); return 2; } fd = open("/dev/udev", O_RDWR); if (fd < 0) { perror("harness: open /dev/udev"); return 1; } if (udev_initiate(fd) < 0) { close(fd); return 1; } if (strcmp(argv[1], "probe") == 0) { /* initiate + immediate close, no churn: control */ } else if (strcmp(argv[1], "stall") == 0) { sec = atoi(argv[2]); printf("harness: initiated; stalling %ds with fd held...\n", sec); fflush(stdout); sleep(sec); printf("harness: closing WITHOUT reading\n"); } else if (strcmp(argv[1], "read") == 0) { struct sigaction sa; sec = atoi(argv[2]); memset(&sa, 0, sizeof(sa)); sa.sa_handler = onalarm; sigemptyset(&sa.sa_mask); sigaction(SIGALRM, &sa, NULL); alarm(sec); for (;;) { n = read(fd, buf, sizeof(buf) - 1); if (n < 0) { if (errno == EINTR) break; perror("harness: read"); break; } buf[n] = '\0'; count++; bytes += n; if (count == 1) printf("harness: first event: %s\n", buf); } printf("harness: read %d events (%lld bytes) in %ds\n", count, bytes, sec); } else if (strcmp(argv[1], "stallcount") == 0) { /* * Stall like "stall", but before closing, switch to * non-blocking and count/drain the backlog so we can * quantify how much accumulated (draining also releases * the dicts properly, isolating this from DF-2804). */ int fl; sec = atoi(argv[2]); printf("harness: initiated; stalling %ds with fd held...\n", sec); fflush(stdout); sleep(sec); fl = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, fl | O_NONBLOCK); for (;;) { n = read(fd, buf, sizeof(buf) - 1); if (n < 0) { if (errno == EWOULDBLOCK) break; perror("harness: drain read"); break; } buf[n] = '\0'; count++; bytes += n; } printf("harness: backlog drained: %d events (%lld bytes)\n", count, bytes); } else { fprintf(stderr, "harness: unknown mode %s\n", argv[1]); close(fd); return 2; } close(fd); printf("harness: fd closed\n"); return 0; } |