1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "lastcomm.h"
/*
* lc_utils contains utility functions used by both the basic and extended
* accounting components of lastcomm. getdev(), on its first call, builds
* the set of tty device name to dev_t mappings.
*/
#define N_DEVS 43 /* hash value for device names */
#define NDEVS 500 /* max number of file names in /dev */
#define HASH(d) (((int)d) % N_DEVS) /* hash function */
struct devhash {
dev_t dev_dev;
char dev_name [PATHNAMLEN];
struct devhash *dev_nxt;
};
static struct devhash *dev_hash[N_DEVS];
static struct devhash *dev_chain;
static int ndevs = NDEVS;
static struct devhash *hashtab;
/*
* Default search list, used if /etc/ttysrch unavailable or unparsable.
*/
static char *def_srch_dirs[] = {
"/dev/term",
"/dev/pts",
"/dev/xt",
NULL
};
static char *raw_sf; /* buffer containing raw image of the search file */
#define SRCH_FILE_NAME "/etc/ttysrch"
/*
* /etc/ttysrch tokens.
*/
#define COMMENT_CHAR '#'
#define EOLN '\n'
/*
* /etc/ttysrch parser states.
*/
#define START_STATE 1
#define COMMENT_STATE 2
#define DIRNAME_STATE 3
/*
* The following 2 routines are modified version of get_pri_dirs
* and srch_dir in ttyname.c.
*/
static char **
get_pri_dirs()
{
int bcount = 0;
int c;
int sf_lines = 0; /* number of lines in search file */
int dirno = 0;
int state;
FILE *sf;
char **pri_dirs; /* priority search list */
char *sfp; /* pointer inside the raw image buffer */
struct stat sfsb; /* search file's stat structure buffer */
if ((sf = fopen(SRCH_FILE_NAME, "r")) == NULL)
return (def_srch_dirs);
if (stat(SRCH_FILE_NAME, &sfsb) < 0) {
(void) fclose(sf);
return (def_srch_dirs);
}
raw_sf = malloc(sfsb.st_size + 1);
sfp = raw_sf;
while ((bcount++ < sfsb.st_size) && ((c = getc(sf)) != EOF)) {
*sfp++ = (char)c;
if (c == EOLN)
sf_lines++;
}
(void) fclose(sf);
*sfp = EOLN;
pri_dirs = malloc(++sf_lines * sizeof (char *));
sfp = raw_sf;
state = START_STATE;
while (--bcount) {
switch (state) {
case START_STATE:
if (*sfp == COMMENT_CHAR) {
state = COMMENT_STATE;
} else if (!isspace(*sfp)) {
state = DIRNAME_STATE;
pri_dirs[dirno++] = sfp;
}
break;
case COMMENT_STATE:
if (*sfp == EOLN)
state = START_STATE;
break;
case DIRNAME_STATE:
if (*sfp == EOLN) {
*sfp = '\0';
state = START_STATE;
} else if (isspace(*sfp)) {
*sfp = '\0';
state = COMMENT_STATE;
}
break;
} /* switch */
sfp++;
}
*sfp = '\0';
pri_dirs[dirno] = NULL;
return (pri_dirs);
}
/*
* Build a chain of character devices in dev_chain, starting with the given
* path.
*/
static int
srch_dir(char *path)
{
DIR *dirp;
struct dirent *direntp;
struct stat st;
char file_name[PATHNAMLEN];
if ((dirp = opendir(path)) == NULL)
return (0);
if ((readdir(dirp) == NULL) || (readdir(dirp) == NULL))
return (0);
while ((direntp = readdir(dirp)) != NULL) {
(void) strcpy(file_name, path);
(void) strcat(file_name, "/");
(void) strcat(file_name, direntp->d_name);
if (stat((const char *)file_name, &st) < 0)
continue;
if ((st.st_mode & S_IFMT) == S_IFCHR) {
(void) strcpy(hashtab->dev_name,
file_name + strlen("/dev/"));
hashtab->dev_nxt = dev_chain;
dev_chain = hashtab;
hashtab++;
if (--ndevs < 0)
return (-1);
}
}
(void) closedir(dirp);
return (1);
}
static void
setupdevs()
{
int dirno = 0;
char **srch_dirs;
hashtab = malloc(NDEVS * sizeof (struct devhash));
if (hashtab == NULL) {
(void) fprintf(stderr, gettext("No memory for device table\n"));
return;
}
srch_dirs = get_pri_dirs();
while (srch_dirs[dirno] != NULL) {
if (srch_dir(srch_dirs[dirno]) < 0)
return;
dirno++;
}
dirno = 0;
while (srch_dirs[dirno] != NULL) {
if (strcmp("/dev", srch_dirs[dirno]) == 0)
/*
* Don't search /dev twice.
*/
return;
dirno++;
}
}
char *
getdev(dev_t dev)
{
struct devhash *hp, *nhp;
struct stat statb;
char name[PATHNAMLEN];
static dev_t lastdev = (dev_t)-1;
static char *lastname;
static int init = 0;
if (dev == NODEV)
return ("__");
if (dev == lastdev)
return (lastname);
if (!init) {
setupdevs();
init++;
}
for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
if (hp->dev_dev == dev) {
lastdev = dev;
return (lastname = hp->dev_name);
}
for (hp = dev_chain; hp; hp = nhp) {
nhp = hp->dev_nxt;
(void) strcpy(name, "/dev/");
(void) strcat(name, hp->dev_name);
if (stat(name, &statb) < 0) /* name truncated usually */
continue;
if ((statb.st_mode & S_IFMT) != S_IFCHR)
continue;
hp->dev_dev = statb.st_rdev;
hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
dev_hash[HASH(hp->dev_dev)] = hp;
if (hp->dev_dev == dev) {
dev_chain = nhp;
lastdev = dev;
return (lastname = hp->dev_name);
}
}
dev_chain = NULL;
return ("??");
}
char *
flagbits(int f)
{
int i = 0;
static char flags[20];
#define BIT(flag, ch) flags[i++] = (f & flag) ? ch : ' '
BIT(ASU, 'S');
BIT(AFORK, 'F');
flags[i] = '\0';
return (flags);
#undef BIT
}
char *
getname(uid_t uid)
{
struct passwd *pw;
static char uidname[NMAX];
if ((pw = getpwuid(uid)) == NULL) {
(void) sprintf(uidname, "%u", uid);
return (uidname);
}
return (pw->pw_name);
}
|