lock_ops.h
ba98af46
 /*
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
ba98af46
  *
ccab6f01
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
ba98af46
  *
ccab6f01
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ba98af46
  */
 
2266bb9c
 /*!
 * \file
 * \brief Kamailio core :: Kamailio locking library
 * \ingroup core
 * \author andrei
 * Module: \ref core
 *
ba98af46
  *   WARNING: do not include this file directly, use instead locking.h
  *   (unless you don't need to alloc/dealloc locks)
  *
  *
 Implements:
 
 	simple locks:
 	-------------
 	gen_lock_t* lock_init(gen_lock_t* lock); - inits the lock
 	void    lock_destroy(gen_lock_t* lock);  - removes the lock (e.g sysv rmid)
 	void    lock_get(gen_lock_t* lock);      - lock (mutex down)
 	void    lock_release(gen_lock_t* lock);  - unlock (mutex up)
d3da8467
 	int     lock_try(gen_lock_t* lock);      - tries to get the lock, returns
8b8fc486
 	                                            0 on success and !=0 on failure
ba98af46
 	
d3da8467
 	lock sets: 
ba98af46
 	----------
194b6a35
 	gen_lock_set_t* lock_set_init(gen_lock_set_t* set);  - inits the lock set
 	void lock_set_destroy(gen_lock_set_t* s);        - removes the lock set
 	void lock_set_get(gen_lock_set_t* s, int i);     - locks sem i from the set
d3da8467
 	void lock_set_release(gen_lock_set_t* s, int i)  - unlocks sem i from the
 	                                                   set
 	int  lock_set_try(gen_lock_set_t* s, int i);    - tries to lock the sem i,
 	                                                  returns 0 on success and
8b8fc486
 	                                                  !=0 on failure
ccab6f01
 	
 	defines:
 	--------
 	GEN_LOCK_T_PREFERRED - defined if using  arrays of gen_lock_t is as good as
 	                      using a lock set (gen_lock_set_t). 
 						  In general is better to have the locks "close" or 
 						  inside the protected data structure rather then 
 						  having a separate array or lock set. However in some
 						  case (e.g. SYSV_LOCKS) is better to use lock sets,
 						  either due to lock number limitations, excesive 
 						  performance or memory overhead. In this cases
 						  GEN_LOCK_T_PREFERRED will not be defined.
 	GEN_LOCK_T_UNLIMITED - defined if there is no system imposed limit on
 	                       the number of locks (other then the memory).
 	GEN_LOCK_SET_T_UNLIMITED
 	                      - like above but for the size of a lock set.
ba98af46
 
6aa0ab09
 WARNING: - lock_set_init may fail for large number of sems (e.g. sysv). 
          - signals are not treated! (some locks are "awakened" by the signals)
ba98af46
 */
 
 #ifndef _lock_ops_h
 #define _lock_ops_h
 
8b8fc486
 #ifdef USE_FUTEX
 #include "futexlock.h"
 /* if no native atomic ops support => USE_FUTEX will be undefined */
 #endif
 
 
 #ifdef USE_FUTEX
 
 typedef futex_lock_t gen_lock_t;
 
 #define lock_destroy(lock) /* do nothing */
 #define lock_init(lock) futex_init(lock)
 #define lock_try(lock)  futex_try(lock)
 #define lock_get(lock)  futex_get(lock)
 #define lock_release(lock) futex_release(lock)
 
ba98af46
 
8b8fc486
 #elif defined FAST_LOCK
ba98af46
 #include "fastlock.h"
 
 typedef fl_lock_t gen_lock_t;
 
 
 #define lock_destroy(lock) /* do nothing */ 
 
 inline static gen_lock_t* lock_init(gen_lock_t* lock)
 {
 	init_lock(*lock);
 	return lock;
 }
 
d3da8467
 #define lock_try(lock) try_lock(lock)
ba98af46
 #define lock_get(lock) get_lock(lock)
 #define lock_release(lock) release_lock(lock)
 
d3da8467
 
ba98af46
 #elif defined USE_PTHREAD_MUTEX
 #include <pthread.h>
 
 typedef pthread_mutex_t gen_lock_t;
 
 #define lock_destroy(lock) /* do nothing */ 
 
 inline static gen_lock_t* lock_init(gen_lock_t* lock)
 {
 	if (pthread_mutex_init(lock, 0)==0) return lock;
 	else return 0;
 }
 
d3da8467
 #define lock_try(lock) pthread_mutex_trylock(lock)
