1 /* 2 * Top users/processes display for Unix 3 * 4 * This program may be freely redistributed, 5 * but this entire comment MUST remain intact. 6 * 7 * Copyright (c) 1984, 1989, William LeFebvre, Rice University 8 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University 9 * 10 * $FreeBSD$ 11 */ 12 13 /* 14 * Username translation code for top. 15 * 16 * These routines handle uid to username mapping. 17 * They use a hashing table scheme to reduce reading overhead. 18 * For the time being, these are very straightforward hashing routines. 19 * Maybe someday I'll put in something better. But with the advent of 20 * "random access" password files, it might not be worth the effort. 21 * 22 * Changes to these have been provided by John Gilmore (gnu@toad.com). 23 * 24 * The hash has been simplified in this release, to avoid the 25 * table overflow problems of previous releases. If the value 26 * at the initial hash location is not right, it is replaced 27 * by the right value. Collisions will cause us to call getpw* 28 * but hey, this is a cache, not the Library of Congress. 29 * This makes the table size independent of the passwd file size. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/types.h> 34 35 #include <pwd.h> 36 #include <stdbool.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 #include "utils.h" 42 #include "username.h" 43 44 struct hash_el { 45 int uid; 46 char name[MAXLOGNAME]; 47 }; 48 49 #define is_empty_hash(x) (hash_table[x].name[0] == 0) 50 51 /* simple minded hashing function */ 52 #define hashit(i) (abs(i) % Table_size) 53 54 /* K&R requires that statically declared tables be initialized to zero. */ 55 /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ 56 static struct hash_el hash_table[Table_size]; 57 58 59 char *username(int uid) 60 { 61 int hashindex; 62 63 hashindex = hashit(uid); 64 if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) 65 { 66 /* not here or not right -- get it out of passwd */ 67 hashindex = get_user(uid); 68 } 69 return(hash_table[hashindex].name); 70 } 71 72 int userid(char username[]) 73 { 74 struct passwd *pwd; 75 76 /* Eventually we want this to enter everything in the hash table, 77 but for now we just do it simply and remember just the result. 78 */ 79 80 if ((pwd = getpwnam(username)) == NULL) 81 { 82 return(-1); 83 } 84 85 /* enter the result in the hash table */ 86 enter_user(pwd->pw_uid, username, 1); 87 88 /* return our result */ 89 return(pwd->pw_uid); 90 } 91 92 /* wecare 1 = enter it always, 0 = nice to have */ 93 int enter_user(int uid, char name[], bool wecare) 94 { 95 int hashindex; 96 97 #ifdef DEBUG 98 fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare); 99 #endif 100 101 hashindex = hashit(uid); 102 103 if (!is_empty_hash(hashindex)) 104 { 105 if (!wecare) 106 return 0; /* Don't clobber a slot for trash */ 107 if (hash_table[hashindex].uid == uid) 108 return(hashindex); /* Fortuitous find */ 109 } 110 111 /* empty or wrong slot -- fill it with new value */ 112 hash_table[hashindex].uid = uid; 113 (void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1); 114 return(hashindex); 115 } 116 117 /* 118 * Get a userid->name mapping from the system. 119 * If the passwd database is hashed (#define RANDOM_PW), we 120 * just handle this uid. 121 */ 122 123 int 124 get_user(int uid) 125 { 126 struct passwd *pwd; 127 128 /* no performance penalty for using getpwuid makes it easy */ 129 if ((pwd = getpwuid(uid)) != NULL) 130 { 131 return(enter_user(pwd->pw_uid, pwd->pw_name, 1)); 132 } 133 134 /* if we can't find the name at all, then use the uid as the name */ 135 return(enter_user(uid, itoa7(uid), 1)); 136 } 137