rxbind-for-servers-20060620
[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 #include <sys/stat.h>
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef AFS_NT40_ENV
24 #include <winsock2.h>
25 #include <WINNT/afsevent.h>
26 #endif
27 #ifdef HAVE_SYS_FILE_H
28 #include <sys/file.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_NETDB_H
32 #include <netdb.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #include <stdio.h>
38
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46
47 #include <rx/xdr.h>
48 #include <rx/rx.h>
49 #include <rx/rx_globals.h>
50 #include <afs/cellconfig.h>
51 #include <afs/keys.h>
52 #include <afs/auth.h>
53 #include <lock.h>
54 #include <ubik.h>
55 #include <afs/afsutil.h>
56 #include "vlserver.h"
57
58
59 #define MAXLWP 16
60 const char *vl_dbaseName;
61 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
62 int lwps = 9;
63
64 struct vldstats dynamic_statistics;
65 struct ubik_dbase *VL_dbase;
66 afs_uint32 HostAddress[MAXSERVERID + 1];
67 extern int afsconf_CheckAuth();
68 extern int afsconf_ServerAuth();
69
70 static CheckSignal();
71 int LogLevel = 0;
72 int smallMem = 0;
73 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
74 int rxMaxMTU = -1;
75 afs_int32 rxBind = 0;
76
77 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
78 afs_uint32 SHostAddrs[ADDRSPERSITE];
79
80 static void
81 CheckSignal_Signal()
82 {
83     IOMGR_SoftSig(CheckSignal, 0);
84 }
85
86 static
87 CheckSignal()
88 {
89     register int i, errorcode;
90     struct ubik_trans *trans;
91
92     if (errorcode =
93         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
94         return errorcode;
95     VLog(0, ("Dump name hash table out\n"));
96     for (i = 0; i < HASHSIZE; i++) {
97         HashNDump(trans, i);
98     }
99     VLog(0, ("Dump id hash table out\n"));
100     for (i = 0; i < HASHSIZE; i++) {
101         HashIdDump(trans, i);
102     }
103     return (ubik_EndTrans(trans));
104 }                               /*CheckSignal */
105
106
107 /* Initialize the stats for the opcodes */
108 void
109 initialize_dstats()
110 {
111     int i;
112
113     dynamic_statistics.start_time = (afs_uint32) time(0);
114     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
115         dynamic_statistics.requests[i] = 0;
116         dynamic_statistics.aborts[i] = 0;
117     }
118 }
119
120 /* check whether caller is authorized to manage RX statistics */
121 int
122 vldb_rxstat_userok(call)
123      struct rx_call *call;
124 {
125     return afsconf_SuperUser(vldb_confdir, call, NULL);
126 }
127
128 /* Main server module */
129
130 #include "AFS_component_version_number.c"
131
132 main(argc, argv)
133      int argc;
134      char **argv;
135 {
136     register afs_int32 code;
137     afs_int32 myHost;
138     struct rx_service *tservice;
139     struct rx_securityClass *sc[3];
140     extern int VL_ExecuteRequest();
141     extern int RXSTATS_ExecuteRequest();
142     struct afsconf_dir *tdir;
143     struct ktc_encryptionKey tkey;
144     struct afsconf_cell info;
145     struct hostent *th;
146     char hostname[VL_MAXNAMELEN];
147     int noAuth = 0, index, i;
148     extern int rx_extraPackets;
149     char commandLine[150];
150     char clones[MAXHOSTSPERCELL];
151     afs_uint32 host = ntohl(INADDR_ANY);
152
153 #ifdef  AFS_AIX32_ENV
154     /*
155      * The following signal action for AIX is necessary so that in case of a 
156      * crash (i.e. core is generated) we can include the user's data section 
157      * in the core dump. Unfortunately, by default, only a partial core is
158      * generated which, in many cases, isn't too useful.
159      */
160     struct sigaction nsa;
161
162     rx_extraPackets = 100;      /* should be a switch, I guess... */
163     sigemptyset(&nsa.sa_mask);
164     nsa.sa_handler = SIG_DFL;
165     nsa.sa_flags = SA_FULLDUMP;
166     sigaction(SIGABRT, &nsa, NULL);
167     sigaction(SIGSEGV, &nsa, NULL);
168 #endif
169     osi_audit_init();
170
171     /* Parse command line */
172     for (index = 1; index < argc; index++) {
173         if (strcmp(argv[index], "-noauth") == 0) {
174             noAuth = 1;
175
176         } else if (strcmp(argv[index], "-p") == 0) {
177             lwps = atoi(argv[++index]);
178             if (lwps > MAXLWP) {
179                 printf("Warning: '-p %d' is too big; using %d instead\n",
180                        lwps, MAXLWP);
181                 lwps = MAXLWP;
182             }
183
184         } else if (strcmp(argv[index], "-nojumbo") == 0) {
185             rxJumbograms = 0;
186
187         } else if (strcmp(argv[index], "-rxbind") == 0) {
188             rxBind = 1;
189
190         } else if (!strcmp(argv[index], "-rxmaxmtu")) {
191             if ((index + 1) >= argc) {
192                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
193                 return -1; 
194             }
195             rxMaxMTU = atoi(argv[++i]);
196             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
197                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
198                 printf("rxMaxMTU %d% invalid; must be between %d-%d\n",
199                        rxMaxMTU, RX_MIN_PACKET_SIZE, 
200                        RX_MAX_PACKET_DATA_SIZE);
201                 return -1;
202             }
203
204         } else if (strcmp(argv[index], "-smallmem") == 0) {
205             smallMem = 1;
206
207         } else if (strcmp(argv[index], "-trace") == 0) {
208             extern char rxi_tracename[80];
209             strcpy(rxi_tracename, argv[++index]);
210
211        } else if (strcmp(argv[index], "-auditlog") == 0) {
212            int tempfd, flags;
213            FILE *auditout;
214            char oldName[MAXPATHLEN];
215            char *fileName = argv[++index];
216
217 #ifndef AFS_NT40_ENV
218            struct stat statbuf;
219
220            if ((lstat(fileName, &statbuf) == 0) 
221                && (S_ISFIFO(statbuf.st_mode))) {
222                flags = O_WRONLY | O_NONBLOCK;
223            } else 
224 #endif
225            {
226                strcpy(oldName, fileName);
227                strcat(oldName, ".old");
228                renamefile(fileName, oldName);
229                flags = O_WRONLY | O_TRUNC | O_CREAT;
230            }
231            tempfd = open(fileName, flags, 0666);
232            if (tempfd > -1) {
233                auditout = fdopen(tempfd, "a");
234                if (auditout) {
235                    osi_audit_file(auditout);
236                } else
237                    printf("Warning: auditlog %s not writable, ignored.\n", fileName);
238            } else
239                printf("Warning: auditlog %s not writable, ignored.\n", fileName);
240         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
241             rx_enablePeerRPCStats();
242         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
243             rx_enableProcessRPCStats();
244 #ifndef AFS_NT40_ENV
245         } else if (strcmp(argv[index], "-syslog") == 0) {
246             /* set syslog logging flag */
247             serverLogSyslog = 1;
248         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
249             serverLogSyslog = 1;
250             serverLogSyslogFacility = atoi(argv[index] + 8);
251 #endif
252         } else {
253             /* support help flag */
254 #ifndef AFS_NT40_ENV
255             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
256                    "[-rxmaxmtu <bytes>] [-rxbind] "
257                    "[-auditlog <log path>] "
258                    "[-syslog[=FACILITY]] "
259                    "[-enable_peer_stats] [-enable_process_stats] "
260                    "[-help]\n");
261 #else
262             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
263                    "[-rxmaxmtu <bytes>] [-rxbind] "
264                    "[-auditlog <log path>] "
265                    "[-enable_peer_stats] [-enable_process_stats] "
266                    "[-help]\n");
267 #endif
268             fflush(stdout);
269             exit(0);
270         }
271     }
272
273     /* Initialize dirpaths */
274     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
275 #ifdef AFS_NT40_ENV
276         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
277 #endif
278         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
279                 argv[0]);
280         exit(2);
281     }
282     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
283
284 #ifndef AFS_NT40_ENV
285     serverLogSyslogTag = "vlserver";
286 #endif
287     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
288     SetupLogSignals();
289
290     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
291     if (!tdir) {
292         printf
293             ("vlserver: can't open configuration files in dir %s, giving up.\n",
294              AFSDIR_SERVER_ETC_DIRPATH);
295         exit(1);
296     }
297 #ifdef AFS_NT40_ENV
298     /* initialize winsock */
299     if (afs_winsockInit() < 0) {
300         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
301         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
302         exit(1);
303     }
304 #endif
305     /* get this host */
306     gethostname(hostname, sizeof(hostname));
307     th = gethostbyname(hostname);
308     if (!th) {
309         printf("vlserver: couldn't get address of this host (%s).\n",
310                hostname);
311         exit(1);
312     }
313     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
314
315 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
316     signal(SIGXCPU, CheckSignal_Signal);
317 #endif
318     /* get list of servers */
319     code =
320         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
321                                     &clones);
322     if (code) {
323         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
324         exit(2);
325     }
326
327     vldb_confdir = tdir;        /* Preserve our configuration dir */
328     /* rxvab no longer supported */
329     memset(&tkey, 0, sizeof(tkey));
330
331     if (noAuth)
332         afsconf_SetNoAuthFlag(tdir, 1);
333
334     if (rxBind) {
335         afs_int32 ccode;
336 #ifndef AFS_NT40_ENV
337         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
338             AFSDIR_SERVER_NETINFO_FILEPATH) {
339             char reason[1024];
340             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
341                                   ADDRSPERSITE, reason,
342                                   AFSDIR_SERVER_NETINFO_FILEPATH,
343                                   AFSDIR_SERVER_NETRESTRICT_FILEPATH);
344         } else 
345 #endif  
346         {
347             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
348         }
349         if (ccode == 1) {
350             host = SHostAddrs[0];
351             rx_InitHost(host, htons(AFSCONF_VLDBPORT));
352         }
353     }
354
355     ubik_nBuffers = 512;
356     ubik_CRXSecurityProc = afsconf_ClientAuth;
357     ubik_CRXSecurityRock = (char *)tdir;
358     ubik_SRXSecurityProc = afsconf_ServerAuth;
359     ubik_SRXSecurityRock = (char *)tdir;
360     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
361     ubik_CheckRXSecurityRock = (char *)tdir;
362     code =
363         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
364                               vl_dbaseName, &VL_dbase);
365     if (code) {
366         printf("vlserver: Ubik init failed with code %d\n", code);
367         exit(2);
368     }
369     if (!rxJumbograms) {
370         rx_SetNoJumbo();
371     }
372     if (rxMaxMTU != -1) {
373         rx_SetMaxMTU(rxMaxMTU);
374     }
375     rx_SetRxDeadTime(50);
376
377     memset(HostAddress, 0, sizeof(HostAddress));
378     initialize_dstats();
379
380     sc[0] = rxnull_NewServerSecurityObject();
381     sc[1] = (struct rx_securityClass *)0;
382     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
383
384     tservice =
385         rx_NewServiceHost(host, 0, USER_SERVICE_ID, "Vldb server", sc, 3,
386                       VL_ExecuteRequest);
387     if (tservice == (struct rx_service *)0) {
388         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
389         exit(3);
390     }
391     rx_SetMinProcs(tservice, 2);
392     if (lwps < 4)
393         lwps = 4;
394     rx_SetMaxProcs(tservice, lwps);
395
396     tservice =
397         rx_NewServiceHost(host, 0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
398                       RXSTATS_ExecuteRequest);
399     if (tservice == (struct rx_service *)0) {
400         printf("vlserver: Could not create rpc stats rx service\n");
401         exit(3);
402     }
403     rx_SetMinProcs(tservice, 2);
404     rx_SetMaxProcs(tservice, 4);
405
406     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
407         if (i > 0)
408             strcat(commandLine, " ");
409         strcat(commandLine, argv[i]);
410     }
411     ViceLog(0,
412             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
413     printf("%s\n", cml_version_number); /* Goes to the log */
414
415     /* allow super users to manage RX statistics */
416     rx_SetRxStatUserOk(vldb_rxstat_userok);
417
418     rx_StartServer(1);          /* Why waste this idle process?? */
419 }