xref: /freebsd/tests/sys/net/randsleep.c (revision 4b40bdbd1f6b0071c143493e6eb79edd677d7648)
1*4b40bdbdSAlan Somers /*
2*4b40bdbdSAlan Somers  * Copyright (c) 2014 Spectra Logic Corporation
3*4b40bdbdSAlan Somers  * All rights reserved.
4*4b40bdbdSAlan Somers  *
5*4b40bdbdSAlan Somers  * Redistribution and use in source and binary forms, with or without
6*4b40bdbdSAlan Somers  * modification, are permitted provided that the following conditions
7*4b40bdbdSAlan Somers  * are met:
8*4b40bdbdSAlan Somers  * 1. Redistributions of source code must retain the above copyright
9*4b40bdbdSAlan Somers  *    notice, this list of conditions, and the following disclaimer,
10*4b40bdbdSAlan Somers  *    without modification.
11*4b40bdbdSAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*4b40bdbdSAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
13*4b40bdbdSAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
14*4b40bdbdSAlan Somers  *    including a substantially similar Disclaimer requirement for further
15*4b40bdbdSAlan Somers  *    binary redistribution.
16*4b40bdbdSAlan Somers  *
17*4b40bdbdSAlan Somers  * NO WARRANTY
18*4b40bdbdSAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*4b40bdbdSAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*4b40bdbdSAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*4b40bdbdSAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*4b40bdbdSAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*4b40bdbdSAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*4b40bdbdSAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*4b40bdbdSAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*4b40bdbdSAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*4b40bdbdSAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*4b40bdbdSAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
29*4b40bdbdSAlan Somers  *
30*4b40bdbdSAlan Somers  * Authors: Alan Somers         (Spectra Logic Corporation)
31*4b40bdbdSAlan Somers  *
32*4b40bdbdSAlan Somers  * $FreeBSD$
33*4b40bdbdSAlan Somers  */
34*4b40bdbdSAlan Somers 
35*4b40bdbdSAlan Somers #include <errno.h>
36*4b40bdbdSAlan Somers #include <stdio.h>
37*4b40bdbdSAlan Somers #include <stdlib.h>
38*4b40bdbdSAlan Somers #include <unistd.h>
39*4b40bdbdSAlan Somers 
40*4b40bdbdSAlan Somers #define RANDOM_MAX ((1<<31) - 1)
41*4b40bdbdSAlan Somers 
42*4b40bdbdSAlan Somers int main(int argc, char** argv){
43*4b40bdbdSAlan Somers 	useconds_t max_usecs, usecs;
44*4b40bdbdSAlan Somers 	double frac;
45*4b40bdbdSAlan Somers 
46*4b40bdbdSAlan Somers 	if (argc != 2) {
47*4b40bdbdSAlan Somers 		printf("Usage: randsleep <max_microseconds>\n");
48*4b40bdbdSAlan Somers 		exit(2);
49*4b40bdbdSAlan Somers 	}
50*4b40bdbdSAlan Somers 
51*4b40bdbdSAlan Somers 	errno = 0;
52*4b40bdbdSAlan Somers 	max_usecs = (useconds_t)strtol(argv[1], NULL, 0);
53*4b40bdbdSAlan Somers 	if (errno != 0) {
54*4b40bdbdSAlan Somers 		perror("strtol");
55*4b40bdbdSAlan Somers 		exit(1);
56*4b40bdbdSAlan Somers 	}
57*4b40bdbdSAlan Somers 	srandomdev();
58*4b40bdbdSAlan Somers 	frac = (double)random() / (double)RANDOM_MAX;
59*4b40bdbdSAlan Somers 	usecs = (useconds_t)((double)max_usecs * frac);
60*4b40bdbdSAlan Somers 	usleep(usecs);
61*4b40bdbdSAlan Somers 
62*4b40bdbdSAlan Somers 	return (0);
63*4b40bdbdSAlan Somers }
64