/*
+ * $Id$
+ *
* Citadel/UX "system dependent" stuff.
* See copyright.txt for copyright information.
*
- * $Id$
- *
- * Here's where we (hopefully) have all the parts of the Citadel server that
+ * Here's where we (hopefully) have most parts of the Citadel server that
* would need to be altered to run the server in a non-POSIX environment.
*
* Eventually we'll try porting to a different platform and either have
* multiple variants of this file or simply load it up with #ifdefs.
+ *
*/
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <limits.h>
#include <netinet/in.h>
#include <netdb.h>
+#include <sys/un.h>
#include <string.h>
#include <pwd.h>
#include <errno.h>
/*
* 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.
+ *
*/
int ig_tcp_server(int port_number, int queue_len)
{
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
-
- if (port_number == 0) {
- lprintf(1, "citserver: illegal port number specified\n");
- return(-1);
- }
-
sin.sin_port = htons((u_short)port_number);
- s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
+ s = socket(PF_INET, SOCK_STREAM,
+ (getprotobyname("tcp")->p_proto));
+
if (s < 0) {
lprintf(1, "citserver: Can't create a socket: %s\n",
strerror(errno));
return(-1);
}
- /* Set the SO_REUSEADDR socket option, because it makes sense. */
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
- lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
+ lprintf(1, "citserver: Can't bind: %s\n",
+ strerror(errno));
return(-1);
}
+/*
+ * Create a Unix domain socket and listen on it
+ */
+int ig_uds_server(char *sockpath, int queue_len)
+{
+ struct sockaddr_un addr;
+ int s;
+
+ unlink(sockpath);
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s < 0) {
+ lprintf(1, "citserver: Can't create a socket: %s\n",
+ strerror(errno));
+ return(-1);
+ }
+
+ if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ lprintf(1, "citserver: Can't bind: %s\n",
+ strerror(errno));
+ return(-1);
+ }
+
+ if (listen(s, queue_len) < 0) {
+ lprintf(1, "citserver: Can't listen: %s\n", strerror(errno));
+ return(-1);
+ }
+
+ chmod(sockpath, 0777);
+ return(s);
+}
+
+
+
/*
* 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
* The system-dependent part of master_cleanup() - close the master socket.
*/
void sysdep_master_cleanup(void) {
- /* FIXME close all protocol master sockets here */
+ struct ServiceFunctionHook *serviceptr;
+
+ /*
+ * close all protocol master sockets
+ */
+ for (serviceptr = ServiceHookTable; serviceptr != NULL;
+ serviceptr = serviceptr->next ) {
+ lprintf(3, "Closing listener on port %d\n",
+ serviceptr->tcp_port);
+ close(serviceptr->msock);
+
+ /* If it's a Unix domain socket, remove the file. */
+ if (serviceptr->sockpath != NULL) {
+ unlink(serviceptr->sockpath);
+ }
+ }
}
/* Tell 'em who's in da house */
lprintf(1,
"\nMultithreaded message server for Citadel/UX\n"
-"Copyright (C) 1987-1999 by the Citadel/UX development team.\n"
+"Copyright (C) 1987-2000 by the Citadel/UX development team.\n"
"Citadel/UX is free software, covered by the GNU General Public License, and\n"
"you are welcome to change it and/or distribute copies of it under certain\n"
"conditions. There is absolutely no warranty for this software. Please\n"
/* Initialize... */
init_sysdep();
- openlog("citserver",LOG_PID,LOG_USER);
+ openlog("citserver", LOG_PID, LOG_USER);
/* Load site-specific parameters */
lprintf(7, "Loading citadel.config\n");
/*
* Bind the server to our favorite ports.
*/
- CtdlRegisterServiceHook(config.c_port_number,
+ CtdlRegisterServiceHook(0, /* Unix */
+ "citadel.socket",
+ citproto_begin_session,
+ do_command_loop);
+ CtdlRegisterServiceHook(config.c_port_number, /* TCP */
+ NULL,
citproto_begin_session,
do_command_loop);
for (serviceptr = ServiceHookTable; serviceptr != NULL;
serviceptr = serviceptr->next ) {
+ lprintf(9, "Will listen on master socket %d\n",
+ serviceptr->msock);
FD_SET(serviceptr->msock, &masterfds);
if (serviceptr->msock > masterhighest) {
masterhighest = serviceptr->msock;
}
/* Next, check to see if it's a new client connecting
- * on the master socket.
+ * on a master socket.
*/
else for (serviceptr = ServiceHookTable; serviceptr != NULL;
serviceptr = serviceptr->next ) {
con->client_socket = ssock;
con->h_command_function =
serviceptr->h_command_function;
+
+ /* Determine whether local socket */
+ if (serviceptr->sockpath != NULL)
+ con->is_local_socket = 1;
/* Set the SO_REUSEADDR socket option */
i = 1;
* thread that the &readfds needs to be refreshed with more
* current data.
*/
- if (!time_to_die) if (FD_ISSET(rescan[0], &readfds)) {
+ if (time_to_die)
+ break;
+
+ if (FD_ISSET(rescan[0], &readfds)) {
read(rescan[0], &junk, 1);
goto SETUP_FD;
}
}
write(rescan[1], &junk, 1);
}
- else {
- lprintf(9, "Thread found nothing to do!\n");
- }
}
dead_session_purge();