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