2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
12 #include <afs/afs_args.h>
23 #include <rx/rx_null.h>
28 #define AFSDIR_CLIENT_ETC_DIRPATH "c:/"
29 #define AFSLOGFILE "afs.log"
30 #define CACHEINFOFILE "cache.info"
32 extern int RXAFSCB_ExecuteRequest();
33 extern int RXSTATS_ExecuteRequest();
37 char cm_rootVolumeName[64];
38 DWORD cm_rootVolumeNameLen;
39 cm_volume_t *cm_rootVolumep = NULL;
40 cm_cell_t *cm_rootCellp = NULL;
42 cm_scache_t *cm_rootSCachep;
43 char cm_mountRoot[1024];
44 DWORD cm_mountRootLen;
47 int afs_diskCacheChunks;
48 char cm_cachePath[128];
49 int cm_diskCacheEnabled = 0;
56 int cacheSetTime = TRUE;
67 char cm_HostName[200];
70 /*char cm_CachePath[200];*/
71 /*DWORD cm_CachePathLen;*/
72 char cm_CacheInfoPath[1024];
74 int sawCacheSize=0, sawDiskCacheSize=0, sawMountRoot=0;
75 int sawCacheBaseDir=0;
76 char cm_AFSLogFile[200];
77 int afsd_CloseSynch = 0;
81 BOOL isGateway = FALSE;
82 BOOL reportSessionStartups = FALSE;
85 cm_initparams_v1 cm_initParams;
88 * AFSD Initialization Log
90 * This is distinct from the regular debug logging facility.
91 * Log items go directly to a file, not to an array in memory, so that even
92 * if AFSD crashes, the log can be inspected.
108 /*code = GetWindowsDirectory(wd, sizeof(wd));
109 if (code == 0) return;*/
111 strcat(wd, "\\afsd_init.log");
112 /*GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));*/
114 strcpy(t, asctime(localtime(&now)));
115 /*afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
116 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);*/
117 /*afsi_file = open(wd, O_RDWR | O_CREAT | O_RSHARE);*/
118 afsi_file = fopen(wd, "wt");
119 setbuf(afsi_file, NULL);
121 /*GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));*/
123 strcpy(u, asctime(localtime(&now)));
124 p = strchr(u, '\n'); if (p) *p = 0;
125 p = strchr(u, '\r'); if (p) *p = 0;
126 strcat(t, ": Create log file\n");
127 strcat(u, ": Created log file\n");
128 /*WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
129 WriteFile(afsi_file, u, strlen(u), &zilch, NULL);*/
130 /*write(afsi_file, t, strlen(t));
131 write(afsi_file, u, strlen(u));*/
137 afsi_log(char *pattern, ...)
139 char s[100], t[100], u[100];
146 va_start(ap, pattern);
148 vsprintf(s, pattern, ap);
149 /*GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));*/
151 strcpy(t, asctime(localtime(&now)));
152 sprintf(u, "%s: %s\n", t, s);
153 if (afsi_file != NULL)
154 /* fputs(u, stderr); */
156 /*write(afsi_file, u, strlen(u));*/
157 /*WriteFile(afsi_file, u, strlen(u), &zilch, NULL);*/
161 * Standard AFSD trace
164 void afsd_ForceTrace(BOOL flush)
170 if (!logReady) return;
172 /*len = GetTempPath(99, buf);*/
173 /*strcpy(&buf[len], "/afsd.log");*/
174 strcpy(buf, "c:/afsd.log");
175 /*handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
176 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);*/
177 /*handle = open(buf, O_RDWR | O_CREAT | O_RSHARE);*/
178 handle = fopen(buf, "wt");
179 if (handle == NULL) {
181 osi_panic("Cannot create log file", __FILE__, __LINE__);
183 osi_LogPrint(afsd_logp, handle);
186 /*FlushFileBuffers(handle);*/
187 /*CloseHandle(handle);*/
191 /*------------------------------------------------------------------------------
195 * Open the file containing the description of the workstation's AFS cache
196 * and pull out its contents. The format of this file is as follows:
198 * cm_mountRoot:cacheBaseDir:cacheBlocks
204 * 0 if everything went well,
208 * Nothing interesting.
212 *---------------------------------------------------------------------------*/
214 int ParseCacheInfoFile()
216 static char rn[]="ParseCacheInfoFile"; /*This routine's name*/
217 FILE *cachefd; /*Descriptor for cache info file*/
218 int parseResult; /*Result of our fscanf()*/
220 char tCacheBaseDir[1024], *tbd, tCacheMountDir[1024], *tmd;
221 /*char cacheBaseDir[1024]; /* cache in mem, this is ignored */
224 printf("%s: Opening cache info file '%s'...\n",
225 rn, cm_CacheInfoPath);
227 cachefd = fopen(cm_CacheInfoPath, "r");
229 printf("%s: Can't read cache info file '%s'\n",
230 rn, cm_CacheInfoPath);
235 * Parse the contents of the cache info file. All chars up to the first
236 * colon are the AFS mount directory, all chars to the next colon are the
237 * full path name of the workstation cache directory and all remaining chars
238 * represent the number of blocks in the cache.
240 tCacheMountDir[0] = tCacheBaseDir[0] = '\0';
241 parseResult = fscanf(cachefd,
242 "%1024[^;];%1024[^;];%d",
243 tCacheMountDir, tCacheBaseDir, &tCacheBlocks);
246 * Regardless of how the parse went, we close the cache info file.
250 if (parseResult == EOF || parseResult < 3) {
251 printf("%s: Format error in cache info file!\n",
253 if (parseResult == EOF)
254 printf("\tEOF encountered before any field parsed.\n");
256 printf("\t%d out of 3 fields successfully parsed.\n",
259 printf("\tcm_mountRoot: '%s'\n\tcm_cachePath: '%s'\n\tcacheBlocks: %d\n",
260 cm_mountRoot, cm_cachePath, cacheBlocks);
264 for (tmd = tCacheMountDir; *tmd == '\n' || *tmd == ' ' || *tmd == '\t'; tmd++) ;
265 for (tbd = tCacheBaseDir; *tbd == '\n' || *tbd == ' ' || *tbd == '\t'; tbd++) ;
266 /* now copy in the fields not explicitly overridden by cmd args */
269 strcpy(cm_mountRoot, tmd);
270 cm_mountRootLen = strlen(tmd);
272 if (!sawCacheBaseDir)
273 strcpy(cm_cachePath, tbd);
275 cacheBlocks = tCacheBlocks;
278 printf("%s: Cache info file successfully parsed:\n",
280 printf("\tcm_mountRoot: '%s'\n\tcm_cachePath: '%s'\n\tcacheBlocks: %d\n",
281 tmd, tbd, tCacheBlocks);
283 /*printf("cm_cachePath: %s\n", cm_cachePath);*/
285 /*PartSizeOverflow(tbd, cacheBlocks);*/
291 * AFSD Initialization
294 int afsd_InitCM(char **reasonP, struct cmd_syndesc *as, char *arock)
303 char rootCellName[256];
304 struct rx_service *serverp;
305 static struct rx_securityClass *nullServerSecurityClassp;
311 struct cmd_syndesc *ts;
316 WSAStartup(0x0101, &WSAjunk);*/
319 /* setup osidebug server at RPC slot 1000 */
320 osi_LongToUID(1000, &debugID);
321 code = osi_InitDebug(&debugID);
322 afsi_log("osi_InitDebug code %d", code);
323 // osi_LockTypeSetDefault("stat"); /* comment this out for speed *
325 *reasonP = "unknown error";
331 gethostname(cm_HostName, sizeof(cm_HostName));
333 /* For some reason, we may be getting space-padded hostnames.
334 If so, we take out the padding so that we can append -AFS later. */
336 char *space = strchr(cm_HostName,' ');
337 if (space) *space = '\0';
340 afsi_log("gethostname %s", cm_HostName);
341 thp = gethostbyname(cm_HostName);
342 memcpy(&cm_HostAddr, thp->h_addr_list[0], 4);
344 /* seed random number generator */
345 srand(ntohl(cm_HostAddr));
347 /* Get configuration parameters from command line */
349 /* call atoi on the appropriate parsed results */
351 if (!as->parms[20].items) {
353 fprintf(stderr, "Please do not run this program directly. Use the AFS Client Windows loader\nto start AFS.\n");
357 if (as->parms[0].items) {
359 LANadapter = atoi(as->parms[0].items->data);
361 afsi_log("LAN adapter number %d", LANadapter);
366 afsi_log("Default LAN adapter number");
369 if (as->parms[1].items) {
371 numSvThreads = atoi(as->parms[1].items->data);
372 afsi_log("%d server threads", numSvThreads);
376 numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
377 afsi_log("Defaulting to %d server threads", numSvThreads);
380 if (as->parms[2].items) {
382 strcpy(cm_rootVolumeName, as->parms[2].items->data);
384 afsi_log("Root volume %s", cm_rootVolumeName);
388 strcpy(cm_rootVolumeName, "root.afs");
389 afsi_log("Default root volume name root.afs");
392 if (as->parms[3].items) {
394 stats = atoi(as->parms[3].items->data);
395 afsi_log("Status cache size %d", stats);
399 stats = CM_CONFIGDEFAULT_STATS;
400 afsi_log("Default status cache size %d", stats);
403 if (as->parms[4].items) {
408 if (as->parms[5].items) {
410 /* no-op; cache is in memory, not mapped file */
411 strcpy(cm_cachePath, as->parms[5].items->data);
415 if (as->parms[6].items) {
417 strcpy(cm_mountRoot, as->parms[6].items->data);
418 cm_mountRootLen = strlen(cm_mountRoot);
420 afsi_log("Mount root %s", cm_mountRoot);
424 strcpy(cm_mountRoot, "/afs");
429 if (as->parms[7].items) {
431 numBkgD = atoi(as->parms[7].items->data);
432 afsi_log("%d background daemons", numBkgD);
436 numBkgD = CM_CONFIGDEFAULT_DAEMONS;
437 afsi_log("Defaulting to %d background daemons", numBkgD);
440 if (as->parms[8].items) {
442 cacheSetTime = FALSE;
445 if (as->parms[9].items) {
450 if (as->parms[10].items) {
456 if (as->parms[11].items) {
458 chunkSize = atoi(as->parms[11].items->data);
459 if (chunkSize < 12 || chunkSize > 30) {
460 afsi_log("Invalid chunk size %d, using default",
462 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
465 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
466 afsi_log("Default chunk size %d", logChunkSize);
468 cm_logChunkSize = logChunkSize;
469 cm_chunkSize = 1 << logChunkSize;
471 if (as->parms[12].items) {
473 cacheSize = atoi(as->parms[12].items->data);
474 afsi_log("Cache size %d", cacheSize);
479 cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
480 afsi_log("Default cache size %d", cacheSize);
483 afsconf_path = getenv("AFSCONF");
485 strcpy(cm_confDir, AFSDIR_CLIENT_ETC_DIRPATH);
487 strcpy(cm_confDir, afsconf_path);
488 if (as->parms[13].items) {
490 strcpy(cm_confDir, as->parms[13].items->data);
493 sprintf(cm_CacheInfoPath, "%s/%s", cm_confDir, CACHEINFOFILE);
495 sprintf(cm_AFSLogFile, "%s/%s", cm_confDir, AFSLOGFILE);
496 if (as->parms[14].items) {
498 strcpy(cm_AFSLogFile, as->parms[14].items->data);
501 if (as->parms[15].items) {
506 if (as->parms[16].items) {
510 * Cold shutdown is the default
512 printf("afsd: Shutting down all afs processes and afs state\n");
513 /*call_syscall(AFSOP_SHUTDOWN, 1);*/
517 if (as->parms[17].items) {
519 strcpy(cm_sysName, as->parms[17].items->data);
522 strcpy(cm_sysName, "i386_win95");
524 if (as->parms[18].items) {
527 afsi_log("Set for %s service",
528 isGateway ? "gateway" : "stand-alone");
533 if (as->parms[19].items) {
535 traceBufSize = atoi(as->parms[19].items->data);
536 afsi_log("Trace Buffer size %d", traceBufSize);
540 traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
541 afsi_log("Default trace buffer size %d", traceBufSize);
544 if (as->parms[21].items) {
546 diskCacheSize = atoi(as->parms[21].items->data);
547 cm_diskCacheEnabled = 1;
548 afsi_log("Disk cache size %d K", diskCacheSize);
549 /*printf("Disk cache size %d K", diskCacheSize);*/
550 sawDiskCacheSize = 1;
554 diskCacheSize = 50000; /*CM_CONFIGDEFAULT_DISKCACHESIZE;*/
555 afsi_log("Default disk cache size %d", diskCacheSize);
558 if (ParseCacheInfoFile()) {
562 /* setup early variables */
563 /* These both used to be configurable. */
565 buf_bufferSize = CM_CONFIGDEFAULT_BLOCKSIZE;
567 /* turn from 1024 byte units into memory blocks */
568 cacheBlocks = (cacheSize * 1024) / buf_bufferSize;
569 afs_diskCacheChunks = (diskCacheSize * 1024) / buf_bufferSize;
570 /*printf("afs_diskCacheChunks=%d\n", afs_diskCacheChunks);*/
573 * Save client configuration for GetCacheConfig requests
575 cm_initParams.nChunkFiles = 0;
576 cm_initParams.nStatCaches = stats;
577 cm_initParams.nDataCaches = 0;
578 cm_initParams.nVolumeCaches = 0;
579 cm_initParams.firstChunkSize = cm_chunkSize;
580 cm_initParams.otherChunkSize = cm_chunkSize;
581 cm_initParams.cacheSize = cacheSize;
582 cm_initParams.setTime = 0;
583 cm_initParams.memCache = 0;
585 /* setup and enable debug log */
586 afsd_logp = osi_LogCreate("afsd", traceBufSize);
587 afsi_log("osi_LogCreate log addr %x", afsd_logp);
588 osi_LogEnable(afsd_logp);
592 /* get network related info */
593 cm_noIPAddr = CM_MAXINTERFACE_ADDR;
594 code = syscfg_GetIFInfo(&cm_noIPAddr,
595 cm_IPAddr, cm_SubnetMask,
596 cm_NetMtu, cm_NetFlags);
598 if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
599 afsi_log("syscfg_GetIFInfo error code %d", code);
601 afsi_log("First Network address %x SubnetMask %x",
602 cm_IPAddr[0], cm_SubnetMask[0]);
605 /* initialize RX, and tell it to listen to port 7001, which is used for
606 * callback RPC messages.
608 code = rx_Init(htons(7001));
609 afsi_log("rx_Init code %x", code);
611 *reasonP = "afsd: failed to init rx client on port 7001";
615 /* Initialize the RPC server for session keys */
618 /* create an unauthenticated service #1 for callbacks */
619 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
620 serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
621 RXAFSCB_ExecuteRequest);
622 afsi_log("rx_NewService addr %x", serverp);
623 if (serverp == NULL) {
624 *reasonP = "unknown error";
628 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
629 serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
630 &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
631 afsi_log("rx_NewService addr %x", serverp);
632 if (serverp == NULL) {
633 *reasonP = "unknown error";
637 /* start server threads, *not* donating this one to the pool */
639 afsi_log("rx_StartServer");
641 /* init user daemon, and other packages */
644 cm_InitACLCache(2*stats);
660 cm_InitSCache(stats);
662 code = cm_InitDCache(0, cacheBlocks);
664 afsi_log("cm_InitDCache code %x", code);
666 *reasonP = "error initializing cache";
670 code = cm_GetRootCellName(rootCellName);
671 afsi_log("cm_GetRootCellName code %d rcn %s", code,
672 (code ? "<none>" : rootCellName));
674 *reasonP = "can't find root cell name in ThisCell";
678 cm_rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
679 afsi_log("cm_GetCell addr %x", cm_rootCellp);
680 if (cm_rootCellp == NULL) {
681 *reasonP = "can't find root cell in CellServDB";
688 int afsd_InitDaemons(char **reasonP)
695 /* this should really be in an init daemon from here on down */
697 code = cm_GetVolumeByName(cm_rootCellp, cm_rootVolumeName, cm_rootUserp, &req, CM_FLAG_CREATE, &cm_rootVolumep);
698 afsi_log("cm_GetVolumeByName code %x root vol %x", code,
699 (code ? 0xffffffff : cm_rootVolumep));
701 *reasonP = "can't find root volume in root cell";
705 /* compute the root fid */
706 cm_rootFid.cell = cm_rootCellp->cellID;
707 cm_rootFid.volume = cm_GetROVolumeID(cm_rootVolumep);
708 cm_rootFid.vnode = 1;
709 cm_rootFid.unique = 1;
711 code = cm_GetSCache(&cm_rootFid, &cm_rootSCachep, cm_rootUserp, &req);
712 afsi_log("cm_GetSCache code %x scache %x", code,
713 (code ? 0xffffffff : cm_rootSCachep));
715 *reasonP = "unknown error";
719 cm_InitDaemon(numBkgD);
720 afsi_log("cm_InitDaemon");
725 int afsd_InitSMB(char **reasonP)
730 /* Do this last so that we don't handle requests before init is done.
731 * Here we initialize the SMB listener.
733 strcpy(hostName, cm_HostName);
734 ctemp = strchr(hostName, '.'); /* turn ntdfs.* into ntdfs */
735 if (ctemp) *ctemp = 0;
736 hostName[11] = 0; /* ensure that even after adding the -A, we
737 * leave one byte free for the netbios server
740 strcat(hostName, "-AFS");
742 smb_Init(afsd_logp, hostName, smb_UseV3, LANadapter, numSvThreads);
743 afsi_log("smb_Init");