printf-sanity-20090317
[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-%lu\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            char *fileName = argv[++index];
219
220            osi_audit_file(fileName);
221         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
222             rx_enablePeerRPCStats();
223         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
224             rx_enableProcessRPCStats();
225 #ifndef AFS_NT40_ENV
226         } else if (strcmp(argv[index], "-syslog") == 0) {
227             /* set syslog logging flag */
228             serverLogSyslog = 1;
229         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
230             serverLogSyslog = 1;
231             serverLogSyslogFacility = atoi(argv[index] + 8);
232 #endif
233         } else {
234             /* support help flag */
235 #ifndef AFS_NT40_ENV
236             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
237                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
238                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
239                    "[-syslog[=FACILITY]] "
240                    "[-enable_peer_stats] [-enable_process_stats] "
241                    "[-help]\n");
242 #else
243             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
244                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
245                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
246                    "[-enable_peer_stats] [-enable_process_stats] "
247                    "[-help]\n");
248 #endif
249             fflush(stdout);
250             exit(0);
251         }
252     }
253
254     /* Initialize dirpaths */
255     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
256 #ifdef AFS_NT40_ENV
257         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
258 #endif
259         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
260                 argv[0]);
261         exit(2);
262     }
263     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
264
265 #ifndef AFS_NT40_ENV
266     serverLogSyslogTag = "vlserver";
267 #endif
268     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
269     SetupLogSignals();
270
271     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
272     if (!tdir) {
273         printf
274             ("vlserver: can't open configuration files in dir %s, giving up.\n",
275              AFSDIR_SERVER_ETC_DIRPATH);
276         exit(1);
277     }
278 #ifdef AFS_NT40_ENV
279     /* initialize winsock */
280     if (afs_winsockInit() < 0) {
281         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
282         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
283         exit(1);
284     }
285 #endif
286     /* get this host */
287     gethostname(hostname, sizeof(hostname));
288     th = gethostbyname(hostname);
289     if (!th) {
290         printf("vlserver: couldn't get address of this host (%s).\n",
291                hostname);
292         exit(1);
293     }
294     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
295
296 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
297     signal(SIGXCPU, CheckSignal_Signal);
298 #endif
299     /* get list of servers */
300     code =
301         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
302                                     &clones);
303     if (code) {
304         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
305         exit(2);
306     }
307
308     vldb_confdir = tdir;        /* Preserve our configuration dir */
309     /* rxvab no longer supported */
310     memset(&tkey, 0, sizeof(tkey));
311
312     if (noAuth)
313         afsconf_SetNoAuthFlag(tdir, 1);
314
315     if (rxBind) {
316         afs_int32 ccode;
317 #ifndef AFS_NT40_ENV
318         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
319             AFSDIR_SERVER_NETINFO_FILEPATH) {
320             char reason[1024];
321             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
322                                   ADDRSPERSITE, reason,
323                                   AFSDIR_SERVER_NETINFO_FILEPATH,
324                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
325         } else 
326 #endif  
327         {
328             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
329         }
330         if (ccode == 1) {
331             host = SHostAddrs[0];
332             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
333         }
334     }
335
336     ubik_nBuffers = 512;
337     ubik_CRXSecurityProc = afsconf_ClientAuth;
338     ubik_CRXSecurityRock = (char *)tdir;
339     ubik_SRXSecurityProc = afsconf_ServerAuth;
340     ubik_SRXSecurityRock = (char *)tdir;
341     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
342     ubik_CheckRXSecurityRock = (char *)tdir;
343     code =
344         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
345                               vl_dbaseName, &VL_dbase);
346     if (code) {
347         printf("vlserver: Ubik init failed with code %d\n", code);
348         exit(2);
349     }
350     if (!rxJumbograms) {
351         rx_SetNoJumbo();
352     }
353     if (rxMaxMTU != -1) {
354         rx_SetMaxMTU(rxMaxMTU);
355     }
356     rx_SetRxDeadTime(50);
357
358     memset(HostAddress, 0, sizeof(HostAddress));
359     initialize_dstats();
360
361     sc[0] = rxnull_NewServerSecurityObject();
362     sc[1] = (struct rx_securityClass *)0;
363     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
364
365     tservice =
366         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server", sc, 3,
367                       VL_ExecuteRequest);
368     if (tservice == (struct rx_service *)0) {
369         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
370         exit(3);
371     }
372     rx_SetMinProcs(tservice, 2);
373     if (lwps < 4)
374         lwps = 4;
375     rx_SetMaxProcs(tservice, lwps);
376
377     if (rxkadDisableDotCheck) {
378         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
379                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
380     }
381
382     tservice =
383         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
384                       RXSTATS_ExecuteRequest);
385     if (tservice == (struct rx_service *)0) {
386         printf("vlserver: Could not create rpc stats rx service\n");
387         exit(3);
388     }
389     rx_SetMinProcs(tservice, 2);
390     rx_SetMaxProcs(tservice, 4);
391
392     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
393         if (i > 0)
394             strcat(commandLine, " ");
395         strcat(commandLine, argv[i]);
396     }
397     ViceLog(0,
398             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
399     printf("%s\n", cml_version_number); /* Goes to the log */
400
401     /* allow super users to manage RX statistics */
402     rx_SetRxStatUserOk(vldb_rxstat_userok);
403
404     rx_StartServer(1);          /* Why waste this idle process?? */
405
406     return 0; /* not reachable */
407 }