Merge branch 'vendor/MDOCML'
[dragonfly.git] / contrib / top / username.c
1 /*
2  * Copyright (c) 1984 through 2008, William LeFebvre
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  * 
16  *     * Neither the name of William LeFebvre nor the names of other
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  *  Top users/processes display for Unix
35  *  Version 3
36  */
37
38 /*
39  *  Username translation code for top.
40  *
41  *  These routines handle uid to username mapping.  They use a hash table to
42  *  reduce reading overhead.  Entries are refreshed every EXPIRETIME seconds.
43  *
44  *  The old ad-hoc hash functions have been replaced with something a little
45  *  more formal and (hopefully) more robust (found in hash.c)
46  */
47
48 #include "os.h"
49
50 #include <pwd.h>
51
52 #include "top.h"
53 #include "utils.h"
54 #include "hash.h"
55
56 #define EXPIRETIME (60 * 5)
57
58 /* we need some sort of idea how long usernames can be */
59 #ifndef MAXLOGNAME
60 #ifdef _POSIX_LOGIN_NAME_MAX 
61 #define MAXLOGNAME _POSIX_LOGIN_NAME_MAX 
62 #else
63 #define MAXLOGNAME 9
64 #endif
65 #endif
66
67 struct hash_data {
68     int    uid;
69     char   name[MAXLOGNAME];  /* big enough? */
70     time_t expire;
71 };
72
73 hash_table *userhash;
74
75
76 void
77 init_username()
78
79 {
80     userhash = hash_create(211);
81 }
82
83 char *
84 username(int uid)
85
86 {
87     struct hash_data *data;
88     struct passwd *pw;
89     time_t now;
90
91     /* what time is it? */
92     now = time(NULL);
93
94     /* get whatever is in the cache */
95     data = hash_lookup_uint(userhash, (unsigned int)uid);
96
97     /* if we had a cache miss, then create space for a new entry */
98     if (data == NULL)
99     {
100         /* make space */
101         data = (struct hash_data *)malloc(sizeof(struct hash_data));
102
103         /* fill in some data, including an already expired time */
104         data->uid = uid;
105         data->expire = (time_t)0;
106
107         /* add it to the hash: the rest gets filled in later */
108         hash_add_uint(userhash, uid, data);
109     }
110
111     /* Now data points to the correct hash entry for "uid".  If this is
112        a new entry, then expire is 0 and the next test will be true. */
113     if (data->expire <= now)
114     {
115         if ((pw = getpwuid(uid)) != NULL)
116         {
117             strncpy(data->name, pw->pw_name, MAXLOGNAME-1);
118             data->expire = now + EXPIRETIME;
119             dprintf("username: updating %d with %s, expires %d\n",
120                     data->uid, data->name, data->expire);
121         }
122         else
123         {
124             /* username doesnt exist ... so invent one */
125             snprintf(data->name, sizeof(data->name), "%d", uid);
126             data->expire = now + EXPIRETIME;
127             dprintf("username: updating %d with %s, expires %d\n",
128                     data->uid, data->name, data->expire);
129         }           
130     }
131
132     /* return what we have */
133     return data->name;
134 }
135
136 int
137 userid(char *username)
138
139 {
140     struct passwd *pwd;
141
142     if ((pwd = getpwnam(username)) == NULL)
143     {
144         return(-1);
145     }
146
147     /* return our result */
148     return(pwd->pw_uid);
149 }
150