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