-/*
- * Return a pointer to the CitContext structure bound to the thread which
- * called this function. If there's no such binding (for example, if it's
- * called by the housekeeper thread) then a generic 'master' CC is returned.
- *
- * This function is used *VERY* frequently and must be kept small.
- */
-struct CitContext *MyContext(void) {
-
- register struct CitContext *c;
-
- return ((c = (struct CitContext *) citthread_getspecific(MyConKey),
- c == NULL) ? &masterCC : c
- );
-}
-
-
-/*
- * Initialize a new context and place it in the list. The session number
- * used to be the PID (which is why it's called cs_pid), but that was when we
- * had one process per session. Now we just assign them sequentially, starting
- * at 1 (don't change it to 0 because masterCC uses 0).
- */
-struct CitContext *CreateNewContext(void) {
- struct CitContext *me;
- static int next_pid = 0;
-
- me = (struct CitContext *) malloc(sizeof(struct CitContext));
- if (me == NULL) {
- CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
- return NULL;
- }
-
- memset(me, 0, sizeof(struct CitContext));
-
- /* Give the contaxt a name. Hopefully makes it easier to track */
- strcpy (me->user.fullname, "SYS_notauth");
-
- /* The new context will be created already in the CON_EXECUTING state
- * in order to prevent another thread from grabbing it while it's
- * being set up.
- */
- me->state = CON_EXECUTING;
- /*
- * Generate a unique session number and insert this context into
- * the list.
- */
- begin_critical_section(S_SESSION_TABLE);
- me->cs_pid = ++next_pid;
- me->prev = NULL;
- me->next = ContextList;
- ContextList = me;
- if (me->next != NULL) {
- me->next->prev = me;
- }
- ++num_sessions;
- end_critical_section(S_SESSION_TABLE);
- return (me);
-}
-
-
-struct CitContext *CtdlGetContextArray(int *count)
-{
- int nContexts, i;
- struct CitContext *nptr, *cptr;
-
- nContexts = num_sessions;
- nptr = malloc(sizeof(struct CitContext) * nContexts);
- if (!nptr)
- return NULL;
- begin_critical_section(S_SESSION_TABLE);
- for (cptr = ContextList, i=0; cptr != NULL && i < nContexts; cptr = cptr->next, i++)
- memcpy(&nptr[i], cptr, sizeof (struct CitContext));
- end_critical_section (S_SESSION_TABLE);
-
- *count = i;
- return nptr;
-}
-
-
-
-/**
- * This function fills in a context and its user field correctly
- * Then creates/loads that user
- */
-void CtdlFillSystemContext(struct CitContext *context, char *name)
-{
- char sysname[USERNAME_SIZE];
-
- memset(context, 0, sizeof(struct CitContext));
- context->internal_pgm = 1;
- context->cs_pid = 0;
- strcpy (sysname, "SYS_");
- strcat (sysname, name);
- /* internal_create_user has the side effect of loading the user regardless of wether they
- * already existed or needed to be created
- */
- internal_create_user (sysname, &(context->user), -1) ;
-
- /* Check to see if the system user needs upgrading */
- if (context->user.usernum == 0)
- { /* old system user with number 0, upgrade it */
- context->user.usernum = get_new_user_number();
- CtdlLogPrintf(CTDL_DEBUG, "Upgrading system user \"%s\" from user number 0 to user number %d\n", context->user.fullname, context->user.usernum);
- /* add user to the database */
- putuser(&(context->user));
- cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
- }
-}
-