9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * CDDL HEADER START
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * The contents of this file are subject to the terms of the
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Common Development and Distribution License (the "License").
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * You may not use this file except in compliance with the License.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * or http://www.opensolaris.org/os/licensing.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * See the License for the specific language governing permissions
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * and limitations under the License.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * When distributing Covered Code, include this CDDL HEADER in each
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * If applicable, add the following below this CDDL HEADER, with the
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * fields enclosed by brackets "[]" replaced with your own identifying
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * information: Portions Copyright [yyyy] [name of copyright owner]
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * CDDL HEADER END
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
4fe01614e64281d82c5a22f0050e55f1e0bdbdaeraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Use is subject to license terms.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#pragma ident "%Z%%M% %I% %E% SMI"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <sys/stat.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <stdio.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <stdlib.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <fcntl.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <sys/varargs.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <errno.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <sys/mman.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <sys/wait.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <unistd.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#define DTRACEIOC (('d' << 24) | ('t' << 16) | ('r' << 8))
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#define DTRACEIOC_MAX 17
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlvoid
9512fe850e98fdd448c638ca63fdd92a8a510255ahlfatal(char *fmt, ...)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl va_list ap;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl va_start(ap, fmt);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fprintf(stderr, "%s: ", "badioctl");
9512fe850e98fdd448c638ca63fdd92a8a510255ahl vfprintf(stderr, fmt, ap);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (fmt[strlen(fmt) - 1] != '\n')
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fprintf(stderr, ": %s\n", strerror(errno));
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl exit(1);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlvoid
9512fe850e98fdd448c638ca63fdd92a8a510255ahlbadioctl(pid_t parent)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl int fd = -1, random, ps = sysconf(_SC_PAGESIZE);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl int i = 0, seconds;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl caddr_t addr;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl hrtime_t now, last = 0, end;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if ((random = open("/dev/random", O_RDONLY)) == -1)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fatal("couldn't open /dev/random");
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if ((addr = mmap(0, ps, PROT_READ | PROT_WRITE,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl MAP_ANON | MAP_PRIVATE, -1, 0)) == (caddr_t)-1)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fatal("mmap");
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl for (;;) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl unsigned int ioc;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if ((now = gethrtime()) - last > NANOSEC) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (kill(parent, 0) == -1 && errno == ESRCH) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Our parent died. We will kill ourselves in
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * sympathy.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl exit(0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Once a second, we'll reopen the device.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (fd != -1)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl close(fd);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fd = open("/devices/pseudo/dtrace@0:dtrace", O_RDONLY);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (fd == -1)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fatal("couldn't open DTrace pseudo device");
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl last = now;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if ((i++ % 1000) == 0) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Every thousand iterations, change our random gunk.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl read(random, addr, ps);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl read(random, &ioc, sizeof (ioc));
9512fe850e98fdd448c638ca63fdd92a8a510255ahl ioc %= DTRACEIOC_MAX;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl ioc++;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl ioctl(fd, DTRACEIOC | ioc, addr);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
4fe01614e64281d82c5a22f0050e55f1e0bdbdaerafint
9512fe850e98fdd448c638ca63fdd92a8a510255ahlmain()
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl pid_t child, parent = getpid();
9512fe850e98fdd448c638ca63fdd92a8a510255ahl int status;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl for (;;) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if ((child = fork()) == 0)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl badioctl(parent);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl while (waitpid(child, &status, WEXITED) != child)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl continue;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (WIFEXITED(status)) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Our child exited by design -- we'll exit with
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * the same status code.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl exit(WEXITSTATUS(status));
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Our child died on a signal. Respawn it.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl printf("badioctl: child died on signal %d; respawning.\n",
9512fe850e98fdd448c638ca63fdd92a8a510255ahl WTERMSIG(status));
9512fe850e98fdd448c638ca63fdd92a8a510255ahl fflush(stdout);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
4fe01614e64281d82c5a22f0050e55f1e0bdbdaeraf
4fe01614e64281d82c5a22f0050e55f1e0bdbdaeraf /* NOTREACHED */
4fe01614e64281d82c5a22f0050e55f1e0bdbdaeraf return (0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}