diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 4aed2c8219774f5d797760606b8489a92ddc5163 (patch) | |
tree | 3f8c130f7d269626bf6a9447407ef6c35954426a /ksysguard/ksysguardd/PWUIDCache.c | |
download | tdebase-4aed2c8219774f5d797760606b8489a92ddc5163.tar.gz tdebase-4aed2c8219774f5d797760606b8489a92ddc5163.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'ksysguard/ksysguardd/PWUIDCache.c')
-rw-r--r-- | ksysguard/ksysguardd/PWUIDCache.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/ksysguard/ksysguardd/PWUIDCache.c b/ksysguard/ksysguardd/PWUIDCache.c new file mode 100644 index 000000000..b0140e0a7 --- /dev/null +++ b/ksysguard/ksysguardd/PWUIDCache.c @@ -0,0 +1,114 @@ +/* + KSysGuard, the KDE System Guard + + Copyright (c) 2000 Chris Schlaeger <[email protected]> + + This program is free software; you can redistribute it and/or + modify it under the terms of version 2 of the GNU General Public + License as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +*/ + +#include <pwd.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <time.h> + +#include "ccont.h" + +#include "PWUIDCache.h" + +/* Cached values become invalid after 5 minutes */ +#define TIMEOUT 300 + +typedef struct { + uid_t uid; + char* uName; + time_t tStamp; +} CachedPWUID; + +static CONTAINER UIDCache = 0; +static time_t lastCleanup = 0; + +void PWUIDCache_cleanup( void* c ); + +static int uidCmp( void* p1, void* p2 ) +{ + return ( ((CachedPWUID*)p1)->uid - ((CachedPWUID*)p2)->uid ); +} + +void PWUIDCache_cleanup( void* c ) +{ + if ( c ) { + if ( ((CachedPWUID*)c)->uName ) + free ( ((CachedPWUID*)c)->uName ); + free ( c ); + } +} + +void initPWUIDCache() +{ + UIDCache = new_ctnr(); +} + +void exitPWUIDCache() +{ + destr_ctnr( UIDCache, PWUIDCache_cleanup ); +} + +const char* getCachedPWUID( uid_t uid ) +{ + CachedPWUID key; + CachedPWUID* entry = 0; + long idx; + time_t stamp; + + stamp = time( 0 ); + if ( stamp - lastCleanup > TIMEOUT ) { + /* Cleanup cache entries every TIMEOUT seconds so that we + * don't pile tons of unused entries, and to make sure that + * our entries are not outdated. */ + for ( entry = first_ctnr( UIDCache ); entry; entry = next_ctnr( UIDCache ) ) { + /* If a cache entry has not been updated for TIMEOUT + * seconds the entry is removed. */ + if ( stamp - entry->tStamp > TIMEOUT ) + PWUIDCache_cleanup( remove_ctnr( UIDCache ) ); + } + + lastCleanup = stamp; + } + + key.uid = uid; + if ( ( idx = search_ctnr( UIDCache, uidCmp, &key ) ) < 0 ) { + struct passwd* pwent; + + /* User id is not yet known */ + entry = (CachedPWUID*)malloc( sizeof( CachedPWUID ) ); + entry->tStamp = stamp; + entry->uid = uid; + + pwent = getpwuid( uid ); + if ( pwent ) + entry->uName = strdup( pwent->pw_name ); + else + entry->uName = strdup( "?" ); + + push_ctnr( UIDCache, entry ); + bsort_ctnr( UIDCache, uidCmp ); + } else { + /* User is is already known */ + entry = get_ctnr( UIDCache, idx ); + } + + return entry->uName; +} |