The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Macros | Functions
sem.c File Reference

Implementation of named semaphores that release on exit. More...

#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <signal.h>
#include <freeradius-devel/util/perm.h>
#include <freeradius-devel/util/sem.h>
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/syserror.h>
+ Include dependency graph for sem.c:

Go to the source code of this file.

Macros

#define DEFAULT_PROJ_ID   ((int)'f') /* Only 8 bits are used */
 

Functions

int fr_sem_cgid (uid_t *gid, int sem_id)
 Return the GID that created the semaphore. More...
 
int fr_sem_close (int sem_id, char const *file)
 Remove the semaphore, this helps with permissions issues. More...
 
int fr_sem_cuid (uid_t *uid, int sem_id)
 Return the UID that created the semaphore. More...
 
int fr_sem_get (char const *file, int proj_id, uid_t uid, gid_t gid, bool check_perm, bool must_exist)
 Returns a semid for the semaphore associated with the file. More...
 
int fr_sem_gid (uid_t *gid, int sem_id)
 Return the GID that last operated on the semaphore. More...
 
int fr_sem_pid (pid_t *pid, int sem_id)
 Return the PID of the process that last operated on the semaphore. More...
 
int fr_sem_post (int sem_id, char const *file, bool undo_on_exit)
 Decrement the semaphore by 1. More...
 
int fr_sem_take (int sem_id, char const *file, bool undo_on_exit)
 Increment the semaphore by 1. More...
 
int fr_sem_uid (uid_t *uid, int sem_id)
 Return the UID that last operated on the semaphore. More...
 
int fr_sem_wait (int sem_id, char const *file, bool undo_on_exit, bool nonblock)
 Wait for a semaphore to reach 0, then increment it by 1. More...
 
static bool sem_check_gid (char const *file, int proj_id, char const *thing, gid_t expected, gid_t got)
 
static bool sem_check_uid (char const *file, int proj_id, char const *thing, uid_t expected, uid_t got)
 

Detailed Description

Implementation of named semaphores that release on exit.

Definition in file sem.c.

Macro Definition Documentation

◆ DEFAULT_PROJ_ID

#define DEFAULT_PROJ_ID   ((int)'f') /* Only 8 bits are used */

Definition at line 43 of file sem.c.

Function Documentation

◆ fr_sem_cgid()

int fr_sem_cgid ( uid_t *  gid,
int  sem_id 
)

Return the GID that created the semaphore.

Parameters
[out]gidthe last modified the semaphore.
[in]sem_idsemaphore ID.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 154 of file sem.c.

+ Here is the call graph for this function:

◆ fr_sem_close()

int fr_sem_close ( int  sem_id,
char const *  file 
)

Remove the semaphore, this helps with permissions issues.

Parameters
[in]sem_idto close.
[in]fileto use in error messages.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 329 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_cuid()

int fr_sem_cuid ( uid_t *  uid,
int  sem_id 
)

Return the UID that created the semaphore.

Parameters
[out]uidthe last modified the semaphore.
[in]sem_idsemaphore ID.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 128 of file sem.c.

+ Here is the call graph for this function:

◆ fr_sem_get()

int fr_sem_get ( char const *  file,
int  proj_id,
uid_t  uid,
gid_t  gid,
bool  check_perm,
bool  must_exist 
)

Returns a semid for the semaphore associated with the file.

Parameters
[in]fileto get or create semaphore from.
[in]proj_idif 0 will default to '0xf4ee4a31'.
[in]uidthat should own the semaphore.
[in]gidthat should own the semaphore.
[in]check_permVerify the semaphore is owned by the specified uid/gid, and that it was created by the specified uid/gid or root. Also verify that it is not world writable.
[in]must_existsemaphore must already exist.
Returns
  • >= 0 the semaphore id.
  • -1 the file specified does not exist, or there is a permissions error.
  • -2 failed getting semaphore.
  • -3 failed creating semaphore.
  • -4 must_exist was true, and the semaphore does not exist.

Definition at line 421 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_gid()

int fr_sem_gid ( uid_t *  gid,
int  sem_id 
)

Return the GID that last operated on the semaphore.

Parameters
[out]gidthe last modified the semaphore.
[in]sem_idsemaphore ID.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 102 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_pid()

int fr_sem_pid ( pid_t *  pid,
int  sem_id 
)

Return the PID of the process that last operated on the semaphore.

Parameters
[out]pidthat last modified the semaphore.
[in]sem_idsemaphore ID.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 53 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_post()

int fr_sem_post ( int  sem_id,
char const *  file,
bool  undo_on_exit 
)

Decrement the semaphore by 1.

Parameters
[in]sem_idto take.
[in]fileto use in error messages.
[in]undo_on_exitdecrement the semaphore on exit.
Returns
  • 1 already at 0.
  • 0 on success.
  • -1 on failure.

Definition at line 182 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_take()

int fr_sem_take ( int  sem_id,
char const *  file,
bool  undo_on_exit 
)

Increment the semaphore by 1.

Parameters
[in]sem_idto take.
[in]fileto use in error messages.
[in]undo_on_exitdecrement the semaphore on exit.
Returns
  • -1 on failure.
  • 0 on success.

Definition at line 221 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_uid()

int fr_sem_uid ( uid_t *  uid,
int  sem_id 
)

Return the UID that last operated on the semaphore.

Parameters
[out]uidthe last modified the semaphore.
[in]sem_idsemaphore ID.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 76 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_sem_wait()

int fr_sem_wait ( int  sem_id,
char const *  file,
bool  undo_on_exit,
bool  nonblock 
)

Wait for a semaphore to reach 0, then increment it by 1.

Parameters
[in]sem_idto operate on.
[in]fileto use in error messages.
[in]undo_on_exitIf true, semaphore will be decremented if this process exits.
[in]nonblockIf true, don't wait and return 1 if the semaphore is not at 0.
Returns
  • 1 would have blocked waiting for semaphore.
  • 0 incremented the semaphore.
  • -1 permissions error (EACCES).
  • -2 another error occurred.

Definition at line 252 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sem_check_gid()

static bool sem_check_gid ( char const *  file,
int  proj_id,
char const *  thing,
gid_t  expected,
gid_t  got 
)
static

Definition at line 370 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sem_check_uid()

static bool sem_check_uid ( char const *  file,
int  proj_id,
char const *  thing,
uid_t  expected,
uid_t  got 
)
static

Definition at line 340 of file sem.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function: