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