/*
- * $Id$
- *
* Citadel "system dependent" stuff.
* See COPYING for copyright information.
*
int syslog_facility = LOG_DAEMON;
int enable_syslog = 0;
-
+int print_to_logfile = 1;
/*
* CtdlLogPrintf() ... Write logging information
}
/* stderr output code */
- if (enable_syslog || running_as_daemon) return;
+ if (enable_syslog || !print_to_logfile) return;
/* if we run in forground and syslog is disabled, log to terminal */
if (loglevel <= verbosity) {
struct timeval tv;
struct tm tim;
time_t unixtime;
- StrBuf *lBuf;
- CitContext *CCC = CC;
-
+ CitContext *CCC = MyContext();
+ ThreadTSD *cTSD = CTP;
+ CtdlThreadNode *node = NULL;
+ long lwpid = 0;
+
+ if (cTSD != NULL) {
+ node = cTSD->self;
+ }
+
+ if ((node != NULL) && (node->reltid != 0)) {
+ lwpid = node->reltid;
+ }
+
gettimeofday(&tv, NULL);
+
/* Promote to time_t; types differ on some OSes (like darwin) */
unixtime = tv.tv_sec;
localtime_r(&unixtime, &tim);
- if (CCC != NULL)
- lBuf = CCC->lBuf;
- else
- lBuf = NewStrBuf();
- if (lBuf == NULL) {
- char buf[SIZ], buf2[SIZ];
-
- if ((CCC != NULL) && (CCC->cs_pid != 0)) {
- sprintf(buf,
- "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx [%3d] ",
- tim.tm_year + 1900, tim.tm_mon + 1,
- tim.tm_mday, tim.tm_hour, tim.tm_min,
- tim.tm_sec, (long)tv.tv_usec,
- CCC->cs_pid);
- } else {
- sprintf(buf,
- "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx ",
- tim.tm_year + 1900, tim.tm_mon + 1,
- tim.tm_mday, tim.tm_hour, tim.tm_min,
- tim.tm_sec, (long)tv.tv_usec);
- }
- vsnprintf(buf2, SIZ, format, arg_ptr);
+ fprintf(stderr,
+ "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
+ tim.tm_year + 1900, tim.tm_mon + 1,
+ tim.tm_mday, tim.tm_hour, tim.tm_min,
+ tim.tm_sec, (long)tv.tv_usec
+ );
- fprintf(stderr, "%s%s", buf, buf2);
+ if (lwpid != 0) {
+ fprintf(stderr, "[LWP:%ld] ", lwpid);
}
- else {
- StrBufPrintf(lBuf,
- "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
- tim.tm_year + 1900, tim.tm_mon + 1,
- tim.tm_mday, tim.tm_hour, tim.tm_min,
- tim.tm_sec, (long)tv.tv_usec);
-
- if (CCC != NULL) {
- if (CCC->cs_pid != 0)
- StrBufAppendPrintf(lBuf,
- "[%3d] ",
- CCC->cs_pid);
- else if (CCC->user.usernum != 0)
- StrBufAppendPrintf(lBuf,
- "[:%d] ",
- CCC->user.usernum);
+
+ if (CCC != NULL) {
+ if (CCC->cs_pid != 0) {
+ fprintf(stderr, "[%3d] ", CCC->cs_pid);
+ }
+ else if (CCC->user.usernum != 0) {
+ fprintf(stderr, "[:%ld] ", CCC->user.usernum);
}
- StrBufVAppendPrintf(lBuf, format, arg_ptr);
- fwrite(ChrPtr(lBuf), 1, StrLength(lBuf), stderr);
}
- fflush(stderr);
- if (CCC == NULL) FreeStrBuf(&lBuf);
+ vfprintf(stderr, format, arg_ptr);
+ fflush(stderr);
}
}
}
-
-
-/*
+/*
* This is a generic function to set up a master socket for listening on
- * a TCP port. The server shuts down if the bind fails.
+ * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
*
+ * ip_addr IP address to bind
+ * port_number port number to bind
+ * queue_len number of incoming connections to allow in the queue
*/
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
+int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len, char *errormessage)
{
- struct sockaddr_in sin;
- int s, i;
- int actual_queue_len;
-
- actual_queue_len = queue_len;
- if (actual_queue_len < 5) actual_queue_len = 5;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons((u_short)port_number);
- if (ip_addr == NULL) {
- sin.sin_addr.s_addr = INADDR_ANY;
+ struct protoent *p;
+ struct sockaddr_in6 sin6;
+ struct sockaddr_in sin4;
+ int s, i, b;
+ int ip_version = 6;
+
+ memset(&sin6, 0, sizeof(sin6));
+ memset(&sin4, 0, sizeof(sin4));
+ sin6.sin6_family = AF_INET6;
+ sin4.sin_family = AF_INET;
+
+ if ( (ip_addr == NULL) /* any IPv6 */
+ || (IsEmptyStr(ip_addr))
+ || (!strcmp(ip_addr, "*"))
+ ) {
+ ip_version = 6;
+ sin6.sin6_addr = in6addr_any;
}
- else {
- sin.sin_addr.s_addr = inet_addr(ip_addr);
+ else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
+ {
+ ip_version = 4;
+ sin4.sin_addr.s_addr = INADDR_ANY;
}
-
- if (sin.sin_addr.s_addr == !INADDR_ANY) {
- sin.sin_addr.s_addr = INADDR_ANY;
+ else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
+ {
+ ip_version = 4;
+ if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
+ snprintf(errormessage, SIZ,
+ "Error binding to [%s] : %s", ip_addr, strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
+ }
+ }
+ else /* specific IPv6 */
+ {
+ ip_version = 6;
+ if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
+ snprintf(errormessage, SIZ,
+ "Error binding to [%s] : %s", ip_addr, strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
+ }
+ }
+
+ if (port_number == 0) {
+ snprintf(errormessage, SIZ,
+ "Can't start: no port number specified."
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
}
+ sin6.sin6_port = htons((u_short) port_number);
+ sin4.sin_port = htons((u_short) port_number);
- s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ p = getprotobyname("tcp");
+ s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
if (s < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
- "citserver: Can't create a socket: %s",
- strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
- return(-1);
+ snprintf(errormessage, SIZ,
+ "Can't create a listening socket: %s", strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
}
-
+ /* Set some socket options that make sense. */
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
- if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
- "citserver: Can't bind: %s",
- strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
- close(s);
- return(-1);
+ if (ip_version == 6) {
+ b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
}
-
- /* set to nonblock - we need this for some obscure situations */
- if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
- "citserver: Can't set socket to non-blocking: %s",
- strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
- close(s);
- return(-1);
+ else {
+ b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
}
- if (listen(s, actual_queue_len) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
- "citserver: Can't listen: %s",
- strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
- close(s);
- return(-1);
+ if (b < 0) {
+ snprintf(errormessage, SIZ,
+ "Can't bind: %s", strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
}
- return(s);
+ fcntl(s, F_SETFL, O_NONBLOCK);
+
+ if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
+ snprintf(errormessage, SIZ,
+ "Can't listen: %s", strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+ return (-1);
+ }
+ return (s);
}
+
+
/*
* Create a Unix domain socket and listen on it
*/
-int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
+int ctdl_uds_server(char *sockpath, int queue_len, char *errormessage)
{
struct sockaddr_un addr;
int s;
i = unlink(sockpath);
if ((i != 0) && (errno != ENOENT)) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
- sockpath, strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+ snprintf(errormessage, SIZ, "citserver: can't unlink %s: %s",
+ sockpath, strerror(errno)
+ );
+ CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
return(-1);
}
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
+ snprintf(errormessage, SIZ,
"citserver: Can't create a socket: %s",
strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+ CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
return(-1);
}
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
+ snprintf(errormessage, SIZ,
"citserver: Can't bind: %s",
strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+ CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
return(-1);
}
/* set to nonblock - we need this for some obscure situations */
if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
+ snprintf(errormessage, SIZ,
"citserver: Can't set socket to non-blocking: %s",
strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+ CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
close(s);
return(-1);
}
if (listen(s, actual_queue_len) < 0) {
- *errormessage = (char*) malloc(SIZ + 1);
- snprintf(*errormessage, SIZ,
+ snprintf(errormessage, SIZ,
"citserver: Can't listen: %s",
strerror(errno));
- CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+ CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
return(-1);
}
if (nbytes < 1) return(0);
-// flush_client_inbuf();
Ctx = CC;
+
+#ifdef BIGBAD_IODBG
+ {
+ int rv = 0;
+ char fn [SIZ];
+ FILE *fd;
+
+ snprintf(fn, SIZ, "/tmp/foolog_%s.%d", Ctx->ServiceName, Ctx->cs_pid);
+
+ fd = fopen(fn, "a+");
+ fprintf(fd, "Sending: BufSize: %d BufContent: [",
+ nbytes);
+ rv = fwrite(buf, nbytes, 1, fd);
+ fprintf(fd, "]\n");
+
+
+ fclose(fd);
+ }
+#endif
+// flush_client_inbuf();
if (Ctx->redirect_buffer != NULL) {
StrBufAppendBufPlain(Ctx->redirect_buffer,
buf, nbytes, 0);
#ifdef HAVE_OPENSSL
if (CCC->redirect_ssl) {
+#ifdef BIGBAD_IODBG
+ int rv = 0;
+ char fn [SIZ];
+ FILE *fd;
+
+ snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+
+ fd = fopen(fn, "a+");
+ fprintf(fd, "Reading BLOB: BufSize: %d ",
+ bytes);
+ rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+ fprintf(fd, "]\n");
+
+
+ fclose(fd);
+#endif
retval = client_read_sslblob(Target, bytes, timeout);
if (retval < 0) {
CtdlLogPrintf(CTDL_CRIT,
"%s failed\n",
__FUNCTION__);
}
+#ifdef BIGBAD_IODBG
+ snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+
+ fd = fopen(fn, "a+");
+ fprintf(fd, "Read: %d BufContent: [",
+ StrLength(Target));
+ rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+ fprintf(fd, "]\n");
+
+
+ fclose(fd);
+#endif
}
else
#endif
{
+#ifdef BIGBAD_IODBG
+ int rv = 0;
+ char fn [SIZ];
+ FILE *fd;
+
+ snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+
+ fd = fopen(fn, "a+");
+ fprintf(fd, "Reading BLOB: BufSize: %d ",
+ bytes);
+ rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+ fprintf(fd, "]\n");
+
+
+ fclose(fd);
+#endif
retval = StrBufReadBLOBBuffered(Target,
CCC->ReadBuf,
&CCC->Pos,
Error);
return retval;
}
- else
- {
#ifdef BIGBAD_IODBG
- int rv = 0;
- char fn [SIZ];
- FILE *fd;
-
- snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
-
- fd = fopen(fn, "a+");
- fprintf(fd, "Read: BufSize: %d BufContent: [",
- StrLength(Target));
- rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
- fprintf(fd, "]\n");
-
-
- fclose(fd);
+ snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+
+ fd = fopen(fn, "a+");
+ fprintf(fd, "Read: %d BufContent: [",
+ StrLength(Target));
+ rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+ fprintf(fd, "]\n");
+
+
+ fclose(fd);
#endif
-
- }
}
return retval;
}
int m;
int i;
int retval;
+ struct CitContext select_on_master_CC;
+
+ CtdlFillSystemContext(&select_on_master_CC, "select_on_master");
+ citthread_setspecific(MyConKey, (void *)&select_on_master_CC);
while (!CtdlThreadCheckStop()) {
/* Initialize the fdset. */
}
}
}
+ CtdlClearSystemContext();
+
return NULL;
}