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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
$Id: PWUIDCache.c,v 1.8 2003/03/07 22:08:25 waba Exp $
25
#include <sys/types.h>
30
#include "PWUIDCache.h"
32
/* Cached values become invalid after 5 minutes */
41
static CONTAINER UIDCache = 0;
42
static time_t lastCleanup = 0;
44
void PWUIDCache_cleanup( void* c );
46
static int uidCmp( void* p1, void* p2 )
48
return ( ((CachedPWUID*)p1)->uid - ((CachedPWUID*)p2)->uid );
51
void PWUIDCache_cleanup( void* c )
54
if ( ((CachedPWUID*)c)->uName )
55
free ( ((CachedPWUID*)c)->uName );
62
UIDCache = new_ctnr();
67
destr_ctnr( UIDCache, PWUIDCache_cleanup );
70
const char* getCachedPWUID( uid_t uid )
73
CachedPWUID* entry = 0;
78
if ( stamp - lastCleanup > TIMEOUT ) {
79
/* Cleanup cache entries every TIMEOUT seconds so that we
80
* don't pile tons of unused entries, and to make sure that
81
* our entries are not outdated. */
82
for ( entry = first_ctnr( UIDCache ); entry; entry = next_ctnr( UIDCache ) ) {
83
/* If a cache entry has not been updated for TIMEOUT
84
* seconds the entry is removed. */
85
if ( stamp - entry->tStamp > TIMEOUT )
86
PWUIDCache_cleanup( remove_ctnr( UIDCache ) );
93
if ( ( idx = search_ctnr( UIDCache, uidCmp, &key ) ) < 0 ) {
96
/* User id is not yet known */
97
entry = (CachedPWUID*)malloc( sizeof( CachedPWUID ) );
98
entry->tStamp = stamp;
101
pwent = getpwuid( uid );
103
entry->uName = strdup( pwent->pw_name );
105
entry->uName = strdup( "?" );
107
push_ctnr( UIDCache, entry );
108
bsort_ctnr( UIDCache, uidCmp );
110
/* User is is already known */
111
entry = get_ctnr( UIDCache, idx );