2 * $Id: sysdep.c 7989 2009-10-31 15:29:37Z davew $
4 * Citadel context management stuff.
5 * See COPYING for copyright information.
7 * Here's where we (hopefully) have all the code that manipulates contexts.
18 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <sys/syslog.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
37 #include <sys/resource.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <arpa/inet.h>
48 #include <libcitadel.h>
51 #include "sysdep_decls.h"
52 #include "citserver.h"
56 #include "housekeeping.h"
57 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
60 #ifdef HAVE_SYS_SELECT_H
61 #include <sys/select.h>
68 #include "ctdl_module.h"
75 citthread_key_t MyConKey; /* TSD key for MyContext() */
79 CitContext *ContextList = NULL;
81 time_t last_purge = 0; /* Last dead session purge */
82 int num_sessions = 0; /* Current number of sessions */
84 /* Flag for single user mode */
85 static int want_single_user = 0;
87 /* Try to go single user */
89 int CtdlTrySingleUser(void)
93 begin_critical_section(S_SINGLE_USER);
101 end_critical_section(S_SINGLE_USER);
105 void CtdlEndSingleUser(void)
107 begin_critical_section(S_SINGLE_USER);
108 want_single_user = 0;
109 end_critical_section(S_SINGLE_USER);
113 int CtdlWantSingleUser(void)
115 return want_single_user;
118 int CtdlIsSingleUser(void)
120 if (want_single_user)
122 /* check for only one context here */
123 if (num_sessions == 1)
133 * Check to see if a user is currently logged in
134 * Take care with what you do as a result of this test.
135 * The user may not have been logged in when this function was called BUT
136 * because of threading the user might be logged in before you test the result.
138 int CtdlIsUserLoggedIn (char *user_name)
143 begin_critical_section (S_SESSION_TABLE);
144 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
145 if (!strcasecmp(cptr->user.fullname, user_name)) {
150 end_critical_section(S_SESSION_TABLE);
157 * Check to see if a user is currently logged in.
158 * Basically same as CtdlIsUserLoggedIn() but uses the user number instead.
159 * Take care with what you do as a result of this test.
160 * The user may not have been logged in when this function was called BUT
161 * because of threading the user might be logged in before you test the result.
163 int CtdlIsUserLoggedInByNum (int usernum)
168 begin_critical_section(S_SESSION_TABLE);
169 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
170 if (cptr->user.usernum == usernum) {
174 end_critical_section(S_SESSION_TABLE);
181 * Return a pointer to the CitContext structure bound to the thread which
182 * called this function. If there's no such binding (for example, if it's
183 * called by the housekeeper thread) then a generic 'master' CC is returned.
185 * This function is used *VERY* frequently and must be kept small.
187 CitContext *MyContext(void) {
189 register CitContext *c;
191 return ((c = (CitContext *) citthread_getspecific(MyConKey),
192 c == NULL) ? &masterCC : c
200 * Terminate a session.
202 void RemoveContext (CitContext *con)
205 CtdlLogPrintf(CTDL_ERR,
206 "WARNING: RemoveContext() called with NULL!\n");
209 CtdlLogPrintf(CTDL_DEBUG, "RemoveContext() session %d\n", con->cs_pid);
211 /* Run any cleanup routines registered by loadable modules.
212 * Note: We have to "become_session()" because the cleanup functions
213 * might make references to "CC" assuming it's the right one.
217 PerformSessionHooks(EVT_STOP);
218 become_session(NULL);
220 CtdlLogPrintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
222 /* If the client is still connected, blow 'em away. */
223 CtdlLogPrintf(CTDL_DEBUG, "Closing socket %d\n", con->client_socket);
224 close(con->client_socket);
226 /* If using AUTHMODE_LDAP, free the DN */
232 CtdlLogPrintf(CTDL_DEBUG, "Done with RemoveContext()\n");
239 * Initialize a new context and place it in the list. The session number
240 * used to be the PID (which is why it's called cs_pid), but that was when we
241 * had one process per session. Now we just assign them sequentially, starting
242 * at 1 (don't change it to 0 because masterCC uses 0).
244 CitContext *CreateNewContext(void) {
246 static int next_pid = 0;
248 me = (CitContext *) malloc(sizeof(CitContext));
250 CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
253 memset(me, 0, sizeof(CitContext));
255 /* Give the contaxt a name. Hopefully makes it easier to track */
256 strcpy (me->user.fullname, "SYS_notauth");
258 /* The new context will be created already in the CON_EXECUTING state
259 * in order to prevent another thread from grabbing it while it's
262 me->state = CON_EXECUTING;
264 * Generate a unique session number and insert this context into
267 begin_critical_section(S_SESSION_TABLE);
268 me->cs_pid = ++next_pid;
270 me->next = ContextList;
272 if (me->next != NULL) {
276 end_critical_section(S_SESSION_TABLE);
281 CitContext *CtdlGetContextArray(int *count)
284 CitContext *nptr, *cptr;
286 nContexts = num_sessions;
287 nptr = malloc(sizeof(CitContext) * nContexts);
290 begin_critical_section(S_SESSION_TABLE);
291 for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++)
292 memcpy(&nptr[i], cptr, sizeof (CitContext));
293 end_critical_section (S_SESSION_TABLE);
302 * This function fills in a context and its user field correctly
303 * Then creates/loads that user
305 void CtdlFillSystemContext(CitContext *context, char *name)
307 char sysname[USERNAME_SIZE];
309 memset(context, 0, sizeof(CitContext));
310 context->internal_pgm = 1;
312 strcpy (sysname, "SYS_");
313 strcat (sysname, name);
314 /* internal_create_user has the side effect of loading the user regardless of wether they
315 * already existed or needed to be created
317 internal_create_user (sysname, &(context->user), -1) ;
319 /* Check to see if the system user needs upgrading */
320 if (context->user.usernum == 0)
321 { /* old system user with number 0, upgrade it */
322 context->user.usernum = get_new_user_number();
323 CtdlLogPrintf(CTDL_DEBUG, "Upgrading system user \"%s\" from user number 0 to user number %d\n", context->user.fullname, context->user.usernum);
324 /* add user to the database */
325 CtdlPutUser(&(context->user));
326 cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
331 * Cleanup any contexts that are left lying around
333 void context_cleanup(void)
335 CitContext *ptr = NULL;
336 CitContext *rem = NULL;
339 * Clean up the contexts.
340 * There are no threads so no critical_section stuff is needed.
344 /* We need to update the ContextList because some modules may want to itterate it
345 * Question is should we NULL it before iterating here or should we just keep updating it
346 * as we remove items?
348 * Answer is to NULL it first to prevent modules from doing any actions on the list at all
352 /* Remove the session from the active list */
356 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
366 * Terminate another session.
367 * (This could justifiably be moved out of sysdep.c because it
368 * no longer does anything that is system-dependent.)
370 void kill_session(int session_to_kill) {
373 begin_critical_section(S_SESSION_TABLE);
374 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
375 if (ptr->cs_pid == session_to_kill) {
379 end_critical_section(S_SESSION_TABLE);
383 * Purge all sessions which have the 'kill_me' flag set.
384 * This function has code to prevent it from running more than once every
385 * few seconds, because running it after every single unbind would waste a lot
386 * of CPU time and keep the context list locked too much. To force it to run
387 * anyway, set "force" to nonzero.
389 void dead_session_purge(int force) {
390 CitContext *ptr, *ptr2; /* general-purpose utility pointer */
391 CitContext *rem = NULL; /* list of sessions to be destroyed */
394 if ( (time(NULL) - last_purge) < 5 ) {
395 return; /* Too soon, go away */
400 if (try_critical_section(S_SESSION_TABLE))
408 if ( (ptr2->state == CON_IDLE) && (ptr2->kill_me) ) {
409 /* Remove the session from the active list */
411 ptr2->prev->next = ptr2->next;
414 ContextList = ptr2->next;
417 ptr2->next->prev = ptr2->prev;
421 /* And put it on our to-be-destroyed list */
426 end_critical_section(S_SESSION_TABLE);
428 /* Now that we no longer have the session list locked, we can take
429 * our time and destroy any sessions on the to-be-killed list, which
430 * is allocated privately on this thread's stack.
432 while (rem != NULL) {
433 CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", rem->cs_pid);
446 * masterCC is the context we use when not attached to a session. This
447 * function initializes it.
449 void InitializeMasterCC(void) {
450 memset(&masterCC, 0, sizeof( CitContext));
451 masterCC.internal_pgm = 1;
459 * Bind a thread to a context. (It's inline merely to speed things up.)
461 INLINE void become_session(CitContext *which_con) {
462 citthread_setspecific(MyConKey, (void *)which_con );