bb4ee2109fc6771290257fcb59d7f8b00b69170c
[openafs.git] / src / viced / viced.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 /*  viced.c     - File Server main loop                                  */
11 /*                                                                       */
12 /*  Date: 5/1/85                                                         */
13 /*                                                                       */
14 /*  Function    - This routine has the initialization code for           */
15 /*                FileServer II                                          */
16 /*                                                                       */
17 /* ********************************************************************** */
18
19 #include <afsconfig.h>
20 #include <afs/param.h>
21
22 RCSID("$Header$");
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
29 #include <sys/stat.h>
30 #ifdef AFS_NT40_ENV
31 #include <fcntl.h>
32 #include <io.h>
33 #include <windows.h>
34 #include <WINNT/afsevent.h>
35 #else
36 #include <sys/file.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <unistd.h>     /* sysconf() */
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #else
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47 #endif
48
49 #ifndef ITIMER_REAL
50 #include <sys/time.h>
51 #endif /* ITIMER_REAL */
52 #include <sys/resource.h>
53 #endif /* AFS_NT40_ENV */
54 #include <afs/stds.h>
55 #undef SHARED
56 #include <rx/xdr.h>
57 #include <afs/nfs.h>
58 #ifdef AFS_PTHREAD_ENV
59 #include <assert.h>
60 #else /* AFS_PTHREAD_ENV */
61 #include <afs/assert.h>
62 #endif /* AFS_PTHREAD_ENV */
63 #include <lwp.h>
64 #include <lock.h>
65 #include <afs/ptclient.h>
66 #include <afs/afsint.h>
67 #include <afs/vldbint.h>
68 #include <afs/errors.h>
69 #include <afs/ihandle.h>
70 #include <afs/vnode.h>
71 #include <afs/volume.h>
72 #include <afs/auth.h>
73 #include <afs/cellconfig.h>
74 #include <afs/acl.h>
75 #include <afs/prs_fs.h>
76 #include <rx/rx.h>
77 #include <rx/rxkad.h>
78 #include <afs/keys.h>
79 #include <afs/afs_args.h>
80 #include <afs/vlserver.h>
81 #include <afs/afsutil.h>
82 #include <afs/fileutil.h>
83 #ifndef AFS_NT40_ENV
84 #include <afs/netutils.h>
85 #endif
86 #include "viced.h"
87 #include "host.h"
88 #if defined(AFS_SGI_ENV)
89 #include "sys/schedctl.h"
90 #include "sys/lock.h"
91 #endif
92 #include <rx/rx_globals.h>
93
94
95 extern int      BreakVolumeCallBacks(), InitCallBack();
96 extern  int     LogLevel, etext;
97 extern afs_int32        BlocksSpare, PctSpare;
98
99 void            ShutDown();
100 static void     ClearXStatValues(), NewParms(), PrintCounters();
101 static void     ResetCheckDescriptors(void), ResetCheckSignal(void);
102 static int      CheckSignal();
103 static int      FiveMinuteCheckLWP(), HostCheckLWP();
104 extern  int     GetKeysFromToken();
105 extern int RXAFS_ExecuteRequest();
106 extern int RXSTATS_ExecuteRequest();
107
108 int eventlog = 0, rxlog = 0;
109 FILE *debugFile;
110 FILE * console = NULL;
111
112 #ifdef AFS_NT40_ENV
113 #define AFS_QUIETFS_ENV 1
114 #define NT_OPEN_MAX    1024 /* This is an arbitrary no. we came up with for 
115                              * now. We hope this will be replaced by a more
116                              * intelligent estimate later. */
117 #endif
118
119 int     SystemId;               /* ViceID of "Administrators"*/
120 int     SystemAnyUser;          /* Viceid of "System:AnyUser" */
121 prlist  SystemAnyUserCPS;       /* CPS for "system:AnyUser */
122 int     AnonymousID = 0;        /* ViceId of "Anonymous"*/
123 prlist  AnonCPS;                /* CPS for "Anonymous"*/
124
125 struct afsconf_dir *confDir;    /* Configuration dir object */
126
127 int     restartMode = RESTART_ORDINARY;
128
129 int Testing = 0; /* for ListViceInodes */
130
131 /*
132  * Home for the performance statistics.
133  */
134 struct afs_PerfStats afs_perfstats;
135
136 extern  int     LogLevel;
137 extern  int     Statistics;
138
139 int     timeout = 30;
140 int     SawSpare;
141 int     SawPctSpare;
142 int     debuglevel = 0;
143 int     printBanner = 0;
144 int     rxJumbograms = 1; /* default is to send and receive jumbograms. */
145 afs_int32       implicitAdminRights = PRSFS_LOOKUP;     /* The ADMINISTER right is 
146                                                    already implied */
147
148 int     stack = 24;
149 int     stackSize = 24000;
150 int     fiveminutes = 300;              /* 5 minutes.  Change this for debugging only */
151 int     CurrentConnections = 0;
152 int     hostaclRefresh = 7200;          /* refresh host clients' acls every 2 hrs */
153 #if defined(AFS_SGI_ENV)
154 int     SawLock;
155 #endif
156 time_t  StartTime;
157
158 int     rxpackets = 150;        /* 100 */
159 int     nSmallVns = 400;        /* 200 */
160 int     large = 400;            /* 200 */
161 int     volcache = 400;         /* 400 */
162 int     numberofcbs = 60000;    /* 60000 */
163 int     lwps = 9;               /* 6 */
164 int     buffs = 90;             /* 70 */
165 int     novbc = 0;              /* Enable Volume Break calls */
166 int     busy_threshold = 600;
167 int     udpBufSize = 0;         /* UDP buffer size for receive*/
168
169 struct timeval  tp;
170
171 /*
172  * FileServer's name and IP address, both network byte order and
173  * host byte order.
174  */
175 #define ADDRSPERSITE 16     /* Same global is in rx/rx_user.c */
176
177 char FS_HostName[128] = "localhost";
178 afs_uint32 FS_HostAddr_NBO;
179 afs_uint32 FS_HostAddr_HBO;
180 afs_uint32 FS_HostAddrs[ADDRSPERSITE], FS_HostAddr_cnt = 0, FS_registered=0;
181 /* All addresses in FS_HostAddrs are in NBO */
182 afsUUID FS_HostUUID;
183
184 static ParseArgs();
185 static FlagMsg();
186
187 /*
188  * Home for the performance statistics.
189  */
190
191 /* DEBUG HACK */
192 static CheckDescriptors()
193 {
194 #ifndef AFS_NT40_ENV
195     struct stat status;
196     register int tsize = getdtablesize();
197     register int i;
198     for (i = 0; i<tsize; i++) {
199         if (fstat(i, &status) != -1) {
200             printf("%d: dev %x, inode %u, length %u, type/mode %x\n",
201                    i, status.st_dev, status.st_ino,
202                    status.st_size, status.st_mode);
203         }
204     }
205     fflush(stdout);
206     ResetCheckDescriptors();
207 #endif
208 } /*CheckDescriptors*/
209
210
211 #ifdef AFS_PTHREAD_ENV
212 void CheckSignal_Signal(x)       {CheckSignal(0);}
213 void ShutDown_Signal(x)          {ShutDown(0);}
214 void CheckDescriptors_Signal(x)  {CheckDescriptors(0);}
215 #else /* AFS_PTHREAD_ENV */
216 void CheckSignal_Signal(x)       {IOMGR_SoftSig(CheckSignal, 0);}
217 void ShutDown_Signal(x)          {IOMGR_SoftSig(ShutDown, 0);}
218 void CheckDescriptors_Signal(x)  {IOMGR_SoftSig(CheckDescriptors, 0);}
219 #endif /* AFS_PTHREAD_ENV */
220
221 /* check whether caller is authorized to manage RX statistics */
222 int fs_rxstat_userok(call)
223     struct rx_call *call;
224 {
225     return afsconf_SuperUser(confDir, call, NULL);
226 }
227
228 static void ResetCheckSignal(void)
229 {
230 #ifdef  AFS_HPUX_ENV
231     signal(SIGPOLL, CheckSignal_Signal);
232 #else
233 #ifdef AFS_NT40_ENV
234     signal(SIGUSR2, CheckSignal_Signal);
235 #else
236     signal(SIGXCPU, CheckSignal_Signal);
237 #endif
238 #endif
239 }
240
241 static void ResetCheckDescriptors(void)
242 {
243 #ifndef AFS_NT40_ENV
244     signal(SIGTERM, CheckDescriptors_Signal);
245 #endif
246 }
247
248
249 /* proc called by rxkad module to get a key */
250 static int get_key(arock, akvno, akey)
251     char *akey;
252     char *arock;
253     register afs_int32 akvno;
254
255 {
256     /* find the key */
257     static struct afsconf_key tkey;
258     register afs_int32 code;
259
260     if (!confDir) {
261         ViceLog(0, ("conf dir not open\n"));
262         return 1;
263     }
264     code = afsconf_GetKey(confDir, akvno, tkey.key);
265     if (code)
266         return code;
267     memcpy(akey, tkey.key, sizeof(tkey.key));
268     return 0;
269
270 } /*get_key*/
271
272 #ifndef AFS_NT40_ENV
273 int viced_syscall(a3, a4, a5)
274 afs_uint32 a3, a4;
275 void * a5;
276 {
277   afs_uint32 rcode;
278   void (*old)();
279         
280 #ifndef AFS_LINUX20_ENV
281   old = (void (*)())signal(SIGSYS, SIG_IGN);    
282 #endif
283   rcode = syscall (AFS_SYSCALL, 28 /* AFSCALL_CALL */, a3, a4, a5);
284 #ifndef AFS_LINUX20_ENV
285   signal(SIGSYS, old);  
286 #endif
287
288   return rcode;
289 }
290 #endif
291
292 #if !defined(AFS_NT40_ENV)
293 #include "AFS_component_version_number.c"
294 #endif /* !AFS_NT40_ENV */
295
296 main(argc, argv)
297     int argc;
298     char * argv[];
299
300 {
301     int     i;
302     afs_int32    code;
303     FILE   *file;
304     char tbuffer[32];
305     struct rx_securityClass *sc[4];
306     struct rx_service *tservice;
307 #ifdef AFS_PTHREAD_ENV
308     pthread_t parentPid, serverPid;
309     pthread_attr_t tattr;
310     AFS_SIGSET_DECL;
311 #else /* AFS_PTHREAD_ENV */
312     PROCESS parentPid, serverPid;
313 #endif /* AFS_PTHREAD_ENV */
314     struct hostent *he;
315     int minVnodesRequired;      /* min size of vnode cache */
316 #ifndef AFS_NT40_ENV
317     struct rlimit rlim;         /* max number of open file descriptors */
318 #endif
319     int curLimit;
320
321 #ifdef  AFS_AIX32_ENV
322     struct sigaction nsa;
323     
324     sigemptyset(&nsa.sa_mask);
325     nsa.sa_handler = SIG_DFL;
326     nsa.sa_flags = SA_FULLDUMP;
327     sigaction(SIGABRT, &nsa, NULL);
328     sigaction(SIGSEGV, &nsa, NULL);
329 #endif
330
331     /* Initialize dirpaths */
332     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
333 #ifdef AFS_NT40_ENV
334         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
335 #endif
336         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
337         exit(2);
338     }
339
340 #ifndef AFS_QUIETFS_ENV
341     console = fopen("/dev/console","w");
342 #endif
343
344     if(ParseArgs(argc,argv)) {
345         FlagMsg();
346         exit(-1);
347     }
348
349 #ifdef AFS_PTHREAD_ENV
350     assert(pthread_mutex_init(&fileproc_glock_mutex, NULL) == 0);
351 #endif /* AFS_PTHREAD_ENV */
352
353 #ifdef AFS_SGI_VNODE_GLUE
354     if (afs_init_kernel_config(-1) <0) {
355         printf("Can't determine NUMA configuration, not starting fileserver.\n");
356         exit(1);
357     }
358 #endif
359     confDir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
360     if (!confDir) {
361         fprintf(stderr, "Unable to open config directory %s\n",
362                 AFSDIR_SERVER_ETC_DIRPATH);
363         exit(-1);
364     }
365
366     NewParms(1);
367
368     /* Open FileLog on stdout, stderr, fd 1 and fd2 (for perror), sigh. */
369     OpenLog(AFSDIR_SERVER_FILELOG_FILEPATH);
370     SetupLogSignals(); 
371
372     if (SawSpare && SawPctSpare) {
373         ViceLog(0, ("Both -spare and -pctspare specified, exiting.\n"));
374         exit(-1);
375     }
376
377 #ifdef AFS_SGI_XFS_IOPS_ENV
378     ViceLog(0, ("XFS/EFS File server starting\n"));
379 #else
380     ViceLog(0, ("File server starting\n"));
381 #endif
382
383     /* install signal handlers for controlling the fileserver process */
384     ResetCheckSignal();  /* set CheckSignal_Signal() sig handler */
385     ResetCheckDescriptors();  /* set CheckDescriptors_Signal() sig handler */
386
387 #if defined(AFS_SGI_ENV)
388     /* give this guy a non-degrading priority so help busy servers */
389     schedctl(NDPRI, 0, NDPNORMMAX);
390     if (SawLock)
391         plock(PROCLOCK);
392 #else
393 #ifndef AFS_NT40_ENV
394     nice(-5); /* TODO: */
395 #endif
396 #endif
397     assert(DInit(buffs) == 0);
398  
399 #ifdef AFS_NT40_ENV
400     if (afs_winsockInit()<0) {
401         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
402         ViceLog(0, ("File server failed to intialize winsock.\n"));
403         exit(1);
404     }
405 #endif
406     CheckAdminName();
407
408     /* if we support more than 16 threads, then we better have the ability 
409     ** to keep open a large number of files simultaneously 
410     */
411 #if     defined(AFS_AIX_ENV) && !defined(AFS_AIX42_ENV)
412     curLimit = OPEN_MAX;        /* for pre AIX 4.2 systems */
413 #elif defined(AFS_NT40_ENV)
414     curLimit = NT_OPEN_MAX;     /* open file descriptor limit on NT */
415 #else
416
417     curLimit = 0;               /* the number of open file descriptors */
418     code     = getrlimit(RLIMIT_NOFILE, &rlim);
419     if (code == 0) {
420         curLimit      = rlim.rlim_cur;
421         rlim.rlim_cur = rlim.rlim_max;
422         code = setrlimit(RLIMIT_NOFILE, &rlim);
423         if ( code == 0 ) 
424             curLimit  = rlim.rlim_max;
425     }
426     if ( code != 0 )
427         ViceLog(0, ("Failed to increase open file limit, using default\n"));
428
429 #endif /* defined(AFS_AIX_ENV) && !defined(AFS_AIX42_ENV) */
430
431     curLimit -= 32;             /* leave a slack of 32 file descriptors */
432     if ( lwps > curLimit ) {
433         if ( curLimit > 0)
434             lwps = curLimit; 
435         else if ( lwps > 16 )
436             lwps = 16;            /* default to a maximum of 16 threads */
437         ViceLog(0, ("The system supports a max of %d open files and we are starting %d threads\n", curLimit, lwps));
438     }
439
440
441 #ifndef AFS_PTHREAD_ENV
442     assert(LWP_InitializeProcessSupport(LWP_MAX_PRIORITY - 2, &parentPid) == LWP_SUCCESS);
443 #endif /* !AFS_PTHREAD_ENV */
444
445     /* Initialize volume support */
446     if (!novbc) {
447         V_BreakVolumeCallbacks = BreakVolumeCallBacks;
448     }
449
450     /* initialize libacl routines */
451     acl_Initialize(ACL_VERSION);
452
453     /* initialize RX support */
454 #ifndef AFS_NT40_ENV
455     rxi_syscallp = viced_syscall;
456 #endif
457     rx_extraPackets = rxpackets;
458     rx_extraQuota = 4;  /* for outgoing prserver calls from R threads */
459     rx_SetBusyThreshold(busy_threshold, VBUSY);
460     rx_SetCallAbortThreshold(10);
461     rx_SetConnAbortThreshold(10);
462     stackSize = lwps * 4000;
463     if (stackSize < 32000)
464         stackSize = 32000;
465     else if (stackSize > 44000)
466         stackSize = 44000;
467 #if    defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV) || defined(AFS_SGI51_ENV)
468     rx_SetStackSize(1, stackSize);      
469 #endif
470     if ( udpBufSize )
471         rx_SetUdpBufSize(udpBufSize);/* set the UDP buffer size for receive */
472     if (rx_Init((int)htons(7000))<0) {
473         ViceLog(0, ("Cannot initialize RX\n"));
474         exit(1);
475     }
476     if (!rxJumbograms) {
477         /* Don't send and don't allow 3.4 clients to send jumbograms. */
478         rx_SetNoJumbo();
479     }
480     rx_GetIFInfo();
481     rx_SetRxDeadTime(30);
482     sc[0] = rxnull_NewServerSecurityObject();
483     sc[1] = 0; /* rxvab_NewServerSecurityObject(key1, 0) */
484     sc[2] = rxkad_NewServerSecurityObject (rxkad_clear, NULL,
485                                            get_key, NULL);
486     sc[3] = rxkad_NewServerSecurityObject (rxkad_crypt, NULL,
487                                            get_key, NULL);
488     tservice = rx_NewService
489         (/* port */ 0, /* service id */ 1, /*service name */ "AFS",
490          /* security classes */ sc, /* numb sec classes */ 4,
491          RXAFS_ExecuteRequest);
492     if (!tservice) {
493         ViceLog(0, ("Failed to initialize RX, probably two servers running.\n"));
494         exit(-1);
495     }
496     rx_SetDestroyConnProc(tservice, h_FreeConnection);
497     rx_SetMinProcs(tservice, 3);
498     rx_SetMaxProcs(tservice, lwps);
499     rx_SetCheckReach(tservice, 1);
500
501     tservice = rx_NewService(0,  RX_STATS_SERVICE_ID, "rpcstats", sc, 4, RXSTATS_ExecuteRequest);
502     if (!tservice) {
503         ViceLog(0, ("Failed to initialize rpc stat service.\n"));
504         exit(-1);
505     }
506     rx_SetMinProcs(tservice, 2);
507     rx_SetMaxProcs(tservice, 4);
508
509     /*
510      * Enable RX hot threads, which allows the listener thread to trade
511      * places with an idle thread and moves the context switch from listener
512      * to worker out of the critical path.
513      */
514     rx_EnableHotThread();
515
516     /* Some rx debugging */
517     if (rxlog || eventlog) {
518         debugFile = fopen("rx_dbg", "w");
519         if (rxlog) rx_debugFile = debugFile;
520         if (eventlog) rxevent_debugFile = debugFile;
521     }
522
523     h_InitHostPackage();  /* set up local cellname and realmname */
524     InitCallBack(numberofcbs);
525     ClearXStatValues();
526
527     code = InitVL();
528     if (code) {
529         ViceLog(0,("Fatal error in library initialization, exiting!!\n"));
530         exit(1);
531     }
532
533     code = InitPR();
534     if (code) {
535         ViceLog(0,("Fatal error in protection initialization, exiting!!\n"));
536         exit(1);
537     }
538
539     /* allow super users to manage RX statistics */
540     rx_SetRxStatUserOk(fs_rxstat_userok);
541
542     rx_StartServer(0);  /* now start handling requests */
543
544     /* we ensure that there is enough space in the vnode buffer to satisfy
545     ** requests from all concurrent threads. 
546     ** the maximum number of vnodes used by a single thread at any one time
547     ** is three ( "link" uses three vnodes simultaneously, one vLarge and
548     ** two vSmall for linking files and two vLarge and one vSmall for linking
549     ** files  ) : dhruba 
550     */
551     minVnodesRequired = 2 * lwps + 1;     
552     if ( minVnodesRequired > nSmallVns ) {
553         nSmallVns = minVnodesRequired;
554         ViceLog(0, ("Overriding -s command line parameter with %d\n",
555                     nSmallVns));
556     }
557     if ( minVnodesRequired > large ) {
558         large = minVnodesRequired;
559         ViceLog(0, ("Overriding -l command line parameter with %d\n", large));
560     }
561
562     /* We now do this after getting the listener up and running, so that client
563        connections don't timeout (maybe) if a file server is restarted, since it
564        will be available "real soon now".  Worry about whether we can satisfy the 
565        calls in the volume package itself.
566      */
567     if (VInitVolumePackage(fileServer,large,nSmallVns,0, volcache)) {
568         ViceLog(0, ("Shutting down: errors encountered initializing volume package\n"));
569         VShutdown();
570         exit(1);
571     }
572
573     /*
574      * We are done calling fopen/fdopen. It is safe to use a large
575      * of the file descriptor cache.
576      */
577     ih_UseLargeCache();
578
579 #ifdef AFS_PTHREAD_ENV
580     assert(pthread_attr_init(&tattr) == 0);
581     assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
582     /* Block signals in the threads */
583     AFS_SIGSET_CLEAR();
584     assert(pthread_create(&serverPid, &tattr, (void *)FiveMinuteCheckLWP, &fiveminutes) == 0);
585     assert(pthread_create(&serverPid, &tattr, (void *)HostCheckLWP, &fiveminutes) == 0);
586     AFS_SIGSET_RESTORE();
587 #else /* AFS_PTHREAD_ENV */
588     assert(LWP_CreateProcess(FiveMinuteCheckLWP, stack*1024, LWP_MAX_PRIORITY - 2,
589             &fiveminutes, "FiveMinuteChecks", &serverPid) == LWP_SUCCESS);
590             
591     assert(LWP_CreateProcess(HostCheckLWP, stack*1024, LWP_MAX_PRIORITY - 2,
592             &fiveminutes, "HostCheck", &serverPid) == LWP_SUCCESS);
593 #endif /* AFS_PTHREAD_ENV */
594
595     TM_GetTimeOfDay(&tp, 0);
596
597 #ifndef AFS_QUIETFS_ENV
598     if (console != NULL) {
599         fprintf(console, "File server has started at %s\r",
600                 afs_ctime(&tp.tv_sec, tbuffer, sizeof(tbuffer)));
601     }
602 #endif
603
604     /*
605      * Figure out the FileServer's name and primary address.
606      */
607     ViceLog(0, ("Getting FileServer name...\n"));
608     code = gethostname(FS_HostName, 64);
609     if (code) {
610         ViceLog(0, ("gethostname() failed\n"));
611     }
612     ViceLog(0, ("FileServer host name is '%s'\n", FS_HostName));
613
614     ViceLog(0, ("Getting FileServer address...\n"));
615     he = gethostbyname(FS_HostName);
616     if (!he) {
617         ViceLog(0, ("Can't find address for FileServer '%s'\n", FS_HostName));
618     }
619     else {
620         char hoststr[16];
621         memcpy(&FS_HostAddr_NBO, he->h_addr, 4);
622         afs_inet_ntoa_r(FS_HostAddr_NBO, hoststr);
623         FS_HostAddr_HBO = ntohl(FS_HostAddr_NBO);
624         ViceLog(0,("FileServer %s has address %s (0x%x or 0x%x in host byte order)\n",
625                    FS_HostName, hoststr, FS_HostAddr_NBO, FS_HostAddr_HBO));
626     }
627
628     /* Install handler to catch the shutdown signal */
629     signal(SIGQUIT, ShutDown_Signal); /* bosserver assumes SIGQUIT shutdown */
630
631     ViceLog(0,("File Server started %s",
632                afs_ctime(&tp.tv_sec, tbuffer, sizeof(tbuffer))));
633 #if FS_STATS_DETAILED
634     afs_FullPerfStats.det.epoch.tv_sec = StartTime = tp.tv_sec;
635 #endif
636 #ifdef AFS_PTHREAD_ENV
637     while(1) {
638         sleep(1000); /* long time */
639     }
640 #else /* AFS_PTHREAD_ENV */
641     assert(LWP_WaitProcess(&parentPid) == LWP_SUCCESS);
642 #endif /* AFS_PTHREAD_ENV */
643 }
644
645
646 /* This LWP does things roughly every 5 minutes */
647 static FiveMinuteCheckLWP()
648
649 {
650     static int msg  = 0;
651     char tbuffer[32];
652
653     ViceLog(1, ("Starting five minute check process\n"));
654     while (1) {
655 #ifdef AFS_PTHREAD_ENV
656         sleep(fiveminutes);
657 #else /* AFS_PTHREAD_ENV */
658         IOMGR_Sleep(fiveminutes);
659 #endif /* AFS_PTHREAD_ENV */
660
661         /* close the log so it can be removed */
662         ReOpenLog(AFSDIR_SERVER_FILELOG_FILEPATH); /* don't trunc, just append */
663         ViceLog(2, ("Cleaning up timed out callbacks\n"));
664         if(CleanupTimedOutCallBacks())
665             ViceLog(5,("Timed out callbacks deleted\n"));
666         ViceLog(2, ("Set disk usage statistics\n"));
667         VSetDiskUsage();
668         if (FS_registered == 1) Do_VLRegisterRPC();
669         if(printBanner && (++msg&1)) { /* Every 10 minutes */
670             time_t now = FT_ApproxTime();
671             if (console != NULL) {
672 #ifndef AFS_QUIETFS_ENV
673                 fprintf(console,"File server is running at %s\r",
674                         afs_ctime(&now, tbuffer, sizeof(tbuffer)));
675 #endif /* AFS_QUIETFS_ENV */
676                 ViceLog(2, ("File server is running at %s\n",
677                         afs_ctime(&now, tbuffer, sizeof(tbuffer))));
678             }
679         }
680     }
681 } /*FiveMinuteCheckLWP*/
682
683
684 /* This LWP does host checks every 5 minutes:  it should not be used for
685  * other 5 minute activities because it may be delayed by timeouts when
686  * it probes the workstations
687  */
688 static HostCheckLWP()
689
690 {
691     ViceLog(1, ("Starting Host check process\n"));
692     while(1) {
693 #ifdef AFS_PTHREAD_ENV
694         sleep(fiveminutes);
695 #else /* AFS_PTHREAD_ENV */
696         IOMGR_Sleep(fiveminutes);
697 #endif /* AFS_PTHREAD_ENV */
698         ViceLog(2, ("Checking for dead venii & clients\n"));
699         h_CheckHosts();
700     }
701 } /*HostCheckLWP*/
702
703
704 #define MAXADMINNAME 64
705 char adminName[MAXADMINNAME];
706
707 CheckAdminName()
708
709 {
710     int             fd = 0;
711     struct stat     status;
712     
713     if ((stat("/AdminName", &status)) ||        /* if file does not exist */
714         (status.st_size <= 0) ||                /* or it is too short */
715         (status.st_size >= (MAXADMINNAME)) ||   /* or it is too long */
716         !(fd = open("/AdminName", O_RDONLY, 0))) {      /* or the open fails */
717         strcpy(adminName, "System:Administrators");     /* use the default name */
718     }
719     else {
720         read(fd, adminName, status.st_size);    /* use name from the file */
721     }
722     if (fd)
723         close(fd);      /* close fd if it was opened */
724
725 } /*CheckAdminName*/
726
727
728 /*------------------------------------------------------------------------
729  * PRIVATE ClearXStatValues
730  *
731  * Description:
732  *      Initialize all of the values collected via the xstat
733  *      interface.
734  *
735  * Arguments:
736  *      None.
737  *
738  * Returns:
739  *      Nothing.
740  *
741  * Environment:
742  *      Must be called during File Server initialization.
743  *
744  * Side Effects:
745  *      As advertised.
746  *------------------------------------------------------------------------*/
747
748 static void ClearXStatValues()
749
750 { /*ClearXStatValues*/
751
752     struct fs_stats_opTimingData *opTimeP;      /*Ptr to timing struct*/
753     struct fs_stats_xferData *opXferP;          /*Ptr to xfer struct*/
754     int i;                                      /*Loop counter*/
755
756     /*
757      * Zero all xstat-related structures.
758      */
759     memset((char *)(&afs_perfstats), 0, sizeof(struct afs_PerfStats));
760 #if FS_STATS_DETAILED
761     memset((char *)(&afs_FullPerfStats), 0, sizeof(struct fs_stats_FullPerfStats));
762
763     /*
764      * That's not enough.  We have to set reasonable minima for
765      * time and xfer values in the detailed stats.
766      */
767     opTimeP = &(afs_FullPerfStats.det.rpcOpTimes[0]);
768     for (i = 0; i < FS_STATS_NUM_RPC_OPS; i++, opTimeP++)
769         opTimeP->minTime.tv_sec = 999999;
770
771     opXferP = &(afs_FullPerfStats.det.xferOpTimes[0]);
772     for (i = 0; i < FS_STATS_NUM_XFER_OPS; i++, opXferP++) {
773         opXferP->minTime.tv_sec = 999999;
774         opXferP->minBytes = 999999999;
775     }
776
777     /*
778      * There's more.  We have to set our unique system identifier, as
779      * declared in param.h.  If such a thing is not defined, we bitch
780      * and declare ourselves to be an unknown system type.
781      */
782 #ifdef SYS_NAME_ID
783     afs_perfstats.sysname_ID = SYS_NAME_ID;
784 #else
785 #ifndef AFS_NT40_ENV
786     ViceLog(0, ("Sys name ID constant not defined in param.h!!\n"));
787     ViceLog(0, ("[Choosing ``undefined'' sys name ID.\n"));
788 #endif
789     afs_perfstats.sysname_ID = SYS_NAME_ID_UNDEFINED;
790 #endif /* SYS_NAME_ID */
791 #endif
792
793 } /*ClearXStatValues*/
794
795
796 static void PrintCounters()
797
798 {
799     int dirbuff, dircall, dirio;
800     struct timeval  tpl;
801     int workstations, activeworkstations, delworkstations;
802     int processSize = 0;
803     char tbuffer[32];
804
805     TM_GetTimeOfDay(&tpl, 0);
806     Statistics = 1;
807     ViceLog(0, ("Vice was last started at %s\n",
808                 afs_ctime(&StartTime, tbuffer, sizeof(tbuffer))));
809
810     VPrintCacheStats();
811     VPrintDiskStats();
812     DStat(&dirbuff, &dircall, &dirio);
813     ViceLog(0,("With %d directory buffers; %d reads resulted in %d read I/Os\n",
814             dirbuff, dircall, dirio));
815     rx_PrintStats(stderr);
816     h_PrintStats();
817     PrintCallBackStats();
818 #ifdef AFS_NT40_ENV
819     processSize = -1; /* TODO: */
820 #else
821     processSize = (int)((long) sbrk(0) >> 10);
822 #endif
823     ViceLog(0,("There are %d connections, process size %d\n",  CurrentConnections, processSize));
824     h_GetWorkStats(&workstations, &activeworkstations, &delworkstations,
825          tpl.tv_sec-15*60);
826     ViceLog(0,
827             ("There are %d workstations, %d are active (req in < 15 mins), %d marked \"down\"\n",
828             workstations, activeworkstations, delworkstations));
829     Statistics = 0;
830
831 } /*PrintCounters*/
832
833
834
835 static CheckSignal()
836
837 {
838     if (FS_registered > 0)  {
839         /*
840          * We have proper ip addresses; tell the vlserver what we got; the following
841          * routine will do the proper reporting for us
842          */
843         Do_VLRegisterRPC();
844     }
845     h_DumpHosts();
846     h_PrintClients();
847     DumpCallBackState();
848     PrintCounters();
849     ResetCheckSignal();
850
851 } /*CheckSignal*/
852
853 void ShutDownAndCore(dopanic)
854 int dopanic;
855 {
856     time_t now = time(0);
857     char *tstr;
858     char tbuffer[32];
859
860     ViceLog(0, ("Shutting down file server at %s",
861                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
862     if (dopanic) 
863         ViceLog(0, ("ABNORMAL SHUTDOWN, see core file.\n"));
864 #ifndef AFS_QUIETFS_ENV
865     if (console != NULL) {
866         fprintf(console,"File server restart/shutdown received at %s\r",
867                 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
868     }
869 #endif
870     DFlush();
871     PrintCounters();
872
873     /* do not allows new reqests to be served from now on, all new requests
874        are returned with an error code of RX_RESTARTING ( transient failure ) */
875     rx_SetRxTranquil();                 /* dhruba */
876     VShutdown();
877
878     if (debugFile) {
879         rx_PrintStats(debugFile);
880         fflush(debugFile);
881     }
882     if (console != NULL) {
883         now = time(0);
884         if (dopanic) {
885 #ifndef AFS_QUIETFS_ENV
886             fprintf(console, "File server has terminated abnormally at %s\r",
887                     afs_ctime(&now, tbuffer, sizeof(tbuffer)));
888 #endif
889             ViceLog(0, ("File server has terminated abnormally at %s\n",
890                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
891         } else {
892 #ifndef AFS_QUIETFS_ENV
893             fprintf(console, "File server has terminated normally at %s\r",
894                     afs_ctime(&now, tbuffer, sizeof(tbuffer)));
895 #endif
896             ViceLog(0, ("File server has terminated normally at %s\n",
897                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
898         }
899     }
900
901     exit(0);
902
903 } /*ShutDown*/
904
905 void ShutDown() /* backward compatibility */
906 {
907   ShutDownAndCore(DONTPANIC);
908 }
909
910
911 static FlagMsg()
912
913 {
914     char buffer[1024];
915
916         /* default supports help flag */
917
918     strcpy(buffer, "Usage: fileserver ");
919     strcat(buffer, "[-d <debug level>] ");
920     strcat(buffer, "[-p <number of processes>] ");
921     strcat(buffer, "[-spare <number of spare blocks>] ");
922     strcat(buffer, "[-pctspare <percentage spare>] ");
923     strcat(buffer, "[-b <buffers>] ");
924     strcat(buffer, "[-l <large vnodes>] ");
925     strcat(buffer, "[-s <small vnodes>] ");
926     strcat(buffer, "[-vc <volume cachesize>] ");
927     strcat(buffer, "[-w <call back wait interval>] ");
928     strcat(buffer, "[-cb <number of call backs>] ");
929     strcat(buffer, "[-banner (print banner every 10 minutes)] ");
930     strcat(buffer, "[-novbc (whole volume cbs disabled)] ");
931     strcat(buffer, "[-implicit <admin mode bits: rlidwka>] ");
932     strcat(buffer, "[-hr <number of hours between refreshing the host cps>] ");
933     strcat(buffer, "[-busyat <redirect clients when queue > n>] ");
934     strcat(buffer, "[-rxpck <number of rx extra packets>] ");
935     strcat(buffer, "[-rxdbg (enable rx debugging)] ");
936     strcat(buffer, "[-rxdbge (enable rxevent debugging)] ");
937 #ifdef  AFS_AIX32_ENV
938     strcat(buffer, "[-m <min percentage spare in partition>] ");
939 #endif
940 #if defined(AFS_SGI_ENV)
941     strcat(buffer, "[-lock (keep fileserver from swapping)] ");
942 #endif
943     strcat(buffer, "[-L (large server conf)] ");
944     strcat(buffer, "[-S (small server conf)] ");
945     strcat(buffer, "[-k <stack size>] ");
946     strcat(buffer, "[-realm <Kerberos realm name>] ");
947     strcat(buffer, "[-udpsize <size of socket buffer in bytes>] ");
948 /*   strcat(buffer, "[-enable_peer_stats] "); */
949 /*   strcat(buffer, "[-enable_process_stats] "); */
950     strcat(buffer, "[-help]\n");
951 /*
952     ViceLog(0, ("%s", buffer));
953 */
954
955         printf("%s",buffer);
956         fflush(stdout);
957
958 } /*FlagMsg*/
959
960
961 static afs_int32 ParseRights(arights)
962     char *arights;
963 {
964     afs_int32 mode = 0;
965     int i, len;
966     char tc;
967
968     if (!arights || !strcmp(arights, "")) {
969         printf("Missing list of mode bits on -implicit option\n");
970         return -1;
971     }
972     if (!strcmp(arights, "none"))
973       mode = 0;
974     else if (!strcmp(arights, "read")) 
975       mode = PRSFS_READ | PRSFS_LOOKUP;
976     else if (!strcmp(arights, "write"))
977       mode = PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | 
978              PRSFS_DELETE | PRSFS_WRITE | PRSFS_LOCK;
979     else if (!strcmp(arights, "all"))
980       mode = PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | 
981              PRSFS_DELETE | PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
982     else {
983         len = strlen(arights);
984         for(i=0;i<len;i++) {
985             tc = *arights++;
986             if (tc == 'r') mode |= PRSFS_READ;
987             else if (tc == 'l') mode |= PRSFS_LOOKUP;
988             else if (tc == 'i') mode |= PRSFS_INSERT;
989             else if (tc == 'd') mode |= PRSFS_DELETE;
990             else if (tc == 'w') mode |= PRSFS_WRITE;
991             else if (tc == 'k') mode |= PRSFS_LOCK;
992             else if (tc == 'a') mode |= PRSFS_ADMINISTER;
993             else if (tc == 'A') mode |= PRSFS_USR0;
994             else if (tc == 'B') mode |= PRSFS_USR1;
995             else if (tc == 'C') mode |= PRSFS_USR2;
996             else if (tc == 'D') mode |= PRSFS_USR3;
997             else if (tc == 'E') mode |= PRSFS_USR4;
998             else if (tc == 'F') mode |= PRSFS_USR5;
999             else if (tc == 'G') mode |= PRSFS_USR6;
1000             else if (tc == 'H') mode |= PRSFS_USR7;
1001             else {
1002                 printf("Illegal -implicit rights character '%c'.\n", tc);
1003                 return -1;
1004             }
1005         }
1006     }
1007     return mode;
1008 }
1009
1010 /*
1011  * Limit MAX_FILESERVER_THREAD by the system limit on the number of
1012  * pthreads (sysconf(_SC_THREAD_THREADS_MAX)), if applicable and
1013  * available.
1014  *
1015  * AIX:         sysconf() limit is real
1016  * HP-UX:       sysconf() limit is real
1017  * IRIX:        sysconf() limit is apparently NOT real -- too small
1018  * DUX:         sysconf() limit is apparently NOT real -- too big
1019  * Linux:       sysconf() limit is apparently NOT real -- too big
1020  * Solaris:     no sysconf() limit
1021  */
1022 static int
1023 max_fileserver_thread(void)
1024 {
1025 #if defined(AFS_PTHREAD_ENV)
1026 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV)
1027         long ans;
1028
1029         ans = sysconf(_SC_THREAD_THREADS_MAX);
1030         if (0 < ans && ans < MAX_FILESERVER_THREAD)
1031                 return (int) ans;
1032 #endif
1033 #endif /* defined(AFS_PTHREAD_ENV) */
1034         return MAX_FILESERVER_THREAD;
1035 }
1036
1037 static ParseArgs(argc, argv)
1038     int argc;
1039     char *argv[];
1040
1041 {
1042     int SawL=0, SawS=0, SawVC=0;
1043     int Sawrxpck = 0, Sawsmall=0, Sawlarge=0, Sawcbs=0, Sawlwps=0, Sawbufs=0;
1044     int Sawbusy=0;
1045     int i;
1046     int bufSize = 0;      /* temp variable to read in udp socket buf size*/
1047
1048     for (i = 1; i < argc; i++) {
1049         if (!strcmp(argv[i], "-d")) {
1050             debuglevel = atoi(argv[++i]);
1051             LogLevel = debuglevel;
1052         }
1053         else
1054             if (!strcmp(argv[i], "-banner")) {
1055                 printBanner = 1;
1056         } else
1057             if (!strcmp(argv[i], "-implicit")) {
1058                 implicitAdminRights = ParseRights(argv[++i]);
1059                 if (implicitAdminRights < 0) return implicitAdminRights;
1060         } else
1061             if (!strcmp(argv[i], "-L")) {
1062                 SawL = 1;
1063         } else
1064             if (!strcmp(argv[i], "-S")) {
1065                 SawS = 1;
1066         }
1067         else
1068             if (!strcmp(argv[i], "-p")) {
1069                 int lwps_max = max_fileserver_thread() - FILESERVER_HELPER_THREADS;
1070                 Sawlwps = 1;
1071                 lwps = atoi(argv[++i]);
1072                 if (lwps > lwps_max)
1073                     lwps = lwps_max;
1074                 else if (lwps < 6)
1075                     lwps = 6;
1076             }
1077         else
1078             if (!strcmp(argv[i], "-b")) {
1079                 Sawbufs = 1;
1080                 buffs = atoi(argv[++i]);
1081             }
1082         else
1083             if (!strcmp(argv[i], "-l")) {
1084                 Sawlarge = 1;
1085                 large = atoi(argv[++i]);
1086             }
1087         else
1088             if (!strcmp(argv[i], "-vc")) {
1089                 SawVC = 1;
1090                 volcache = atoi(argv[++i]);
1091             }
1092         else
1093             if (!strcmp(argv[i], "-novbc")) {
1094                 novbc = 1;
1095             }
1096         else
1097             if (!strcmp(argv[i], "-rxpck")) {
1098                 Sawrxpck = 1;
1099                 rxpackets = atoi(argv[++i]);
1100             }
1101         else
1102             if (!strcmp(argv[i], "-s")) {
1103                 Sawsmall = 1;
1104                 nSmallVns = atoi(argv[++i]);
1105             }
1106         else    
1107             if (!strcmp(argv[i], "-k"))
1108                 stack = atoi(argv[++i]);
1109 #if defined(AFS_SGI_ENV)
1110         else
1111             if (!strcmp(argv[i], "-lock")) {
1112                 SawLock = 1;
1113             }
1114 #endif
1115         else
1116             if (!strcmp(argv[i], "-spare")) {
1117                 BlocksSpare = atoi(argv[++i]);
1118                 SawSpare = 1;
1119             }
1120         else
1121             if (!strcmp(argv[i], "-pctspare")) {
1122                 PctSpare = atoi(argv[++i]);
1123                 BlocksSpare = 0;                /* has non-zero default */
1124                 SawPctSpare = 1;
1125             }
1126         else
1127             if (!strcmp(argv[i], "-w"))
1128                 fiveminutes = atoi(argv[++i]);
1129         else
1130             if (!strcmp(argv[i], "-hr")) {
1131                 int hr = atoi(argv[++i]);
1132                 if ((hr < 1) || (hr > 36)) {
1133                     printf("host acl refresh interval of %d hours is invalid; hours must be between 1 and 36\n\n",
1134                            hr);
1135                     return -1;
1136                 }                   
1137                 hostaclRefresh = hr*60*60;
1138         } else
1139             if (!strcmp(argv[i], "-rxdbg"))
1140                 rxlog = 1;
1141         else 
1142             if (!strcmp(argv[i], "-rxdbge"))
1143                 eventlog = 1;
1144         else
1145             if (!strcmp(argv[i], "-cb")) {
1146                 Sawcbs = 1;
1147                 numberofcbs = atoi(argv[++i]);
1148                 if ((numberofcbs < 10000) || (numberofcbs > 65535)) {
1149                     printf("number of cbs %d invalid; must be between 10000 and 65535\n",
1150                            numberofcbs);
1151                     return -1;
1152                 }
1153             }
1154             else
1155               if (!strcmp(argv[i], "-busyat")) {
1156                 Sawbusy = 1;
1157                 busy_threshold = atoi(argv[++i]);
1158                 if (busy_threshold < 10) {
1159                     printf("Busy threshold %d is too low, will compute default.\n",
1160                            busy_threshold);
1161                     Sawbusy = 0;
1162                 }
1163             }
1164 #ifdef  AFS_AIX32_ENV
1165         else
1166             if (!strcmp(argv[i], "-m")) {
1167                 extern int aixlow_water;
1168                  aixlow_water = atoi(argv[++i]);
1169                 if ((aixlow_water < 0) || (aixlow_water > 30)) {
1170                     printf("space reserved %d% invalid; must be between 0-30%\n", aixlow_water);
1171                     return -1;
1172                 }
1173             }
1174 #endif
1175         else
1176             if (!strcmp(argv[i], "-nojumbo")) {
1177                 rxJumbograms = 0;
1178             }
1179         else
1180             if (!strcmp(argv[i], "-realm")) {
1181                 extern char local_realm[AFS_REALM_SZ];
1182                 if (strlen(argv[++i]) >= AFS_REALM_SZ) {
1183                     printf("-realm argument must contain fewer than %d characters.\n",AFS_REALM_SZ);
1184                     return -1;
1185                 }
1186                 strncpy (local_realm, argv[i], AFS_REALM_SZ);
1187             }
1188         else
1189             if ( !strcmp(argv[i], "-udpsize")) {
1190                 if ( (i+1) >= argc ) {
1191                     printf("You have to specify -udpsize <integer value>\n");
1192                     return -1;
1193                 }
1194                 bufSize = atoi(argv[++i]);
1195                 if ( bufSize < rx_GetMinUdpBufSize() )
1196                     printf("Warning:udpsize %d is less than minimum %d; ignoring\n",
1197                                 bufSize, rx_GetMinUdpBufSize() );
1198                 else
1199                     udpBufSize = bufSize;
1200             }
1201         else
1202             if (!strcmp(argv[i], "-enable_peer_stats")) {
1203                 rx_enablePeerRPCStats();
1204             }
1205         else
1206             if (!strcmp(argv[i], "-enable_process_stats")) {
1207                 rx_enableProcessRPCStats();
1208             }
1209 #ifndef AFS_NT40_ENV
1210         else 
1211             if (strcmp(argv[i], "-syslog")==0) {
1212                 /* set syslog logging flag */
1213                 serverLogSyslog = 1;
1214             } 
1215         else 
1216             if (strncmp(argv[i], "-syslog=", 8)==0) {
1217                 serverLogSyslog = 1;
1218                 serverLogSyslogFacility = atoi(argv[i]+8);
1219             }
1220 #endif
1221         else {
1222             return(-1);
1223         }
1224     }
1225     if (SawS && SawL) {
1226         printf("Only one of -L, or -S must be specified\n");
1227         return -1;
1228     }
1229     if (SawS) {
1230         if (!Sawrxpck) rxpackets = 100;
1231         if (!Sawsmall) nSmallVns = 200;
1232         if (!Sawlarge) large = 200;
1233         if (!Sawcbs) numberofcbs = 20000;
1234         if (!Sawlwps) lwps = 6;
1235         if (!Sawbufs) buffs = 70;
1236         if (!SawVC) volcache = 200;
1237     }
1238     if (SawL) {
1239         if (!Sawrxpck) rxpackets = 200;
1240         if (!Sawsmall) nSmallVns = 600;
1241         if (!Sawlarge) large = 600;
1242         if (!Sawcbs) numberofcbs = 64000;
1243         if (!Sawlwps) lwps = 12;
1244         if (!Sawbufs) buffs = 120;
1245         if (!SawVC) volcache = 600;
1246     }
1247     if (!Sawbusy) 
1248         busy_threshold = 3*rxpackets/2;
1249
1250     return(0);
1251
1252 } /*ParseArgs*/
1253
1254
1255 #define MAXPARMS 15
1256
1257 static void NewParms(initializing)
1258     int initializing;
1259
1260 {
1261     static struct stat sbuf;
1262     register int i, fd;
1263     char *parms;
1264     char *argv[MAXPARMS];
1265     register int argc;
1266
1267     if (!(stat("/vice/file/parms",&sbuf))) {
1268         parms = (char *)malloc(sbuf.st_size);
1269         if(!parms) return;
1270         fd = open("parms", O_RDONLY, 0666);
1271         if(fd <= 0) {
1272             ViceLog(0, ("Open for parms failed with errno = %d\n", errno));
1273             return;
1274         }
1275
1276         i = read(fd, parms, sbuf.st_size);
1277         close(fd);
1278         if(i != sbuf.st_size) {
1279             if (i < 0 ) {
1280                 ViceLog(0, ("Read on parms failed with errno = %d\n", errno));
1281             } else {
1282                 ViceLog(0,
1283                         ("Read on parms failed; expected %d bytes but read %d\n",
1284                         sbuf.st_size, i));
1285             }
1286             free(parms);
1287             return;
1288         }
1289
1290         for (i = 0;i < MAXPARMS; argv[i++] = 0 );
1291         
1292         for (argc = i = 0; i < sbuf.st_size; i++) {
1293             if ((*(parms + i) != ' ') && (*(parms + i) != '\n')){
1294                 if(argv[argc] == 0) argv[argc] = (parms+i);
1295             }
1296             else {
1297                 *(parms + i) = '\0';
1298                 if(argv[argc] != 0) {
1299                     if(++argc == MAXPARMS) break;
1300                 }
1301                 while ((*(parms + i + 1) == ' ') || (*(parms + i + 1) == '\n'))
1302                     i++;
1303             }
1304         }
1305         if(ParseArgs(argc, argv) == 0) {
1306             ViceLog(0, ("Change parameters to:"));
1307         } else {
1308             ViceLog(0, ("Invalid parameter in:"));
1309         }
1310         for(i = 0; i < argc; i++) {
1311             ViceLog(0, (" %s", argv[i]));
1312         }
1313         ViceLog(0,("\n"));
1314         free(parms);
1315     }
1316     else
1317         if(!initializing)
1318             ViceLog(0, ("Received request to change parms but no parms file exists\n"));
1319
1320 } /*NewParms*/
1321
1322
1323 /* Miscellaneous routines */
1324 Die (msg)
1325     char *msg;
1326
1327 {
1328     ViceLog (0,("%s\n", msg));
1329     assert(0);
1330
1331 } /*Die*/
1332
1333
1334 InitPR()
1335
1336 {
1337     register code;
1338
1339     /*
1340      * If this fails, it's because something major is wrong, and is not
1341      * likely to be time dependent.
1342      */
1343     code = pr_Initialize(2, AFSDIR_SERVER_ETC_DIRPATH, 0);
1344     if (code != 0) {
1345         ViceLog(0, ("Couldn't initialize protection library; code=%d.\n", code));
1346         return code; 
1347     }
1348     SystemId = SYSADMINID;
1349     SystemAnyUser = ANYUSERID;
1350     SystemAnyUserCPS.prlist_len = 0;
1351     SystemAnyUserCPS.prlist_val = NULL;
1352     AnonCPS.prlist_len = 0;
1353     AnonCPS.prlist_val = NULL;
1354     while (1) {
1355         code = pr_GetCPS(SystemAnyUser, &SystemAnyUserCPS);
1356         if (code != 0) {
1357             ViceLog(0,
1358                     ("Couldn't get CPS for AnyUser, will try again in 30 seconds; code=%d.\n",
1359                     code));
1360             goto sleep;
1361         }
1362         code = pr_GetCPS(ANONYMOUSID,&AnonCPS);
1363         if (code != 0) {
1364             ViceLog(0,("Couldn't get Anonymous CPS, exiting; code=%d.\n", code));
1365             return -1;
1366         }
1367         AnonymousID = ANONYMOUSID;
1368         return 0;
1369 sleep:
1370 #ifdef AFS_PTHREAD_ENV
1371         sleep(30);
1372 #else /* AFS_PTHREAD_ENV */
1373         IOMGR_Sleep(30);
1374 #endif /* AFS_PTHREAD_ENV */
1375     }
1376 } /*InitPR*/
1377
1378 struct rx_connection *serverconns[MAXSERVERS];
1379 struct ubik_client *cstruct;
1380 afs_int32 vl_Initialize(confDir)
1381 char *confDir;
1382 {   afs_int32 code, scIndex = 0, i;
1383     struct afsconf_dir *tdir;
1384     struct rx_securityClass *sc;
1385     struct afsconf_cell info;
1386
1387     tdir = afsconf_Open(confDir);
1388     if (!tdir) {
1389         ViceLog(0, ("Could not open configuration directory (%s).\n", confDir));
1390         exit(1);
1391     }
1392     code = afsconf_ClientAuth(tdir, &sc, &scIndex);
1393     if (code) {
1394         ViceLog(0, ("Could not get security object for localAuth\n"));
1395         exit(1);
1396     }
1397     code = afsconf_GetCellInfo(tdir,NULL, AFSCONF_VLDBSERVICE, &info);
1398     if (info.numServers > MAXSERVERS) {
1399         ViceLog(0, ("vl_Initialize: info.numServers=%d (> MAXSERVERS=%d)\n",info.numServers, MAXSERVERS));
1400         exit(1);
1401     }
1402     for (i = 0;i<info.numServers;i++) 
1403         serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr, info.hostAddr[i].sin_port,
1404                                           USER_SERVICE_ID, sc, scIndex);
1405     code = ubik_ClientInit(serverconns, &cstruct);
1406     if (code) {
1407         ViceLog(0, ("vl_Initialize: ubik client init failed.\n"));
1408         return code;
1409     }
1410     return 0;
1411 }
1412
1413 #define SYSIDMAGIC      0x88aabbcc
1414 #define SYSIDVERSION    1
1415
1416 ReadSysIdFile() {
1417     afs_int32 fd, nentries, i;
1418     struct versionStamp vsn;
1419     struct stat     status;
1420     afsUUID uuid;
1421
1422     if ((stat(AFSDIR_SERVER_SYSID_FILEPATH, &status)) || (status.st_size <= 0)) {
1423         ViceLog(0, ("%s: doesn't exist\n", AFSDIR_SERVER_SYSID_FILEPATH));
1424         return ENOENT;
1425     }
1426     if (!(fd = open(AFSDIR_SERVER_SYSID_FILEPATH, O_RDONLY, 0))) {
1427         ViceLog(0, ("%s: can't open (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1428         return EIO;
1429     }
1430     if ((i = read(fd, (char *)&vsn, sizeof(vsn))) != sizeof(vsn)) {
1431         ViceLog(0, ("%s: Read failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1432         return EIO;
1433     }
1434     if (vsn.magic != SYSIDMAGIC) {
1435         ViceLog(0, ("%s: wrong magic %x (we support %x)\n", AFSDIR_SERVER_SYSID_FILEPATH, vsn.magic, SYSIDMAGIC));
1436         return EIO;
1437     }
1438     if (vsn.version != SYSIDVERSION) {
1439         ViceLog(0, ("%s: wrong version %d (we support %d)\n", AFSDIR_SERVER_SYSID_FILEPATH, vsn.version, SYSIDVERSION));
1440         return EIO;
1441     }
1442     if ((i = read(fd, (char *)&uuid, sizeof(struct afsUUID))) != sizeof(struct afsUUID)) {
1443         ViceLog(0, ("%s: read of uuid failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1444         return EIO;
1445     }
1446     afs_ntohuuid(&uuid);
1447     FS_HostUUID = uuid;
1448     if ((i = read(fd, (char *)&nentries, sizeof(afs_int32))) != sizeof(afs_int32)) {
1449         ViceLog(0, ("%s: Read of entries failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1450         return EIO;
1451     }
1452     if (nentries <= 0 || nentries > ADDRSPERSITE) {
1453         ViceLog(0, ("%s: invalid num of interfaces: %d\n", AFSDIR_SERVER_SYSID_FILEPATH, nentries));
1454         return EIO;
1455     }
1456     FS_HostAddr_cnt = nentries;
1457     for (i = 0; i < nentries; i++) {
1458         if (read(fd, (char *)&FS_HostAddrs[i], sizeof(afs_int32)) != sizeof(afs_int32)) {       
1459             ViceLog(0, ("%s: Read of addresses failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1460             FS_HostAddr_cnt = 0;        /* reset it */
1461             return EIO;
1462         }
1463     }
1464     close(fd);
1465     return 0;
1466 }
1467
1468 WriteSysIdFile() {
1469     afs_int32 fd, nentries, i;
1470     struct versionStamp vsn;
1471     struct stat     status;
1472     afsUUID uuid;
1473     
1474     if (!stat(AFSDIR_SERVER_SYSID_FILEPATH, &status)) {
1475         /*
1476          * File exists; keep the old one around
1477          */
1478         renamefile(AFSDIR_SERVER_SYSID_FILEPATH, AFSDIR_SERVER_OLDSYSID_FILEPATH);      
1479     }
1480     fd = open(AFSDIR_SERVER_SYSID_FILEPATH, O_WRONLY|O_TRUNC|O_CREAT, 0666);
1481     if (fd < 1) {
1482         ViceLog(0, ("%s: can't create (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1483         return EIO;
1484     }
1485     vsn.magic = SYSIDMAGIC;
1486     vsn.version = 1;
1487     if ((i = write(fd, (char *)&vsn, sizeof(vsn))) != sizeof(vsn)) {
1488         ViceLog(0, ("%s: write failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1489         return EIO;
1490     }
1491     uuid = FS_HostUUID;
1492     afs_htonuuid(&uuid);
1493     if ((i = write(fd, (char *)&uuid, sizeof(struct afsUUID))) != sizeof(struct afsUUID)) {
1494         ViceLog(0, ("%s: write of uuid failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1495         return EIO;
1496     }
1497     if ((i = write(fd, (char *)&FS_HostAddr_cnt, sizeof(afs_int32))) != sizeof(afs_int32)) {
1498         ViceLog(0, ("%s: write of # of entries failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1499         return EIO;
1500     }
1501     for (i = 0; i < FS_HostAddr_cnt; i++) {
1502         if (write(fd, (char *)&FS_HostAddrs[i], sizeof(afs_int32)) != sizeof(afs_int32)) {      
1503             ViceLog(0, ("%s: write of addresses failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1504             return EIO;
1505         }
1506     }
1507     close(fd);
1508     return 0;
1509 }
1510
1511 /*
1512  * defect 10966 
1513  * This routine sets up the buffers for the VL_RegisterAddrs RPC. All addresses
1514  * in FS_HostAddrs[] are in NBO, while the RPC treats them as a "blob" of data
1515  * and so we need to convert each of them into HBO which is what the extra 
1516  * array called FS_HostAddrs_HBO is used here.
1517  */ 
1518 Do_VLRegisterRPC() {
1519     register int code;
1520     bulkaddrs addrs;
1521     extern int VL_RegisterAddrs();
1522     afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
1523     int i=0;
1524  
1525     for (i=0; i < FS_HostAddr_cnt ; i++) 
1526      FS_HostAddrs_HBO[i]=ntohl(FS_HostAddrs[i]);
1527     addrs.bulkaddrs_len = FS_HostAddr_cnt;
1528     addrs.bulkaddrs_val = (afs_uint32 *)FS_HostAddrs_HBO;
1529     code = ubik_Call(VL_RegisterAddrs, cstruct, 0, &FS_HostUUID, 0, &addrs);
1530     if (code) {
1531        if (code == VL_MULTIPADDR) {
1532           ViceLog(0, ("VL_RegisterAddrs rpc failed; The ethernet address exist on a different server; repair it\n"));
1533           ViceLog(0, ("VL_RegisterAddrs rpc failed; See VLLog for details\n"));
1534           return code;
1535        } else if (code == RXGEN_OPCODE) {
1536           ViceLog(0, ("vlserver doesn't support VL_RegisterAddrs rpc; ignored\n"));
1537           FS_registered = 2;    /* So we don't have to retry in the gc daemon */
1538        } else {
1539           ViceLog(0, ("VL_RegisterAddrs rpc failed; will retry periodically (code=%d, err=%d)\n",
1540                       code, errno));
1541        }
1542     } else {
1543        FS_registered = 2;       /* So we don't have to retry in the gc daemon */
1544        WriteSysIdFile();
1545     }
1546
1547     return 0;
1548 }
1549
1550 #if 0
1551 static int AddrsEqual(cnt, addr1, addr2) 
1552     int cnt;
1553     afs_int32 *addr1, *addr2; 
1554 {
1555     register int i, j;
1556
1557     for (i = 0; i < cnt; i++) {
1558         for (j = 0; j < cnt; j++) {
1559             if (addr1[i] == addr2[j]) break;
1560         }
1561         if (j == cnt) return 0;
1562     }
1563     return 1;
1564 }
1565 #endif
1566
1567 InitVL() {
1568     int (*old)();
1569     afs_int32 code;
1570     afs_int32 cnt, i;
1571     extern int rxi_numNetAddrs;
1572     extern afs_uint32 rxi_NetAddrs[];
1573
1574     /*
1575      * If this fails, it's because something major is wrong, and is not
1576      * likely to be time dependent.
1577      */
1578     code = vl_Initialize(AFSDIR_SERVER_ETC_DIRPATH);
1579     if (code != 0) {
1580         ViceLog(0, ("Couldn't initialize protection library; code=%d.\n", code));
1581         return code;
1582     }
1583
1584     /* Read or create the sysid file and register the fileserver's
1585      * IP addresses with the vlserver.
1586      */
1587     code = ReadSysIdFile();
1588     if (code) {
1589        /* Need to create the file */
1590        ViceLog(0, ("Creating new SysID file\n")); 
1591        if ((code = afs_uuid_create(&FS_HostUUID))) {
1592           ViceLog(0, ("Failed to create new uuid: %d\n", code)); 
1593           exit(1);
1594        }
1595     }
1596     /* A good sysid file exists; inform the vlserver. If any conflicts,
1597      * we always use the latest interface available as the real truth.
1598      */
1599 #ifndef AFS_NT40_ENV
1600     if(AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
1601       /*
1602        * Find addresses we are supposed to register as per the netrestrict 
1603        * and netinfo files (/usr/afs/local/NetInfo and 
1604        * /usr/afs/local/NetRestict)
1605        */
1606       char reason[1024];
1607       afs_int32 code = parseNetFiles(FS_HostAddrs,NULL, NULL,
1608                                  ADDRSPERSITE, reason,
1609                                  AFSDIR_SERVER_NETINFO_FILEPATH,
1610                                  AFSDIR_SERVER_NETRESTRICT_FILEPATH);
1611       if (code < 0) {
1612         ViceLog(0,("Can't register any valid addresses: %s\n",reason));
1613         exit(1);
1614       }
1615       FS_HostAddr_cnt = (afs_uint32) code;
1616     }
1617     else 
1618 #endif
1619     {
1620       FS_HostAddr_cnt = rx_getAllAddr(FS_HostAddrs, ADDRSPERSITE);
1621     }
1622
1623     FS_registered = 1;
1624     code = Do_VLRegisterRPC();
1625     return code;
1626