Rename printf cast helpers and clean up format string warnings
[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 <rx/rxstat_prototypes.h>
43 #include <afs/cellconfig.h>
44 #include <afs/keys.h>
45 #include <afs/auth.h>
46 #include <afs/audit.h>
47 #include <lock.h>
48 #include <ubik.h>
49 #include <afs/afsutil.h>
50 #include "vlserver.h"
51 #include "vlserver_internal.h"
52
53 #define MAXLWP 16
54 const char *vl_dbaseName;
55 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
56 int lwps = 9;
57
58 struct vldstats dynamic_statistics;
59 struct ubik_dbase *VL_dbase;
60 afs_uint32 HostAddress[MAXSERVERID + 1];
61
62 static void *CheckSignal(void*);
63 int LogLevel = 0;
64 int smallMem = 0;
65 int rxJumbograms = 0;           /* default is to not send and receive jumbo grams */
66 int rxMaxMTU = -1;
67 afs_int32 rxBind = 0;
68 int rxkadDisableDotCheck = 0;
69 int debuglevel = 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(int unused)
76 {
77 #if defined(AFS_PTHREAD_ENV)
78     CheckSignal(0);
79 #else
80     IOMGR_SoftSig(CheckSignal, 0);
81 #endif
82 }
83
84 static void *
85 CheckSignal(void *unused)
86 {
87     register int i, errorcode;
88     struct ubik_trans *trans;
89
90     if ((errorcode =
91         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE)))
92         return (void *)errorcode;
93     VLog(0, ("Dump name hash table out\n"));
94     for (i = 0; i < HASHSIZE; i++) {
95         HashNDump(trans, i);
96     }
97     VLog(0, ("Dump id hash table out\n"));
98     for (i = 0; i < HASHSIZE; i++) {
99         HashIdDump(trans, i);
100     }
101     return ((void *)ubik_EndTrans(trans));
102 }                               /*CheckSignal */
103
104
105 /* Initialize the stats for the opcodes */
106 void
107 initialize_dstats(void)
108 {
109     int i;
110
111     dynamic_statistics.start_time = (afs_uint32) time(0);
112     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
113         dynamic_statistics.requests[i] = 0;
114         dynamic_statistics.aborts[i] = 0;
115     }
116 }
117
118 /* check whether caller is authorized to manage RX statistics */
119 int
120 vldb_rxstat_userok(struct rx_call *call)
121 {
122     return afsconf_SuperUser(vldb_confdir, call, NULL);
123 }
124
125 /* Main server module */
126
127 #include "AFS_component_version_number.c"
128
129 int
130 main(int argc, char **argv)
131 {
132     register afs_int32 code;
133     afs_int32 myHost;
134     struct rx_service *tservice;
135     struct rx_securityClass *sc[3];
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     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         } 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-%lu\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            char *fileName = argv[++index];
213
214            osi_audit_file(fileName);
215         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
216             rx_enablePeerRPCStats();
217         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
218             rx_enableProcessRPCStats();
219 #ifndef AFS_NT40_ENV
220         } else if (strcmp(argv[index], "-syslog") == 0) {
221             /* set syslog logging flag */
222             serverLogSyslog = 1;
223         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
224             serverLogSyslog = 1;
225             serverLogSyslogFacility = atoi(argv[index] + 8);
226 #endif
227         } else {
228             /* support help flag */
229 #ifndef AFS_NT40_ENV
230             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
231                    "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
232                    "[-auditlog <log path>] [-jumbo] [-d <debug level>] "
233                    "[-syslog[=FACILITY]] "
234                    "[-enable_peer_stats] [-enable_process_stats] "
235                    "[-help]\n");
236 #else
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                    "[-enable_peer_stats] [-enable_process_stats] "
241                    "[-help]\n");
242 #endif
243             fflush(stdout);
244             exit(0);
245         }
246     }
247
248     /* Initialize dirpaths */
249     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
250 #ifdef AFS_NT40_ENV
251         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
252 #endif
253         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
254                 argv[0]);
255         exit(2);
256     }
257     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
258
259 #ifndef AFS_NT40_ENV
260     serverLogSyslogTag = "vlserver";
261 #endif
262     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
263     SetupLogSignals();
264
265     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
266     if (!tdir) {
267         printf
268             ("vlserver: can't open configuration files in dir %s, giving up.\n",
269              AFSDIR_SERVER_ETC_DIRPATH);
270         exit(1);
271     }
272 #ifdef AFS_NT40_ENV
273     /* initialize winsock */
274     if (afs_winsockInit() < 0) {
275         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
276         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
277         exit(1);
278     }
279 #endif
280     /* get this host */
281     gethostname(hostname, sizeof(hostname));
282     th = gethostbyname(hostname);
283     if (!th) {
284         printf("vlserver: couldn't get address of this host (%s).\n",
285                hostname);
286         exit(1);
287     }
288     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
289
290 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
291     signal(SIGXCPU, CheckSignal_Signal);
292 #endif
293     /* get list of servers */
294     code =
295         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
296                                     &clones);
297     if (code) {
298         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
299         exit(2);
300     }
301
302     vldb_confdir = tdir;        /* Preserve our configuration dir */
303     /* rxvab no longer supported */
304     memset(&tkey, 0, sizeof(tkey));
305
306     if (noAuth)
307         afsconf_SetNoAuthFlag(tdir, 1);
308
309     if (rxBind) {
310         afs_int32 ccode;
311 #ifndef AFS_NT40_ENV
312         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
313             AFSDIR_SERVER_NETINFO_FILEPATH) {
314             char reason[1024];
315             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
316                                   ADDRSPERSITE, reason,
317                                   AFSDIR_SERVER_NETINFO_FILEPATH,
318                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
319         } else 
320 #endif  
321         {
322             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
323         }
324         if (ccode == 1) {
325             host = SHostAddrs[0];
326             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
327         }
328     }
329
330     ubik_nBuffers = 512;
331     ubik_CRXSecurityProc = afsconf_ClientAuth;
332     ubik_CRXSecurityRock = (char *)tdir;
333     ubik_SRXSecurityProc = afsconf_ServerAuth;
334     ubik_SRXSecurityRock = (char *)tdir;
335     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
336     ubik_CheckRXSecurityRock = (char *)tdir;
337     code =
338         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, clones,
339                               vl_dbaseName, &VL_dbase);
340     if (code) {
341         printf("vlserver: Ubik init failed with code %d\n", code);
342         exit(2);
343     }
344     if (!rxJumbograms) {
345         rx_SetNoJumbo();
346     }
347     if (rxMaxMTU != -1) {
348         rx_SetMaxMTU(rxMaxMTU);
349     }
350     rx_SetRxDeadTime(50);
351
352     memset(HostAddress, 0, sizeof(HostAddress));
353     initialize_dstats();
354
355     sc[0] = rxnull_NewServerSecurityObject();
356     sc[1] = (struct rx_securityClass *)0;
357     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
358
359     tservice =
360         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server", sc, 3,
361                       VL_ExecuteRequest);
362     if (tservice == (struct rx_service *)0) {
363         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
364         exit(3);
365     }
366     rx_SetMinProcs(tservice, 2);
367     if (lwps < 4)
368         lwps = 4;
369     rx_SetMaxProcs(tservice, lwps);
370
371     if (rxkadDisableDotCheck) {
372         rx_SetSecurityConfiguration(tservice, RXS_CONFIG_FLAGS,
373                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
374     }
375
376     tservice =
377         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
378                       RXSTATS_ExecuteRequest);
379     if (tservice == (struct rx_service *)0) {
380         printf("vlserver: Could not create rpc stats rx service\n");
381         exit(3);
382     }
383     rx_SetMinProcs(tservice, 2);
384     rx_SetMaxProcs(tservice, 4);
385
386     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
387         if (i > 0)
388             strcat(commandLine, " ");
389         strcat(commandLine, argv[i]);
390     }
391     ViceLog(0,
392             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
393     printf("%s\n", cml_version_number); /* Goes to the log */
394
395     /* allow super users to manage RX statistics */
396     rx_SetRxStatUserOk(vldb_rxstat_userok);
397
398     rx_StartServer(1);          /* Why waste this idle process?? */
399
400     return 0; /* not reachable */
401 }