* migrated SUBST stuff to hash
[citadel.git] / webcit / sysdep.c
1 /*
2  * $Id: sysdep.c 5691 2007-11-04 23:19:17Z dothebart $
3  *
4  * Citadel "system dependent" stuff.
5  * See copyright.txt for copyright information.
6  *
7  * Here's where we (hopefully) have most parts of the Citadel server that
8  * would need to be altered to run the server in a non-POSIX environment.
9  * 
10  * If we ever port to a different platform and either have multiple
11  * variants of this file or simply load it up with #ifdefs.
12  *
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <sys/socket.h>
26 #include <syslog.h>
27 #include <sys/syslog.h>
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <time.h>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <time.h>
37 # endif
38 #endif
39
40 #include <limits.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <sys/un.h>
47 #include <string.h>
48 #include <pwd.h>
49 #include <errno.h>
50 #include <stdarg.h>
51 #include <grp.h>
52 #ifdef HAVE_PTHREAD_H
53 #include <pthread.h>
54 #endif
55 #include "webcit.h"
56 #include "sysdep.h"
57
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
60 #endif
61
62 #ifndef HAVE_SNPRINTF
63 #include "snprintf.h"
64 #endif
65
66 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
67 pthread_key_t MyConKey;                         /* TSD key for MyContext() */
68 pthread_key_t MyReq;                            /* TSD key for MyReq() */
69
70 void InitialiseSemaphores(void)
71 {
72         int i;
73
74         /* Set up a bunch of semaphores to be used for critical sections */
75         for (i=0; i<MAX_SEMAPHORES; ++i) {
76                 pthread_mutex_init(&Critters[i], NULL);
77         }
78 }
79
80 /*
81  * Obtain a semaphore lock to begin a critical section.
82  */
83 void begin_critical_section(int which_one)
84 {
85         /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
86         pthread_mutex_lock(&Critters[which_one]);
87 }
88
89 /*
90  * Release a semaphore lock to end a critical section.
91  */
92 void end_critical_section(int which_one)
93 {
94         pthread_mutex_unlock(&Critters[which_one]);
95 }
96