* New back end function CtdlRenameRoom() which is used to rename a room and/or
[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         size_t size;
74         
75         /* specify default port name and trace file */
76         strcpy(tracefile, "");
77
78         /* initialize the master context */
79         InitializeMasterCC();
80
81         /* parse command-line arguments */
82         for (a=1; a<argc; ++a) {
83
84                 /* -t specifies where to log trace messages to */
85                 if (!strncmp(argv[a], "-t", 2)) {
86                         strcpy(tracefile, argv[a]);
87                         strcpy(tracefile, &tracefile[2]);
88                         freopen(tracefile, "r", stdin);
89                         freopen(tracefile, "w", stdout);
90                         freopen(tracefile, "w", stderr);
91                         chmod(tracefile, 0600);
92                 }
93
94                 /* run in the background if -d was specified */
95                 else if (!strcmp(argv[a], "-d")) {
96                         start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
97                 }
98
99                 /* -x specifies the desired logging level */
100                 else if (!strncmp(argv[a], "-x", 2)) {
101                         verbosity = atoi(&argv[a][2]);
102                 }
103
104                 else if (!strncmp(argv[a], "-h", 2)) {
105                         safestrncpy(bbs_home_directory, &argv[a][2],
106                                     sizeof bbs_home_directory);
107                         home_specified = 1;
108                 }
109
110                 else if (!strncmp(argv[a], "-f", 2)) {
111                         do_defrag = 1;
112                 }
113
114                 /* -r tells the server not to drop root permissions. don't use
115                  * this unless you know what you're doing. this should be
116                  * removed in the next release if it proves unnecessary. */
117                 else if (!strcmp(argv[a], "-r"))
118                         drop_root_perms = 0;
119
120                 /* any other parameter makes it crash and burn */
121                 else {
122                         lprintf(1,      "citserver: usage: "
123                                         "citserver [-tTraceFile] [-d] [-f]"
124                                         " [-xLogLevel] [-hHomeDir]\n");
125                         exit(1);
126                 }
127
128         }
129
130         /* Tell 'em who's in da house */
131         lprintf(1,
132 "\n"
133 "Citadel/UX messaging server engine v%d.%02d\n"
134 "Copyright (C) 1987-2002 by the Citadel/UX development team.\n"
135 "Citadel/UX is released under the terms of the GNU General Public License.\n"
136 "If you paid for this software, someone is ripping you off.\n\n",
137         (REV_LEVEL/100),
138         (REV_LEVEL%100));
139
140         /* Initialize... */
141         init_sysdep();
142         openlog("citserver", LOG_PID, LOG_USER);
143
144         /* Load site-specific parameters */
145         lprintf(7, "Loading citadel.config\n");
146         get_config();
147
148
149         /*
150          * Do non system dependent startup functions.
151          */
152         master_startup();
153
154         /*
155          * Bind the server to a Unix-domain socket.
156          */
157         CtdlRegisterServiceHook(0,
158                                 "citadel.socket",
159                                 citproto_begin_session,
160                                 do_command_loop);
161
162         /*
163          * Bind the server to our favorite TCP port (usually 504).
164          */
165         CtdlRegisterServiceHook(config.c_port_number,
166                                 NULL,
167                                 citproto_begin_session,
168                                 do_command_loop);
169
170         /*
171          * Load any server-side modules (plugins) available here.
172          */
173         lprintf(7, "Initializing loadable modules\n");
174         size = strlen(bbs_home_directory) + 9;
175         if ((moddir = mallok(size)) != NULL) {
176                 snprintf(moddir, size, "%s/modules", bbs_home_directory);
177                 DLoader_Init(moddir);
178                 free(moddir);
179         }
180
181         /*
182          * The rescan pipe exists so that worker threads can be woken up and
183          * told to re-scan the context list for fd's to listen on.  This is
184          * necessary, for example, when a context is about to go idle and needs
185          * to get back on that list.
186          */
187         if (pipe(rescan)) {
188                 lprintf(1, "Can't create rescan pipe!\n");
189                 exit(errno);
190         }
191
192         init_master_fdset();
193
194         /*
195          * Now that we've bound the sockets, change to the BBS user id and its
196          * corresponding group ids
197          */
198         if (drop_root_perms) {
199                 if ((pw = getpwuid(BBSUID)) == NULL)
200                         lprintf(1, "WARNING: getpwuid(%ld): %s\n"
201                                    "Group IDs will be incorrect.\n", (long)BBSUID,
202                                 strerror(errno));
203                 else {
204                         initgroups(pw->pw_name, pw->pw_gid);
205                         if (setgid(pw->pw_gid))
206                                 lprintf(3, "setgid(%ld): %s\n", (long)pw->pw_gid,
207                                         strerror(errno));
208                 }
209                 lprintf(7, "Changing uid to %ld\n", (long)BBSUID);
210                 if (setuid(BBSUID) != 0) {
211                         lprintf(3, "setuid() failed: %s\n", strerror(errno));
212                 }
213         }
214
215         /* We want to check for idle sessions once per minute */
216         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
217
218         /*
219          * Now create a bunch of worker threads.
220          */
221         lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
222         begin_critical_section(S_WORKER_LIST);
223         for (i=0; i<(config.c_min_workers-1); ++i) {
224                 create_worker();
225         }
226         end_critical_section(S_WORKER_LIST);
227
228         /* Now this thread can become a worker as well. */
229         initial_thread = pthread_self();
230         worker_thread(NULL);
231
232         /* Server is exiting. Wait for workers to shutdown. */
233         lprintf(7, "Waiting for worker threads to shut down\n");
234
235         begin_critical_section(S_WORKER_LIST);
236         while (worker_list != NULL) {
237                 wnp = worker_list;
238                 worker_list = wnp->next;
239
240                 /* avoid deadlock with an exiting thread */
241                 end_critical_section(S_WORKER_LIST);
242                 if ((i = pthread_join(wnp->tid, NULL)))
243                         lprintf(1, "pthread_join: %s\n", strerror(i));
244                 phree(wnp);
245                 begin_critical_section(S_WORKER_LIST);
246         }
247         end_critical_section(S_WORKER_LIST);
248
249         master_cleanup();
250
251         return(0);
252 }