#include "ctdl_module.h"
#include "threads.h"
+#include "user_ops.h"
+#include "control.h"
+
#ifdef DEBUG_MEMORY_LEAKS
struct igheap {
/*
- * Create an interface to lprintf that follows the coding convention.
- * This is here until such time as we have replaced all calls to lprintf with CtdlLogPrintf
- */
-
-void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...)
-{
- va_list arg_ptr;
- va_start(arg_ptr, format);
- vlprintf(loglevel, format, arg_ptr);
- va_end(arg_ptr);
-}
-
-
-/*
- * lprintf() ... Write logging information
+ * CtdlLogPrintf() ... Write logging information
*/
-void lprintf(enum LogLevel loglevel, const char *format, ...) {
+void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {
va_list arg_ptr;
va_start(arg_ptr, format);
- vlprintf(loglevel, format, arg_ptr);
+ vCtdlLogPrintf(loglevel, format, arg_ptr);
va_end(arg_ptr);
}
-void vlprintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
+void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
{
char buf[SIZ], buf2[SIZ];
volatile int running_as_daemon = 0;
static RETSIGTYPE signal_cleanup(int signum) {
- CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
#ifdef THREADS_USESIGNALS
if (CT)
- {
- CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, signum);
CT->signal = signum;
- }
else
#endif
+ {
+ CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
exit_signal = signum;
+ }
}
CtdlLogPrintf(CTDL_ALERT, "citserver: can't allocate memory!!\n");
return NULL;
}
- memset(me, 0, sizeof(struct CitContext));
+ 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.
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);
+ }
+}
+
/*
* The following functions implement output buffering. If the kernel supplies
* native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
/*
* client_write() ... Send binary data to the client.
*/
-void client_write(char *buf, int nbytes)
+int client_write(char *buf, int nbytes)
{
int bytes_written = 0;
int retval;
memcpy(&Ctx->redirect_buffer[Ctx->redirect_len], buf, nbytes);
Ctx->redirect_len += nbytes;
Ctx->redirect_buffer[Ctx->redirect_len] = 0;
- return;
+ return 0;
}
#ifndef HAVE_TCP_BUFFERING
Ctx->buffer_len += nbytes;
Ctx->output_buffer = realloc(Ctx->output_buffer, Ctx->buffer_len);
memcpy(&Ctx->output_buffer[old_buffer_len], buf, nbytes);
- return;
+ return 0;
}
#endif
#ifdef HAVE_OPENSSL
if (Ctx->redirect_ssl) {
client_write_ssl(buf, nbytes);
- return;
+ return 0;
}
#endif
strerror(errno), errno);
cit_backtrace();
Ctx->kill_me = 1;
- return;
+ return -1;
}
}
cit_backtrace();
// CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s", &buf[bytes_written]);
Ctx->kill_me = 1;
- return;
+ return -1;
}
bytes_written = bytes_written + retval;
}
+ return 0;
}
retval = select( (fd)+1,
&rfds, NULL, NULL, &tv);
+ if (retval < 0)
+ {
+ if (errno == EINTR)
+ {
+ CtdlLogPrintf(CTDL_DEBUG, "Interrupted select().\n");
+ CC->kill_me = 1;
+ return (-1);
+ }
+ }
if (FD_ISSET(fd, &rfds) == 0) {
return(0);
rem = ptr->next;
--num_sessions;
- lprintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
+ CtdlLogPrintf(CTDL_DEBUG, "Purging session %d\n", ptr->cs_pid);
RemoveContext(ptr);
free (ptr);
ptr = rem;
-
-
/*
* Bind a thread to a context. (It's inline merely to speed things up.)
*/
if (errno != EINTR) {
CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
CtdlThreadStopAll();
- } else if (!CtdlThreadCheckStop()) {
- CtdlLogPrintf(CTDL_DEBUG, "Interrupted select.\n");
+ } else {
+ CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
+ if (CtdlThreadCheckStop()) return(NULL);
goto do_select;
}
}