4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/varargs.h>
39 #define DTRACEIOC (('d' << 24) | ('t' << 16) | ('r' << 8))
40 #define DTRACEIOC_MAX 17
49 fprintf(stderr, "%s: ", "badioctl");
50 vfprintf(stderr, fmt, ap);
52 if (fmt[strlen(fmt) - 1] != '\n')
53 fprintf(stderr, ": %s\n", strerror(errno));
59 badioctl(pid_t parent)
61 int fd = -1, random, ps = sysconf(_SC_PAGESIZE);
64 hrtime_t now, last = 0, end;
66 if ((random = open("/dev/random", O_RDONLY)) == -1)
67 fatal("couldn't open /dev/random");
69 if ((addr = mmap(0, ps, PROT_READ | PROT_WRITE,
70 MAP_ANON | MAP_PRIVATE, -1, 0)) == (caddr_t)-1)
76 if ((now = gethrtime()) - last > NANOSEC) {
77 if (kill(parent, 0) == -1 && errno == ESRCH) {
79 * Our parent died. We will kill ourselves in
86 * Once a second, we'll reopen the device.
91 fd = open("/devices/pseudo/dtrace@0:dtrace", O_RDONLY);
94 fatal("couldn't open DTrace pseudo device");
100 if ((i++ % 1000) == 0) {
102 * Every thousand iterations, change our random gunk.
104 read(random, addr, ps);
107 read(random, &ioc, sizeof (ioc));
108 ioc %= DTRACEIOC_MAX;
110 ioctl(fd, DTRACEIOC | ioc, addr);
117 pid_t child, parent = getpid();
121 if ((child = fork()) == 0)
124 while (waitpid(child, &status, WEXITED) != child)
127 if (WIFEXITED(status)) {
129 * Our child exited by design -- we'll exit with
130 * the same status code.
132 exit(WEXITSTATUS(status));
136 * Our child died on a signal. Respawn it.
138 printf("badioctl: child died on signal %d; respawning.\n",