ba98af46
 #define lock_get(lock) pthread_mutex_lock(lock)
 #define lock_release(lock) pthread_mutex_unlock(lock)
 
 
 
 #elif defined USE_POSIX_SEM
 #include <semaphore.h>
 
 typedef sem_t gen_lock_t;
 
 #define lock_destroy(lock) /* do nothing */ 
 
 inline static gen_lock_t* lock_init(gen_lock_t* lock)
 {
 	if (sem_init(lock, 1, 1)<0) return 0;
 	return lock;
 }
 
d3da8467
 #define lock_try(lock) sem_trywait(lock)
ba98af46
 #define lock_get(lock) sem_wait(lock)
 #define lock_release(lock) sem_post(lock)
 
 
 #elif defined USE_SYSV_SEM
 #include <sys/ipc.h>
 #include <sys/sem.h>
 
dd0e65a8
 #include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include "dprint.h"
 #include "globals.h" /* uid */
 
ba98af46
 #if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED)) 
 	
 	/* union semun is defined by including sem.h */
 #else
 	/* according to X/OPEN we have to define it ourselves */
 	union semun {
 		int val;                      /* value for SETVAL */
 		struct semid_ds *buf;         /* buffer for IPC_STAT, IPC_SET */
 		unsigned short int *array;    /* array for GETALL, SETALL */
 		struct seminfo *__buf;        /* buffer for IPC_INFO */
 	};
 #endif
 
 typedef int gen_lock_t;
 
 
 
 
 inline static gen_lock_t* lock_init(gen_lock_t* lock)
 {
 	union semun su;
dd0e65a8
 	int euid;
ba98af46
 	
dd0e65a8
 	euid=geteuid();
 	if (uid && uid!=euid)
 		seteuid(uid); /* set euid to the cfg. requested one */
ba98af46
 	*lock=semget(IPC_PRIVATE, 1, 0700);
dd0e65a8
 	if (uid && uid!=euid)
 		seteuid(euid); /* restore it */
ba98af46
 	if (*lock==-1) return 0;
 	su.val=1;
 	if (semctl(*lock, 0, SETVAL, su)==-1){
 		/* init error*/
 		return 0;
 	}
 	return lock;
 }
 
 inline static void lock_destroy(gen_lock_t* lock)
 {
 	semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
 }
 
 
