void ft_a(t_steve **list, char *name, char *path, t_opts *opt) { t_steve *file; t_steve *tmp; struct stat info; file = (t_steve *)malloc(sizeof(t_steve)); file->file = ft_strdup(name); path = ft_path_steve(path); file->path = ft_strjoin(path, file->file); stat(file->path, &info); file->time = info.st_mtime; ft_ls_access(file, info); file->next = NULL; if (*list == NULL) *list = file; else { if (ft_time(list, file, opt) == 1) { tmp = *list; while (tmp->next != NULL) tmp = tmp->next; tmp->next = file; } } }
int MemMapSessionManager::_reapExpiredSessions() { // Locking should be done by the caller _sessionblock s_entry; void *temp = (void *) &s_entry; PRUintn ignore; PRBool delete_it = PR_FALSE; time_t timeNow; time_t timeSlack = 300; // 300 seconds PRUintn max = _sessionFile->getMaxBlocks(); int count = 0, nActive = 0; for (PRUintn n = 1; n < max; n++) { if (_sessionFile->getEntry(n, temp, ignore) != PR_TRUE) continue; timeNow = ft_time(); delete_it = PR_FALSE; // Decide if this session needs to be reaped if (s_entry.validator != _sessionValidator) { delete_it = PR_TRUE; } else { if (s_entry.invalidated == PR_TRUE || (s_entry.prevreqendtime > 0 && ( (timeNow - s_entry.prevreqendtime) > (((time_t) s_entry.inactivitytime) + timeSlack))) ) delete_it = PR_TRUE; else nActive++; } // Delete this session if (delete_it == PR_TRUE) { _deleteSessionFromLocation(n, s_entry); count++; } } if (count > 0) { char* logMsg = get_message(j2eeResourceBundle, "j2ee.MemMapSessionManager.ERR_REAPER_SESSION_EXPIRED"); NSJavaUtil::log(LOG_INFORM, logMsg, count, nActive); FREE(logMsg); } return count; }
NSAPI_PUBLIC int dns_cache_valid(dns_cache_entry_t *entry) { time_t now; now = ft_time(); if (now > (time_t)(entry->last_access + dns_expire_time)) return -1; return cache_valid(dns_cache, (cache_entry_t *)entry); }
PRBool MemMapSessionManager::updateTimeStamp (const char* id) { SemLock semLock (_lockMgr, _lockId); _sessionblock sessionEntry; PRUintn sessionlocation = _getSessionBlock (id, sessionEntry); if (sessionlocation < _sessionFile->getMaxBlocks ()) { sessionEntry.lastaccesstime = sessionEntry.timestamp; sessionEntry.timestamp = ft_time (); return _sessionFile->setEntry (sessionlocation, &sessionEntry, sizeof (_sessionblock)); } return PR_FALSE; }
NSAPI_PUBLIC dns_cache_entry_t * dns_cache_insert(char *host, unsigned int ip, unsigned int verified) { dns_cache_entry_t *newentry; if ( !dns_cache || !ip) { return NULL; } if ( (newentry = (dns_cache_entry_t *)PERM_MALLOC(sizeof(dns_cache_entry_t))) == NULL) { ereport(LOG_FAILURE, XP_GetAdminStr(DBT_dnsCacheInsertErrorAllocatingEnt_)); goto error; } if (host) { if ( (newentry->host = PERM_STRDUP(host)) == NULL) { ereport(LOG_FAILURE, XP_GetAdminStr(DBT_dnsCacheInsertMallocFailure_)); goto error; } } else newentry->host = NULL; #ifdef CACHE_DEBUG newentry->cache.magic = CACHE_ENTRY_MAGIC; #endif newentry->ip = ip; newentry->verified = verified; newentry->last_access = ft_time(); if ( cache_insert_p(dns_cache, &(newentry->cache), (void *)&(newentry->ip), (void *)newentry, &dns_cache_entry_functions) < 0) { /* Not a bad error; it just means the cache is full */ goto error; } return newentry; error: if (newentry) { if (newentry->host) PERM_FREE(newentry->host); PERM_FREE(newentry); } return NULL; }
void MemMapSessionManager::updateTimeStampAtLocation(PRUintn location) { // Locking should be done by the caller _sessionblock s_entry; void *temp = (void *) &s_entry; PRUintn ignore; if (_sessionFile->getEntry(location, temp, ignore) == PR_TRUE) { s_entry.timestamp = ft_time(); // we set the first 'lastaccesstime' to be the creationtime s_entry.lastaccesstime = s_entry.timestamp; _sessionFile->setEntry(0, temp, sizeof(_sessionblock)); } }
NSAPI_PUBLIC void cache_collect_garbage(cache_t *cache) { unsigned int now; cache_entry_t *ptr, *last; NS_ASSERT(cache_crit); NS_ASSERT(cache); #ifdef CACHE_DEBUG NS_ASSERT(cache->magic == CACHE_MAGIC); #endif if(!cache->fast_mode) return; now = ft_time(); if(now - cache->gc_time < GC_INTERVAL) return; crit_enter(cache->lock); last = NULL; ptr = cache->garbage_list_head; while(ptr) { NS_ASSERT(ptr->delete_pending); if((ptr->delete_time + SAFE_INTERVAL < now) && (ptr->access_count == 0)) { if(last) last->next_deleted = ptr->next_deleted; else cache->garbage_list_head = ptr->next_deleted; ptr->fn_list->cleanup_fn(ptr->data); PERM_FREE(ptr); ptr = (last) ? (last->next_deleted) : (cache->garbage_list_head); } else { last = ptr; ptr = ptr->next_deleted; } } crit_exit(cache->lock); cache->gc_time = now; }
PRBool MemMapSessionManager::createSession ( const char *id, PRUintn timeout, PRUintn &location, time_t &lastAccessTime, time_t &creationTime ) { SemLock semLock (_lockMgr, _lockId); _sessionblock sessionentry; PRUintn sessionLocation; //prepare a session entry template and insert into the session file. sessionentry.invalidated = PR_FALSE; sessionentry.prevreqendtime = 0; sessionentry.lastaccesstime = sessionentry.timestamp = sessionentry.createtimestamp = ft_time(); sessionentry.inactivitytime = timeout; sessionentry.validator = _sessionValidator; sessionentry.numitems = 0; strcpy (sessionentry.id, id); sessionentry.cookie = _controlFile->getMaxBlocks (); sessionLocation = _sessionFile->setEntry (&sessionentry, sizeof (_sessionblock)); if (sessionLocation >= _sessionFile->getMaxBlocks ()) { //cleanup data //failed sessionentry.validator = 0; return PR_FALSE; } location = sessionLocation; lastAccessTime = sessionentry.lastaccesstime; creationTime = sessionentry.createtimestamp; return PR_TRUE; }
PRBool MemMapSessionManager::_isSessionEntryValid(PRUintn sessionLocation, _sessionblock &sessionEntry) { if (sessionLocation < _sessionFile->getMaxBlocks ()) { if (sessionEntry.validator != _sessionValidator) return PR_FALSE; time_t timeNow = ft_time (); if ((sessionEntry.validator == _sessionValidator) && sessionEntry.invalidated == PR_FALSE && sessionEntry.prevreqendtime > 0 && ((timeNow - sessionEntry.prevreqendtime ) < (time_t) sessionEntry.inactivitytime)) { return PR_TRUE; } } return PR_FALSE; }
void MemMapSessionManager::clearSessionItem ( const char *id, const char *name ) { SemLock semLock (_lockMgr, _lockId); _sessionblock sessionentry; PRUintn sessionlocation = _getSessionBlock (id,sessionentry); if (sessionlocation >= _sessionFile->getMaxBlocks ()) { return; } _controlblock controlentry; PRUintn controllocation = _getControlBlock(sessionentry, name, controlentry); if (controllocation >= _controlFile->getMaxBlocks()) { return; } //fix the control entries if (controlentry.forward < _controlFile->getMaxBlocks()) { _controlblock forwardcontrolentry; void *temp = (void *) &forwardcontrolentry; PRUintn dummy; if (_controlFile->getEntry (controlentry.forward,temp, dummy) == PR_TRUE) { //something wrong } forwardcontrolentry.back = controlentry.back; _controlFile->setEntry (controlentry.forward, &forwardcontrolentry, sizeof(_controlblock)); } if (controlentry.back < _controlFile->getMaxBlocks ()) { _controlblock backcontrolentry; void *temp = (void *) &backcontrolentry; PRUintn dummy; if (_controlFile->getEntry (controlentry.back,temp, dummy) == PR_TRUE) { //something wrong } backcontrolentry.forward = controlentry.forward; _controlFile->setEntry (controlentry.back, &backcontrolentry, sizeof (_controlblock)); } //check if session pointer changes if (sessionentry.cookie == controllocation) { sessionentry.cookie = controlentry.forward; } //do session time stamp sessionentry.timestamp = ft_time (); sessionentry.numitems--; _sessionFile->setEntry (sessionlocation, &sessionentry, sizeof(_sessionblock)); //clear the control file _controlFile->clearEntry (controllocation); //clear the data file _dataFile->clearEntry (controlentry.cookie); }
MemMapSessionManager::MemMapSessionManager ( const char *filelocation, PRUintn blocksize, PRUintn maxsessions, PRUintn maxValues, PRUintn defaultinactivitytime, const char *snContext, PRUint32 maxLocks ) : _initialization_success(PR_FALSE), _controlFile(NULL), _dataFile(NULL), _sessionFile(NULL), _blocksize(blocksize), _maxSessions (maxsessions), _maxValuesPerSession (maxValues), _controlValidator (0xabcd), _sessionValidator (0xdcba), _maxLocks (maxLocks) { _tempdir = strdup(system_get_temp_dir()); if (_tempdir == NULL) { char* logMsg = get_message(j2eeResourceBundle, "j2ee.MemMapSessionManager.ERR_MEMORY_ALLOCATION_ERROR"); NSJavaUtil::log(LOG_CATASTROPHE, logMsg); FREE(logMsg); return; } _sem = SemPool::get ("SessionSem"); _isSingleProcess = NSJavaUtil::isSingleProcess (); if (acquirePrimordialLock(_sem) == PR_FALSE) { char* logMsg = get_message(j2eeResourceBundle, "j2ee.MemMapSessionManager.ERR_CANNOT_ACQUIRE_PRIMORDIAL"); NSJavaUtil::log(LOG_CATASTROPHE, logMsg); FREE(logMsg); return; } char filename[MAX_PATH]; PRBool done = PR_FALSE; _defaultinactivitytime = defaultinactivitytime; // we use one additional lock for protecting the MMap'ed memory. // rest of the locks are used to protect individual session locks; // the id for the global lock is the _maxLocks value _lockMgr = new LockManager(snContext, (_maxLocks+1)); if (_lockMgr == NULL) { char* logMsg = get_message(j2eeResourceBundle, "j2ee.MemMapSessionManager.ERR_CANNOT_CREATE_LOCK_MANAGER"); NSJavaUtil::log(LOG_CATASTROPHE, logMsg); FREE(logMsg); return; } _lockId = maxLocks; _initialization_time = ft_time(); util_snprintf (filename, sizeof(filename), "%s/%s", filelocation, _sessionfilename); _sessionFile = new MemMapFile (filename, sizeof (_sessionblock), maxsessions, done); if (done != PR_TRUE) goto bail; util_snprintf (filename, sizeof(filename), "%s/%s", filelocation, _controlfilename); _controlFile = new MemMapFile(filename, sizeof (_controlblock), maxsessions * maxValues, done); if (done != PR_TRUE) goto bail; util_snprintf (filename, sizeof(filename), "%s/%s", filelocation, _datafilename); _dataFile = new MemMapFile (filename, blocksize, maxsessions * maxValues, done); if (done != PR_TRUE) goto bail; _initialization_success = PR_TRUE; /* * Session at location 0 is special, it's a bootstrap session. * Just update the time so that it doesn't expire */ updateTimeStampAtLocation(0); // reap the expired sessions if any at startup _reapExpiredSessions(); bail: releasePrimordialLock(_sem); }
PRBool MemMapSessionManager::getSessionDataAtLocation (const char *name, char *&data, PRUintn &size, PRUintn location, time_t &lastAccessedTime, time_t &prevReqEndTime, long &maxInactiveInterval, time_t &creationTime, time_t ¤tAccessTime, PRBool &isValid) { PRBool ret = PR_TRUE; SemLock semLock (_lockMgr, _lockId); _sessionblock sessionEntry; PRUintn sessionLocation; // first get the session block at the specified location sessionLocation = _getSessionBlockAtLocation (location, sessionEntry); if (sessionLocation >= _sessionFile->getMaxBlocks()) { isValid = PR_FALSE; return PR_FALSE; } if ((sessionEntry.validator == _sessionValidator) && sessionEntry.invalidated == PR_TRUE) { isValid = PR_FALSE; return PR_FALSE; } // check if this session is still valid? time_t timeNow = ft_time (); time_t reqend = sessionEntry.prevreqendtime; if ((sessionEntry.validator == _sessionValidator) && sessionEntry.prevreqendtime > 0 && ((timeNow - reqend) > (time_t) sessionEntry.inactivitytime)) { isValid = PR_FALSE; return PR_FALSE; } // update the lastaccesstime and current timestamps in tandem. sessionEntry.lastaccesstime = sessionEntry.timestamp; sessionEntry.timestamp = ft_time(); ret = _sessionFile->setEntry (sessionLocation, &sessionEntry, sizeof (_sessionblock)); // get the session item (or the attribute) by the given name void *ptr; if (ret == PR_TRUE && (ret = _getSessionItemByEntry(name, ptr, size, sessionLocation, sessionEntry, PR_TRUE)) == PR_TRUE) { lastAccessedTime = sessionEntry.lastaccesstime; prevReqEndTime = sessionEntry.prevreqendtime; maxInactiveInterval = sessionEntry.inactivitytime; creationTime = sessionEntry.createtimestamp; currentAccessTime = sessionEntry.timestamp; data = (char *)ptr; } return ret; }
int cookie_set(Session *sn, Request *rq, const char *name, const char *value, const char *path, const char *domain, const char *expires, PRInt64 max_age, PRBool secure) { int name_len = strcspn(name, "\"=;, \v\f\t\r\n"); if (name[name_len] != '\0') { NsprError::setError(PR_INVALID_ARGUMENT_ERROR, XP_GetAdminStr(DBT_badCookieName)); return REQ_ABORTED; } int value_len; if (value) { value_len = strcspn(value, "\";\r\n"); if (value[value_len] != '\0') { NsprError::setError(PR_INVALID_ARGUMENT_ERROR, XP_GetAdminStr(DBT_badCookieValue)); return REQ_ABORTED; } } else { value = "null"; value_len = 4; } int path_nv_len; if (path) { path_nv_len = COOKIE_PATH_PREFIX_LEN + strlen(path); } else { path = "/"; path_nv_len = COOKIE_PATH_PREFIX_LEN + 1; } int domain_nv_len = domain ? COOKIE_DOMAIN_PREFIX_LEN + strlen(domain) : 0; int expires_nv_len; if (expires) { expires_nv_len = COOKIE_EXPIRES_PREFIX_LEN + strlen(expires); } else if (max_age != COOKIE_MAX_AGE_SESSION) { expires_nv_len = COOKIE_EXPIRES_PREFIX_LEN + COOKIE_DATE_LEN; } else { expires_nv_len = 0; } int secure_len = secure ? COOKIE_SECURE_LEN : 0; int cookie_len = name_len + 1 + value_len + path_nv_len + domain_nv_len + expires_nv_len + secure_len; pb_param *pp = pblock_key_param_create(rq->srvhdrs, pb_key_set_cookie, NULL, cookie_len); if (!pp) return REQ_ABORTED; char *buf = pp->value; APPEND(buf, name, name_len); *buf++ = '='; APPEND(buf, value, value_len); APPEND_NAME_VALUE(buf, COOKIE_PATH_PREFIX, path, path_nv_len); APPEND_NAME_VALUE(buf, COOKIE_DOMAIN_PREFIX, domain, domain_nv_len); if (expires) { APPEND_NAME_VALUE(buf, COOKIE_EXPIRES_PREFIX, expires, expires_nv_len); } else if (max_age != COOKIE_MAX_AGE_SESSION) { // N.B. some browsers won't grok time_t values > 31 bits PRInt64 t; if (max_age == COOKIE_MAX_AGE_INFINITE) { t = PR_INT32_MAX; } else { t = ft_time() + max_age; } if (t < PR_INT32_MAX) { APPEND(buf, COOKIE_EXPIRES_PREFIX, COOKIE_EXPIRES_PREFIX_LEN); buf += cookie_format_date(buf, COOKIE_DATE_SIZE, t); } else { APPEND(buf, COOKIE_EXPIRES_PREFIX, COOKIE_EXPIRES_PREFIX_LEN); APPEND(buf, COOKIE_DATE_END_OF_TIME, COOKIE_DATE_END_OF_TIME_LEN); } } else { PR_ASSERT(expires_nv_len == 0); } APPEND(buf, COOKIE_SECURE, secure_len); PR_ASSERT(buf - pp->value == cookie_len); *buf = '\0'; pblock_kpinsert(pb_key_set_cookie, pp, rq->srvhdrs); return REQ_PROCEED; }