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