~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/PWUIDCache.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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