~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/PWUIDCache.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 2000 Chris Schlaeger <cs@kde.org>
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
    $Id: PWUIDCache.c,v 1.8 2003/03/07 22:08:25 waba Exp $
 
20
*/
 
21
 
 
22
#include <pwd.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <sys/types.h>
 
26
#include <time.h>
 
27
 
 
28
#include "ccont.h"
 
29
 
 
30
#include "PWUIDCache.h"
 
31
 
 
32
/* Cached values become invalid after 5 minutes */
 
33
#define TIMEOUT 300
 
34
 
 
35
typedef struct {
 
36
  uid_t uid;
 
37
  char* uName;
 
38
  time_t tStamp;
 
39
} CachedPWUID;
 
40
 
 
41
static CONTAINER UIDCache = 0;
 
42
static time_t lastCleanup = 0;
 
43
 
 
44
void PWUIDCache_cleanup( void* c );
 
45
 
 
46
static int uidCmp( void* p1, void* p2 )
 
47
{
 
48
  return ( ((CachedPWUID*)p1)->uid - ((CachedPWUID*)p2)->uid );
 
49
}
 
50
 
 
51
void PWUIDCache_cleanup( void* c )
 
52
{
 
53
  if ( c ) {
 
54
    if ( ((CachedPWUID*)c)->uName )
 
55
      free ( ((CachedPWUID*)c)->uName );
 
56
    free ( c );
 
57
        }
 
58
}
 
59
                        
 
60
void initPWUIDCache()
 
61
{
 
62
  UIDCache = new_ctnr();
 
63
}
 
64
 
 
65
void exitPWUIDCache()
 
66
{
 
67
  destr_ctnr( UIDCache, PWUIDCache_cleanup );
 
68
}
 
69
 
 
70
const char* getCachedPWUID( uid_t uid )
 
71
{
 
72
  CachedPWUID key;
 
73
  CachedPWUID* entry = 0;
 
74
  long idx;
 
75
  time_t stamp;
 
76
 
 
77
  stamp = time( 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 ) );
 
87
    }
 
88
 
 
89
    lastCleanup = stamp;
 
90
  }
 
91
 
 
92
  key.uid = uid;
 
93
  if ( ( idx = search_ctnr( UIDCache, uidCmp, &key ) ) < 0 ) {
 
94
    struct passwd* pwent;
 
95
 
 
96
    /* User id is not yet known */
 
97
    entry = (CachedPWUID*)malloc( sizeof( CachedPWUID ) );
 
98
    entry->tStamp = stamp;
 
99
    entry->uid = uid;
 
100
 
 
101
    pwent = getpwuid( uid );
 
102
    if ( pwent )
 
103
      entry->uName = strdup( pwent->pw_name );
 
104
    else
 
105
      entry->uName = strdup( "?" );
 
106
 
 
107
    push_ctnr( UIDCache, entry );
 
108
    bsort_ctnr( UIDCache, uidCmp );
 
109
        } else {
 
110
    /* User is is already known */
 
111
    entry = get_ctnr( UIDCache, idx );
 
112
  }
 
113
 
 
114
  return entry->uName;
 
115
}