xref: /illumos-gate/usr/src/cmd/lastcomm/lc_utils.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f48205beScasper  * Common Development and Distribution License (the "License").
6*f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*f48205beScasper  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "lastcomm.h"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * lc_utils contains utility functions used by both the basic and extended
307c478bd9Sstevel@tonic-gate  * accounting components of lastcomm.  getdev(), on its first call, builds
317c478bd9Sstevel@tonic-gate  * the set of tty device name to dev_t mappings.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #define	N_DEVS		43		/* hash value for device names */
357c478bd9Sstevel@tonic-gate #define	NDEVS		500		/* max number of file names in /dev */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	HASH(d)	(((int)d) % N_DEVS)	/* hash function */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate struct	devhash {
407c478bd9Sstevel@tonic-gate 	dev_t	dev_dev;
417c478bd9Sstevel@tonic-gate 	char	dev_name [PATHNAMLEN];
427c478bd9Sstevel@tonic-gate 	struct	devhash *dev_nxt;
437c478bd9Sstevel@tonic-gate };
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static struct devhash *dev_hash[N_DEVS];
467c478bd9Sstevel@tonic-gate static struct devhash *dev_chain;
477c478bd9Sstevel@tonic-gate static int ndevs = NDEVS;
487c478bd9Sstevel@tonic-gate static struct devhash *hashtab;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Default search list, used if /etc/ttysrch unavailable or unparsable.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static char *def_srch_dirs[] = {
547c478bd9Sstevel@tonic-gate 	"/dev/term",
557c478bd9Sstevel@tonic-gate 	"/dev/pts",
567c478bd9Sstevel@tonic-gate 	"/dev/xt",
577c478bd9Sstevel@tonic-gate 	NULL
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate static char *raw_sf;	/* buffer containing raw image of the search file */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	SRCH_FILE_NAME  "/etc/ttysrch"
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * /etc/ttysrch tokens.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate #define	COMMENT_CHAR    '#'
667c478bd9Sstevel@tonic-gate #define	EOLN		'\n'
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * /etc/ttysrch parser states.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #define	START_STATE	1
717c478bd9Sstevel@tonic-gate #define	COMMENT_STATE   2
727c478bd9Sstevel@tonic-gate #define	DIRNAME_STATE   3
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * The following 2 routines are modified version of get_pri_dirs
767c478bd9Sstevel@tonic-gate  * and srch_dir in ttyname.c.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static char **
get_pri_dirs()797c478bd9Sstevel@tonic-gate get_pri_dirs()
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	int bcount = 0;
827c478bd9Sstevel@tonic-gate 	int c;
837c478bd9Sstevel@tonic-gate 	int sf_lines = 0;	/* number of lines in search file */
847c478bd9Sstevel@tonic-gate 	int dirno = 0;
857c478bd9Sstevel@tonic-gate 	int state;
867c478bd9Sstevel@tonic-gate 	FILE *sf;
877c478bd9Sstevel@tonic-gate 	char **pri_dirs;	/* priority search list */
887c478bd9Sstevel@tonic-gate 	char *sfp;		/* pointer inside the raw image buffer */
897c478bd9Sstevel@tonic-gate 	struct stat sfsb;	/* search file's stat structure buffer */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if ((sf = fopen(SRCH_FILE_NAME, "r")) == NULL)
937c478bd9Sstevel@tonic-gate 		return (def_srch_dirs);
947c478bd9Sstevel@tonic-gate 	if (stat(SRCH_FILE_NAME, &sfsb) < 0) {
957c478bd9Sstevel@tonic-gate 		(void) fclose(sf);
967c478bd9Sstevel@tonic-gate 		return (def_srch_dirs);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	raw_sf = malloc(sfsb.st_size + 1);
997c478bd9Sstevel@tonic-gate 	sfp = raw_sf;
1007c478bd9Sstevel@tonic-gate 	while ((bcount++ < sfsb.st_size) && ((c = getc(sf)) != EOF)) {
1017c478bd9Sstevel@tonic-gate 		*sfp++ = (char)c;
1027c478bd9Sstevel@tonic-gate 		if (c == EOLN)
1037c478bd9Sstevel@tonic-gate 			sf_lines++;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	(void) fclose(sf);
1067c478bd9Sstevel@tonic-gate 	*sfp = EOLN;
1077c478bd9Sstevel@tonic-gate 	pri_dirs = malloc(++sf_lines * sizeof (char *));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	sfp = raw_sf;
1107c478bd9Sstevel@tonic-gate 	state = START_STATE;
1117c478bd9Sstevel@tonic-gate 	while (--bcount) {
1127c478bd9Sstevel@tonic-gate 		switch (state) {
1137c478bd9Sstevel@tonic-gate 		case START_STATE:
1147c478bd9Sstevel@tonic-gate 			if (*sfp == COMMENT_CHAR) {
1157c478bd9Sstevel@tonic-gate 				state = COMMENT_STATE;
1167c478bd9Sstevel@tonic-gate 			} else if (!isspace(*sfp)) {
1177c478bd9Sstevel@tonic-gate 				state = DIRNAME_STATE;
1187c478bd9Sstevel@tonic-gate 				pri_dirs[dirno++] = sfp;
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 		case COMMENT_STATE:
1227c478bd9Sstevel@tonic-gate 			if (*sfp == EOLN)
1237c478bd9Sstevel@tonic-gate 				state = START_STATE;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case DIRNAME_STATE:
1267c478bd9Sstevel@tonic-gate 			if (*sfp == EOLN) {
1277c478bd9Sstevel@tonic-gate 				*sfp = '\0';
1287c478bd9Sstevel@tonic-gate 				state = START_STATE;
1297c478bd9Sstevel@tonic-gate 			} else if (isspace(*sfp)) {
1307c478bd9Sstevel@tonic-gate 				*sfp = '\0';
1317c478bd9Sstevel@tonic-gate 				state = COMMENT_STATE;
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		} /* switch */
1367c478bd9Sstevel@tonic-gate 		sfp++;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	*sfp = '\0';
1407c478bd9Sstevel@tonic-gate 	pri_dirs[dirno] = NULL;
1417c478bd9Sstevel@tonic-gate 	return (pri_dirs);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Build a chain of character devices in dev_chain, starting with the given
1467c478bd9Sstevel@tonic-gate  * path.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate static int
srch_dir(char * path)1497c478bd9Sstevel@tonic-gate srch_dir(char *path)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	DIR *dirp;
1527c478bd9Sstevel@tonic-gate 	struct dirent *direntp;
1537c478bd9Sstevel@tonic-gate 	struct stat st;
1547c478bd9Sstevel@tonic-gate 	char file_name[PATHNAMLEN];
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL)
1577c478bd9Sstevel@tonic-gate 		return (0);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if ((readdir(dirp) == NULL) || (readdir(dirp) == NULL))
1607c478bd9Sstevel@tonic-gate 		return (0);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	while ((direntp = readdir(dirp)) != NULL) {
1637c478bd9Sstevel@tonic-gate 		(void) strcpy(file_name, path);
1647c478bd9Sstevel@tonic-gate 		(void) strcat(file_name, "/");
1657c478bd9Sstevel@tonic-gate 		(void) strcat(file_name, direntp->d_name);
1667c478bd9Sstevel@tonic-gate 		if (stat((const char *)file_name, &st) < 0)
1677c478bd9Sstevel@tonic-gate 			continue;
1687c478bd9Sstevel@tonic-gate 		if ((st.st_mode & S_IFMT) == S_IFCHR) {
1697c478bd9Sstevel@tonic-gate 			(void) strcpy(hashtab->dev_name,
1707c478bd9Sstevel@tonic-gate 			    file_name + strlen("/dev/"));
1717c478bd9Sstevel@tonic-gate 			hashtab->dev_nxt = dev_chain;
1727c478bd9Sstevel@tonic-gate 			dev_chain = hashtab;
1737c478bd9Sstevel@tonic-gate 			hashtab++;
1747c478bd9Sstevel@tonic-gate 			if (--ndevs < 0)
1757c478bd9Sstevel@tonic-gate 				return (-1);
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
1797c478bd9Sstevel@tonic-gate 	return (1);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void
setupdevs()1847c478bd9Sstevel@tonic-gate setupdevs()
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int dirno = 0;
1877c478bd9Sstevel@tonic-gate 	char **srch_dirs;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	hashtab = malloc(NDEVS * sizeof (struct devhash));
1907c478bd9Sstevel@tonic-gate 	if (hashtab == NULL) {
1917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No memory for device table\n"));
1927c478bd9Sstevel@tonic-gate 		return;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	srch_dirs = get_pri_dirs();
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	while (srch_dirs[dirno] != NULL) {
1987c478bd9Sstevel@tonic-gate 		if (srch_dir(srch_dirs[dirno]) < 0)
1997c478bd9Sstevel@tonic-gate 			return;
2007c478bd9Sstevel@tonic-gate 		dirno++;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	dirno = 0;
2047c478bd9Sstevel@tonic-gate 	while (srch_dirs[dirno] != NULL) {
2057c478bd9Sstevel@tonic-gate 		if (strcmp("/dev", srch_dirs[dirno]) == 0)
2067c478bd9Sstevel@tonic-gate 			/*
2077c478bd9Sstevel@tonic-gate 			 * Don't search /dev twice.
2087c478bd9Sstevel@tonic-gate 			 */
2097c478bd9Sstevel@tonic-gate 			return;
2107c478bd9Sstevel@tonic-gate 		dirno++;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate char *
getdev(dev_t dev)2157c478bd9Sstevel@tonic-gate getdev(dev_t dev)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	struct devhash *hp, *nhp;
2187c478bd9Sstevel@tonic-gate 	struct stat statb;
2197c478bd9Sstevel@tonic-gate 	char name[PATHNAMLEN];
2207c478bd9Sstevel@tonic-gate 	static dev_t lastdev = (dev_t)-1;
2217c478bd9Sstevel@tonic-gate 	static char *lastname;
2227c478bd9Sstevel@tonic-gate 	static int init = 0;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (dev == NODEV)
2257c478bd9Sstevel@tonic-gate 		return ("__");
2267c478bd9Sstevel@tonic-gate 	if (dev == lastdev)
2277c478bd9Sstevel@tonic-gate 		return (lastname);
2287c478bd9Sstevel@tonic-gate 	if (!init) {
2297c478bd9Sstevel@tonic-gate 		setupdevs();
2307c478bd9Sstevel@tonic-gate 		init++;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
2347c478bd9Sstevel@tonic-gate 		if (hp->dev_dev == dev) {
2357c478bd9Sstevel@tonic-gate 			lastdev = dev;
2367c478bd9Sstevel@tonic-gate 			return (lastname = hp->dev_name);
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	for (hp = dev_chain; hp; hp = nhp) {
2407c478bd9Sstevel@tonic-gate 		nhp = hp->dev_nxt;
2417c478bd9Sstevel@tonic-gate 		(void) strcpy(name, "/dev/");
2427c478bd9Sstevel@tonic-gate 		(void) strcat(name, hp->dev_name);
2437c478bd9Sstevel@tonic-gate 		if (stat(name, &statb) < 0)	/* name truncated usually */
2447c478bd9Sstevel@tonic-gate 			continue;
2457c478bd9Sstevel@tonic-gate 		if ((statb.st_mode & S_IFMT) != S_IFCHR)
2467c478bd9Sstevel@tonic-gate 			continue;
2477c478bd9Sstevel@tonic-gate 		hp->dev_dev = statb.st_rdev;
2487c478bd9Sstevel@tonic-gate 		hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
2497c478bd9Sstevel@tonic-gate 		dev_hash[HASH(hp->dev_dev)] = hp;
2507c478bd9Sstevel@tonic-gate 		if (hp->dev_dev == dev) {
2517c478bd9Sstevel@tonic-gate 			dev_chain = nhp;
2527c478bd9Sstevel@tonic-gate 			lastdev = dev;
2537c478bd9Sstevel@tonic-gate 			return (lastname = hp->dev_name);
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	dev_chain = NULL;
2577c478bd9Sstevel@tonic-gate 	return ("??");
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate char *
flagbits(int f)2617c478bd9Sstevel@tonic-gate flagbits(int f)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	int i = 0;
2647c478bd9Sstevel@tonic-gate 	static char flags[20];
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #define	BIT(flag, ch)	flags[i++] = (f & flag) ? ch : ' '
2677c478bd9Sstevel@tonic-gate 	BIT(ASU, 'S');
2687c478bd9Sstevel@tonic-gate 	BIT(AFORK, 'F');
2697c478bd9Sstevel@tonic-gate 	flags[i] = '\0';
2707c478bd9Sstevel@tonic-gate 	return (flags);
2717c478bd9Sstevel@tonic-gate #undef	BIT
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate char *
getname(uid_t uid)2757c478bd9Sstevel@tonic-gate getname(uid_t uid)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	struct passwd *pw;
2787c478bd9Sstevel@tonic-gate 	static char uidname[NMAX];
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if ((pw = getpwuid(uid)) == NULL) {
281*f48205beScasper 		(void) sprintf(uidname, "%u", uid);
2827c478bd9Sstevel@tonic-gate 		return (uidname);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	return (pw->pw_name);
2857c478bd9Sstevel@tonic-gate }
286