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