winnt-build-cleanup-20030825
[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 #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 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #else
41 #ifdef HAVE_STRINGS_H
42 #include <strings.h>
43 #endif
44 #endif
45
46 #include <rx/xdr.h>
47 #include <rx/rx.h>
48 #include <rx/rx_globals.h>
49 #include <afs/cellconfig.h>
50 #include <afs/keys.h>
51 #include <afs/auth.h>
52 #include <lock.h>
53 #include <ubik.h>
54 #include <afs/afsutil.h>
55 #include "vlserver.h"
56
57
58 #define MAXLWP 16
59 const char *vl_dbaseName;
60 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
61 int lwps = 9;
62
63 struct vldstats dynamic_statistics;
64 struct ubik_dbase *VL_dbase;
65 afs_uint32 HostAddress[MAXSERVERID + 1];
66 extern int afsconf_CheckAuth();
67 extern int afsconf_ServerAuth();
68
69 extern afs_int32 ubik_lastYesTime;
70 extern afs_int32 ubik_nBuffers;
71
72 static CheckSignal();
73 int LogLevel = 0;
74 int smallMem = 0;
75 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
76
77 static void
78 CheckSignal_Signal()
79 {
80     IOMGR_SoftSig(CheckSignal, 0);
81 }
82
83 static
84 CheckSignal()
85 {
86     register int i, errorcode;
87     struct ubik_trans *trans;
88
89     if (errorcode =
90         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
91         return errorcode;
92     VLog(0, ("Dump name hash table out\n"));
93     for (i = 0; i < HASHSIZE; i++) {
94         HashNDump(trans, i);
95     }
96     VLog(0, ("Dump id hash table out\n"));
97     for (i = 0; i < HASHSIZE; i++) {
98         HashIdDump(trans, i);
99     }
100     return (ubik_EndTrans(trans));
101 }                               /*CheckSignal */
102
103
104 /* Initialize the stats for the opcodes */
105 void
106 initialize_dstats()
107 {
108     int i;
109
110     dynamic_statistics.start_time = (afs_uint32) time(0);
111     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
112         dynamic_statistics.requests[i] = 0;
113         dynamic_statistics.aborts[i] = 0;
114     }
115 }
116
117 /* check whether caller is authorized to manage RX statistics */
118 int
119 vldb_rxstat_userok(call)
120      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 main(argc, argv)
130      int argc;
131      char **argv;
132 {
133     register afs_int32 code;
134     afs_int32 myHost;
135     struct rx_service *tservice;
136     struct rx_securityClass *sc[3];
137     extern int VL_ExecuteRequest();
138     extern int RXSTATS_ExecuteRequest();
139     struct afsconf_dir *tdir;
140     struct ktc_encryptionKey tkey;
141     struct afsconf_cell info;
142     struct hostent *th;
143     char hostname[VL_MAXNAMELEN];
144     int noAuth = 0, index, i;
145     extern int rx_extraPackets;
146     char commandLine[150];
147     char clones[MAXHOSTSPERCELL];
148
149 #ifdef  AFS_AIX32_ENV
150     /*
151      * The following signal action for AIX is necessary so that in case of a 
152      * crash (i.e. core is generated) we can include the user's data section 
153      * in the core dump. Unfortunately, by default, only a partial core is
154      * generated which, in many cases, isn't too useful.
155      */
156     struct sigaction nsa;
157
158     rx_extraPackets = 100;      /* should be a switch, I guess... */
159     sigemptyset(&nsa.sa_mask);
160     nsa.sa_handler = SIG_DFL;
161     nsa.sa_flags = SA_FULLDUMP;
162     sigaction(SIGABRT, &nsa, NULL);
163     sigaction(SIGSEGV, &nsa, NULL);
164 #endif
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], "-smallmem") == 0) {
182             smallMem = 1;
183
184         } else if (strcmp(argv[index], "-trace") == 0) {
185             extern char rxi_tracename[80];
186             strcpy(rxi_tracename, argv[++index]);
187
188         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
189             rx_enablePeerRPCStats();
190         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
191             rx_enableProcessRPCStats();
192 #ifndef AFS_NT40_ENV
193         } else if (strcmp(argv[index], "-syslog") == 0) {
194             /* set syslog logging flag */
195             serverLogSyslog = 1;
196         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
197             serverLogSyslog = 1;
198             serverLogSyslogFacility = atoi(argv[index] + 8);
199 #endif
200         } else {
201             /* support help flag */
202 #ifndef AFS_NT40_ENV
203             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
204                    "[-syslog[=FACILITY]] "
205                    "[-enable_peer_stats] [-enable_process_stats] "
206                    "[-help]\n");
207 #else
208             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
209                    "[-enable_peer_stats] [-enable_process_stats] "
210                    "[-help]\n");
211 #endif
212             fflush(stdout);
213             exit(0);
214         }
215     }
216
217     /* Initialize dirpaths */
218     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
219 #ifdef AFS_NT40_ENV
220         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
221 #endif
222         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
223                 argv[0]);
224         exit(2);
225     }
226     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
227
228 #ifndef AFS_NT40_ENV
229     serverLogSyslogTag = "vlserver";
230 #endif
231     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
232     SetupLogSignals();
233
234     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
235     if (!tdir) {
236         printf
237             ("vlserver: can't open configuration files in dir %s, giving up.\n",
238              AFSDIR_SERVER_ETC_DIRPATH);
239         exit(1);
240     }
241 #ifdef AFS_NT40_ENV
242     /* initialize winsock */
243     if (afs_winsockInit() < 0) {
244         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
245         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
246         exit(1);
247     }
248 #endif
249     /* get this host */
250     gethostname(hostname, sizeof(hostname));
251     th = gethostbyname(hostname);
252     if (!th) {
253         printf("vlserver: couldn't get address of this host (%s).\n",
254                hostname);
255         exit(1);
256     }
257     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
258
259 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
260     signal(SIGXCPU, CheckSignal_Signal);
261 #endif
262     /* get list of servers */
263     code =
264         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
265                                     &clones);
266     if (code) {
267         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
268         exit(2);
269     }
270
271     vldb_confdir = tdir;        /* Preserve our configuration dir */
272     /* rxvab no longer supported */
273     memset(&tkey, 0, sizeof(tkey));
274
275     if (noAuth)
276         afsconf_SetNoAuthFlag(tdir, 1);
277
278     ubik_nBuffers = 512;
279     ubik_CRXSecurityProc = afsconf_ClientAuth;
280     ubik_CRXSecurityRock = (char *)tdir;
281     ubik_SRXSecurityProc = afsconf_ServerAuth;
282     ubik_SRXSecurityRock = (char *)tdir;
283     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
284     ubik_CheckRXSecurityRock = (char *)tdir;
285     code =
286         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
287                               vl_dbaseName, &VL_dbase);
288     if (code) {
289         printf("vlserver: Ubik init failed with code %d\n", code);
290         exit(2);
291     }
292     if (!rxJumbograms) {
293         rx_SetNoJumbo();
294     }
295     rx_SetRxDeadTime(50);
296
297     memset(HostAddress, 0, sizeof(HostAddress));
298     initialize_dstats();
299
300     sc[0] = rxnull_NewServerSecurityObject();
301     sc[1] = (struct rx_securityClass *)0;
302     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
303     tservice =
304         rx_NewService(0, USER_SERVICE_ID, "Vldb server", sc, 3,
305                       VL_ExecuteRequest);
306     if (tservice == (struct rx_service *)0) {
307         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
308         exit(3);
309     }
310     rx_SetMinProcs(tservice, 2);
311     if (lwps < 4)
312         lwps = 4;
313     rx_SetMaxProcs(tservice, lwps);
314
315     tservice =
316         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
317                       RXSTATS_ExecuteRequest);
318     if (tservice == (struct rx_service *)0) {
319         printf("vlserver: Could not create rpc stats rx service\n");
320         exit(3);
321     }
322     rx_SetMinProcs(tservice, 2);
323     rx_SetMaxProcs(tservice, 4);
324
325     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
326         if (i > 0)
327             strcat(commandLine, " ");
328         strcat(commandLine, argv[i]);
329     }
330     ViceLog(0,
331             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
332     printf("%s\n", cml_version_number); /* Goes to the log */
333
334     /* allow super users to manage RX statistics */
335     rx_SetRxStatUserOk(vldb_rxstat_userok);
336
337     rx_StartServer(1);          /* Why waste this idle process?? */
338 }