Merge commit 'dfc115332c94a2f62058ac7f2bce7631fbd20b3d'
[unleashed/tickless.git] / lib / libcrypto / arc4random / arc4random_aix.h
blob3142a1f278995b73ec046e3242417f6e9d050a63
1 /* $OpenBSD: arc4random_aix.h,v 1.2 2016/06/30 12:19:51 bcook Exp $ */
3 /*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Stub functions for portability.
26 #include <sys/mman.h>
28 #include <pthread.h>
29 #include <signal.h>
31 static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
32 #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
33 #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
35 #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
37 static inline void
38 _getentropy_fail(void)
40 raise(SIGKILL);
43 static volatile sig_atomic_t _rs_forked;
45 static inline void
46 _rs_forkhandler(void)
48 _rs_forked = 1;
51 static inline void
52 _rs_forkdetect(void)
54 static pid_t _rs_pid = 0;
55 pid_t pid = getpid();
57 if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
58 _rs_pid = pid;
59 _rs_forked = 0;
60 if (rs)
61 memset(rs, 0, sizeof(*rs));
65 static inline int
66 _rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
68 if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
69 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
70 return (-1);
72 if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
73 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
74 munmap(*rsp, sizeof(**rsp));
75 *rsp = NULL;
76 return (-1);
79 _ARC4_ATFORK(_rs_forkhandler);
80 return (0);