2 * $Id: sysdep.c 5691 2007-11-04 23:19:17Z dothebart $
4 * Citadel "system dependent" stuff.
5 * See copyright.txt for copyright information.
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.
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.
22 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <sys/syslog.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
66 #include "webserver.h"
68 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
69 pthread_key_t MyConKey; /* TSD key for MyContext() */
70 pthread_key_t MyReq; /* TSD key for MyReq() */
72 void InitialiseSemaphores(void)
76 /* Set up a bunch of semaphores to be used for critical sections */
77 for (i=0; i<MAX_SEMAPHORES; ++i) {
78 pthread_mutex_init(&Critters[i], NULL);
83 * Obtain a semaphore lock to begin a critical section.
85 void begin_critical_section(int which_one)
87 /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
88 pthread_mutex_lock(&Critters[which_one]);
92 * Release a semaphore lock to end a critical section.
94 void end_critical_section(int which_one)
96 pthread_mutex_unlock(&Critters[which_one]);
99 void drop_root(uid_t UID)
101 struct passwd pw, *pwp = NULL;
104 * Now that we've bound the sockets, change to the Citadel user id and its
105 * corresponding group ids
109 #ifdef HAVE_GETPWUID_R
110 #ifdef SOLARIS_GETPWUID
111 pwp = getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf));
112 #else // SOLARIS_GETPWUID
113 getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf), &pwp);
114 #endif // SOLARIS_GETPWUID
115 #else // HAVE_GETPWUID_R
117 #endif // HAVE_GETPWUID_R
120 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
121 "Group IDs will be incorrect.\n", UID,
124 initgroups(pw.pw_name, pw.pw_gid);
125 if (setgid(pw.pw_gid))
126 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
129 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)UID);
130 if (setuid(UID) != 0) {
131 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
133 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
134 prctl(PR_SET_DUMPABLE, 1);