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>
24 #include <rx\rx_null.h>
26 #include <WINNT/syscfg.h>
30 #include "lanahelper.h"
32 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
33 extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
35 extern afs_int32 cryptall;
37 char AFSConfigKeyName[] =
38 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
42 char cm_rootVolumeName[64];
43 DWORD cm_rootVolumeNameLen;
44 cm_volume_t *cm_rootVolumep = NULL;
45 cm_cell_t *cm_rootCellp = NULL;
47 cm_scache_t *cm_rootSCachep;
48 char cm_mountRoot[1024];
49 DWORD cm_mountRootLen;
52 #ifdef AFS_FREELANCE_CLIENT
54 #endif /* freelance */
67 char cm_HostName[200];
70 char cm_NetbiosName[MAX_NB_NAME_LENGTH];
72 char cm_CachePath[200];
73 DWORD cm_CachePathLen;
75 BOOL isGateway = FALSE;
77 BOOL reportSessionStartups = FALSE;
79 cm_initparams_v1 cm_initParams;
82 * AFSD Initialization Log
84 * This is distinct from the regular debug logging facility.
85 * Log items go directly to a file, not to an array in memory, so that even
86 * if AFSD crashes, the log can be inspected.
92 int cm_dnsEnabled = 1;
95 char cm_NetBiosName[32];
97 extern initUpperCaseTable();
98 void afsd_initUpperCaseTable()
100 initUpperCaseTable();
107 char t[100], u[100], *p, *path;
111 afsi_file = INVALID_HANDLE_VALUE;
114 strcpy(wd, getenv("TEMP"));
118 code = GetWindowsDirectory(wd, sizeof(wd));
119 if (code == 0) return;
121 strcat(wd, "\\afsd_init.log");
122 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
123 afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
124 OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
125 SetFilePointer(afsi_file, 0, NULL, FILE_END);
126 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
127 strcat(t, ": Create log file\n");
128 strcat(u, ": Created log file\n");
129 WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
130 WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
132 path = getenv("PATH");
133 WriteFile(afsi_file, p, strlen(p), &zilch, NULL);
134 WriteFile(afsi_file, path, strlen(path), &zilch, NULL);
135 WriteFile(afsi_file, "\n", 1, &zilch, NULL);
138 static int afsi_log_useTimestamp = 1;
141 afsi_log(char *pattern, ...)
143 char s[100], t[100], d[100], u[300];
146 va_start(ap, pattern);
148 vsprintf(s, pattern, ap);
149 if ( afsi_log_useTimestamp ) {
150 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
151 GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
152 sprintf(u, "%s %s: %s\n", d, t, s);
153 if (afsi_file != INVALID_HANDLE_VALUE)
154 WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
159 if (afsi_file != INVALID_HANDLE_VALUE)
160 WriteFile(afsi_file, s, strlen(s), &zilch, NULL);
165 * Standard AFSD trace
168 void afsd_ForceTrace(BOOL flush)
177 len = GetTempPath(sizeof(buf)-10, buf);
178 strcpy(&buf[len], "/afsd.log");
179 handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
180 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
181 if (handle == INVALID_HANDLE_VALUE) {
183 osi_panic("Cannot create log file", __FILE__, __LINE__);
185 osi_LogPrint(afsd_logp, handle);
187 FlushFileBuffers(handle);
192 * AFSD Initialization
195 int afsd_InitCM(char **reasonP)
204 long rx_mtu, rx_nojumbo;
205 char rootCellName[256];
206 struct rx_service *serverp;
207 static struct rx_securityClass *nullServerSecurityClassp;
214 /*int freelanceEnabled;*/
216 lana_number_t lanaNum;
218 WSAStartup(0x0101, &WSAjunk);
220 afsd_initUpperCaseTable();
222 /* setup osidebug server at RPC slot 1000 */
223 osi_LongToUID(1000, &debugID);
224 code = osi_InitDebug(&debugID);
225 afsi_log("osi_InitDebug code %d", code);
227 // osi_LockTypeSetDefault("stat"); /* comment this out for speed *
229 *reasonP = "unknown error";
234 gethostname(cm_HostName, sizeof(cm_HostName));
235 afsi_log("gethostname %s", cm_HostName);
236 thp = gethostbyname(cm_HostName);
237 memcpy(&cm_HostAddr, thp->h_addr_list[0], 4);
239 /* seed random number generator */
240 srand(ntohl(cm_HostAddr));
242 /* Look up configuration parameters in Registry */
243 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
244 0, KEY_QUERY_VALUE, &parmKey);
245 if (code != ERROR_SUCCESS) {
246 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
247 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
248 NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
250 "Failure in configuration while opening Registry: %s",
252 osi_panic(buf, __FILE__, __LINE__);
255 dummyLen = sizeof(traceBufSize);
256 code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
257 (BYTE *) &traceBufSize, &dummyLen);
258 if (code == ERROR_SUCCESS)
259 afsi_log("Trace Buffer size %d", traceBufSize);
261 traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
262 afsi_log("Default trace buffer size %d", traceBufSize);
265 /* setup and enable debug log */
266 afsd_logp = osi_LogCreate("afsd", traceBufSize);
267 afsi_log("osi_LogCreate log addr %x", (int)afsd_logp);
268 osi_LogEnable(afsd_logp);
271 osi_Log0(afsd_logp, "Log init");
273 dummyLen = sizeof(cacheSize);
274 code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
275 (BYTE *) &cacheSize, &dummyLen);
276 if (code == ERROR_SUCCESS)
277 afsi_log("Cache size %d", cacheSize);
279 cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
280 afsi_log("Default cache size %d", cacheSize);
283 dummyLen = sizeof(logChunkSize);
284 code = RegQueryValueEx(parmKey, "ChunkSize", NULL, NULL,
285 (BYTE *) &logChunkSize, &dummyLen);
286 if (code == ERROR_SUCCESS) {
287 if (logChunkSize < 12 || logChunkSize > 30) {
288 afsi_log("Invalid chunk size %d, using default",
290 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
292 afsi_log("Chunk size %d", logChunkSize);
294 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
295 afsi_log("Default chunk size %d", logChunkSize);
297 cm_logChunkSize = logChunkSize;
298 cm_chunkSize = 1 << logChunkSize;
300 dummyLen = sizeof(numBkgD);
301 code = RegQueryValueEx(parmKey, "Daemons", NULL, NULL,
302 (BYTE *) &numBkgD, &dummyLen);
303 if (code == ERROR_SUCCESS)
304 afsi_log("%d background daemons", numBkgD);
306 numBkgD = CM_CONFIGDEFAULT_DAEMONS;
307 afsi_log("Defaulting to %d background daemons", numBkgD);
310 dummyLen = sizeof(numSvThreads);
311 code = RegQueryValueEx(parmKey, "ServerThreads", NULL, NULL,
312 (BYTE *) &numSvThreads, &dummyLen);
313 if (code == ERROR_SUCCESS)
314 afsi_log("%d server threads", numSvThreads);
316 numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
317 afsi_log("Defaulting to %d server threads", numSvThreads);
320 dummyLen = sizeof(stats);
321 code = RegQueryValueEx(parmKey, "Stats", NULL, NULL,
322 (BYTE *) &stats, &dummyLen);
323 if (code == ERROR_SUCCESS)
324 afsi_log("Status cache size %d", stats);
326 stats = CM_CONFIGDEFAULT_STATS;
327 afsi_log("Default status cache size %d", stats);
330 dummyLen = sizeof(ltt);
331 code = RegQueryValueEx(parmKey, "LogoffTokenTransfer", NULL, NULL,
332 (BYTE *) <t, &dummyLen);
333 if (code == ERROR_SUCCESS)
334 afsi_log("Logoff token transfer %s", (ltt ? "on" : "off"));
337 afsi_log("Logoff token transfer on by default");
339 smb_LogoffTokenTransfer = ltt;
342 dummyLen = sizeof(ltto);
343 code = RegQueryValueEx(parmKey, "LogoffTokenTransferTimeout",
344 NULL, NULL, (BYTE *) <to, &dummyLen);
345 if (code == ERROR_SUCCESS)
346 afsi_log("Logoff token tranfer timeout %d seconds",
350 afsi_log("Default logoff token transfer timeout 10 seconds");
355 smb_LogoffTransferTimeout = ltto;
357 dummyLen = sizeof(cm_rootVolumeName);
358 code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
359 cm_rootVolumeName, &dummyLen);
360 if (code == ERROR_SUCCESS)
361 afsi_log("Root volume %s", cm_rootVolumeName);
363 strcpy(cm_rootVolumeName, "root.afs");
364 afsi_log("Default root volume name root.afs");
367 cm_mountRootLen = sizeof(cm_mountRoot);
368 code = RegQueryValueEx(parmKey, "Mountroot", NULL, NULL,
369 cm_mountRoot, &cm_mountRootLen);
370 if (code == ERROR_SUCCESS) {
371 afsi_log("Mount root %s", cm_mountRoot);
372 cm_mountRootLen = strlen(cm_mountRoot);
374 strcpy(cm_mountRoot, "/afs");
379 dummyLen = sizeof(cm_CachePath);
380 code = RegQueryValueEx(parmKey, "CachePath", NULL, NULL,
381 cm_CachePath, &dummyLen);
382 if (code == ERROR_SUCCESS)
383 afsi_log("Cache path %s", cm_CachePath);
385 GetWindowsDirectory(cm_CachePath, sizeof(cm_CachePath));
386 cm_CachePath[2] = 0; /* get drive letter only */
387 strcat(cm_CachePath, "\\AFSCache");
388 afsi_log("Default cache path %s", cm_CachePath);
391 dummyLen = sizeof(traceOnPanic);
392 code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
393 (BYTE *) &traceOnPanic, &dummyLen);
394 if (code == ERROR_SUCCESS)
395 afsi_log("Set to %s on panic",
396 traceOnPanic ? "trap" : "not trap");
402 dummyLen = sizeof(reportSessionStartups);
403 code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
404 (BYTE *) &reportSessionStartups, &dummyLen);
405 if (code == ERROR_SUCCESS)
406 afsi_log("Session startups %s be recorded in the Event Log",
407 reportSessionStartups ? "will" : "will not");
409 reportSessionStartups = 0;
413 dummyLen = sizeof(cm_sysName);
414 code = RegQueryValueEx(parmKey, "SysName", NULL, NULL,
415 cm_sysName, &dummyLen);
416 if (code == ERROR_SUCCESS)
417 afsi_log("Sys name %s", cm_sysName);
419 strcat(cm_sysName, "i386_nt40");
420 afsi_log("Default sys name %s", cm_sysName);
423 dummyLen = sizeof(cryptall);
424 code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
425 (BYTE *) &cryptall, &dummyLen);
426 if (code == ERROR_SUCCESS)
427 afsi_log("SecurityLevel is %s", cryptall?"crypt":"clear");
429 cryptall = rxkad_clear;
430 afsi_log("Default SecurityLevel is clear");
434 dummyLen = sizeof(cm_dnsEnabled);
435 code = RegQueryValueEx(parmKey, "UseDNS", NULL, NULL,
436 (BYTE *) &cm_dnsEnabled, &dummyLen);
437 if (code == ERROR_SUCCESS) {
438 afsi_log("DNS %s be used to find AFS cell servers",
439 cm_dnsEnabled ? "will" : "will not");
442 cm_dnsEnabled = 1; /* default on */
443 afsi_log("Default to use DNS to find AFS cell servers");
445 #else /* AFS_AFSDB_ENV */
446 afsi_log("AFS not built with DNS support to find AFS cell servers");
447 #endif /* AFS_AFSDB_ENV */
449 #ifdef AFS_FREELANCE_CLIENT
450 dummyLen = sizeof(cm_freelanceEnabled);
451 code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
452 (BYTE *) &cm_freelanceEnabled, &dummyLen);
453 if (code == ERROR_SUCCESS) {
454 afsi_log("Freelance client feature %s activated",
455 cm_freelanceEnabled ? "is" : "is not");
458 cm_freelanceEnabled = 0; /* default off */
460 #endif /* AFS_FREELANCE_CLIENT */
462 dummyLen = sizeof(buf);
463 code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
464 (BYTE *) &buf, &dummyLen);
465 if (code == ERROR_SUCCESS) {
466 DWORD len = ExpandEnvironmentStrings(buf, cm_NetBiosName, MAX_NB_NAME_LENGTH);
467 if ( len > 0 && len <= MAX_NB_NAME_LENGTH ) {
468 afsi_log("Explicit NetBios name is used %s", cm_NetBiosName);
470 afsi_log("Unable to Expand Explicit NetBios name: %s", buf);
471 cm_NetBiosName[0] = 0; /* turn it off */
475 cm_NetBiosName[0] = 0; /* default off */
478 dummyLen = sizeof(smb_hideDotFiles);
479 code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
480 (BYTE *) &smb_hideDotFiles, &dummyLen);
481 if (code != ERROR_SUCCESS) {
482 smb_hideDotFiles = 1; /* default on */
484 afsi_log("Dot files/dirs will %sbe marked hidden",
485 smb_hideDotFiles ? "" : "not ");
487 dummyLen = sizeof(smb_maxMpxRequests);
488 code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
489 (BYTE *) &smb_maxMpxRequests, &dummyLen);
490 if (code != ERROR_SUCCESS) {
491 smb_maxMpxRequests = 50;
493 afsi_log("Maximum number of multiplexed sessions is %d", smb_maxMpxRequests);
495 dummyLen = sizeof(smb_maxVCPerServer);
496 code = RegQueryValueEx(parmKey, "MaxVCPerServer", NULL, NULL,
497 (BYTE *) &smb_maxVCPerServer, &dummyLen);
498 if (code != ERROR_SUCCESS) {
499 smb_maxVCPerServer = 100;
501 afsi_log("Maximum number of VCs per server is %d", smb_maxVCPerServer);
503 dummyLen = sizeof(rx_nojumbo);
504 code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
505 (BYTE *) &rx_nojumbo, &dummyLen);
506 if (code != ERROR_SUCCESS) {
510 afsi_log("RX Jumbograms are disabled");
512 dummyLen = sizeof(rx_mtu);
513 code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
514 (BYTE *) &rx_mtu, &dummyLen);
515 if (code != ERROR_SUCCESS || !rx_mtu) {
519 afsi_log("RX maximum MTU is %d", rx_mtu);
521 dummyLen = sizeof(ConnDeadtimeout);
522 code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
523 (BYTE *) &ConnDeadtimeout, &dummyLen);
524 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
526 dummyLen = sizeof(HardDeadtimeout);
527 code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
528 (BYTE *) &HardDeadtimeout, &dummyLen);
529 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
531 RegCloseKey (parmKey);
533 /* Call lanahelper to get Netbios name, lan adapter number and gateway flag */
534 if(SUCCEEDED(code = lana_GetUncServerNameEx(cm_NetbiosName, &lanaNum, &isGateway, LANA_NETBIOS_NAME_FULL))) {
535 LANadapter = (lanaNum == LANA_INVALID)? -1: lanaNum;
538 afsi_log("LAN adapter number %d", LANadapter);
540 afsi_log("LAN adapter number not determined");
543 afsi_log("Set for gateway service");
545 afsi_log("Using >%s< as SMB server name", cm_NetbiosName);
547 /* something went horribly wrong. We can't proceed without a netbios name */
548 sprintf(buf,"Netbios name could not be determined: %li", code);
549 osi_panic(buf, __FILE__, __LINE__);
552 /* setup early variables */
553 /* These both used to be configurable. */
555 buf_bufferSize = CM_CONFIGDEFAULT_BLOCKSIZE;
557 /* turn from 1024 byte units into memory blocks */
558 cacheBlocks = (cacheSize * 1024) / buf_bufferSize;
560 /* get network related info */
561 cm_noIPAddr = CM_MAXINTERFACE_ADDR;
562 code = syscfg_GetIFInfo(&cm_noIPAddr,
563 cm_IPAddr, cm_SubnetMask,
564 cm_NetMtu, cm_NetFlags);
566 if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
567 afsi_log("syscfg_GetIFInfo error code %d", code);
569 afsi_log("First Network address %x SubnetMask %x",
570 cm_IPAddr[0], cm_SubnetMask[0]);
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 /* Set RX parameters before initializing RX */
588 afsi_log("rx_SetNoJumbo successful");
591 if ( rx_mtu != -1 ) {
592 rx_SetMaxMTU(rx_mtu);
593 afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
596 /* Open Microsoft Firewall to allow in port 7001 */
600 TCHAR* value = TEXT("7001:UDP:*:Enabled:AFS Cache Manager Callback");
601 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
602 "SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\DomainProfile\\GloballyOpenP",
603 0, TEXT("container"), 0, KEY_SET_VALUE, NULL, &hk, &dwDisp) == ERROR_SUCCESS)
605 RegSetValueEx (hk, TEXT("7001:UDP"), 0, REG_SZ, (PBYTE)value, sizeof(TCHAR) * (1+lstrlen(value)));
608 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
609 "SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\GloballyOpenP",
610 0, TEXT("container"), 0, KEY_SET_VALUE, NULL, &hk, &dwDisp) == ERROR_SUCCESS)
612 RegSetValueEx (hk, TEXT("7001:UDP"), 0, REG_SZ, (PBYTE)value, sizeof(TCHAR) * (1+lstrlen(value)));
617 /* initialize RX, and tell it to listen to port 7001, which is used for
618 * callback RPC messages.
620 code = rx_Init(htons(7001));
621 afsi_log("rx_Init code %x", code);
623 *reasonP = "afsd: failed to init rx client on port 7001";
627 /* Initialize the RPC server for session keys */
630 /* create an unauthenticated service #1 for callbacks */
631 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
632 serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
633 RXAFSCB_ExecuteRequest);
634 afsi_log("rx_NewService addr %x", (int)serverp);
635 if (serverp == NULL) {
636 *reasonP = "unknown error";
640 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
641 serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
642 &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
643 afsi_log("rx_NewService addr %x", (int)serverp);
644 if (serverp == NULL) {
645 *reasonP = "unknown error";
649 /* start server threads, *not* donating this one to the pool */
651 afsi_log("rx_StartServer");
653 /* init user daemon, and other packages */
656 cm_InitACLCache(2*stats);
672 cm_InitSCache(stats);
674 code = cm_InitDCache(0, cacheBlocks);
675 afsi_log("cm_InitDCache code %x", code);
677 *reasonP = "error initializing cache";
682 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
683 if (cm_InitDNS(cm_dnsEnabled) == -1)
684 cm_dnsEnabled = 0; /* init failed, so deactivate */
685 afsi_log("cm_InitDNS %d", cm_dnsEnabled);
689 code = cm_GetRootCellName(rootCellName);
690 afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s",
691 code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
692 if (code != 0 && !cm_freelanceEnabled)
694 *reasonP = "can't find root cell name in afsd.ini";
697 else if (cm_freelanceEnabled)
700 if (code == 0 && !cm_freelanceEnabled)
702 cm_rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
703 afsi_log("cm_GetCell addr %x", (int)cm_rootCellp);
704 if (cm_rootCellp == NULL)
706 *reasonP = "can't find root cell in afsdcell.ini";
711 #ifdef AFS_FREELANCE_CLIENT
712 if (cm_freelanceEnabled)
719 int afsd_InitDaemons(char **reasonP)
726 /* this should really be in an init daemon from here on down */
728 if (!cm_freelanceEnabled) {
729 osi_Log0(afsd_logp, "Loading Root Volume from cell");
730 code = cm_GetVolumeByName(cm_rootCellp, cm_rootVolumeName, cm_rootUserp,
731 &req, CM_FLAG_CREATE, &cm_rootVolumep);
732 afsi_log("cm_GetVolumeByName code %x root vol %x", code,
733 (code ? (cm_volume_t *)-1 : cm_rootVolumep));
735 *reasonP = "can't find root volume in root cell";
740 /* compute the root fid */
741 if (!cm_freelanceEnabled) {
742 cm_rootFid.cell = cm_rootCellp->cellID;
743 cm_rootFid.volume = cm_GetROVolumeID(cm_rootVolumep);
744 cm_rootFid.vnode = 1;
745 cm_rootFid.unique = 1;
748 cm_FakeRootFid(&cm_rootFid);
750 code = cm_GetSCache(&cm_rootFid, &cm_rootSCachep, cm_rootUserp, &req);
751 afsi_log("cm_GetSCache code %x scache %x", code,
752 (code ? (cm_scache_t *)-1 : cm_rootSCachep));
754 *reasonP = "unknown error";
758 cm_InitDaemon(numBkgD);
759 afsi_log("cm_InitDaemon");
764 int afsd_InitSMB(char **reasonP, void *aMBfunc)
766 /* Do this last so that we don't handle requests before init is done.
767 * Here we initialize the SMB listener.
769 smb_Init(afsd_logp, cm_NetbiosName, smb_UseV3, LANadapter, numSvThreads, aMBfunc);
770 afsi_log("smb_Init");
783 #pragma pack( push, before_imagehlp, 8 )
784 #include <imagehlp.h>
785 #pragma pack( pop, before_imagehlp )
787 #define MAXNAMELEN 1024
789 void afsd_printStack(HANDLE hThread, CONTEXT *c)
791 HANDLE hProcess = GetCurrentProcess();
795 char functionName[MAXNAMELEN];
797 IMAGEHLP_MODULE Module;
801 IMAGEHLP_SYMBOL *pSym;
803 afsi_log_useTimestamp = 0;
805 pSym = (IMAGEHLP_SYMBOL *) GlobalAlloc(0, sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN);
807 memset( &s, '\0', sizeof s );
808 if (!SymInitialize(hProcess, NULL, 1) )
810 afsi_log("SymInitialize(): GetLastError() = %lu\n", GetLastError() );
812 SymCleanup( hProcess );
818 symOptions = SymGetOptions();
819 symOptions |= SYMOPT_LOAD_LINES;
820 symOptions &= ~SYMOPT_UNDNAME;
821 SymSetOptions( symOptions );
824 * init STACKFRAME for first call
825 * Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
826 * Notes: will have to be #ifdef-ed for Alphas; MIPSes are dead anyway,
829 #if defined (_ALPHA_) || defined (_MIPS_) || defined (_PPC_)
830 #error The STACKFRAME initialization in afsd_printStack() for this platform
831 #error must be properly configured
833 s.AddrPC.Offset = c->Eip;
834 s.AddrPC.Mode = AddrModeFlat;
835 s.AddrFrame.Offset = c->Ebp;
836 s.AddrFrame.Mode = AddrModeFlat;
839 memset( pSym, '\0', sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN );
840 pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL);
841 pSym->MaxNameLength = MAXNAMELEN;
843 memset( &Line, '\0', sizeof Line );
844 Line.SizeOfStruct = sizeof Line;
846 memset( &Module, '\0', sizeof Module );
847 Module.SizeOfStruct = sizeof Module;
851 afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
853 for ( frameNum = 0; ; ++ frameNum )
856 * get next stack frame (StackWalk(), SymFunctionTableAccess(),
857 * SymGetModuleBase()). if this returns ERROR_INVALID_ADDRESS (487) or
858 * ERROR_NOACCESS (998), you can assume that either you are done, or
859 * that the stack is so hosed that the next deeper frame could not be
862 if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c,
863 NULL, SymFunctionTableAccess, SymGetModuleBase,
867 /* display its contents */
868 afsi_log("\n%3d %c%c %08lx %08lx %08lx %08lx ",
869 frameNum, s.Far? 'F': '.', s.Virtual? 'V': '.',
870 s.AddrPC.Offset, s.AddrReturn.Offset,
871 s.AddrFrame.Offset, s.AddrStack.Offset );
873 if ( s.AddrPC.Offset == 0 )
875 afsi_log("(-nosymbols- PC == 0)" );
879 /* show procedure info from a valid PC */
880 if (!SymGetSymFromAddr(hProcess, s.AddrPC.Offset, &offset, pSym))
882 if ( GetLastError() != ERROR_INVALID_ADDRESS )
884 afsi_log("SymGetSymFromAddr(): errno = %lu",
890 UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN,
892 afsi_log("%s", functionName );
896 afsi_log(" %+ld bytes", (long) offset);
900 if (!SymGetLineFromAddr(hProcess, s.AddrPC.Offset, &offset, &Line))
902 if (GetLastError() != ERROR_INVALID_ADDRESS)
904 afsi_log("Error: SymGetLineFromAddr(): errno = %lu",
910 afsi_log(" Line: %s(%lu) %+ld bytes", Line.FileName,
911 Line.LineNumber, offset);
915 /* no return address means no deeper stackframe */
916 if (s.AddrReturn.Offset == 0)
923 if (GetLastError() != 0)
925 afsi_log("\nStackWalk(): errno = %lu\n", GetLastError());
928 SymCleanup(hProcess);
933 static DWORD *afsd_crtDbgBreakCurrent = NULL;
934 static DWORD afsd_crtDbgBreaks[256];
937 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
941 BOOL allocRequestBrk = FALSE;
944 afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n",
945 ep->ExceptionRecord->ExceptionCode,
946 ep->ExceptionRecord->ExceptionAddress);
949 if (afsd_crtDbgBreakCurrent &&
950 *afsd_crtDbgBreakCurrent == _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent))
952 allocRequestBrk = TRUE;
953 afsi_log("Breaking on alloc request # %d\n", *afsd_crtDbgBreakCurrent);
957 /* save context if we want to print the stack information */
958 context = *ep->ContextRecord;
960 afsd_printStack(GetCurrentThread(), &context);
962 if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
964 afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
969 afsd_crtDbgBreakCurrent++;
970 _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
974 ep->ContextRecord->Eip++;
975 return EXCEPTION_CONTINUE_EXECUTION;
979 return EXCEPTION_CONTINUE_SEARCH;
983 void afsd_SetUnhandledExceptionFilter()
985 SetUnhandledExceptionFilter(afsd_ExceptionFilter);
989 void afsd_DbgBreakAllocInit()
991 memset(afsd_crtDbgBreaks, -1, sizeof(afsd_crtDbgBreaks));
992 afsd_crtDbgBreakCurrent = afsd_crtDbgBreaks;
995 void afsd_DbgBreakAdd(DWORD requestNumber)
998 for (i = 0; i < sizeof(afsd_crtDbgBreaks) - 1; i++)
1000 if (afsd_crtDbgBreaks[i] == -1)
1005 afsd_crtDbgBreaks[i] = requestNumber;
1007 _CrtSetBreakAlloc(afsd_crtDbgBreaks[0]);