2
KSysGuard, the KDE System Guard
4
Copyright (c) 2000 Chris Schlaeger <cs@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
#include <sys/types.h>
29
#include "PWUIDCache.h"
31
/* Cached values become invalid after 5 minutes */
40
static CONTAINER UIDCache = 0;
41
static time_t lastCleanup = 0;
43
void PWUIDCache_cleanup( void* c );
45
static int uidCmp( void* p1, void* p2 )
47
return ( ((CachedPWUID*)p1)->uid - ((CachedPWUID*)p2)->uid );
50
void PWUIDCache_cleanup( void* c )
53
if ( ((CachedPWUID*)c)->uName )
54
free ( ((CachedPWUID*)c)->uName );
61
UIDCache = new_ctnr();
66
destr_ctnr( UIDCache, PWUIDCache_cleanup );
69
const char* getCachedPWUID( uid_t uid )
72
CachedPWUID* entry = 0;
77
if ( stamp - lastCleanup > TIMEOUT ) {
78
/* Cleanup cache entries every TIMEOUT seconds so that we
79
* don't pile tons of unused entries, and to make sure that
80
* our entries are not outdated. */
81
for ( entry = first_ctnr( UIDCache ); entry; entry = next_ctnr( UIDCache ) ) {
82
/* If a cache entry has not been updated for TIMEOUT
83
* seconds the entry is removed. */
84
if ( stamp - entry->tStamp > TIMEOUT )
85
PWUIDCache_cleanup( remove_ctnr( UIDCache ) );
92
if ( ( idx = search_ctnr( UIDCache, uidCmp, &key ) ) < 0 ) {
95
/* User id is not yet known */
96
entry = (CachedPWUID*)malloc( sizeof( CachedPWUID ) );
97
entry->tStamp = stamp;
100
pwent = getpwuid( uid );
102
entry->uName = strdup( pwent->pw_name );
104
entry->uName = strdup( "?" );
106
push_ctnr( UIDCache, entry );
107
bsort_ctnr( UIDCache, uidCmp );
109
/* User is is already known */
110
entry = get_ctnr( UIDCache, idx );