cleanup: remove unneeded includes.
[citadel.git] / citadel / context.c
1 /*
2  * Citadel context management stuff.
3  * Here's where we (hopefully) have all the code that manipulates contexts.
4  *
5  * Copyright (c) 1987-2011 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <sys/socket.h>
27 #include <syslog.h>
28 #include <sys/syslog.h>
29 /*
30 #include <sys/syscall.h>
31 */
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <limits.h>
45 #include <sys/resource.h>
46 #include <netinet/in.h>
47 #include <netinet/tcp.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50 #include <sys/un.h>
51 #include <string.h>
52 #include <pwd.h>
53 #include <errno.h>
54 #include <stdarg.h>
55 #include <grp.h>
56 #include <libcitadel.h>
57 #include "citadel.h"
58 #include "server.h"
59 #include "sysdep_decls.h"
60 #include "citserver.h"
61 #include "support.h"
62 #include "config.h"
63 #include "database.h"
64 #include "housekeeping.h"
65 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
66 #include "ecrash.h"
67
68 #ifdef HAVE_SYS_SELECT_H
69 #include <sys/select.h>
70 #endif
71
72 #include "ctdl_module.h"
73 #include "threads.h"
74 #include "user_ops.h"
75 #include "control.h"
76
77 int DebugSession = 0;
78
79 pthread_key_t MyConKey;                         /* TSD key for MyContext() */
80
81
82 CitContext masterCC;
83 CitContext *ContextList = NULL;
84
85 time_t last_purge = 0;                          /* Last dead session purge */
86 int num_sessions = 0;                           /* Current number of sessions */
87 int next_pid = 0;
88
89 /* Flag for single user mode */
90 static int want_single_user = 0;
91
92 /* Try to go single user */
93
94 int CtdlTrySingleUser(void)
95 {
96         int can_do = 0;
97         
98         begin_critical_section(S_SINGLE_USER);
99         if (want_single_user)
100                 can_do = 0;
101         else
102         {
103                 can_do = 1;
104                 want_single_user = 1;
105         }
106         end_critical_section(S_SINGLE_USER);
107         return can_do;
108 }
109
110 void CtdlEndSingleUser(void)
111 {
112         begin_critical_section(S_SINGLE_USER);
113         want_single_user = 0;
114         end_critical_section(S_SINGLE_USER);
115 }
116
117
118 int CtdlWantSingleUser(void)
119 {
120         return want_single_user;
121 }
122
123 int CtdlIsSingleUser(void)
124 {
125         if (want_single_user)
126         {
127                 /* check for only one context here */
128                 if (num_sessions == 1)
129                         return TRUE;
130         }
131         return FALSE;
132 }
133
134
135
136
137
138 /*
139  * Locate a context by its session number and terminate it if the user is able.
140  * User can NOT terminate their current session.
141  * User CAN terminate any other session that has them logged in.
142  * Aide CAN terminate any session except the current one.
143  */
144 int CtdlTerminateOtherSession (int session_num)
145 {
146         struct CitContext *CCC = CC;
147         int ret = 0;
148         CitContext *ccptr;
149         int aide;
150
151         if (session_num == CCC->cs_pid) return TERM_NOTALLOWED;
152
153         aide = ( (CCC->user.axlevel >= AxAideU) || (CCC->internal_pgm) ) ;
154
155         CONM_syslog(LOG_DEBUG, "Locating session to kill\n");
156         begin_critical_section(S_SESSION_TABLE);
157         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
158                 if (session_num == ccptr->cs_pid) {
159                         ret |= TERM_FOUND;
160                         if ((ccptr->user.usernum == CCC->user.usernum) || aide) {
161                                 ret |= TERM_ALLOWED;
162                         }
163                         break;
164                 }
165         }
166
167         if (((ret & TERM_FOUND) != 0) && ((ret & TERM_ALLOWED) != 0))
168         {
169                 if (ccptr->IO != NULL) {
170                         AsyncIO *IO = ccptr->IO;
171                         end_critical_section(S_SESSION_TABLE);
172                         KillAsyncIOContext(IO);
173                 }
174                 else
175                 {
176                         if (ccptr->user.usernum == CCC->user.usernum)
177                                 ccptr->kill_me = KILLME_ADMIN_TERMINATE;
178                         else
179                                 ccptr->kill_me = KILLME_IDLE;
180                         end_critical_section(S_SESSION_TABLE);
181                 }
182         }
183         else
184                 end_critical_section(S_SESSION_TABLE);
185
186         return ret;
187 }
188
189
190
191 /*
192  * Check to see if the user who we just sent mail to is logged in.  If yes,
193  * bump the 'new mail' counter for their session.  That enables them to
194  * receive a new mail notification without having to hit the database.
195  */
196 void BumpNewMailCounter(long which_user) 
197 {
198         CtdlBumpNewMailCounter(which_user);
199 }
200
201 void CtdlBumpNewMailCounter(long which_user)
202 {
203         CitContext *ptr;
204
205         begin_critical_section(S_SESSION_TABLE);
206
207         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
208                 if (ptr->user.usernum == which_user) {
209                         ptr->newmail += 1;
210                 }
211         }
212
213         end_critical_section(S_SESSION_TABLE);
214 }
215
216
217 /*
218  * Check to see if a user is currently logged in
219  * Take care with what you do as a result of this test.
220  * The user may not have been logged in when this function was called BUT
221  * because of threading the user might be logged in before you test the result.
222  */
223 int CtdlIsUserLoggedIn (char *user_name)
224 {
225         CitContext *cptr;
226         int ret = 0;
227
228         begin_critical_section (S_SESSION_TABLE);
229         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
230                 if (!strcasecmp(cptr->user.fullname, user_name)) {
231                         ret = 1;
232                         break;
233                 }
234         }
235         end_critical_section(S_SESSION_TABLE);
236         return ret;
237 }
238
239
240
241 /*
242  * Check to see if a user is currently logged in.
243  * Basically same as CtdlIsUserLoggedIn() but uses the user number instead.
244  * Take care with what you do as a result of this test.
245  * The user may not have been logged in when this function was called BUT
246  * because of threading the user might be logged in before you test the result.
247  */
248 int CtdlIsUserLoggedInByNum (long usernum)
249 {
250         CitContext *cptr;
251         int ret = 0;
252
253         begin_critical_section(S_SESSION_TABLE);
254         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
255                 if (cptr->user.usernum == usernum) {
256                         ret = 1;
257                 }
258         }
259         end_critical_section(S_SESSION_TABLE);
260         return ret;
261 }
262
263
264
265 /*
266  * Return a pointer to the CitContext structure bound to the thread which
267  * called this function.  If there's no such binding (for example, if it's
268  * called by the housekeeper thread) then a generic 'master' CC is returned.
269  *
270  * This function is used *VERY* frequently and must be kept small.
271  */
272 CitContext *MyContext(void) {
273         register CitContext *c;
274         return ((c = (CitContext *) pthread_getspecific(MyConKey), c == NULL) ? &masterCC : c);
275 }
276
277
278
279
280 /*
281  * Terminate idle sessions.  This function pounds through the session table
282  * comparing the current time to each session's time-of-last-command.  If an
283  * idle session is found it is terminated, then the search restarts at the
284  * beginning because the pointer to our place in the list becomes invalid.
285  */
286 void terminate_idle_sessions(void)
287 {
288         CitContext *ccptr;
289         time_t now;
290         int killed = 0;
291         int longrunners = 0;
292
293         now = time(NULL);
294         begin_critical_section(S_SESSION_TABLE);
295         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
296                 if (
297                         (ccptr != CC)
298                         && (config.c_sleeping > 0)
299                         && (now - (ccptr->lastcmd) > config.c_sleeping)
300                 ) {
301                         if (!ccptr->dont_term) {
302                                 ccptr->kill_me = KILLME_IDLE;
303                                 ++killed;
304                         }
305                         else {
306                                 ++longrunners;
307                         }
308                 }
309         }
310         end_critical_section(S_SESSION_TABLE);
311         if (killed > 0)
312                 CON_syslog(LOG_INFO, "Scheduled %d idle sessions for termination\n", killed);
313         if (longrunners > 0)
314                 CON_syslog(LOG_INFO, "Didn't terminate %d protected idle sessions", longrunners);
315 }
316
317
318 /*
319  * During shutdown, close the sockets of any sessions still connected.
320  */
321 void terminate_all_sessions(void)
322 {
323         CitContext *ccptr;
324         int killed = 0;
325
326         begin_critical_section(S_SESSION_TABLE);
327         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
328                 if (ccptr->client_socket != -1)
329                 {
330                         CON_syslog(LOG_INFO, "terminate_all_sessions() is murdering %s", ccptr->curr_user);
331                         close(ccptr->client_socket);
332                         ccptr->client_socket = -1;
333                         killed++;
334                 }
335         }
336         end_critical_section(S_SESSION_TABLE);
337         if (killed > 0) {
338                 CON_syslog(LOG_INFO, "Flushed %d stuck sessions\n", killed);
339         }
340 }
341
342
343
344 /*
345  * Terminate a session.
346  */
347 void RemoveContext (CitContext *con)
348 {
349         const char *c;
350         if (con == NULL) {
351                 CONM_syslog(LOG_ERR, "WARNING: RemoveContext() called with NULL!");
352                 return;
353         }
354         c = con->ServiceName;
355         if (c == NULL) {
356                 c = "WTF?";
357         }
358         CON_syslog(LOG_DEBUG, "RemoveContext(%s) session %d", c, con->cs_pid);
359 ///     cit_backtrace();
360
361         /* Run any cleanup routines registered by loadable modules.
362          * Note: We have to "become_session()" because the cleanup functions
363          *       might make references to "CC" assuming it's the right one.
364          */
365         become_session(con);
366         CtdlUserLogout();
367         PerformSessionHooks(EVT_STOP);
368         client_close();                         /* If the client is still connected, blow 'em away. */
369         become_session(NULL);
370
371         CON_syslog(LOG_NOTICE, "[%3d]SRV[%s] Session ended.", con->cs_pid, c);
372
373         /* 
374          * If the client is still connected, blow 'em away. 
375          * if the socket is 0 or -1, its already gone or was never there.
376          */
377         if (con->client_socket > 0)
378         {
379                 CON_syslog(LOG_NOTICE, "Closing socket %d", con->client_socket);
380                 close(con->client_socket);
381         }
382
383         /* If using AUTHMODE_LDAP, free the DN */
384         if (con->ldap_dn) {
385                 free(con->ldap_dn);
386                 con->ldap_dn = NULL;
387         }
388         FreeStrBuf(&con->StatusMessage);
389         FreeStrBuf(&con->MigrateBuf);
390         FreeStrBuf(&con->RecvBuf.Buf);
391         if (con->cached_msglist) {
392                 free(con->cached_msglist);
393         }
394
395         CONM_syslog(LOG_DEBUG, "Done with RemoveContext()");
396 }
397
398
399
400 /*
401  * Initialize a new context and place it in the list.  The session number
402  * used to be the PID (which is why it's called cs_pid), but that was when we
403  * had one process per session.  Now we just assign them sequentially, starting
404  * at 1 (don't change it to 0 because masterCC uses 0).
405  */
406 CitContext *CreateNewContext(void) {
407         CitContext *me;
408
409         me = (CitContext *) malloc(sizeof(CitContext));
410         if (me == NULL) {
411                 CONM_syslog(LOG_ALERT, "citserver: can't allocate memory!!\n");
412                 return NULL;
413         }
414         memset(me, 0, sizeof(CitContext));
415
416         /* Give the context a name. Hopefully makes it easier to track */
417         strcpy (me->user.fullname, "SYS_notauth");
418         
419         /* The new context will be created already in the CON_EXECUTING state
420          * in order to prevent another thread from grabbing it while it's
421          * being set up.
422          */
423         me->state = CON_EXECUTING;
424         /*
425          * Generate a unique session number and insert this context into
426          * the list.
427          */
428         me->MigrateBuf = NewStrBuf();
429
430         me->RecvBuf.Buf = NewStrBuf();
431
432         me->lastcmd = time(NULL);       /* set lastcmd to now to prevent idle timer infanticide TODO: if we have a valid IO, use that to set the timer. */
433
434
435         begin_critical_section(S_SESSION_TABLE);
436         me->cs_pid = ++next_pid;
437         me->prev = NULL;
438         me->next = ContextList;
439         ContextList = me;
440         if (me->next != NULL) {
441                 me->next->prev = me;
442         }
443         ++num_sessions;
444         end_critical_section(S_SESSION_TABLE);
445         return (me);
446 }
447
448
449 /*
450  * Initialize a new context and place it in the list.  The session number
451  * used to be the PID (which is why it's called cs_pid), but that was when we
452  * had one process per session.  Now we just assign them sequentially, starting
453  * at 1 (don't change it to 0 because masterCC uses 0).
454  */
455 CitContext *CloneContext(CitContext *CloneMe) {
456         CitContext *me;
457
458         me = (CitContext *) malloc(sizeof(CitContext));
459         if (me == NULL) {
460                 CONM_syslog(LOG_ALERT, "citserver: can't allocate memory!!\n");
461                 return NULL;
462         }
463         memcpy(me, CloneMe, sizeof(CitContext));
464
465         memset(&me->RecvBuf, 0, sizeof(IOBuffer));
466         memset(&me->SendBuf, 0, sizeof(IOBuffer));
467         memset(&me->SBuf, 0, sizeof(IOBuffer));
468         me->MigrateBuf = NULL;
469         me->sMigrateBuf = NULL;
470         me->redirect_buffer = NULL;
471 #ifdef HAVE_OPENSSL
472         me->ssl = NULL;
473 #endif
474
475         me->download_fp = NULL;
476         me->upload_fp = NULL;
477         /// TODO: what about the room/user?
478         me->ma = NULL;
479         me->openid_data = NULL;
480         me->ldap_dn = NULL;
481         me->session_specific_data = NULL;
482         
483         me->CIT_ICAL = NULL;
484
485         me->cached_msglist = NULL;
486         me->download_fp = NULL;
487         me->upload_fp = NULL;
488         me->client_socket = 0;
489
490         me->MigrateBuf = NewStrBuf();
491         me->RecvBuf.Buf = NewStrBuf();
492         
493         begin_critical_section(S_SESSION_TABLE);
494         {
495                 me->cs_pid = ++next_pid;
496                 me->prev = NULL;
497                 me->next = ContextList;
498                 me->lastcmd = time(NULL);       /* set lastcmd to now to prevent idle timer infanticide */
499                 ContextList = me;
500                 if (me->next != NULL) {
501                         me->next->prev = me;
502                 }
503                 ++num_sessions;
504         }
505         end_critical_section(S_SESSION_TABLE);
506         return (me);
507 }
508
509
510 /*
511  * Return an array containing a copy of the context list.
512  * This allows worker threads to perform "for each context" operations without
513  * having to lock and traverse the live list.
514  */
515 CitContext *CtdlGetContextArray(int *count)
516 {
517         int nContexts, i;
518         CitContext *nptr, *cptr;
519         
520         nContexts = num_sessions;
521         nptr = malloc(sizeof(CitContext) * nContexts);
522         if (!nptr) {
523                 *count = 0;
524                 return NULL;
525         }
526         begin_critical_section(S_SESSION_TABLE);
527         for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++) {
528                 memcpy(&nptr[i], cptr, sizeof (CitContext));
529         }
530         end_critical_section (S_SESSION_TABLE);
531         
532         *count = i;
533         return nptr;
534 }
535
536
537
538 /*
539  * Back-end function for starting a session
540  */
541 void begin_session(CitContext *con)
542 {
543         /* 
544          * Initialize some variables specific to our context.
545          */
546         con->logged_in = 0;
547         con->internal_pgm = 0;
548         con->download_fp = NULL;
549         con->upload_fp = NULL;
550         con->cached_msglist = NULL;
551         con->cached_num_msgs = 0;
552         con->FirstExpressMessage = NULL;
553         time(&con->lastcmd);
554         time(&con->lastidle);
555         strcpy(con->lastcmdname, "    ");
556         strcpy(con->cs_clientname, "(unknown)");
557         strcpy(con->curr_user, NLI);
558         *con->net_node = '\0';
559         *con->fake_username = '\0';
560         *con->fake_hostname = '\0';
561         *con->fake_roomname = '\0';
562         *con->cs_clientinfo = '\0';
563         safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
564         safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
565         con->cs_UDSclientUID = -1;
566         con->cs_host[sizeof con->cs_host - 1] = 0;
567         if (!CC->is_local_socket) {
568                 locate_host(con->cs_host, sizeof con->cs_host,
569                         con->cs_addr, sizeof con->cs_addr,
570                         con->client_socket
571                 );
572         }
573         else {
574                 con->cs_host[0] = 0;
575                 con->cs_addr[0] = 0;
576 #ifdef HAVE_STRUCT_UCRED
577                 {
578                         /* as http://www.wsinnovations.com/softeng/articles/uds.html told us... */
579                         struct ucred credentials;
580                         socklen_t ucred_length = sizeof(struct ucred);
581                         
582                         /*fill in the user data structure */
583                         if(getsockopt(con->client_socket, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
584                                 syslog(LOG_NOTICE, "could obtain credentials from unix domain socket");
585                                 
586                         }
587                         else {          
588                                 /* the process ID of the process on the other side of the socket */
589                                 /* credentials.pid; */
590                                 
591                                 /* the effective UID of the process on the other side of the socket  */
592                                 con->cs_UDSclientUID = credentials.uid;
593                                 
594                                 /* the effective primary GID of the process on the other side of the socket */
595                                 /* credentials.gid; */
596                                 
597                                 /* To get supplemental groups, we will have to look them up in our account
598                                    database, after a reverse lookup on the UID to get the account name.
599                                    We can take this opportunity to check to see if this is a legit account.
600                                 */
601                                 snprintf(con->cs_clientinfo, sizeof(con->cs_clientinfo),
602                                          "PID: "F_PID_T"; UID: "F_UID_T"; GID: "F_XPID_T" ", 
603                                          credentials.pid,
604                                          credentials.uid,
605                                          credentials.gid);
606                         }
607                 }
608 #endif
609         }
610         con->cs_flags = 0;
611         con->upload_type = UPL_FILE;
612         con->dl_is_net = 0;
613
614         con->nologin = 0;
615         if (((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions)) || CtdlWantSingleUser()) {
616                 con->nologin = 1;
617         }
618
619         if (!CC->is_local_socket) {
620                 syslog(LOG_NOTICE, "Session (%s) started from %s (%s).\n", con->ServiceName, con->cs_host, con->cs_addr);
621         }
622         else {
623                 syslog(LOG_NOTICE, "Session (%s) started via local socket UID:%d.\n", con->ServiceName, con->cs_UDSclientUID);
624         }
625
626         /* Run any session startup routines registered by loadable modules */
627         PerformSessionHooks(EVT_START);
628 }
629
630
631 /*
632  * This function fills in a context and its user field correctly
633  * Then creates/loads that user
634  */
635 void CtdlFillSystemContext(CitContext *context, char *name)
636 {
637         char sysname[SIZ];
638         long len;
639
640         memset(context, 0, sizeof(CitContext));
641         context->internal_pgm = 1;
642         context->cs_pid = 0;
643         strcpy (sysname, "SYS_");
644         strcat (sysname, name);
645         len = cutuserkey(sysname);
646         memcpy(context->curr_user, sysname, len + 1);
647         context->client_socket = (-1);
648         context->state = CON_SYS;
649         context->ServiceName = name;
650
651         /* internal_create_user has the side effect of loading the user regardless of wether they
652          * already existed or needed to be created
653          */
654         internal_create_user (sysname, len, &(context->user), -1) ;
655         
656         /* Check to see if the system user needs upgrading */
657         if (context->user.usernum == 0)
658         {       /* old system user with number 0, upgrade it */
659                 context->user.usernum = get_new_user_number();
660                 CON_syslog(LOG_INFO, "Upgrading system user \"%s\" from user number 0 to user number %ld\n", context->user.fullname, context->user.usernum);
661                 /* add user to the database */
662                 CtdlPutUser(&(context->user));
663                 cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
664         }
665 }
666
667
668 /*
669  * Cleanup any contexts that are left lying around
670  */
671 void context_cleanup(void)
672 {
673         CitContext *ptr = NULL;
674         CitContext *rem = NULL;
675
676         /*
677          * Clean up the contexts.
678          * There are no threads so no critical_section stuff is needed.
679          */
680         ptr = ContextList;
681         
682         /* We need to update the ContextList because some modules may want to itterate it
683          * Question is should we NULL it before iterating here or should we just keep updating it
684          * as we remove items?
685          *
686          * Answer is to NULL it first to prevent modules from doing any actions on the list at all
687          */
688         ContextList=NULL;
689         while (ptr != NULL){
690                 /* Remove the session from the active list */
691                 rem = ptr->next;
692                 --num_sessions;
693
694                 CON_syslog(LOG_DEBUG, "context_cleanup(): purging session %d\n", ptr->cs_pid);
695                 RemoveContext(ptr);
696                 free (ptr);
697                 ptr = rem;
698         }
699 }
700
701
702
703 /*
704  * Purge all sessions which have the 'kill_me' flag set.
705  * This function has code to prevent it from running more than once every
706  * few seconds, because running it after every single unbind would waste a lot
707  * of CPU time and keep the context list locked too much.  To force it to run
708  * anyway, set "force" to nonzero.
709  */
710 void dead_session_purge(int force) {
711         CitContext *ptr, *ptr2;         /* general-purpose utility pointer */
712         CitContext *rem = NULL;         /* list of sessions to be destroyed */
713         
714         if (force == 0) {
715                 if ( (time(NULL) - last_purge) < 5 ) {
716                         return; /* Too soon, go away */
717                 }
718         }
719         time(&last_purge);
720
721         if (try_critical_section(S_SESSION_TABLE))
722                 return;
723                 
724         ptr = ContextList;
725         while (ptr) {
726                 ptr2 = ptr;
727                 ptr = ptr->next;
728                 
729                 if ( (ptr2->state == CON_IDLE) && (ptr2->kill_me) ) {
730                         /* Remove the session from the active list */
731                         if (ptr2->prev) {
732                                 ptr2->prev->next = ptr2->next;
733                         }
734                         else {
735                                 ContextList = ptr2->next;
736                         }
737                         if (ptr2->next) {
738                                 ptr2->next->prev = ptr2->prev;
739                         }
740
741                         --num_sessions;
742                         /* And put it on our to-be-destroyed list */
743                         ptr2->next = rem;
744                         rem = ptr2;
745                 }
746         }
747         end_critical_section(S_SESSION_TABLE);
748
749         /* Now that we no longer have the session list locked, we can take
750          * our time and destroy any sessions on the to-be-killed list, which
751          * is allocated privately on this thread's stack.
752          */
753         while (rem != NULL) {
754                 CON_syslog(LOG_DEBUG, "dead_session_purge(): purging session %d, reason=%d\n", rem->cs_pid, rem->kill_me);
755                 RemoveContext(rem);
756                 ptr = rem;
757                 rem = rem->next;
758                 free(ptr);
759         }
760 }
761
762
763
764
765
766 /*
767  * masterCC is the context we use when not attached to a session.  This
768  * function initializes it.
769  */
770 void InitializeMasterCC(void) {
771         memset(&masterCC, 0, sizeof(struct CitContext));
772         masterCC.internal_pgm = 1;
773         masterCC.cs_pid = 0;
774 }
775
776
777
778
779
780 /*
781  * Set the "async waiting" flag for a session, if applicable
782  */
783 void set_async_waiting(struct CitContext *ccptr)
784 {
785         CON_syslog(LOG_DEBUG, "Setting async_waiting flag for session %d\n", ccptr->cs_pid);
786         if (ccptr->is_async) {
787                 ccptr->async_waiting++;
788                 if (ccptr->state == CON_IDLE) {
789                         ccptr->state = CON_READY;
790                 }
791         }
792 }
793
794
795 void DebugSessionEnable(const int n)
796 {
797         DebugSession = n;
798 }
799 CTDL_MODULE_INIT(session)
800 {
801         if (!threading) {
802                 CtdlRegisterDebugFlagHook(HKEY("session"), DebugSessionEnable, &DebugSession);
803         }
804         return "session";
805 }