xref: /freebsd/tests/sys/net/randsleep.c (revision c27ac74f9cfea08a96087135183ad70a372f735d)
14b40bdbdSAlan Somers /*
24b40bdbdSAlan Somers  * Copyright (c) 2014 Spectra Logic Corporation
34b40bdbdSAlan Somers  * All rights reserved.
44b40bdbdSAlan Somers  *
54b40bdbdSAlan Somers  * Redistribution and use in source and binary forms, with or without
64b40bdbdSAlan Somers  * modification, are permitted provided that the following conditions
74b40bdbdSAlan Somers  * are met:
84b40bdbdSAlan Somers  * 1. Redistributions of source code must retain the above copyright
94b40bdbdSAlan Somers  *    notice, this list of conditions, and the following disclaimer,
104b40bdbdSAlan Somers  *    without modification.
114b40bdbdSAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
124b40bdbdSAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
134b40bdbdSAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
144b40bdbdSAlan Somers  *    including a substantially similar Disclaimer requirement for further
154b40bdbdSAlan Somers  *    binary redistribution.
164b40bdbdSAlan Somers  *
174b40bdbdSAlan Somers  * NO WARRANTY
184b40bdbdSAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194b40bdbdSAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
204b40bdbdSAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
214b40bdbdSAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
224b40bdbdSAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
234b40bdbdSAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
244b40bdbdSAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
254b40bdbdSAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
264b40bdbdSAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
274b40bdbdSAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284b40bdbdSAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
294b40bdbdSAlan Somers  *
304b40bdbdSAlan Somers  * Authors: Alan Somers         (Spectra Logic Corporation)
314b40bdbdSAlan Somers  *
324b40bdbdSAlan Somers  * $FreeBSD$
334b40bdbdSAlan Somers  */
344b40bdbdSAlan Somers 
354b40bdbdSAlan Somers #include <errno.h>
364b40bdbdSAlan Somers #include <stdio.h>
374b40bdbdSAlan Somers #include <stdlib.h>
384b40bdbdSAlan Somers #include <unistd.h>
394b40bdbdSAlan Somers 
40*c27ac74fSLi-Wen Hsu #define RANDOM_MAX ((1U<<31) - 1)
414b40bdbdSAlan Somers 
424b40bdbdSAlan Somers int main(int argc, char** argv){
434b40bdbdSAlan Somers 	useconds_t max_usecs, usecs;
444b40bdbdSAlan Somers 	double frac;
454b40bdbdSAlan Somers 
464b40bdbdSAlan Somers 	if (argc != 2) {
474b40bdbdSAlan Somers 		printf("Usage: randsleep <max_microseconds>\n");
484b40bdbdSAlan Somers 		exit(2);
494b40bdbdSAlan Somers 	}
504b40bdbdSAlan Somers 
514b40bdbdSAlan Somers 	errno = 0;
524b40bdbdSAlan Somers 	max_usecs = (useconds_t)strtol(argv[1], NULL, 0);
534b40bdbdSAlan Somers 	if (errno != 0) {
544b40bdbdSAlan Somers 		perror("strtol");
554b40bdbdSAlan Somers 		exit(1);
564b40bdbdSAlan Somers 	}
574b40bdbdSAlan Somers 	srandomdev();
584b40bdbdSAlan Somers 	frac = (double)random() / (double)RANDOM_MAX;
594b40bdbdSAlan Somers 	usecs = (useconds_t)((double)max_usecs * frac);
604b40bdbdSAlan Somers 	usleep(usecs);
614b40bdbdSAlan Somers 
624b40bdbdSAlan Somers 	return (0);
634b40bdbdSAlan Somers }
64