8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / test / libc-tests / tests / random / arc4random_forkall.c
blobbadd3fe9e86496d51704c53c51975dd70dd26943
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2015, Joyent, Inc.
17 * Tests to make sure that a parent and child do not get the same arc4random
18 * state across a forkall. This source file is used to make two tests. One which
19 * initializes the data in advance, one of which does not.
22 #include <stdlib.h>
23 #include <sys/mman.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <sys/wait.h>
30 typedef struct arc4_fork {
31 uint32_t af_parent;
32 uint32_t af_child;
33 uint8_t af_pbuf[4096];
34 uint8_t af_cbuf[4096];
35 } arc4_fork_t;
37 arc4_fork_t *fork_data;
39 int
40 main(void)
42 int e, i;
43 pid_t p, child;
45 #ifdef ARC4_PREINIT
46 (void) arc4random();
47 #endif
49 fork_data = (arc4_fork_t *)mmap(NULL, sizeof (arc4_fork_t),
50 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
51 assert(fork_data != MAP_FAILED);
53 p = forkall();
54 assert(p != -1);
55 if (p == 0) {
56 fork_data->af_child = arc4random();
57 arc4random_buf(fork_data->af_cbuf, sizeof (fork_data->af_cbuf));
58 exit(0);
61 fork_data->af_parent = arc4random();
62 arc4random_buf(fork_data->af_pbuf, sizeof (fork_data->af_pbuf));
63 do {
64 child = wait(&e);
65 } while (child == -1 && errno == EINTR);
66 assert(child == p);
68 /* Now verify our data doesn't match */
69 assert(fork_data->af_parent != fork_data->af_child);
72 * For the buffer here, we're mostly concerned that they aren't somehow
73 * getting the same stream.
75 for (i = 0; i < sizeof (fork_data->af_pbuf); i++) {
76 if (fork_data->af_pbuf[i] != fork_data->af_cbuf[i])
77 break;
79 assert(i != sizeof (fork_data->af_pbuf));
81 return (0);