username.c revision 25076
1/*
2 *  Top users/processes display for Unix
3 *  Version 3
4 *
5 *  This program may be freely redistributed,
6 *  but this entire comment MUST remain intact.
7 *
8 *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
10 */
11
12/*
13 *  Username translation code for top.
14 *
15 *  These routines handle uid to username mapping.
16 *  They use a hashing table scheme to reduce reading overhead.
17 *  For the time being, these are very straightforward hashing routines.
18 *  Maybe someday I'll put in something better.  But with the advent of
19 *  "random access" password files, it might not be worth the effort.
20 *
21 *  Changes to these have been provided by John Gilmore (gnu@toad.com).
22 *
23 *  The hash has been simplified in this release, to avoid the
24 *  table overflow problems of previous releases.  If the value
25 *  at the initial hash location is not right, it is replaced
26 *  by the right value.  Collisions will cause us to call getpw*
27 *  but hey, this is a cache, not the Library of Congress.
28 *  This makes the table size independent of the passwd file size.
29 */
30
31#include <stdio.h>
32#include <pwd.h>
33#include <utmp.h>
34
35#include "top.local.h"
36#include "utils.h"
37
38struct hash_el {
39    int  uid;
40    char name[UT_NAMESIZE + 1];
41};
42
43#define    is_empty_hash(x)	(hash_table[x].name[0] == 0)
44
45/* simple minded hashing function */
46/* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
47   the hash_table.  Applied abs() function to fix. 2/16/96 tpugh
48*/
49#define    hashit(i)	(abs(i) % Table_size)
50
51/* K&R requires that statically declared tables be initialized to zero. */
52/* We depend on that for hash_table and YOUR compiler had BETTER do it! */
53struct hash_el hash_table[Table_size];
54
55init_hash()
56
57{
58    /*
59     *  There used to be some steps we had to take to initialize things.
60     *  We don't need to do that anymore, but we will leave this stub in
61     *  just in case future changes require initialization steps.
62     */
63}
64
65char *username(uid)
66
67register int uid;
68
69{
70    register int hashindex;
71
72    hashindex = hashit(uid);
73    if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
74    {
75	/* not here or not right -- get it out of passwd */
76	hashindex = get_user(uid);
77    }
78    return(hash_table[hashindex].name);
79}
80
81int userid(username)
82
83char *username;
84
85{
86    struct passwd *pwd;
87
88    /* Eventually we want this to enter everything in the hash table,
89       but for now we just do it simply and remember just the result.
90     */
91
92    if ((pwd = getpwnam(username)) == NULL)
93    {
94	return(-1);
95    }
96
97    /* enter the result in the hash table */
98    enter_user(pwd->pw_uid, username, 1);
99
100    /* return our result */
101    return(pwd->pw_uid);
102}
103
104int enter_user(uid, name, wecare)
105
106register int  uid;
107register char *name;
108int wecare;		/* 1 = enter it always, 0 = nice to have */
109
110{
111    register int hashindex;
112
113#ifdef DEBUG
114    fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
115#endif
116
117    hashindex = hashit(uid);
118
119    if (!is_empty_hash(hashindex))
120    {
121	if (!wecare)
122	    return 0;		/* Don't clobber a slot for trash */
123	if (hash_table[hashindex].uid == uid)
124	    return(hashindex);	/* Fortuitous find */
125    }
126
127    /* empty or wrong slot -- fill it with new value */
128    hash_table[hashindex].uid = uid;
129    (void) strncpy(hash_table[hashindex].name, name, UT_NAMESIZE);
130    return(hashindex);
131}
132
133/*
134 * Get a userid->name mapping from the system.
135 * If the passwd database is hashed (#define RANDOM_PW), we
136 * just handle this uid.  Otherwise we scan the passwd file
137 * and cache any entries we pass over while looking.
138 */
139
140int get_user(uid)
141
142register int uid;
143
144{
145    struct passwd *pwd;
146
147#ifdef RANDOM_PW
148    /* no performance penalty for using getpwuid makes it easy */
149    if ((pwd = getpwuid(uid)) != NULL)
150    {
151	return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
152    }
153#else
154
155    int from_start = 0;
156
157    /*
158     *  If we just called getpwuid each time, things would be very slow
159     *  since that just iterates through the passwd file each time.  So,
160     *  we walk through the file instead (using getpwent) and cache each
161     *  entry as we go.  Once the right record is found, we cache it and
162     *  return immediately.  The next time we come in, getpwent will get
163     *  the next record.  In theory, we never have to read the passwd file
164     *  a second time (because we cache everything we read).  But in
165     *  practice, the cache may not be large enough, so if we don't find
166     *  it the first time we have to scan the file a second time.  This
167     *  is not very efficient, but it will do for now.
168     */
169
170    while (from_start++ < 2)
171    {
172	while ((pwd = getpwent()) != NULL)
173	{
174	    if (pwd->pw_uid == uid)
175	    {
176		return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
177	    }
178	    (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
179	}
180	/* try again */
181	setpwent();
182    }
183#endif
184    /* if we can't find the name at all, then use the uid as the name */
185    return(enter_user(uid, itoa7(uid), 1));
186}
187