assorted-warning-cleanup-20071126
[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 CheckSignal();
64 int LogLevel = 0;
65 int smallMem = 0;
66 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
67 int rxMaxMTU = -1;
68 afs_int32 rxBind = 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()
75 {
76     IOMGR_SoftSig(CheckSignal, 0);
77 }
78
79 static
80 CheckSignal()
81 {
82     register int i, errorcode;
83     struct ubik_trans *trans;
84
85     if (errorcode =
86         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
87         return errorcode;
88     VLog(0, ("Dump name hash table out\n"));
89     for (i = 0; i < HASHSIZE; i++) {
90         HashNDump(trans, i);
91     }
92     VLog(0, ("Dump id hash table out\n"));
93     for (i = 0; i < HASHSIZE; i++) {
94         HashIdDump(trans, i);
95     }
96     return (ubik_EndTrans(trans));
97 }                               /*CheckSignal */
98
99
100 /* Initialize the stats for the opcodes */
101 void
102 initialize_dstats()
103 {
104     int i;
105
106     dynamic_statistics.start_time = (afs_uint32) time(0);
107     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
108         dynamic_statistics.requests[i] = 0;
109         dynamic_statistics.aborts[i] = 0;
110     }
111 }
112
113 /* check whether caller is authorized to manage RX statistics */
114 int
115 vldb_rxstat_userok(call)
116      struct rx_call *call;
117 {
118     return afsconf_SuperUser(vldb_confdir, call, NULL);
119 }
120
121 /* Main server module */
122
123 #include "AFS_component_version_number.c"
124
125 main(argc, argv)
126      int argc;
127      char **argv;
128 {
129     register afs_int32 code;
130     afs_int32 myHost;
131     struct rx_service *tservice;
132     struct rx_securityClass *sc[3];
133     extern int VL_ExecuteRequest();
134     extern int RXSTATS_ExecuteRequest();
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     extern int rx_extraPackets;
142     char commandLine[150];
143     char clones[MAXHOSTSPERCELL];
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
169         } else if (strcmp(argv[index], "-p") == 0) {
170             lwps = atoi(argv[++index]);
171             if (lwps > MAXLWP) {
172                 printf("Warning: '-p %d' is too big; using %d instead\n",
173                        lwps, MAXLWP);
174                 lwps = MAXLWP;
175             }
176
177         } else if (strcmp(argv[index], "-nojumbo") == 0) {
178             rxJumbograms = 0;
179
180         } else if (strcmp(argv[index], "-rxbind") == 0) {
181             rxBind = 1;
182
183         } else if (!strcmp(argv[index], "-rxmaxmtu")) {
184             if ((index + 1) >= argc) {
185                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
186                 return -1; 
187             }
188             rxMaxMTU = atoi(argv[++i]);
189             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
190                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
191                 printf("rxMaxMTU %d% invalid; must be between %d-%d\n",
192                        rxMaxMTU, RX_MIN_PACKET_SIZE, 
193                        RX_MAX_PACKET_DATA_SIZE);
194                 return -1;
195             }
196
197         } else if (strcmp(argv[index], "-smallmem") == 0) {
198             smallMem = 1;
199
200         } else if (strcmp(argv[index], "-trace") == 0) {
201             extern char rxi_tracename[80];
202             strcpy(rxi_tracename, argv[++index]);
203
204        } else if (strcmp(argv[index], "-auditlog") == 0) {
205            int tempfd, flags;
206            FILE *auditout;
207            char oldName[MAXPATHLEN];
208            char *fileName = argv[++index];
209
210 #ifndef AFS_NT40_ENV
211            struct stat statbuf;
212
213            if ((lstat(fileName, &statbuf) == 0) 
214                && (S_ISFIFO(statbuf.st_mode))) {
215                flags = O_WRONLY | O_NONBLOCK;
216            } else 
217 #endif
218            {
219                strcpy(oldName, fileName);
220                strcat(oldName, ".old");
221                renamefile(fileName, oldName);
222                flags = O_WRONLY | O_TRUNC | O_CREAT;
223            }
224            tempfd = open(fileName, flags, 0666);
225            if (tempfd > -1) {
226                auditout = fdopen(tempfd, "a");
227                if (auditout) {
228                    osi_audit_file(auditout);
229                } else
230                    printf("Warning: auditlog %s not writable, ignored.\n", fileName);
231            } else
232                printf("Warning: auditlog %s not writable, ignored.\n", fileName);
233         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
234             rx_enablePeerRPCStats();
235         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
236             rx_enableProcessRPCStats();
237 #ifndef AFS_NT40_ENV
238         } else if (strcmp(argv[index], "-syslog") == 0) {
239             /* set syslog logging flag */
240             serverLogSyslog = 1;
241         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
242             serverLogSyslog = 1;
243             serverLogSyslogFacility = atoi(argv[index] + 8);
244 #endif
245         } else {
246             /* support help flag */
247 #ifndef AFS_NT40_ENV
248             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
249                    "[-rxmaxmtu <bytes>] [-rxbind] "
250                    "[-auditlog <log path>] "
251                    "[-syslog[=FACILITY]] "
252                    "[-enable_peer_stats] [-enable_process_stats] "
253                    "[-help]\n");
254 #else
255             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
256                    "[-rxmaxmtu <bytes>] [-rxbind] "
257                    "[-auditlog <log path>] "
258                    "[-enable_peer_stats] [-enable_process_stats] "
259                    "[-help]\n");
260 #endif
261             fflush(stdout);
262             exit(0);
263         }
264     }
265
266     /* Initialize dirpaths */
267     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
268 #ifdef AFS_NT40_ENV
269         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
270 #endif
271         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
272                 argv[0]);
273         exit(2);
274     }
275     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
276
277 #ifndef AFS_NT40_ENV
278     serverLogSyslogTag = "vlserver";
279 #endif
280     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
281     SetupLogSignals();
282
283     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
284     if (!tdir) {
285         printf
286             ("vlserver: can't open configuration files in dir %s, giving up.\n",
287              AFSDIR_SERVER_ETC_DIRPATH);
288         exit(1);
289     }
290 #ifdef AFS_NT40_ENV
291     /* initialize winsock */
292     if (afs_winsockInit() < 0) {
293         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
294         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
295         exit(1);
296     }
297 #endif
298     /* get this host */
299     gethostname(hostname, sizeof(hostname));
300     th = gethostbyname(hostname);
301     if (!th) {
302         printf("vlserver: couldn't get address of this host (%s).\n",
303                hostname);
304         exit(1);
305     }
306     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
307
308 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
309     signal(SIGXCPU, CheckSignal_Signal);
310 #endif
311     /* get list of servers */
312     code =
313         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
314                                     &clones);
315     if (code) {
316         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
317         exit(2);
318     }
319
320     vldb_confdir = tdir;        /* Preserve our configuration dir */
321     /* rxvab no longer supported */
322     memset(&tkey, 0, sizeof(tkey));
323
324     if (noAuth)
325         afsconf_SetNoAuthFlag(tdir, 1);
326
327     if (rxBind) {
328         afs_int32 ccode;
329 #ifndef AFS_NT40_ENV
330         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
331             AFSDIR_SERVER_NETINFO_FILEPATH) {
332             char reason[1024];
333             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
334                                   ADDRSPERSITE, reason,
335                                   AFSDIR_SERVER_NETINFO_FILEPATH,
336                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
337         } else 
338 #endif  
339         {
340             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
341         }
342         if (ccode == 1) {
343             host = SHostAddrs[0];
344             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
345         }
346     }
347
348     ubik_nBuffers = 512;
349     ubik_CRXSecurityProc = afsconf_ClientAuth;
350     ubik_CRXSecurityRock = (char *)tdir;
351     ubik_SRXSecurityProc = afsconf_ServerAuth;
352     ubik_SRXSecurityRock = (char *)tdir;
353     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
354     ubik_CheckRXSecurityRock = (char *)tdir;
355     code =
356         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
357                               vl_dbaseName, &VL_dbase);
358     if (code) {
359         printf("vlserver: Ubik init failed with code %d\n", code);
360         exit(2);
361     }
362     if (!rxJumbograms) {
363         rx_SetNoJumbo();
364     }
365     if (rxMaxMTU != -1) {
366         rx_SetMaxMTU(rxMaxMTU);
367     }
368     rx_SetRxDeadTime(50);
369
370     memset(HostAddress, 0, sizeof(HostAddress));
371     initialize_dstats();
372
373     sc[0] = rxnull_NewServerSecurityObject();
374     sc[1] = (struct rx_securityClass *)0;
375     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
376
377     tservice =
378         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server", sc, 3,
379                       VL_ExecuteRequest);
380     if (tservice == (struct rx_service *)0) {
381         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
382         exit(3);
383     }
384     rx_SetMinProcs(tservice, 2);
385     if (lwps < 4)
386         lwps = 4;
387     rx_SetMaxProcs(tservice, lwps);
388
389     tservice =
390         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
391                       RXSTATS_ExecuteRequest);
392     if (tservice == (struct rx_service *)0) {
393         printf("vlserver: Could not create rpc stats rx service\n");
394         exit(3);
395     }
396     rx_SetMinProcs(tservice, 2);
397     rx_SetMaxProcs(tservice, 4);
398
399     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
400         if (i > 0)
401             strcat(commandLine, " ");
402         strcat(commandLine, argv[i]);
403     }
404     ViceLog(0,
405             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
406     printf("%s\n", cml_version_number); /* Goes to the log */
407
408     /* allow super users to manage RX statistics */
409     rx_SetRxStatUserOk(vldb_rxstat_userok);
410
411     rx_StartServer(1);          /* Why waste this idle process?? */
412
413     return 0; /* not reachable */
414 }