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