ptserver: Rationalise usage message
[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/cellconfig.h>
29 #include <afs/keys.h>
30 #include <afs/auth.h>
31 #include <afs/audit.h>
32 #include <afs/com_err.h>
33 #include <lock.h>
34 #include <ubik.h>
35 #include <afs/afsutil.h>
36
37 #include "vlserver.h"
38 #include "vlserver_internal.h"
39
40 #define MAXLWP 16
41 const char *vl_dbaseName;
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 int debuglevel = 0;
58
59 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
60 afs_uint32 SHostAddrs[ADDRSPERSITE];
61
62 static void
63 CheckSignal_Signal(int unused)
64 {
65 #if defined(AFS_PTHREAD_ENV)
66     CheckSignal(0);
67 #else
68     IOMGR_SoftSig(CheckSignal, 0);
69 #endif
70 }
71
72 static void *
73 CheckSignal(void *unused)
74 {
75     int i, errorcode;
76     struct vl_ctx ctx;
77
78     if ((errorcode =
79         Init_VLdbase(&ctx, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
80         return (void *)(intptr_t)errorcode;
81     VLog(0, ("Dump name hash table out\n"));
82     for (i = 0; i < HASHSIZE; i++) {
83         HashNDump(&ctx, i);
84     }
85     VLog(0, ("Dump id hash table out\n"));
86     for (i = 0; i < HASHSIZE; i++) {
87         HashIdDump(&ctx, i);
88     }
89     return ((void *)(intptr_t)ubik_EndTrans(ctx.trans));
90 }                               /*CheckSignal */
91
92
93 /* Initialize the stats for the opcodes */
94 void
95 initialize_dstats(void)
96 {
97     int i;
98
99     dynamic_statistics.start_time = (afs_uint32) time(0);
100     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
101         dynamic_statistics.requests[i] = 0;
102         dynamic_statistics.aborts[i] = 0;
103     }
104 }
105
106 /* check whether caller is authorized to manage RX statistics */
107 int
108 vldb_rxstat_userok(struct rx_call *call)
109 {
110     return afsconf_SuperUser(vldb_confdir, call, NULL);
111 }
112
113 /* Main server module */
114
115 #include "AFS_component_version_number.c"
116
117 int
118 main(int argc, char **argv)
119 {
120     afs_int32 code;
121     afs_uint32 myHost;
122     struct rx_service *tservice;
123     struct rx_securityClass **securityClasses;
124     afs_int32 numClasses;
125     struct afsconf_dir *tdir;
126     struct ktc_encryptionKey tkey;
127     struct afsconf_cell info;
128     struct hostent *th;
129     char hostname[VL_MAXNAMELEN];
130     int noAuth = 0, index;
131     char clones[MAXHOSTSPERCELL];
132     char *auditFileName = NULL;
133     afs_uint32 host = ntohl(INADDR_ANY);
134
135 #ifdef  AFS_AIX32_ENV
136     /*
137      * The following signal action for AIX is necessary so that in case of a
138      * crash (i.e. core is generated) we can include the user's data section
139      * in the core dump. Unfortunately, by default, only a partial core is
140      * generated which, in many cases, isn't too useful.
141      */
142     struct sigaction nsa;
143
144     rx_extraPackets = 100;      /* should be a switch, I guess... */
145     sigemptyset(&nsa.sa_mask);
146     nsa.sa_handler = SIG_DFL;
147     nsa.sa_flags = SA_FULLDUMP;
148     sigaction(SIGABRT, &nsa, NULL);
149     sigaction(SIGSEGV, &nsa, NULL);
150 #endif
151     osi_audit_init();
152
153     /* Parse command line */
154     for (index = 1; index < argc; index++) {
155         if (strcmp(argv[index], "-noauth") == 0) {
156             noAuth = 1;
157         } else if (strcmp(argv[index], "-p") == 0) {
158             lwps = atoi(argv[++index]);
159             if (lwps > MAXLWP) {
160                 printf("Warning: '-p %d' is too big; using %d instead\n",
161                        lwps, MAXLWP);
162                 lwps = MAXLWP;
163             }
164         } else if (strcmp(argv[index], "-d") == 0) {
165             if ((index + 1) >= argc) {
166                 fprintf(stderr, "missing argument for -d\n");
167                 return -1;
168             }
169             debuglevel = atoi(argv[++index]);
170             LogLevel = debuglevel;
171         } else if (strcmp(argv[index], "-nojumbo") == 0) {
172             rxJumbograms = 0;
173         } else if (strcmp(argv[index], "-jumbo") == 0) {
174             rxJumbograms = 1;
175         } else if (strcmp(argv[index], "-rxbind") == 0) {
176             rxBind = 1;
177         } else if (strcmp(argv[index], "-allow-dotted-principals") == 0) {
178             rxkadDisableDotCheck = 1;
179         } else if (!strcmp(argv[index], "-rxmaxmtu")) {
180             if ((index + 1) >= argc) {
181                 fprintf(stderr, "missing argument for -rxmaxmtu\n");
182                 return -1;
183             }
184             rxMaxMTU = atoi(argv[++index]);
185             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) ||
186                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
187                 printf("rxMaxMTU %d invalid; must be between %d-%" AFS_SIZET_FMT "\n",
188                        rxMaxMTU, RX_MIN_PACKET_SIZE,
189                        RX_MAX_PACKET_DATA_SIZE);
190                 return -1;
191             }
192
193         } else if (strcmp(argv[index], "-smallmem") == 0) {
194             smallMem = 1;
195
196         } else if (strcmp(argv[index], "-trace") == 0) {
197             extern char rxi_tracename[80];
198             strcpy(rxi_tracename, argv[++index]);
199
200         } else if (strcmp(argv[index], "-auditlog") == 0) {
201             auditFileName = argv[++index];
202
203         } else if (strcmp(argv[index], "-audit-interface") == 0) {
204             char *interface = argv[++index];
205
206             if (osi_audit_interface(interface)) {
207                 printf("Invalid audit interface '%s'\n", interface);
208                 return -1;
209             }
210
211         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
212             rx_enablePeerRPCStats();
213         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
214             rx_enableProcessRPCStats();
215 #ifndef AFS_NT40_ENV
216         } else if (strcmp(argv[index], "-syslog") == 0) {
217             /* set syslog logging flag */
218             serverLogSyslog = 1;
219         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
220             serverLogSyslog = 1;
221             serverLogSyslogFacility = atoi(argv[index] + 8);
222 #endif
223         } else {
224             /* support help flag */
225 #ifndef AFS_NT40_ENV
226             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
227                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
228                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
229                    "[-syslog[=FACILITY]] "
230                    "[-enable_peer_stats] [-enable_process_stats] "
231                    "[-help]\n");
232 #else
233             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
234                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
235                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
236                    "[-enable_peer_stats] [-enable_process_stats] "
237                    "[-help]\n");
238 #endif
239             fflush(stdout);
240             exit(0);
241         }
242     }
243
244     if (auditFileName) {
245         osi_audit_file(auditFileName);
246     }
247
248     /* Initialize dirpaths */
249     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
250 #ifdef AFS_NT40_ENV
251         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
252 #endif
253         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
254                 argv[0]);
255         exit(2);
256     }
257     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
258
259 #ifndef AFS_NT40_ENV
260     serverLogSyslogTag = "vlserver";
261 #endif
262     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
263     SetupLogSignals();
264
265     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
266     if (!tdir) {
267         printf
268             ("vlserver: can't open configuration files in dir %s, giving up.\n",
269              AFSDIR_SERVER_ETC_DIRPATH);
270         exit(1);
271     }
272 #ifdef AFS_NT40_ENV
273     /* initialize winsock */
274     if (afs_winsockInit() < 0) {
275         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
276         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
277         exit(1);
278     }
279 #endif
280     /* get this host */
281     gethostname(hostname, sizeof(hostname));
282     th = gethostbyname(hostname);
283     if (!th) {
284         printf("vlserver: couldn't get address of this host (%s).\n",
285                hostname);
286         exit(1);
287     }
288     memcpy(&myHost, th->h_addr, sizeof(afs_uint32));
289
290 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
291     signal(SIGXCPU, CheckSignal_Signal);
292 #endif
293     /* get list of servers */
294     code =
295         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
296                                     clones);
297     if (code) {
298         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
299         exit(2);
300     }
301
302     vldb_confdir = tdir;        /* Preserve our configuration dir */
303     /* rxvab no longer supported */
304     memset(&tkey, 0, sizeof(tkey));
305
306     if (noAuth)
307         afsconf_SetNoAuthFlag(tdir, 1);
308
309     if (rxBind) {
310         afs_int32 ccode;
311 #ifndef AFS_NT40_ENV
312         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
313             AFSDIR_SERVER_NETINFO_FILEPATH) {
314             char reason[1024];
315             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
316                                   ADDRSPERSITE, reason,
317                                   AFSDIR_SERVER_NETINFO_FILEPATH,
318                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
319         } else
320 #endif
321         {
322             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
323         }
324         if (ccode == 1) {
325             host = SHostAddrs[0];
326             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
327         }
328     }
329
330     ubik_nBuffers = 512;
331     ubik_SetClientSecurityProcs(afsconf_ClientAuth, afsconf_UpToDate, tdir);
332     ubik_SetServerSecurityProcs(afsconf_BuildServerSecurityObjects,
333                                 afsconf_CheckAuth, tdir);
334
335     ubik_SyncWriterCacheProc = vlsynccache;
336     code =
337         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
338                               vl_dbaseName, &VL_dbase);
339     if (code) {
340         printf("vlserver: Ubik init failed: %s\n", afs_error_message(code));
341         exit(2);
342     }
343     if (!rxJumbograms) {
344         rx_SetNoJumbo();
345     }
346     if (rxMaxMTU != -1) {
347         rx_SetMaxMTU(rxMaxMTU);
348     }
349     rx_SetRxDeadTime(50);
350
351     memset(rd_HostAddress, 0, sizeof(rd_HostAddress));
352     memset(wr_HostAddress, 0, sizeof(wr_HostAddress));
353     initialize_dstats();
354
355     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
356
357     tservice =
358         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server",
359                           securityClasses, numClasses,
360                           VL_ExecuteRequest);
361     if (tservice == (struct rx_service *)0) {
362         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
363         exit(3);
364     }
365     rx_SetMinProcs(tservice, 2);
366     if (lwps < 4)
367         lwps = 4;
368     rx_SetMaxProcs(tservice, lwps);
369
370     if (rxkadDisableDotCheck) {
371         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
372                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
373     }
374
375     tservice =
376         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats",
377                           securityClasses, numClasses,
378                           RXSTATS_ExecuteRequest);
379     if (tservice == (struct rx_service *)0) {
380         printf("vlserver: Could not create rpc stats rx service\n");
381         exit(3);
382     }
383     rx_SetMinProcs(tservice, 2);
384     rx_SetMaxProcs(tservice, 4);
385
386     LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog);
387     printf("%s\n", cml_version_number); /* Goes to the log */
388
389     /* allow super users to manage RX statistics */
390     rx_SetRxStatUserOk(vldb_rxstat_userok);
391
392     rx_StartServer(1);          /* Why waste this idle process?? */
393
394     return 0; /* not reachable */
395 }