d3da8467
 /* returns 0 if it got the lock, -1 otherwise */
 inline static int lock_try(gen_lock_t* lock)
 {
 	struct sembuf sop;
 
 	sop.sem_num=0;
 	sop.sem_op=-1; /* down */
 	sop.sem_flg=IPC_NOWAIT; 
 tryagain:
 	if (semop(*lock, &sop, 1)==-1){
 		if (errno==EAGAIN){
 			return -1;
 		}else if (errno==EINTR){
 			DBG("lock_get: signal received while waiting for on a mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
d3da8467
 			return -1;
 		}
 	}
 	return 0;
 }
 
ba98af46
 inline static void lock_get(gen_lock_t* lock)
 {
 	struct sembuf sop;
 
 	sop.sem_num=0;
 	sop.sem_op=-1; /* down */
 	sop.sem_flg=0; 
e7d698cc
 tryagain:
 	if (semop(*lock, &sop, 1)==-1){
 		if (errno==EINTR){
 			DBG("lock_get: signal received while waiting for on a mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
e7d698cc
 		}
 	}
ba98af46
 }
 
 inline static void lock_release(gen_lock_t* lock)
 {
 	struct sembuf sop;
 	
 	sop.sem_num=0;
 	sop.sem_op=1; /* up */
 	sop.sem_flg=0; 
e7d698cc
 tryagain:
 	if (semop(*lock, &sop, 1)==-1){
 		if (errno==EINTR){
 			/* very improbable*/
 			DBG("lock_release: signal received while releasing a mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
e7d698cc
 		}
 	}
ba98af46
 }
 
6419a43f
 
 #else
 #error "no locking method selected"
 #endif
 
 
ba98af46
 /* lock sets */
 
8b8fc486
 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || \
 	defined(USE_POSIX_SEM) || defined(USE_FUTEX)
ccab6f01
 #define GEN_LOCK_T_PREFERRED
 #define GEN_LOCK_T_PREFERED  /* backwards compat. */
 #define GEN_LOCK_T_UNLIMITED
 #define GEN_LOCK_SET_T_UNLIMITED
6419a43f
 
194b6a35
 struct gen_lock_set_t_ {
6419a43f
 	long size;
 	gen_lock_t* locks;
 }; /* must be  aligned (32 bits or 64 depending on the arch)*/
194b6a35
 typedef struct gen_lock_set_t_ gen_lock_set_t;
6419a43f
 
 
 #define lock_set_destroy(lock_set) /* do nothing */
 
194b6a35
 inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
6419a43f
 {
 	int r;
 	for (r=0; r<s->size; r++) if (lock_init(&s->locks[r])==0) return 0;
 	return s;
 }
 
 /* WARNING: no boundary checks!*/
d3da8467
 #define lock_set_try(set, i) lock_try(&set->locks[i])
6419a43f
 #define lock_set_get(set, i) lock_get(&set->locks[i])
 #define lock_set_release(set, i) lock_release(&set->locks[i])
 
 #elif defined(USE_SYSV_SEM)
ccab6f01
 #undef GEN_LOCK_T_PREFERRED
 #undef GEN_LOCK_T_PREFERED  /* backwards compat. */
 #undef GEN_LOCK_T_UNLIMITED
 #undef GEN_LOCK_SET_T_UNLIMITED
 #define GEN_LOCK_T_LIMITED
 #define GEN_LOCK_SET_T_LIMITED
6419a43f
 
194b6a35
 struct gen_lock_set_t_ {
6419a43f
 	int size;
 	int semid;
 };
 
 
194b6a35
 typedef struct gen_lock_set_t_ gen_lock_set_t;
 inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
ba98af46
 {
 	union semun su;
 	int r;
dd0e65a8
 	int euid;
 
 	euid=geteuid();
 	if (uid && uid!=euid)
 		seteuid(uid); /* set euid to the cfg. requested one */
ba98af46
 	s->semid=semget(IPC_PRIVATE, s->size, 0700);
dd0e65a8
 	if (uid && uid!=euid)
 		seteuid(euid); /* restore euid */
ba98af46
 	if (s->semid==-1){
5f1205c8
 		LM_CRIT("(SYSV): semget (..., %d, 0700) failed: %s\n",
dd0e65a8
 				s->size, strerror(errno));
ba98af46
 		return 0;
 	}
 	su.val=1;
 	for (r=0; r<s->size; r++){
 		if (semctl(s->semid, r, SETVAL, su)==-1){
5f1205c8
 			LM_CRIT("(SYSV): semctl failed on sem %d: %s\n", r, strerror(errno));
ba98af46
 			semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
 			return 0;
 		}
 	}
 	return s;
 }
 
194b6a35
 inline static void lock_set_destroy(gen_lock_set_t* s)
ba98af46
 {
 	semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
 }
 
d3da8467
 
 /* returns 0 if it "gets" the lock, -1 otherwise */
 inline static int lock_set_try(gen_lock_set_t* s, int n)
 {
 	struct sembuf sop;
 	
 	sop.sem_num=n;
 	sop.sem_op=-1; /* down */
 	sop.sem_flg=IPC_NOWAIT; 
 tryagain:
 	if (semop(s->semid, &sop, 1)==-1){
 		if (errno==EAGAIN){
 			return -1;
 		}else if (errno==EINTR){
 			DBG("lock_get: signal received while waiting for on a mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
d3da8467
 			return -1;
 		}
 	}
 	return 0;
 }
 
 
194b6a35
 inline static void lock_set_get(gen_lock_set_t* s, int n)
ba98af46
 {
 	struct sembuf sop;
 	sop.sem_num=n;
 	sop.sem_op=-1; /* down */
 	sop.sem_flg=0;
e7d698cc
 tryagain:
 	if (semop(s->semid, &sop, 1)==-1){
 		if (errno==EINTR){
 			DBG("lock_set_get: signal received while waiting on a mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
e7d698cc
 		}
 	}
ba98af46
 }
 
194b6a35
 inline static void lock_set_release(gen_lock_set_t* s, int n)
ba98af46
 {
 	struct sembuf sop;
 	sop.sem_num=n;
 	sop.sem_op=1; /* up */
 	sop.sem_flg=0;
e7d698cc
 tryagain:
 	if (semop(s->semid, &sop, 1)==-1){
 		if (errno==EINTR){
 			/* very improbable */
 			DBG("lock_set_release: signal received while releasing mutex\n");
 			goto tryagain;
 		}else{
5f1205c8
 			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
e7d698cc
 		}
 	}
ba98af46
 }
6419a43f
 #else 
 #error "no lock set method selected"
ba98af46
 #endif
 
 
 #endif