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