/src/freeradius-server/src/lib/util/semaphore.c
Line | Count | Source |
1 | | /* |
2 | | * This program is free software; you can redistribute it and/or modify |
3 | | * it under the terms of the GNU General Public License as published by |
4 | | * the Free Software Foundation; either version 2 of the License, or (at |
5 | | * your option) any later version. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
15 | | */ |
16 | | |
17 | | /** API for POSIX semaphores in mmapped memory. |
18 | | * |
19 | | * @file src/lib/util/semaphore.c |
20 | | * |
21 | | * @copyright 2025 Network RADIUS SAS (legal@networkradius.com) |
22 | | */ |
23 | | RCSID("$Id: 703d29eb02b69dd7696627d6074ff17660bfe0a8 $") |
24 | | |
25 | | #include <sys/mman.h> |
26 | | #include <freeradius-devel/util/semaphore.h> |
27 | | #include <freeradius-devel/util/strerror.h> |
28 | | #include <freeradius-devel/util/syserror.h> |
29 | | |
30 | | fr_sem_t *fr_sem_alloc(void) |
31 | 0 | { |
32 | 0 | fr_sem_t *sem; |
33 | |
|
34 | 0 | sem = mmap(NULL, sizeof(fr_sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); |
35 | |
|
36 | 0 | if (sem == MAP_FAILED) { |
37 | 0 | fr_strerror_printf("Failed allocating mmap memory: %s", fr_syserror(errno)); |
38 | 0 | return NULL; |
39 | 0 | } |
40 | | |
41 | 0 | if (sem_init(sem, 1, SEMAPHORE_LOCKED) != 0) { |
42 | 0 | sem_destroy(sem); |
43 | 0 | munmap(sem, sizeof(fr_sem_t)); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | | |
47 | 0 | return sem; |
48 | 0 | } |
49 | | |
50 | | void fr_sem_free(fr_sem_t *sem) |
51 | 0 | { |
52 | 0 | if (!sem) return; |
53 | | |
54 | 0 | sem_destroy(sem); |
55 | 0 | munmap(sem, sizeof(fr_sem_t)); |
56 | 0 | } |