vlserver: Log to VLLog by default, not PtLog
[openafs.git] / src / vlserver / vlserver.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #ifdef AFS_NT40_ENV
17 #include <WINNT/afsevent.h>
18 #endif
19
20 #ifdef HAVE_SYS_FILE_H
21 #include <sys/file.h>
22 #endif
23
24 #include <rx/xdr.h>
25 #include <rx/rx.h>
26 #include <rx/rx_globals.h>
27 #include <rx/rxstat.h>
28 #include <afs/cmd.h>
29 #include <afs/cellconfig.h>
30 #include <afs/keys.h>
31 #include <afs/auth.h>
32 #include <afs/audit.h>
33 #include <afs/com_err.h>
34 #include <lock.h>
35 #include <ubik.h>
36 #include <afs/afsutil.h>
37
38 #include "vlserver.h"
39 #include "vlserver_internal.h"
40
41 #define MAXLWP 16
42 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
43 int lwps = 9;
44
45 struct vldstats dynamic_statistics;
46 struct ubik_dbase *VL_dbase;
47 afs_uint32 rd_HostAddress[MAXSERVERID + 1];
48 afs_uint32 wr_HostAddress[MAXSERVERID + 1];
49
50 static void *CheckSignal(void*);
51 int LogLevel = 0;
52 int smallMem = 0;
53 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
54 int rxMaxMTU = -1;
55 afs_int32 rxBind = 0;
56 int rxkadDisableDotCheck = 0;
57
58 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
59 afs_uint32 SHostAddrs[ADDRSPERSITE];
60
61 static void
62 CheckSignal_Signal(int unused)
63 {
64 #if defined(AFS_PTHREAD_ENV)
65     CheckSignal(0);
66 #else
67     IOMGR_SoftSig(CheckSignal, 0);
68 #endif
69 }
70
71 static void *
72 CheckSignal(void *unused)
73 {
74     int i, errorcode;
75     struct vl_ctx ctx;
76
77     if ((errorcode =
78         Init_VLdbase(&ctx, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
79         return (void *)(intptr_t)errorcode;
80     VLog(0, ("Dump name hash table out\n"));
81     for (i = 0; i < HASHSIZE; i++) {
82         HashNDump(&ctx, i);
83     }
84     VLog(0, ("Dump id hash table out\n"));
85     for (i = 0; i < HASHSIZE; i++) {
86         HashIdDump(&ctx, i);
87     }
88     return ((void *)(intptr_t)ubik_EndTrans(ctx.trans));
89 }                               /*CheckSignal */
90
91
92 /* Initialize the stats for the opcodes */
93 void
94 initialize_dstats(void)
95 {
96     int i;
97
98     dynamic_statistics.start_time = (afs_uint32) time(0);
99     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
100         dynamic_statistics.requests[i] = 0;
101         dynamic_statistics.aborts[i] = 0;
102     }
103 }
104
105 /* check whether caller is authorized to manage RX statistics */
106 int
107 vldb_rxstat_userok(struct rx_call *call)
108 {
109     return afsconf_SuperUser(vldb_confdir, call, NULL);
110 }
111
112 /* Main server module */
113
114 #include "AFS_component_version_number.c"
115
116 enum optionsList {
117     OPT_noauth,
118     OPT_smallmem,
119     OPT_auditlog,
120     OPT_auditiface,
121     OPT_config,
122     OPT_debug,
123     OPT_database,
124     OPT_logfile,
125     OPT_threads,
126     OPT_syslog,
127     OPT_peer,
128     OPT_process,
129     OPT_nojumbo,
130     OPT_jumbo,
131     OPT_rxbind,
132     OPT_rxmaxmtu,
133     OPT_trace,
134     OPT_dotted
135 };
136
137 int
138 main(int argc, char **argv)
139 {
140     afs_int32 code;
141     afs_uint32 myHost;
142     struct rx_service *tservice;
143     struct rx_securityClass **securityClasses;
144     afs_int32 numClasses;
145     struct afsconf_dir *tdir;
146     struct ktc_encryptionKey tkey;
147     struct afsconf_cell info;
148     struct hostent *th;
149     char hostname[VL_MAXNAMELEN];
150     int noAuth = 0;
151     char clones[MAXHOSTSPERCELL];
152     afs_uint32 host = ntohl(INADDR_ANY);
153     struct cmd_syndesc *opts;
154
155     char *vl_dbaseName;
156     char *configDir;
157     char *logFile;
158
159     char *auditFileName = NULL;
160     char *interface = NULL;
161     char *optstring = NULL;
162
163 #ifdef  AFS_AIX32_ENV
164     /*
165      * The following signal action for AIX is necessary so that in case of a
166      * crash (i.e. core is generated) we can include the user's data section
167      * in the core dump. Unfortunately, by default, only a partial core is
168      * generated which, in many cases, isn't too useful.
169      */
170     struct sigaction nsa;
171
172     rx_extraPackets = 100;      /* should be a switch, I guess... */
173     sigemptyset(&nsa.sa_mask);
174     nsa.sa_handler = SIG_DFL;
175     nsa.sa_flags = SA_FULLDUMP;
176     sigaction(SIGABRT, &nsa, NULL);
177     sigaction(SIGSEGV, &nsa, NULL);
178 #endif
179     osi_audit_init();
180
181     /* Initialize dirpaths */
182     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
183 #ifdef AFS_NT40_ENV
184         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
185 #endif
186         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
187                 argv[0]);
188         exit(2);
189     }
190
191     vl_dbaseName = strdup(AFSDIR_SERVER_VLDB_FILEPATH);
192     configDir = strdup(AFSDIR_SERVER_ETC_DIRPATH);
193     logFile = strdup(AFSDIR_SERVER_VLOG_FILEPATH);
194
195     cmd_DisableAbbreviations();
196     cmd_DisablePositionalCommands();
197     opts = cmd_CreateSyntax(NULL, NULL, NULL, NULL);
198
199     /* vlserver specific options */
200     cmd_AddParmAtOffset(opts, OPT_noauth, "-noauth", CMD_FLAG,
201                         CMD_OPTIONAL, "disable authentication");
202     cmd_AddParmAtOffset(opts, OPT_smallmem, "-smallmem", CMD_FLAG,
203                         CMD_OPTIONAL, "optimise for small memory systems");
204
205     /* general server options */
206     cmd_AddParmAtOffset(opts, OPT_auditlog, "-auditlog", CMD_SINGLE,
207                         CMD_OPTIONAL, "location of audit log");
208     cmd_AddParmAtOffset(opts, OPT_auditiface, "-audit-interface", CMD_SINGLE,
209                         CMD_OPTIONAL, "interface to use for audit logging");
210     cmd_AddParmAtOffset(opts, OPT_config, "-config", CMD_SINGLE,
211                         CMD_OPTIONAL, "configuration location");
212     cmd_AddParmAtOffset(opts, OPT_debug, "-d", CMD_SINGLE,
213                         CMD_OPTIONAL, "debug level");
214     cmd_AddParmAtOffset(opts, OPT_database, "-database", CMD_SINGLE,
215                         CMD_OPTIONAL, "database file");
216     cmd_AddParmAlias(opts, OPT_database, "-db");
217     cmd_AddParmAtOffset(opts, OPT_logfile, "-logfile", CMD_SINGLE,
218                         CMD_OPTIONAL, "location of logfile");
219     cmd_AddParmAtOffset(opts, OPT_threads, "-p", CMD_SINGLE, CMD_OPTIONAL,
220                         "number of threads");
221 #if !defined(AFS_NT40_ENV)
222     cmd_AddParmAtOffset(opts, OPT_syslog, "-syslog", CMD_SINGLE_OR_FLAG,
223                         CMD_OPTIONAL, "log to syslog");
224 #endif
225
226     /* rx options */
227     cmd_AddParmAtOffset(opts, OPT_peer, "-enable_peer_stats", CMD_FLAG,
228                         CMD_OPTIONAL, "enable RX transport statistics");
229     cmd_AddParmAtOffset(opts, OPT_process, "-enable_process_stats", CMD_FLAG,
230                         CMD_OPTIONAL, "enable RX RPC statistics");
231     cmd_AddParmAtOffset(opts, OPT_nojumbo, "-nojumbo", CMD_FLAG,
232                         CMD_OPTIONAL, "disable jumbograms");
233     cmd_AddParmAtOffset(opts, OPT_jumbo, "-jumbo", CMD_FLAG,
234                         CMD_OPTIONAL, "enable jumbograms");
235     cmd_AddParmAtOffset(opts, OPT_rxbind, "-rxbind", CMD_FLAG,
236                         CMD_OPTIONAL, "bind only to the primary interface");
237     cmd_AddParmAtOffset(opts, OPT_rxmaxmtu, "-rxmaxmtu", CMD_SINGLE,
238                         CMD_OPTIONAL, "maximum MTU for RX");
239     cmd_AddParmAtOffset(opts, OPT_trace, "-trace", CMD_SINGLE,
240                         CMD_OPTIONAL, "rx trace file");
241
242     /* rxkad options */
243     cmd_AddParmAtOffset(opts, OPT_dotted, "-allow-dotted-principals",
244                         CMD_FLAG, CMD_OPTIONAL,
245                         "permit Kerberos 5 principals with dots");
246
247     code = cmd_Parse(argc, argv, &opts);
248     if (code)
249         return -1;
250
251     /* vlserver options */
252     cmd_OptionAsFlag(opts, OPT_noauth, &noAuth);
253     cmd_OptionAsFlag(opts, OPT_smallmem, &smallMem);
254     if (cmd_OptionAsString(opts, OPT_trace, &optstring) == 0) {
255         extern char rxi_tracename[80];
256         strcpy(rxi_tracename, optstring);
257         free(optstring);
258         optstring = NULL;
259     }
260
261     /* general server options */
262
263     cmd_OptionAsString(opts, OPT_auditlog, &auditFileName);
264
265     if (cmd_OptionAsString(opts, OPT_auditiface, &interface) == 0) {
266         if (osi_audit_interface(interface)) {
267             printf("Invalid audit interface '%s'\n", interface);
268             return -1;
269         }
270         free(interface);
271     }
272
273     cmd_OptionAsString(opts, OPT_config, &configDir);
274     cmd_OptionAsInt(opts, OPT_debug, &LogLevel);
275     cmd_OptionAsString(opts, OPT_database, &vl_dbaseName);
276     cmd_OptionAsString(opts, OPT_logfile, &logFile);
277
278     if (cmd_OptionAsInt(opts, OPT_threads, &lwps) == 0) {
279         if (lwps > MAXLWP) {
280              printf("Warning: '-p %d' is too big; using %d instead\n",
281                     lwps, MAXLWP);
282              lwps = MAXLWP;
283         }
284     }
285 #ifndef AFS_NT40_ENV
286     if (cmd_OptionPresent(opts, OPT_syslog)) {
287         serverLogSyslog = 1;
288         cmd_OptionAsInt(opts, OPT_syslog, &serverLogSyslogFacility);
289     }
290 #endif
291
292     /* rx options */
293     if (cmd_OptionPresent(opts, OPT_peer))
294         rx_enablePeerRPCStats();
295     if (cmd_OptionPresent(opts, OPT_process))
296         rx_enableProcessRPCStats();
297     if (cmd_OptionPresent(opts, OPT_nojumbo))
298         rxJumbograms = 0;
299     if (cmd_OptionPresent(opts, OPT_jumbo))
300         rxJumbograms = 1;
301
302     cmd_OptionAsFlag(opts, OPT_rxbind, &rxBind);
303
304     if (cmd_OptionAsInt(opts, OPT_rxmaxmtu, &rxMaxMTU) == 0) {
305         if ((rxMaxMTU < RX_MIN_PACKET_SIZE) ||
306             (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
307             printf("rxMaxMTU %d invalid; must be between %d-%" AFS_SIZET_FMT "\n",
308                    rxMaxMTU, RX_MIN_PACKET_SIZE,
309                    RX_MAX_PACKET_DATA_SIZE);
310             return -1;
311         }
312     }
313
314     /* rxkad options */
315     cmd_OptionAsFlag(opts, OPT_dotted, &rxkadDisableDotCheck);
316
317     if (auditFileName) {
318         osi_audit_file(auditFileName);
319     }
320
321 #ifndef AFS_NT40_ENV
322     serverLogSyslogTag = "vlserver";
323 #endif
324     OpenLog(logFile);   /* set up logging */
325     SetupLogSignals();
326
327     tdir = afsconf_Open(configDir);
328     if (!tdir) {
329         printf
330             ("vlserver: can't open configuration files in dir %s, giving up.\n",
331              configDir);
332         exit(1);
333     }
334 #ifdef AFS_NT40_ENV
335     /* initialize winsock */
336     if (afs_winsockInit() < 0) {
337         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
338         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
339         exit(1);
340     }
341 #endif
342     /* get this host */
343     gethostname(hostname, sizeof(hostname));
344     th = gethostbyname(hostname);
345     if (!th) {
346         printf("vlserver: couldn't get address of this host (%s).\n",
347                hostname);
348         exit(1);
349     }
350     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
351
352 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
353     signal(SIGXCPU, CheckSignal_Signal);
354 #endif
355     /* get list of servers */
356     code =
357         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
358                                     clones);
359     if (code) {
360         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
361         exit(2);
362     }
363
364     vldb_confdir = tdir;        /* Preserve our configuration dir */
365     /* rxvab no longer supported */
366     memset(&tkey, 0, sizeof(tkey));
367
368     if (noAuth)
369         afsconf_SetNoAuthFlag(tdir, 1);
370
371     if (rxBind) {
372         afs_int32 ccode;
373 #ifndef AFS_NT40_ENV
374         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
375             AFSDIR_SERVER_NETINFO_FILEPATH) {
376             char reason[1024];
377             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
378                                   ADDRSPERSITE, reason,
379                                   AFSDIR_SERVER_NETINFO_FILEPATH,
380                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
381         } else
382 #endif
383         {
384             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
385         }
386         if (ccode == 1) {
387             host = SHostAddrs[0];
388             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
389         }
390     }
391
392     ubik_nBuffers = 512;
393     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, tdir);
394     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
395                                 afsconf_CheckAuth, tdir);
396
397     ubik_SyncWriterCacheProc = vlsynccache;
398     code =
399         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
400                               vl_dbaseName, &VL_dbase);
401     if (code) {
402         printf("vlserver: Ubik init failed: %s\n", afs_error_message(code));
403         exit(2);
404     }
405     if (!rxJumbograms) {
406         rx_SetNoJumbo();
407     }
408     if (rxMaxMTU != -1) {
409         rx_SetMaxMTU(rxMaxMTU);
410     }
411     rx_SetRxDeadTime(50);
412
413     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
414     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
415     initialize_dstats();
416
417     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
418
419     tservice =
420         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
421                           securityClasses, numClasses,
422                           VL_ExecuteRequest);
423     if (tservice == (struct rx_service *)0) {
424         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
425         exit(3);
426     }
427     rx_SetMinProcs(tservice, 2);
428     if (lwps < 4)
429         lwps = 4;
430     rx_SetMaxProcs(tservice, lwps);
431
432     if (rxkadDisableDotCheck) {
433         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
434                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
435     }
436
437     tservice =
438         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
439                           securityClasses, numClasses,
440                           RXSTATS_ExecuteRequest);
441     if (tservice == (struct rx_service *)0) {
442         printf("vlserver: Could not create rpc stats rx service\n");
443         exit(3);
444     }
445     rx_SetMinProcs(tservice, 2);
446     rx_SetMaxProcs(tservice, 4);
447
448     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
449     printf("%s\n", cml_version_number); /* Goes to the log */
450
451     /* allow super users to manage RX statistics */
452     rx_SetRxStatUserOk(vldb_rxstat_userok);
453
454     rx_StartServer(1);          /* Why waste this idle process?? */
455
456     return 0; /* not reachable */
457 }