]> code.citadel.org Git - citadel.git/blob - citadel/server_main.c
- fix library flags, includes for portability
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  *
4  * $Id$
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <limits.h>
31 #include <netinet/in.h>
32 #include <netdb.h>
33 #include <sys/un.h>
34 #include <string.h>
35 #include <pwd.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <syslog.h>
39 #include <grp.h>
40 #ifdef HAVE_PTHREAD_H
41 #include <pthread.h>
42 #endif
43 #include "citadel.h"
44 #include "server.h"
45 #include "dynloader.h"
46 #include "sysdep_decls.h"
47 #include "citserver.h"
48 #include "support.h"
49 #include "config.h"
50 #include "database.h"
51 #include "housekeeping.h"
52 #include "tools.h"
53
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif
57
58 #ifndef HAVE_SNPRINTF
59 #include "snprintf.h"
60 #endif
61
62 /*
63  * Here's where it all begins.
64  */
65 int main(int argc, char **argv)
66 {
67         char tracefile[128];            /* Name of file to log traces to */
68         int a, i;                       /* General-purpose variables */
69         struct passwd *pw;
70         int drop_root_perms = 1;
71         char *moddir;
72         struct worker_node *wnp;
73         
74         /* specify default port name and trace file */
75         strcpy(tracefile, "");
76
77         /* initialize the master context */
78         InitializeMasterCC();
79
80         /* parse command-line arguments */
81         for (a=1; a<argc; ++a) {
82
83                 /* -t specifies where to log trace messages to */
84                 if (!strncmp(argv[a], "-t", 2)) {
85                         strcpy(tracefile, argv[a]);
86                         strcpy(tracefile, &tracefile[2]);
87                         freopen(tracefile, "r", stdin);
88                         freopen(tracefile, "w", stdout);
89                         freopen(tracefile, "w", stderr);
90                 }
91
92                 /* run in the background if -d was specified */
93                 else if (!strcmp(argv[a], "-d")) {
94                         start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
95                 }
96
97                 /* -x specifies the desired logging level */
98                 else if (!strncmp(argv[a], "-x", 2)) {
99                         verbosity = atoi(&argv[a][2]);
100                 }
101
102                 else if (!strncmp(argv[a], "-h", 2)) {
103                         safestrncpy(bbs_home_directory, &argv[a][2],
104                                     sizeof bbs_home_directory);
105                         home_specified = 1;
106                 }
107
108                 else if (!strncmp(argv[a], "-f", 2)) {
109                         do_defrag = 1;
110                 }
111
112                 /* -r tells the server not to drop root permissions. don't use
113                  * this unless you know what you're doing. this should be
114                  * removed in the next release if it proves unnecessary. */
115                 else if (!strcmp(argv[a], "-r"))
116                         drop_root_perms = 0;
117
118                 /* any other parameter makes it crash and burn */
119                 else {
120                         lprintf(1,      "citserver: usage: "
121                                         "citserver [-tTraceFile] [-d] [-f]"
122                                         " [-xLogLevel] [-hHomeDir]\n");
123                         exit(1);
124                 }
125
126         }
127
128         /* Tell 'em who's in da house */
129         lprintf(1,
130 "\nMultithreaded message server for Citadel/UX\n"
131 "Copyright (C) 1987-2001 by the Citadel/UX development team.\n"
132 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
133 "you are welcome to change it and/or distribute copies of it under certain\n"
134 "conditions.  There is absolutely no warranty for this software.  Please\n"
135 "read the 'COPYING.txt' file for details.\n\n");
136
137         /* Initialize... */
138         init_sysdep();
139         openlog("citserver", LOG_PID, LOG_USER);
140
141         /* Load site-specific parameters */
142         lprintf(7, "Loading citadel.config\n");
143         get_config();
144
145
146         /*
147          * Do non system dependent startup functions.
148          */
149         master_startup();
150
151         /*
152          * Bind the server to a Unix-domain socket.
153          */
154         CtdlRegisterServiceHook(0,
155                                 "citadel.socket",
156                                 citproto_begin_session,
157                                 do_command_loop);
158
159         /*
160          * Bind the server to our favorite TCP port (usually 504).
161          */
162         CtdlRegisterServiceHook(config.c_port_number,
163                                 NULL,
164                                 citproto_begin_session,
165                                 do_command_loop);
166
167         /*
168          * Load any server-side modules (plugins) available here.
169          */
170         lprintf(7, "Initializing loadable modules\n");
171         if ((moddir = malloc(strlen(bbs_home_directory) + 9)) != NULL) {
172                 sprintf(moddir, "%s/modules", bbs_home_directory);
173                 DLoader_Init(moddir);
174                 free(moddir);
175         }
176
177         /*
178          * The rescan pipe exists so that worker threads can be woken up and
179          * told to re-scan the context list for fd's to listen on.  This is
180          * necessary, for example, when a context is about to go idle and needs
181          * to get back on that list.
182          */
183         if (pipe(rescan)) {
184                 lprintf(1, "Can't create rescan pipe!\n");
185                 exit(errno);
186         }
187
188         init_master_fdset();
189
190         /*
191          * Now that we've bound the sockets, change to the BBS user id and its
192          * corresponding group ids
193          */
194         if (drop_root_perms) {
195                 if ((pw = getpwuid(BBSUID)) == NULL)
196                         lprintf(1, "WARNING: getpwuid(%d): %s\n"
197                                    "Group IDs will be incorrect.\n", BBSUID,
198                                 strerror(errno));
199                 else {
200                         initgroups(pw->pw_name, pw->pw_gid);
201                         if (setgid(pw->pw_gid))
202                                 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
203                                         strerror(errno));
204                 }
205                 lprintf(7, "Changing uid to %d\n", BBSUID);
206                 if (setuid(BBSUID) != 0) {
207                         lprintf(3, "setuid() failed: %s\n", strerror(errno));
208                 }
209         }
210
211         /* We want to check for idle sessions once per minute */
212         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
213
214         /*
215          * Now create a bunch of worker threads.
216          */
217         lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
218         begin_critical_section(S_WORKER_LIST);
219         for (i=0; i<(config.c_min_workers-1); ++i) {
220                 create_worker();
221         }
222         end_critical_section(S_WORKER_LIST);
223
224         /* Now this thread can become a worker as well. */
225         initial_thread = pthread_self();
226         worker_thread(NULL);
227
228         /* Server is exiting. Wait for workers to shutdown. */
229         lprintf(7, "Waiting for worker threads to shut down\n");
230
231         begin_critical_section(S_WORKER_LIST);
232         while (worker_list != NULL) {
233                 wnp = worker_list;
234                 worker_list = wnp->next;
235
236                 /* avoid deadlock with an exiting thread */
237                 end_critical_section(S_WORKER_LIST);
238                 if ((i = pthread_join(wnp->tid, NULL)))
239                         lprintf(1, "pthread_join: %s\n", strerror(i));
240                 phree(wnp);
241                 begin_critical_section(S_WORKER_LIST);
242         }
243         end_critical_section(S_WORKER_LIST);
244
245         master_cleanup();
246
247         return(0);
248 }