Annotation of src/usr.bin/top/username.c, Revision 1.16
1.16 ! deraadt 1: /* $OpenBSD: username.c,v 1.15 2008/04/02 16:41:24 deraadt Exp $ */
1.1 downsj 2:
3: /*
4: * Top users/processes display for Unix
5: * Version 3
6: *
1.7 deraadt 7: * Copyright (c) 1984, 1989, William LeFebvre, Rice University
8: * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1 downsj 9: *
1.7 deraadt 10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 downsj 29: */
30:
31: /*
32: * Username translation code for top.
33: *
34: * These routines handle uid to username mapping.
35: * They use a hashing table scheme to reduce reading overhead.
36: * For the time being, these are very straightforward hashing routines.
37: * Maybe someday I'll put in something better. But with the advent of
38: * "random access" password files, it might not be worth the effort.
39: *
40: * Changes to these have been provided by John Gilmore (gnu@toad.com).
41: *
42: * The hash has been simplified in this release, to avoid the
43: * table overflow problems of previous releases. If the value
44: * at the initial hash location is not right, it is replaced
45: * by the right value. Collisions will cause us to call getpw*
46: * but hey, this is a cache, not the Library of Congress.
47: * This makes the table size independent of the passwd file size.
48: */
49:
1.2 downsj 50: #include <sys/types.h>
1.1 downsj 51: #include <stdio.h>
1.2 downsj 52: #include <string.h>
1.1 downsj 53: #include <pwd.h>
54:
55: #include "top.local.h"
1.13 deraadt 56: #include "top.h"
1.1 downsj 57: #include "utils.h"
58:
59: struct hash_el {
1.11 millert 60: uid_t uid;
1.12 millert 61: char name[_PW_NAME_LEN + 1];
1.1 downsj 62: };
63:
1.16 ! deraadt 64: static int enter_user(uid_t, char *);
1.14 deraadt 65: static int get_user(uid_t);
1.2 downsj 66:
1.11 millert 67: #define is_empty_hash(x) (hash_table[x].name[0] == 0)
1.1 downsj 68:
1.9 deraadt 69: /*
1.12 millert 70: * Simple minded hashing function, assumes i is unsigned.
1.9 deraadt 71: */
1.12 millert 72: #define hashit(i) (i % Table_size)
1.1 downsj 73:
1.9 deraadt 74: struct hash_el hash_table[Table_size];
1.1 downsj 75:
1.8 pvalchev 76: char *
77: username(uid_t uid)
1.1 downsj 78: {
1.9 deraadt 79: int hashindex;
1.1 downsj 80:
1.9 deraadt 81: hashindex = hashit(uid);
82: if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) {
83: /* not here or not right -- get it out of passwd */
84: hashindex = get_user(uid);
85: }
86: return (hash_table[hashindex].name);
1.1 downsj 87: }
88:
1.8 pvalchev 89: uid_t
90: userid(char *username)
1.1 downsj 91: {
1.9 deraadt 92: struct passwd *pwd;
93:
94: /*
95: * Eventually we want this to enter everything in the hash table, but
96: * for now we just do it simply and remember just the result.
97: */
98: if ((pwd = getpwnam(username)) == NULL)
1.11 millert 99: return ((uid_t)-1);
1.1 downsj 100:
1.9 deraadt 101: /* enter the result in the hash table */
1.16 ! deraadt 102: enter_user(pwd->pw_uid, username);
1.1 downsj 103:
1.9 deraadt 104: /* return our result */
105: return (pwd->pw_uid);
1.1 downsj 106: }
107:
1.8 pvalchev 108: static int
1.16 ! deraadt 109: enter_user(uid_t uid, char *name)
1.1 downsj 110: {
1.9 deraadt 111: int hashindex;
1.1 downsj 112:
113: #ifdef DEBUG
1.16 ! deraadt 114: fprintf(stderr, "enter_hash(%u, %s)\n", uid, name);
1.1 downsj 115: #endif
116:
1.9 deraadt 117: hashindex = hashit(uid);
1.1 downsj 118:
1.9 deraadt 119: if (!is_empty_hash(hashindex)) {
120: if (hash_table[hashindex].uid == uid)
121: return (hashindex); /* Fortuitous find */
122: }
123: /* empty or wrong slot -- fill it with new value */
124: hash_table[hashindex].uid = uid;
125: (void) strlcpy(hash_table[hashindex].name, name,
126: sizeof(hash_table[hashindex].name));
127: return (hashindex);
1.1 downsj 128: }
129:
130: /*
131: * Get a userid->name mapping from the system.
132: */
1.8 pvalchev 133: static int
134: get_user(uid_t uid)
1.1 downsj 135: {
1.9 deraadt 136: struct passwd *pwd;
1.1 downsj 137:
1.9 deraadt 138: /* no performance penalty for using getpwuid makes it easy */
139: if ((pwd = getpwuid(uid)) != NULL)
1.16 ! deraadt 140: return (enter_user(pwd->pw_uid, pwd->pw_name));
1.1 downsj 141:
1.9 deraadt 142: /* if we can't find the name at all, then use the uid as the name */
1.16 ! deraadt 143: return (enter_user(uid, format_uid(uid)));
1.1 downsj 144: }