ticket-2618-patches-20031207
[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 static CheckSignal();
70 int LogLevel = 0;
71 int smallMem = 0;
72 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
73
74 static void
75 CheckSignal_Signal()
76 {
77     IOMGR_SoftSig(CheckSignal, 0);
78 }
79
80 static
81 CheckSignal()
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 (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
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     /* Parse command line */
163     for (index = 1; index < argc; index++) {
164         if (strcmp(argv[index], "-noauth") == 0) {
165             noAuth = 1;
166
167         } else if (strcmp(argv[index], "-p") == 0) {
168             lwps = atoi(argv[++index]);
169             if (lwps > MAXLWP) {
170                 printf("Warning: '-p %d' is too big; using %d instead\n",
171                        lwps, MAXLWP);
172                 lwps = MAXLWP;
173             }
174
175         } else if (strcmp(argv[index], "-nojumbo") == 0) {
176             rxJumbograms = 0;
177
178         } else if (strcmp(argv[index], "-smallmem") == 0) {
179             smallMem = 1;
180
181         } else if (strcmp(argv[index], "-trace") == 0) {
182             extern char rxi_tracename[80];
183             strcpy(rxi_tracename, argv[++index]);
184
185         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
186             rx_enablePeerRPCStats();
187         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
188             rx_enableProcessRPCStats();
189 #ifndef AFS_NT40_ENV
190         } else if (strcmp(argv[index], "-syslog") == 0) {
191             /* set syslog logging flag */
192             serverLogSyslog = 1;
193         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
194             serverLogSyslog = 1;
195             serverLogSyslogFacility = atoi(argv[index] + 8);
196 #endif
197         } else {
198             /* support help flag */
199 #ifndef AFS_NT40_ENV
200             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
201                    "[-syslog[=FACILITY]] "
202                    "[-enable_peer_stats] [-enable_process_stats] "
203                    "[-help]\n");
204 #else
205             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
206                    "[-enable_peer_stats] [-enable_process_stats] "
207                    "[-help]\n");
208 #endif
209             fflush(stdout);
210             exit(0);
211         }
212     }
213
214     /* Initialize dirpaths */
215     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
216 #ifdef AFS_NT40_ENV
217         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
218 #endif
219         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
220                 argv[0]);
221         exit(2);
222     }
223     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
224
225 #ifndef AFS_NT40_ENV
226     serverLogSyslogTag = "vlserver";
227 #endif
228     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
229     SetupLogSignals();
230
231     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
232     if (!tdir) {
233         printf
234             ("vlserver: can't open configuration files in dir %s, giving up.\n",
235              AFSDIR_SERVER_ETC_DIRPATH);
236         exit(1);
237     }
238 #ifdef AFS_NT40_ENV
239     /* initialize winsock */
240     if (afs_winsockInit() < 0) {
241         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
242         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
243         exit(1);
244     }
245 #endif
246     /* get this host */
247     gethostname(hostname, sizeof(hostname));
248     th = gethostbyname(hostname);
249     if (!th) {
250         printf("vlserver: couldn't get address of this host (%s).\n",
251                hostname);
252         exit(1);
253     }
254     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
255
256 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
257     signal(SIGXCPU, CheckSignal_Signal);
258 #endif
259     /* get list of servers */
260     code =
261         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
262                                     &clones);
263     if (code) {
264         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
265         exit(2);
266     }
267
268     vldb_confdir = tdir;        /* Preserve our configuration dir */
269     /* rxvab no longer supported */
270     memset(&tkey, 0, sizeof(tkey));
271
272     if (noAuth)
273         afsconf_SetNoAuthFlag(tdir, 1);
274
275     ubik_nBuffers = 512;
276     ubik_CRXSecurityProc = afsconf_ClientAuth;
277     ubik_CRXSecurityRock = (char *)tdir;
278     ubik_SRXSecurityProc = afsconf_ServerAuth;
279     ubik_SRXSecurityRock = (char *)tdir;
280     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
281     ubik_CheckRXSecurityRock = (char *)tdir;
282     code =
283         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
284                               vl_dbaseName, &VL_dbase);
285     if (code) {
286         printf("vlserver: Ubik init failed with code %d\n", code);
287         exit(2);
288     }
289     if (!rxJumbograms) {
290         rx_SetNoJumbo();
291     }
292     rx_SetRxDeadTime(50);
293
294     memset(HostAddress, 0, sizeof(HostAddress));
295     initialize_dstats();
296
297     sc[0] = rxnull_NewServerSecurityObject();
298     sc[1] = (struct rx_securityClass *)0;
299     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
300     tservice =
301         rx_NewService(0, USER_SERVICE_ID, "Vldb server", sc, 3,
302                       VL_ExecuteRequest);
303     if (tservice == (struct rx_service *)0) {
304         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
305         exit(3);
306     }
307     rx_SetMinProcs(tservice, 2);
308     if (lwps < 4)
309         lwps = 4;
310     rx_SetMaxProcs(tservice, lwps);
311
312     tservice =
313         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
314                       RXSTATS_ExecuteRequest);
315     if (tservice == (struct rx_service *)0) {
316         printf("vlserver: Could not create rpc stats rx service\n");
317         exit(3);
318     }
319     rx_SetMinProcs(tservice, 2);
320     rx_SetMaxProcs(tservice, 4);
321
322     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
323         if (i > 0)
324             strcat(commandLine, " ");
325         strcat(commandLine, argv[i]);
326     }
327     ViceLog(0,
328             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
329     printf("%s\n", cml_version_number); /* Goes to the log */
330
331     /* allow super users to manage RX statistics */
332     rx_SetRxStatUserOk(vldb_rxstat_userok);
333
334     rx_StartServer(1);          /* Why waste this idle process?? */
335 }