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>
30 #include <rx\rx_null.h>
31 #include <WINNT/syscfg.h>
32 #include <WINNT/afsreg.h>
36 #include "lanahelper.h"
38 #include "cm_memmap.h"
40 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
41 extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
43 extern afs_uint32 cryptall;
44 extern afs_uint32 cm_anonvldb;
45 extern int cm_enableServerLocks;
46 extern int cm_followBackupPath;
47 extern int cm_deleteReadOnly;
49 extern afs_int32 cm_BPlusTrees;
51 extern afs_int32 cm_OfflineROIsValid;
52 extern afs_int32 cm_giveUpAllCBs;
53 extern const char **smb_ExecutableExtensions;
57 cm_config_data_t cm_data;
59 char cm_rootVolumeName[VL_MAXNAMELEN];
60 DWORD cm_rootVolumeNameLen;
61 char cm_mountRoot[1024];
62 DWORD cm_mountRootLen;
77 char cm_HostName[200];
79 unsigned short cm_callbackport = CM_DEFAULT_CALLBACKPORT;
81 char cm_NetbiosName[MAX_NB_NAME_LENGTH] = "";
83 char cm_CachePath[MAX_PATH];
84 DWORD cm_CachePathLen;
85 DWORD cm_ValidateCache = 1;
87 BOOL reportSessionStartups = FALSE;
89 cm_initparams_v1 cm_initParams;
92 unsigned int cm_sysNameCount = 0;
93 char *cm_sysNameList[MAXNUMSYSNAMES];
95 DWORD TraceOption = 0;
98 * AFSD Initialization Log
100 * This is distinct from the regular debug logging facility.
101 * Log items go directly to a file, not to an array in memory, so that even
102 * if AFSD crashes, the log can be inspected.
108 int cm_dnsEnabled = 1;
112 static int afsi_log_useTimestamp = 1;
115 afsi_log(char *pattern, ...)
117 char s[256], t[100], d[100], u[512];
120 va_start(ap, pattern);
122 StringCbVPrintfA(s, sizeof(s), pattern, ap);
123 if ( afsi_log_useTimestamp ) {
124 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
125 GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
126 StringCbPrintfA(u, sizeof(u), "%s %s: %s\r\n", d, t, s);
127 if (afsi_file != INVALID_HANDLE_VALUE)
128 WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
133 if (afsi_file != INVALID_HANDLE_VALUE)
134 WriteFile(afsi_file, s, (DWORD)strlen(s), &zilch, NULL);
138 extern initUpperCaseTable();
139 void afsd_initUpperCaseTable()
141 initUpperCaseTable();
148 char t[100], u[100], *p, *path;
154 DWORD maxLogSize = 100 * 1024;
156 afsi_file = INVALID_HANDLE_VALUE;
157 code = GetTempPath(sizeof(wd)-15, wd);
158 if ( code == 0 || code > (sizeof(wd)-15) )
159 return; /* unable to create a log */
161 StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
162 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
163 afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
164 OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
166 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
167 0, KEY_QUERY_VALUE, &parmKey);
168 if (code == ERROR_SUCCESS) {
169 dummyLen = sizeof(maxLogSize);
170 code = RegQueryValueEx(parmKey, "MaxLogSize", NULL, NULL,
171 (BYTE *) &maxLogSize, &dummyLen);
172 RegCloseKey (parmKey);
176 dwLow = GetFileSize( afsi_file, &dwHigh );
177 if ( dwHigh > 0 || dwLow >= maxLogSize ) {
178 CloseHandle(afsi_file);
179 afsi_file = CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
180 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
184 SetFilePointer(afsi_file, 0, NULL, FILE_END);
185 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
186 StringCbCatA(t, sizeof(t), ": Create log file\r\n");
187 StringCbCatA(u, sizeof(u), ": Created log file\r\n");
188 WriteFile(afsi_file, t, (DWORD)strlen(t), &zilch, NULL);
189 WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
191 code = GetEnvironmentVariable("PATH", NULL, 0);
193 code = GetEnvironmentVariable("PATH", path, code);
194 WriteFile(afsi_file, p, (DWORD)strlen(p), &zilch, NULL);
195 WriteFile(afsi_file, path, (DWORD)strlen(path), &zilch, NULL);
196 WriteFile(afsi_file, "\r\n", (DWORD)1, &zilch, NULL);
199 /* Initialize C RTL Code Page conversion functions */
200 /* All of the path info obtained from the SMB client is in the OEM code page */
201 afsi_log("OEM Code Page = %d", GetOEMCP());
202 afsi_log("locale = %s", setlocale(LC_ALL,NULL));
204 /* Two things to look into. First, should mbstowcs() be performing
205 * character set translations from OEM to Unicode in smb3.c;
206 * Second, do we need to set this translation in each function
207 * due to multi-threading.
209 afsi_log("locale -> %s", setlocale(LC_ALL, ".OCP"));
210 afsi_log("_setmbcp = %d -> %d", _setmbcp(_MB_CP_OEM), _getmbcp());
215 * Standard AFSD trace
218 void afsd_ForceTrace(BOOL flush)
227 len = GetTempPath(sizeof(buf)-10, buf);
228 StringCbCopyA(&buf[len], sizeof(buf)-len, "/afsd.log");
229 handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
230 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
231 if (handle == INVALID_HANDLE_VALUE) {
233 osi_panic("Cannot create log file", __FILE__, __LINE__);
235 osi_LogPrint(afsd_logp, handle);
237 FlushFileBuffers(handle);
242 configureBackConnectionHostNames(void)
244 /* On Windows XP SP2, Windows 2003 SP1, and all future Windows operating systems
245 * there is a restriction on the use of SMB authentication on loopback connections.
246 * There are two work arounds available:
248 * (1) We can disable the check for matching host names. This does not
250 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
251 * "DisableLoopbackCheck"=dword:00000001
253 * (2) We can add the AFS SMB/CIFS service name to an approved list. This
254 * does require a reboot:
255 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0]
256 * "BackConnectionHostNames"=multi-sz
258 * The algorithm will be:
259 * (1) Check to see if cm_NetbiosName exists in the BackConnectionHostNames list
260 * (2a) If not, add it to the list. (This will not take effect until the next reboot.)
261 * (2b1) and check to see if DisableLoopbackCheck is set.
262 * (2b2) If not set, set the DisableLoopbackCheck value to 0x1
263 * (2b3) and create HKLM\SOFTWARE\OpenAFS\Client UnsetDisableLoopbackCheck
264 * (2c) else If cm_NetbiosName exists in the BackConnectionHostNames list,
265 * check for the UnsetDisableLoopbackCheck value.
266 * If set, set the DisableLoopbackCheck flag to 0x0
267 * and delete the UnsetDisableLoopbackCheck value
269 * Starting in Longhorn Beta 1, an entry in the BackConnectionHostNames value will
270 * force Windows to use the loopback authentication mechanism for the specified
277 DWORD dwSize, dwAllocSize;
279 PBYTE pHostNames = NULL, pName = NULL;
280 BOOL bNameFound = FALSE;
282 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
283 "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0",
286 &hkMSV10) == ERROR_SUCCESS )
288 if ((RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0,
289 &dwType, NULL, &dwAllocSize) == ERROR_SUCCESS) &&
290 (dwType == REG_MULTI_SZ))
292 dwAllocSize += 1 /* in case the source string is not nul terminated */
293 + strlen(cm_NetbiosName) + 2;
294 pHostNames = malloc(dwAllocSize);
295 dwSize = dwAllocSize;
296 if (RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType,
297 pHostNames, &dwSize) == ERROR_SUCCESS)
299 for (pName = pHostNames;
300 (pName - pHostNames < dwSize) && *pName ;
301 pName += strlen(pName) + 1)
303 if ( !stricmp(pName, cm_NetbiosName) ) {
312 size_t size = strlen(cm_NetbiosName) + 2;
314 pHostNames = malloc(size);
317 StringCbCopyA(pName, size, cm_NetbiosName);
319 *pName = '\0'; /* add a second nul terminator */
321 dwType = REG_MULTI_SZ;
322 dwSize = pName - pHostNames + 1;
323 RegSetValueEx( hkMSV10, "BackConnectionHostNames", 0, dwType, pHostNames, dwSize);
325 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
326 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
329 &hkLsa) == ERROR_SUCCESS )
331 dwSize = sizeof(DWORD);
332 if ( RegQueryValueEx( hkLsa, "DisableLoopbackCheck", 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS ||
335 dwSize = sizeof(DWORD);
337 RegSetValueEx( hkLsa, "DisableLoopbackCheck", 0, dwType, (LPBYTE)&dwValue, dwSize);
339 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
340 AFSREG_CLT_OPENAFS_SUBKEY,
343 REG_OPTION_NON_VOLATILE,
347 NULL) == ERROR_SUCCESS) {
350 dwSize = sizeof(DWORD);
352 RegSetValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, dwType, (LPBYTE)&dwValue, dwSize);
353 RegCloseKey(hkClient);
359 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
360 AFSREG_CLT_OPENAFS_SUBKEY,
363 REG_OPTION_NON_VOLATILE,
367 NULL) == ERROR_SUCCESS) {
369 dwSize = sizeof(DWORD);
370 if ( RegQueryValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, &dwType, (LPBYTE)&dwValue, &dwSize) == ERROR_SUCCESS &&
372 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
373 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
376 &hkLsa) == ERROR_SUCCESS )
378 RegDeleteValue(hkLsa, "DisableLoopbackCheck");
382 RegDeleteValue(hkClient, "RemoveDisableLoopbackCheck");
383 RegCloseKey(hkClient);
386 RegCloseKey(hkMSV10);
394 static void afsd_InitServerPreferences(void)
397 DWORD dwType, dwSize;
401 DWORD dwHostSize = 256;
403 struct sockaddr_in saddr;
406 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
407 AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\VLDB",
409 KEY_READ|KEY_QUERY_VALUE,
410 &hkPrefs) == ERROR_SUCCESS) {
412 RegQueryInfoKey( hkPrefs,
415 NULL, /* lpReserved */
416 NULL, /* lpcSubKeys */
417 NULL, /* lpcMaxSubKeyLen */
418 NULL, /* lpcMaxClassLen */
419 &dwPrefs, /* lpcValues */
420 NULL, /* lpcMaxValueNameLen */
421 NULL, /* lpcMaxValueLen */
422 NULL, /* lpcbSecurityDescriptor */
423 NULL /* lpftLastWriteTime */
426 for ( dwIndex = 0 ; dwIndex < dwPrefs; dwIndex++ ) {
428 dwSize = sizeof(DWORD);
431 if (RegEnumValue( hkPrefs, dwIndex, szHost, &dwHostSize, NULL,
432 &dwType, (LPBYTE)&dwRank, &dwSize))
434 afsi_log("RegEnumValue(hkPrefs) failed");
438 afsi_log("VLDB Server Preference: %s = %d",szHost, dwRank);
440 if (isdigit(szHost[0]))
442 if ((saddr.sin_addr.S_un.S_addr = inet_addr (szHost)) == INADDR_NONE)
446 if ((pEntry = gethostbyname (szHost)) == NULL)
449 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
451 saddr.sin_family = AF_INET;
452 dwRank += (rand() & 0x000f);
454 tsp = cm_FindServer(&saddr, CM_SERVER_VLDB);
455 if ( tsp ) /* an existing server - ref count increased */
457 tsp->ipRank = (USHORT)dwRank; /* no need to protect by mutex*/
459 /* set preferences for an existing vlserver */
460 cm_ChangeRankCellVLServer(tsp);
461 cm_PutServer(tsp); /* decrease refcount */
463 else /* add a new server without a cell */
465 tsp = cm_NewServer(&saddr, CM_SERVER_VLDB, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
466 tsp->ipRank = (USHORT)dwRank;
470 RegCloseKey(hkPrefs);
473 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
474 AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\File",
476 KEY_READ|KEY_QUERY_VALUE,
477 &hkPrefs) == ERROR_SUCCESS) {
479 RegQueryInfoKey( hkPrefs,
482 NULL, /* lpReserved */
483 NULL, /* lpcSubKeys */
484 NULL, /* lpcMaxSubKeyLen */
485 NULL, /* lpcMaxClassLen */
486 &dwPrefs, /* lpcValues */
487 NULL, /* lpcMaxValueNameLen */
488 NULL, /* lpcMaxValueLen */
489 NULL, /* lpcbSecurityDescriptor */
490 NULL /* lpftLastWriteTime */
493 for ( dwIndex = 0 ; dwIndex < dwPrefs; dwIndex++ ) {
495 dwSize = sizeof(DWORD);
498 if (RegEnumValue( hkPrefs, dwIndex, szHost, &dwHostSize, NULL,
499 &dwType, (LPBYTE)&dwRank, &dwSize))
501 afsi_log("RegEnumValue(hkPrefs) failed");
505 afsi_log("File Server Preference: %s = %d",szHost, dwRank);
507 if (isdigit(szHost[0]))
509 if ((saddr.sin_addr.S_un.S_addr = inet_addr (szHost)) == INADDR_NONE)
513 if ((pEntry = gethostbyname (szHost)) == NULL)
516 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
518 saddr.sin_family = AF_INET;
519 dwRank += (rand() & 0x000f);
521 tsp = cm_FindServer(&saddr, CM_SERVER_FILE);
522 if ( tsp ) /* an existing server - ref count increased */
524 tsp->ipRank = (USHORT)dwRank; /* no need to protect by mutex*/
526 /* find volumes which might have RO copy
527 /* on server and change the ordering of
530 cm_ChangeRankVolume(tsp);
531 cm_PutServer(tsp); /* decrease refcount */
533 else /* add a new server without a cell */
535 tsp = cm_NewServer(&saddr, CM_SERVER_FILE, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
536 tsp->ipRank = (USHORT)dwRank;
540 RegCloseKey(hkPrefs);
545 * AFSD Initialization
548 int afsd_InitCM(char **reasonP)
551 afs_uint64 cacheBlocks;
558 DWORD rx_enable_peer_stats;
559 DWORD rx_enable_process_stats;
564 long virtualCache = 0;
565 char rootCellName[256];
566 struct rx_service *serverp;
567 static struct rx_securityClass *nullServerSecurityClassp;
575 /*int freelanceEnabled;*/
579 int cm_noIPAddr; /* number of client network interfaces */
580 int cm_IPAddr[CM_MAXINTERFACE_ADDR]; /* client's IP address in host order */
581 int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
582 int cm_NetMtu[CM_MAXINTERFACE_ADDR]; /* client's MTU sizes */
583 int cm_NetFlags[CM_MAXINTERFACE_ADDR]; /* network flags */
585 WSAStartup(0x0101, &WSAjunk);
587 afsd_initUpperCaseTable();
588 init_et_to_sys_error();
590 /* setup osidebug server at RPC slot 1000 */
591 osi_LongToUID(1000, &debugID);
592 code = osi_InitDebug(&debugID);
593 afsi_log("osi_InitDebug code %d", code);
595 // osi_LockTypeSetDefault("stat"); /* comment this out for speed */
597 if (code == RPC_S_NO_PROTSEQS)
598 *reasonP = "No RPC Protocol Sequences registered. Check HKLM\\SOFTWARE\\Microsoft\\RPC\\ClientProtocols";
600 *reasonP = "unknown error";
605 gethostname(cm_HostName, sizeof(cm_HostName));
606 afsi_log("gethostname %s", cm_HostName);
607 thp = gethostbyname(cm_HostName);
608 memcpy(&cm_HostAddr, thp->h_addr_list[0], 4);
610 /* seed random number generator */
611 srand(ntohl(cm_HostAddr));
613 /* Look up configuration parameters in Registry */
614 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
615 0, KEY_QUERY_VALUE, &parmKey);
616 if (code != ERROR_SUCCESS) {
617 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
618 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
619 NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
620 StringCbPrintfA(buf, sizeof(buf),
621 "Failure in configuration while opening Registry: %s",
623 osi_panic(buf, __FILE__, __LINE__);
626 dummyLen = sizeof(maxcpus);
627 code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
628 (BYTE *) &maxcpus, &dummyLen);
629 if (code == ERROR_SUCCESS) {
631 DWORD_PTR processAffinityMask, systemAffinityMask;
633 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION,
634 FALSE, GetCurrentProcessId());
635 if ( hProcess != NULL &&
636 GetProcessAffinityMask(hProcess, &processAffinityMask, &systemAffinityMask) )
639 DWORD_PTR mask, newAffinityMask;
646 for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits && n<maxcpus; i++ ) {
647 if ( processAffinityMask & mask ) {
648 newAffinityMask |= mask;
654 SetProcessAffinityMask(hProcess, newAffinityMask);
655 CloseHandle(hProcess);
656 afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, n);
658 afsi_log("CPU Restrictions set to %d cpu(s); unable to access process information", maxcpus);
662 dummyLen = sizeof(TraceOption);
663 code = RegQueryValueEx(parmKey, "TraceOption", NULL, NULL,
664 (BYTE *) &TraceOption, &dummyLen);
665 afsi_log("Trace Options = %lX", TraceOption);
667 dummyLen = sizeof(traceBufSize);
668 code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
669 (BYTE *) &traceBufSize, &dummyLen);
670 if (code == ERROR_SUCCESS)
671 afsi_log("Trace Buffer size %d", traceBufSize);
673 traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
674 afsi_log("Default trace buffer size %d", traceBufSize);
677 /* setup and enable debug log */
678 afsd_logp = osi_LogCreate("afsd", traceBufSize);
679 afsi_log("osi_LogCreate log addr %x", PtrToUlong(afsd_logp));
680 if ((TraceOption & 0x8)
685 osi_LogEnable(afsd_logp);
689 osi_Log0(afsd_logp, "Log init");
691 dummyLen = sizeof(cacheSize);
692 code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
693 (BYTE *) &cacheSize, &dummyLen);
694 if (code == ERROR_SUCCESS)
695 afsi_log("Cache size %d", cacheSize);
697 cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
698 afsi_log("Default cache size %d", cacheSize);
701 dummyLen = sizeof(logChunkSize);
702 code = RegQueryValueEx(parmKey, "ChunkSize", NULL, NULL,
703 (BYTE *) &logChunkSize, &dummyLen);
704 if (code == ERROR_SUCCESS) {
705 if (logChunkSize < 12 || logChunkSize > 30) {
706 afsi_log("Invalid chunk size %d, using default",
708 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
711 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
713 cm_logChunkSize = logChunkSize;
714 cm_chunkSize = 1 << logChunkSize;
715 afsi_log("Chunk size %u (%d)", cm_chunkSize, cm_logChunkSize);
717 dummyLen = sizeof(blockSize);
718 code = RegQueryValueEx(parmKey, "blockSize", NULL, NULL,
719 (BYTE *) &blockSize, &dummyLen);
720 if (code == ERROR_SUCCESS) {
722 (blockSize > 1024 && (blockSize % CM_CONFIGDEFAULT_BLOCKSIZE != 0)))
724 afsi_log("Invalid block size %u specified, using default", blockSize);
725 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
728 * if the blockSize is less than 1024 we permit the blockSize to be
729 * specified in multiples of the default blocksize
731 if (blockSize <= 1024)
732 blockSize *= CM_CONFIGDEFAULT_BLOCKSIZE;
735 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
737 if (blockSize > cm_chunkSize) {
738 afsi_log("Block size (%d) cannot be larger than Chunk size (%d).",
739 blockSize, cm_chunkSize);
740 blockSize = cm_chunkSize;
742 if (cm_chunkSize % blockSize != 0) {
743 afsi_log("Block size (%d) must be a factor of Chunk size (%d).",
744 blockSize, cm_chunkSize);
745 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
747 afsi_log("Block size %u", blockSize);
749 dummyLen = sizeof(numBkgD);
750 code = RegQueryValueEx(parmKey, "Daemons", NULL, NULL,
751 (BYTE *) &numBkgD, &dummyLen);
752 if (code == ERROR_SUCCESS) {
753 if (numBkgD > CM_MAX_DAEMONS)
754 numBkgD = CM_MAX_DAEMONS;
755 afsi_log("%d background daemons", numBkgD);
757 numBkgD = CM_CONFIGDEFAULT_DAEMONS;
758 afsi_log("Defaulting to %d background daemons", numBkgD);
761 dummyLen = sizeof(numSvThreads);
762 code = RegQueryValueEx(parmKey, "ServerThreads", NULL, NULL,
763 (BYTE *) &numSvThreads, &dummyLen);
764 if (code == ERROR_SUCCESS)
765 afsi_log("%d server threads", numSvThreads);
767 numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
768 afsi_log("Defaulting to %d server threads", numSvThreads);
771 dummyLen = sizeof(stats);
772 code = RegQueryValueEx(parmKey, "Stats", NULL, NULL,
773 (BYTE *) &stats, &dummyLen);
774 if (code == ERROR_SUCCESS)
775 afsi_log("Status cache entries: %d", stats);
777 stats = CM_CONFIGDEFAULT_STATS;
778 afsi_log("Default status cache entries: %d", stats);
781 dummyLen = sizeof(volumes);
782 code = RegQueryValueEx(parmKey, "Volumes", NULL, NULL,
783 (BYTE *) &volumes, &dummyLen);
784 if (code == ERROR_SUCCESS)
785 afsi_log("Volumes cache entries: %d", volumes);
787 volumes = CM_CONFIGDEFAULT_STATS / 3;
788 afsi_log("Default volume cache entries: %d", volumes);
791 dummyLen = sizeof(ltt);
792 code = RegQueryValueEx(parmKey, "LogoffTokenTransfer", NULL, NULL,
793 (BYTE *) <t, &dummyLen);
794 if (code != ERROR_SUCCESS)
796 smb_LogoffTokenTransfer = ltt;
797 afsi_log("Logoff token transfer %s", (ltt ? "on" : "off"));
800 dummyLen = sizeof(ltto);
801 code = RegQueryValueEx(parmKey, "LogoffTokenTransferTimeout",
802 NULL, NULL, (BYTE *) <to, &dummyLen);
803 if (code != ERROR_SUCCESS)
808 smb_LogoffTransferTimeout = ltto;
809 afsi_log("Logoff token transfer timeout %d seconds", ltto);
811 dummyLen = sizeof(cm_rootVolumeName);
812 code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
813 cm_rootVolumeName, &dummyLen);
814 if (code == ERROR_SUCCESS)
815 afsi_log("Root volume %s", cm_rootVolumeName);
817 StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
818 afsi_log("Default root volume name root.afs");
821 cm_mountRootLen = sizeof(cm_mountRoot);
822 code = RegQueryValueEx(parmKey, "MountRoot", NULL, NULL,
823 cm_mountRoot, &cm_mountRootLen);
824 if (code == ERROR_SUCCESS) {
825 afsi_log("Mount root %s", cm_mountRoot);
826 cm_mountRootLen = (DWORD)strlen(cm_mountRoot);
828 StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
833 dummyLen = sizeof(buf);
834 code = RegQueryValueEx(parmKey, "CachePath", NULL, ®Type,
836 if (code == ERROR_SUCCESS && buf[0]) {
837 if (regType == REG_EXPAND_SZ) {
838 dummyLen = ExpandEnvironmentStrings(buf, cm_CachePath, sizeof(cm_CachePath));
839 if (dummyLen > sizeof(cm_CachePath)) {
840 afsi_log("Cache path [%s] longer than %d after expanding env strings", buf, sizeof(cm_CachePath));
841 osi_panic("CachePath too long", __FILE__, __LINE__);
844 StringCbCopyA(cm_CachePath, sizeof(cm_CachePath), buf);
846 afsi_log("Cache path %s", cm_CachePath);
848 dummyLen = ExpandEnvironmentStrings("%TEMP%\\AFSCache", cm_CachePath, sizeof(cm_CachePath));
849 if (dummyLen > sizeof(cm_CachePath)) {
850 afsi_log("Cache path [%%TEMP%%\\AFSCache] longer than %d after expanding env strings",
851 sizeof(cm_CachePath));
852 osi_panic("CachePath too long", __FILE__, __LINE__);
854 afsi_log("Default cache path %s", cm_CachePath);
857 dummyLen = sizeof(virtualCache);
858 code = RegQueryValueEx(parmKey, "NonPersistentCaching", NULL, NULL,
859 (LPBYTE)&virtualCache, &dummyLen);
860 afsi_log("Cache type is %s", (virtualCache?"VIRTUAL":"FILE"));
863 dummyLen = sizeof(cm_ValidateCache);
864 code = RegQueryValueEx(parmKey, "ValidateCache", NULL, NULL,
865 (LPBYTE)&cm_ValidateCache, &dummyLen);
866 if ( cm_ValidateCache < 0 || cm_ValidateCache > 2 )
867 cm_ValidateCache = 1;
868 switch (cm_ValidateCache) {
870 afsi_log("Cache Validation disabled");
873 afsi_log("Cache Validation on Startup");
876 afsi_log("Cache Validation on Startup and Shutdown");
881 dummyLen = sizeof(traceOnPanic);
882 code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
883 (BYTE *) &traceOnPanic, &dummyLen);
884 if (code != ERROR_SUCCESS)
885 traceOnPanic = 1; /* log */
886 afsi_log("Set to %s on panic", traceOnPanic ? "trap" : "not trap");
888 dummyLen = sizeof(reportSessionStartups);
889 code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
890 (BYTE *) &reportSessionStartups, &dummyLen);
891 if (code == ERROR_SUCCESS)
892 afsi_log("Session startups %s be recorded in the Event Log",
893 reportSessionStartups ? "will" : "will not");
895 reportSessionStartups = 0;
899 for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
900 cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
901 cm_sysNameList[i][0] = '\0';
903 cm_sysName = cm_sysNameList[0];
905 dummyLen = sizeof(buf);
906 code = RegQueryValueEx(parmKey, "SysName", NULL, NULL, buf, &dummyLen);
907 if (code != ERROR_SUCCESS || !buf[0]) {
909 StringCbCopyA(buf, sizeof(buf), "ia64_win64");
910 #elif defined(_AMD64_)
911 StringCbCopyA(buf, sizeof(buf), "amd64_win64 x86_win32 i386_w2k");
912 #else /* assume x86 32-bit */
913 StringCbCopyA(buf, sizeof(buf), "x86_win32 i386_w2k i386_nt40");
916 afsi_log("Sys name %s", buf);
918 /* breakup buf into individual search string entries */
919 for (p = q = buf; p < buf + dummyLen; p++)
921 if (*p == '\0' || isspace(*p)) {
922 memcpy(cm_sysNameList[cm_sysNameCount],q,p-q);
923 cm_sysNameList[cm_sysNameCount][p-q] = '\0';
930 } while (*p == '\0' || isspace(*p));
936 StringCbCopyA(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
938 dummyLen = sizeof(cryptall);
939 code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
940 (BYTE *) &cryptall, &dummyLen);
941 if (code == ERROR_SUCCESS) {
942 afsi_log("SecurityLevel is %s", cryptall?"crypt":"clear");
945 afsi_log("Default SecurityLevel is clear");
949 LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_ON);
951 LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_OFF);
953 dummyLen = sizeof(cryptall);
954 code = RegQueryValueEx(parmKey, "ForceAnonVLDB", NULL, NULL,
955 (BYTE *) &cm_anonvldb, &dummyLen);
956 afsi_log("CM ForceAnonVLDB is %s", cm_anonvldb ? "on" : "off");
959 dummyLen = sizeof(cm_dnsEnabled);
960 code = RegQueryValueEx(parmKey, "UseDNS", NULL, NULL,
961 (BYTE *) &cm_dnsEnabled, &dummyLen);
962 if (code == ERROR_SUCCESS) {
963 afsi_log("DNS %s be used to find AFS cell servers",
964 cm_dnsEnabled ? "will" : "will not");
967 cm_dnsEnabled = 1; /* default on */
968 afsi_log("Default to use DNS to find AFS cell servers");
970 #else /* AFS_AFSDB_ENV */
971 afsi_log("AFS not built with DNS support to find AFS cell servers");
972 #endif /* AFS_AFSDB_ENV */
974 #ifdef AFS_FREELANCE_CLIENT
975 dummyLen = sizeof(cm_freelanceEnabled);
976 code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
977 (BYTE *) &cm_freelanceEnabled, &dummyLen);
978 if (code == ERROR_SUCCESS) {
979 afsi_log("Freelance client feature %s activated",
980 cm_freelanceEnabled ? "is" : "is not");
983 cm_freelanceEnabled = 1; /* default on */
985 #endif /* AFS_FREELANCE_CLIENT */
987 dummyLen = sizeof(smb_hideDotFiles);
988 code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
989 (BYTE *) &smb_hideDotFiles, &dummyLen);
990 if (code != ERROR_SUCCESS) {
991 smb_hideDotFiles = 1; /* default on */
993 afsi_log("Dot files/dirs will %sbe marked hidden",
994 smb_hideDotFiles ? "" : "not ");
996 dummyLen = sizeof(smb_maxMpxRequests);
997 code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
998 (BYTE *) &smb_maxMpxRequests, &dummyLen);
999 if (code != ERROR_SUCCESS) {
1000 smb_maxMpxRequests = 50;
1002 afsi_log("Maximum number of multiplexed sessions is %d", smb_maxMpxRequests);
1004 dummyLen = sizeof(smb_maxVCPerServer);
1005 code = RegQueryValueEx(parmKey, "MaxVCPerServer", NULL, NULL,
1006 (BYTE *) &smb_maxVCPerServer, &dummyLen);
1007 if (code != ERROR_SUCCESS) {
1008 smb_maxVCPerServer = 100;
1010 afsi_log("Maximum number of VCs per server is %d", smb_maxVCPerServer);
1012 dummyLen = sizeof(smb_authType);
1013 code = RegQueryValueEx(parmKey, "SMBAuthType", NULL, NULL,
1014 (BYTE *) &smb_authType, &dummyLen);
1016 if (code != ERROR_SUCCESS ||
1017 (smb_authType != SMB_AUTH_EXTENDED && smb_authType != SMB_AUTH_NTLM && smb_authType != SMB_AUTH_NONE)) {
1018 smb_authType = SMB_AUTH_EXTENDED; /* default is to use extended authentication */
1020 afsi_log("SMB authentication type is %s", ((smb_authType == SMB_AUTH_NONE)?"NONE":((smb_authType == SMB_AUTH_EXTENDED)?"EXTENDED":"NTLM")));
1022 dummyLen = sizeof(rx_nojumbo);
1023 code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
1024 (BYTE *) &rx_nojumbo, &dummyLen);
1025 if (code != ERROR_SUCCESS) {
1029 afsi_log("RX Jumbograms are disabled");
1031 dummyLen = sizeof(rx_extraPackets);
1032 code = RegQueryValueEx(parmKey, "RxExtraPackets", NULL, NULL,
1033 (BYTE *) &rx_extraPackets, &dummyLen);
1034 if (code != ERROR_SUCCESS) {
1035 rx_extraPackets = 120;
1037 if (rx_extraPackets)
1038 afsi_log("RX extraPackets is %d", rx_extraPackets);
1040 dummyLen = sizeof(rx_mtu);
1041 code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
1042 (BYTE *) &rx_mtu, &dummyLen);
1043 if (code != ERROR_SUCCESS || !rx_mtu) {
1047 afsi_log("RX maximum MTU is %d", rx_mtu);
1049 dummyLen = sizeof(rx_enable_peer_stats);
1050 code = RegQueryValueEx(parmKey, "RxEnablePeerStats", NULL, NULL,
1051 (BYTE *) &rx_enable_peer_stats, &dummyLen);
1052 if (code != ERROR_SUCCESS) {
1053 rx_enable_peer_stats = 1;
1055 if (rx_enable_peer_stats)
1056 afsi_log("RX Peer Statistics gathering is enabled");
1058 afsi_log("RX Peer Statistics gathering is disabled");
1060 dummyLen = sizeof(rx_enable_process_stats);
1061 code = RegQueryValueEx(parmKey, "RxEnableProcessStats", NULL, NULL,
1062 (BYTE *) &rx_enable_process_stats, &dummyLen);
1063 if (code != ERROR_SUCCESS) {
1064 rx_enable_process_stats = 1;
1066 if (rx_enable_process_stats)
1067 afsi_log("RX Process Statistics gathering is enabled");
1069 afsi_log("RX Process Statistics gathering is disabled");
1071 dummyLen = sizeof(dwValue);
1073 code = RegQueryValueEx(parmKey, "RxEnableHotThread", NULL, NULL,
1074 (BYTE *) &dwValue, &dummyLen);
1075 if (code != ERROR_SUCCESS || dwValue != 0) {
1076 rx_EnableHotThread();
1077 afsi_log("RX Hot Thread is enabled");
1080 afsi_log("RX Hot Thread is disabled");
1082 dummyLen = sizeof(DWORD);
1083 code = RegQueryValueEx(parmKey, "CallBackPort", NULL, NULL,
1084 (BYTE *) &dwValue, &dummyLen);
1085 if (code == ERROR_SUCCESS) {
1086 cm_callbackport = (unsigned short) dwValue;
1088 afsi_log("CM CallBackPort is %u", cm_callbackport);
1090 dummyLen = sizeof(DWORD);
1091 code = RegQueryValueEx(parmKey, "EnableServerLocks", NULL, NULL,
1092 (BYTE *) &dwValue, &dummyLen);
1093 if (code == ERROR_SUCCESS) {
1094 cm_enableServerLocks = (unsigned short) dwValue;
1096 switch (cm_enableServerLocks) {
1098 afsi_log("EnableServerLocks: never");
1101 afsi_log("EnableServerLocks: always");
1105 afsi_log("EnableServerLocks: server requested");
1109 dummyLen = sizeof(DWORD);
1110 code = RegQueryValueEx(parmKey, "DeleteReadOnly", NULL, NULL,
1111 (BYTE *) &dwValue, &dummyLen);
1112 if (code == ERROR_SUCCESS) {
1113 cm_deleteReadOnly = (unsigned short) dwValue;
1115 afsi_log("CM DeleteReadOnly is %u", cm_deleteReadOnly);
1118 dummyLen = sizeof(DWORD);
1119 code = RegQueryValueEx(parmKey, "BPlusTrees", NULL, NULL,
1120 (BYTE *) &dwValue, &dummyLen);
1121 if (code == ERROR_SUCCESS) {
1122 cm_BPlusTrees = (unsigned short) dwValue;
1124 afsi_log("CM BPlusTrees is %u", cm_BPlusTrees);
1126 if (cm_BPlusTrees && !cm_InitBPlusDir()) {
1128 afsi_log("CM BPlusTree initialization failure; disabled for this session");
1131 afsi_log("CM BPlusTrees is not supported");
1134 if ((RegQueryValueEx( parmKey, "PrefetchExecutableExtensions", 0,
1135 ®Type, NULL, &dummyLen) == ERROR_SUCCESS) &&
1136 (regType == REG_MULTI_SZ))
1139 dummyLen += 3; /* in case the source string is not nul terminated */
1140 pSz = malloc(dummyLen);
1141 if ((RegQueryValueEx( parmKey, "PrefetchExecutableExtensions", 0, ®Type,
1142 pSz, &dummyLen) == ERROR_SUCCESS) &&
1143 (regType == REG_MULTI_SZ))
1148 for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += strlen(p) + 1);
1150 smb_ExecutableExtensions = malloc(sizeof(char *) * (cnt+1));
1152 for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += strlen(p) + 1)
1154 smb_ExecutableExtensions[cnt] = p;
1155 afsi_log("PrefetchExecutableExtension: \"%s\"", p);
1157 smb_ExecutableExtensions[cnt] = NULL;
1160 if (!smb_ExecutableExtensions)
1163 if (!smb_ExecutableExtensions)
1164 afsi_log("No PrefetchExecutableExtensions");
1166 dummyLen = sizeof(DWORD);
1167 code = RegQueryValueEx(parmKey, "OfflineReadOnlyIsValid", NULL, NULL,
1168 (BYTE *) &dwValue, &dummyLen);
1169 if (code == ERROR_SUCCESS) {
1170 cm_OfflineROIsValid = (unsigned short) dwValue;
1172 afsi_log("CM OfflineReadOnlyIsValid is %u", cm_deleteReadOnly);
1174 dummyLen = sizeof(DWORD);
1175 code = RegQueryValueEx(parmKey, "GiveUpAllCallBacks", NULL, NULL,
1176 (BYTE *) &dwValue, &dummyLen);
1177 if (code == ERROR_SUCCESS) {
1178 cm_giveUpAllCBs = (unsigned short) dwValue;
1180 afsi_log("CM GiveUpAllCallBacks is %u", cm_giveUpAllCBs);
1182 dummyLen = sizeof(DWORD);
1183 code = RegQueryValueEx(parmKey, "FollowBackupPath", NULL, NULL,
1184 (BYTE *) &dwValue, &dummyLen);
1185 if (code == ERROR_SUCCESS) {
1186 cm_followBackupPath = (unsigned short) dwValue;
1188 afsi_log("CM FollowBackupPath is %u", cm_followBackupPath);
1190 RegCloseKey (parmKey);
1192 cacheBlocks = ((afs_uint64)cacheSize * 1024) / blockSize;
1194 /* get network related info */
1195 cm_noIPAddr = CM_MAXINTERFACE_ADDR;
1196 code = syscfg_GetIFInfo(&cm_noIPAddr,
1197 cm_IPAddr, cm_SubnetMask,
1198 cm_NetMtu, cm_NetFlags);
1200 if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
1201 afsi_log("syscfg_GetIFInfo error code %d", code);
1203 afsi_log("First Network address %x SubnetMask %x",
1204 cm_IPAddr[0], cm_SubnetMask[0]);
1207 * Save client configuration for GetCacheConfig requests
1209 cm_initParams.nChunkFiles = 0;
1210 cm_initParams.nStatCaches = stats;
1211 cm_initParams.nDataCaches = (afs_uint32)(cacheBlocks > 0xFFFFFFFF ? 0xFFFFFFFF : cacheBlocks);
1212 cm_initParams.nVolumeCaches = volumes;
1213 cm_initParams.firstChunkSize = cm_chunkSize;
1214 cm_initParams.otherChunkSize = cm_chunkSize;
1215 cm_initParams.cacheSize = cacheSize;
1216 cm_initParams.setTime = 0;
1217 cm_initParams.memCache = 1;
1219 /* Ensure the AFS Netbios Name is registered to allow loopback access */
1220 configureBackConnectionHostNames();
1222 /* init user daemon, and other packages */
1235 code = cm_InitMappedMemory(virtualCache, cm_CachePath, stats, volumes, cm_chunkSize, cacheBlocks, blockSize);
1236 afsi_log("cm_InitMappedMemory code %x", code);
1238 *reasonP = "error initializing cache file";
1242 #ifdef AFS_AFSDB_ENV
1243 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
1244 if (cm_InitDNS(cm_dnsEnabled) == -1)
1245 cm_dnsEnabled = 0; /* init failed, so deactivate */
1246 afsi_log("cm_InitDNS %d", cm_dnsEnabled);
1250 /* Set RX parameters before initializing RX */
1253 afsi_log("rx_SetNoJumbo successful");
1256 if ( rx_mtu != -1 ) {
1257 rx_SetMaxMTU(rx_mtu);
1258 afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
1261 /* initialize RX, and tell it to listen to the callbackport,
1262 * which is used for callback RPC messages.
1264 code = rx_Init(htons(cm_callbackport));
1266 afsi_log("rx_Init code %x - retrying with a random port number", code);
1269 afsi_log("rx_Init code %x", code);
1271 *reasonP = "afsd: failed to init rx client";
1275 /* create an unauthenticated service #1 for callbacks */
1276 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
1277 serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
1278 RXAFSCB_ExecuteRequest);
1279 afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
1280 if (serverp == NULL) {
1281 *reasonP = "unknown error";
1285 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
1286 serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
1287 &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
1288 afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
1289 if (serverp == NULL) {
1290 *reasonP = "unknown error";
1294 /* start server threads, *not* donating this one to the pool */
1296 afsi_log("rx_StartServer");
1298 if (rx_enable_peer_stats)
1299 rx_enablePeerRPCStats();
1301 if (rx_enable_process_stats)
1302 rx_enableProcessRPCStats();
1304 code = cm_GetRootCellName(rootCellName);
1305 afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s",
1306 code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
1307 if (code != 0 && !cm_freelanceEnabled)
1309 *reasonP = "can't find root cell name in " AFS_CELLSERVDB;
1312 else if (cm_freelanceEnabled)
1313 cm_data.rootCellp = NULL;
1315 if (code == 0 && !cm_freelanceEnabled)
1317 cm_data.rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
1318 afsi_log("cm_GetCell addr %x", PtrToUlong(cm_data.rootCellp));
1319 if (cm_data.rootCellp == NULL)
1321 *reasonP = "can't find root cell in " AFS_CELLSERVDB;
1326 #ifdef AFS_FREELANCE_CLIENT
1327 if (cm_freelanceEnabled)
1331 /* Initialize the RPC server for session keys */
1334 afsd_InitServerPreferences();
1338 int afsd_ShutdownCM(void)
1340 cm_ReleaseSCache(cm_data.rootSCachep);
1345 int afsd_InitDaemons(char **reasonP)
1352 /* this should really be in an init daemon from here on down */
1354 if (!cm_freelanceEnabled) {
1357 osi_Log0(afsd_logp, "Loading Root Volume from cell");
1359 code = cm_FindVolumeByName(cm_data.rootCellp, cm_rootVolumeName, cm_rootUserp,
1360 &req, CM_GETVOL_FLAG_CREATE, &cm_data.rootVolumep);
1361 afsi_log("cm_FindVolumeByName code %x root vol %x", code,
1362 (code ? (cm_volume_t *)-1 : cm_data.rootVolumep));
1363 } while (code && --attempts);
1365 *reasonP = "can't find root volume in root cell";
1370 /* compute the root fid */
1371 if (!cm_freelanceEnabled) {
1372 cm_SetFid(&cm_data.rootFid, cm_data.rootCellp->cellID, cm_GetROVolumeID(cm_data.rootVolumep), 1, 1);
1375 cm_FakeRootFid(&cm_data.rootFid);
1377 code = cm_GetSCache(&cm_data.rootFid, &cm_data.rootSCachep, cm_rootUserp, &req);
1378 afsi_log("cm_GetSCache code %x scache %x", code,
1379 (code ? (cm_scache_t *)-1 : cm_data.rootSCachep));
1381 *reasonP = "unknown error";
1385 cm_InitDaemon(numBkgD);
1386 afsi_log("cm_InitDaemon complete");
1391 int afsd_InitSMB(char **reasonP, void *aMBfunc)
1398 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
1399 0, KEY_QUERY_VALUE, &parmKey);
1400 if (code == ERROR_SUCCESS) {
1401 dummyLen = sizeof(DWORD);
1402 code = RegQueryValueEx(parmKey, "StoreAnsiFilenames", NULL, NULL,
1403 (BYTE *) &dwValue, &dummyLen);
1404 if (code == ERROR_SUCCESS)
1405 smb_StoreAnsiFilenames = dwValue ? 1 : 0;
1406 afsi_log("StoreAnsiFilenames = %d", smb_StoreAnsiFilenames);
1408 dummyLen = sizeof(DWORD);
1409 code = RegQueryValueEx(parmKey, "EnableSMBAsyncStore", NULL, NULL,
1410 (BYTE *) &dwValue, &dummyLen);
1411 if (code == ERROR_SUCCESS)
1412 smb_AsyncStore = dwValue == 2 ? 2 : (dwValue ? 1 : 0);
1413 afsi_log("EnableSMBAsyncStore = %d", smb_AsyncStore);
1415 dummyLen = sizeof(DWORD);
1416 code = RegQueryValueEx(parmKey, "SMBAsyncStoreSize", NULL, NULL,
1417 (BYTE *) &dwValue, &dummyLen);
1418 if (code == ERROR_SUCCESS) {
1419 /* Should check for >= blocksize && <= chunksize && round down to multiple of blocksize */
1420 if (dwValue > cm_chunkSize)
1421 smb_AsyncStoreSize = cm_chunkSize;
1422 else if (dwValue < cm_data.buf_blockSize)
1423 smb_AsyncStoreSize = cm_data.buf_blockSize;
1425 smb_AsyncStoreSize = (dwValue & ~(cm_data.buf_blockSize-1));
1427 smb_AsyncStoreSize = CM_CONFIGDEFAULT_ASYNCSTORESIZE;
1428 afsi_log("SMBAsyncStoreSize = %d", smb_AsyncStoreSize);
1430 RegCloseKey (parmKey);
1433 /* Do this last so that we don't handle requests before init is done.
1434 * Here we initialize the SMB listener.
1436 smb_Init(afsd_logp, smb_UseV3, numSvThreads, aMBfunc);
1437 afsi_log("smb_Init complete");
1450 #pragma pack( push, before_imagehlp, 8 )
1451 #include <imagehlp.h>
1452 #pragma pack( pop, before_imagehlp )
1454 #define MAXNAMELEN 1024
1456 void afsd_printStack(HANDLE hThread, CONTEXT *c)
1458 HANDLE hProcess = GetCurrentProcess();
1460 #if defined(_AMD64_)
1462 #elif defined(_X86_)
1466 char functionName[MAXNAMELEN];
1468 IMAGEHLP_MODULE Module;
1472 IMAGEHLP_SYMBOL *pSym;
1474 afsi_log_useTimestamp = 0;
1476 pSym = (IMAGEHLP_SYMBOL *) GlobalAlloc(0, sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN);
1478 memset( &s, '\0', sizeof s );
1479 if (!SymInitialize(hProcess, NULL, 1) )
1481 afsi_log("SymInitialize(): GetLastError() = %lu\n", GetLastError() );
1483 SymCleanup( hProcess );
1489 symOptions = SymGetOptions();
1490 symOptions |= SYMOPT_LOAD_LINES;
1491 symOptions &= ~SYMOPT_UNDNAME;
1492 SymSetOptions( symOptions );
1495 * init STACKFRAME for first call
1496 * Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
1497 * Notes: will have to be #ifdef-ed for Alphas; MIPSes are dead anyway,
1498 * and good riddance.
1500 #if defined (_ALPHA_) || defined (_MIPS_) || defined (_PPC_)
1501 #error The STACKFRAME initialization in afsd_printStack() for this platform
1502 #error must be properly configured
1503 #elif defined(_AMD64_)
1504 s.AddrPC.Offset = c->Rip;
1505 s.AddrPC.Mode = AddrModeFlat;
1506 s.AddrFrame.Offset = c->Rbp;
1507 s.AddrFrame.Mode = AddrModeFlat;
1509 s.AddrPC.Offset = c->Eip;
1510 s.AddrPC.Mode = AddrModeFlat;
1511 s.AddrFrame.Offset = c->Ebp;
1512 s.AddrFrame.Mode = AddrModeFlat;
1515 memset( pSym, '\0', sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN );
1516 pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL);
1517 pSym->MaxNameLength = MAXNAMELEN;
1519 memset( &Line, '\0', sizeof Line );
1520 Line.SizeOfStruct = sizeof Line;
1522 memset( &Module, '\0', sizeof Module );
1523 Module.SizeOfStruct = sizeof Module;
1527 afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
1529 for ( frameNum = 0; ; ++ frameNum )
1532 * get next stack frame (StackWalk(), SymFunctionTableAccess(),
1533 * SymGetModuleBase()). if this returns ERROR_INVALID_ADDRESS (487) or
1534 * ERROR_NOACCESS (998), you can assume that either you are done, or
1535 * that the stack is so hosed that the next deeper frame could not be
1538 if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c,
1539 NULL, SymFunctionTableAccess, SymGetModuleBase,
1543 /* display its contents */
1544 afsi_log("\n%3d %c%c %08lx %08lx %08lx %08lx ",
1545 frameNum, s.Far? 'F': '.', s.Virtual? 'V': '.',
1546 s.AddrPC.Offset, s.AddrReturn.Offset,
1547 s.AddrFrame.Offset, s.AddrStack.Offset );
1549 if ( s.AddrPC.Offset == 0 )
1551 afsi_log("(-nosymbols- PC == 0)" );
1555 /* show procedure info from a valid PC */
1556 if (!SymGetSymFromAddr(hProcess, s.AddrPC.Offset, &offset, pSym))
1558 if ( GetLastError() != ERROR_INVALID_ADDRESS )
1560 afsi_log("SymGetSymFromAddr(): errno = %lu",
1566 UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN,
1568 afsi_log("%s", functionName );
1572 afsi_log(" %+ld bytes", (long) offset);
1576 if (!SymGetLineFromAddr(hProcess, s.AddrPC.Offset, &offset, &Line))
1578 if (GetLastError() != ERROR_INVALID_ADDRESS)
1580 afsi_log("Error: SymGetLineFromAddr(): errno = %lu",
1586 afsi_log(" Line: %s(%lu) %+ld bytes", Line.FileName,
1587 Line.LineNumber, offset);
1591 /* no return address means no deeper stackframe */
1592 if (s.AddrReturn.Offset == 0)
1599 if (GetLastError() != 0)
1601 afsi_log("\nStackWalk(): errno = %lu\n", GetLastError());
1604 SymCleanup(hProcess);
1609 static DWORD *afsd_crtDbgBreakCurrent = NULL;
1610 static DWORD afsd_crtDbgBreaks[256];
1613 static EFaultRepRetVal (WINAPI *pReportFault)(LPEXCEPTION_POINTERS pep, DWORD dwMode) = NULL;
1614 static BOOL (WINAPI *pMiniDumpWriteDump)(HANDLE hProcess,DWORD ProcessId,HANDLE hFile,
1615 MINIDUMP_TYPE DumpType,
1616 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
1617 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
1618 PMINIDUMP_CALLBACK_INFORMATION CallbackParam) = NULL;
1627 code = GetEnvironmentVariable("TEMP", wd, sizeof(wd));
1628 if ( code == 0 || code > sizeof(wd) )
1630 if (!GetWindowsDirectory(wd, sizeof(wd)))
1633 StringCbCatA(wd, sizeof(wd), "\\afsd.dmp");
1634 return CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1635 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
1639 GenerateMiniDump(PEXCEPTION_POINTERS ep)
1641 if (IsDebuggerPresent())
1646 // Generate exception to get proper context in dump
1649 RaiseException(DBG_CONTINUE, 0, 0, NULL);
1651 __except(GenerateMiniDump(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION)
1657 MINIDUMP_EXCEPTION_INFORMATION eInfo;
1658 HANDLE hFile = NULL;
1659 HMODULE hDbgHelp = NULL;
1661 hDbgHelp = LoadLibrary("Dbghelp.dll");
1662 if ( hDbgHelp == NULL )
1665 (FARPROC) pMiniDumpWriteDump = GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
1666 if ( pMiniDumpWriteDump == NULL ) {
1667 FreeLibrary(hDbgHelp);
1671 hFile = OpenDumpFile();
1678 DWORD dwMiniDumpType = MiniDumpWithDataSegs;
1680 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
1681 0, KEY_QUERY_VALUE, &parmKey);
1682 if (code == ERROR_SUCCESS) {
1683 dummyLen = sizeof(DWORD);
1684 code = RegQueryValueEx(parmKey, "MiniDumpType", NULL, NULL,
1685 (BYTE *) &dwValue, &dummyLen);
1686 if (code == ERROR_SUCCESS)
1687 dwMiniDumpType = dwValue;
1688 RegCloseKey (parmKey);
1691 eInfo.ThreadId = GetCurrentThreadId();
1692 eInfo.ExceptionPointers = ep;
1693 eInfo.ClientPointers = FALSE;
1695 pMiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
1696 hFile, dwMiniDumpType, ep ? &eInfo : NULL,
1701 FreeLibrary(hDbgHelp);
1705 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
1709 BOOL allocRequestBrk = FALSE;
1711 HMODULE hLib = NULL;
1713 afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n",
1714 ep->ExceptionRecord->ExceptionCode,
1715 ep->ExceptionRecord->ExceptionAddress);
1718 if (afsd_crtDbgBreakCurrent &&
1719 *afsd_crtDbgBreakCurrent == _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent))
1721 allocRequestBrk = TRUE;
1722 afsi_log("Breaking on alloc request # %d\n", *afsd_crtDbgBreakCurrent);
1726 /* save context if we want to print the stack information */
1727 context = *ep->ContextRecord;
1729 afsd_printStack(GetCurrentThread(), &context);
1731 GenerateMiniDump(ep);
1733 hLib = LoadLibrary("Faultrep.dll");
1735 (FARPROC) pReportFault = GetProcAddress(hLib, "ReportFault");
1737 pReportFault(ep, 0);
1741 if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
1743 afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
1746 if (allocRequestBrk)
1748 afsd_crtDbgBreakCurrent++;
1749 _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
1753 ep->ContextRecord->Eip++;
1755 #if defined(_AMD64_)
1756 ep->ContextRecord->Rip++;
1758 return EXCEPTION_CONTINUE_EXECUTION;
1762 return EXCEPTION_CONTINUE_SEARCH;
1766 void afsd_SetUnhandledExceptionFilter()
1768 SetUnhandledExceptionFilter(afsd_ExceptionFilter);
1772 void afsd_DbgBreakAllocInit()
1774 memset(afsd_crtDbgBreaks, -1, sizeof(afsd_crtDbgBreaks));
1775 afsd_crtDbgBreakCurrent = afsd_crtDbgBreaks;
1778 void afsd_DbgBreakAdd(DWORD requestNumber)
1781 for (i = 0; i < sizeof(afsd_crtDbgBreaks) - 1; i++)
1783 if (afsd_crtDbgBreaks[i] == -1)
1788 afsd_crtDbgBreaks[i] = requestNumber;
1790 _CrtSetBreakAlloc(afsd_crtDbgBreaks[0]);