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