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