The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
semaphore.c
Go to the documentation of this file.
1/*
2 * This program is 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 */
23RCSID("$Id: a390513e889eed32f384ae57085a210bfa8796e2 $")
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
31{
32 fr_sem_t *sem;
33
34 sem = mmap(NULL, sizeof(fr_sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
35
36 if (sem == MAP_FAILED) {
37 fr_strerror_printf("Failed allocating mmap memory: %s", fr_syserror(errno));
38 return NULL;
39 }
40
41 if (sem_init(sem, 1, SEMAPHORE_LOCKED) != 0) {
42 sem_destroy(sem);
43 munmap(sem, sizeof(fr_sem_t));
44 return NULL;
45 }
46
47 return sem;
48}
49
51{
52 if (!sem) return;
53
54 sem_destroy(sem);
55 munmap(sem, sizeof(fr_sem_t));
56}
#define RCSID(id)
Definition build.h:488
void fr_sem_free(fr_sem_t *sem)
Definition semaphore.c:50
fr_sem_t * fr_sem_alloc(void)
Definition semaphore.c:30
sem_t fr_sem_t
Definition semaphore.h:53
#define SEMAPHORE_LOCKED
Definition semaphore.h:36
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64