xref: /freebsd/usr.bin/who/who.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
138a99942STim J. Robbins /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
438a99942STim J. Robbins  * Copyright (c) 2002 Tim J. Robbins.
538a99942STim J. Robbins  * All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
159b50d902SRodney W. Grimes  *
1638a99942STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1938a99942STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269b50d902SRodney W. Grimes  * SUCH DAMAGE.
279b50d902SRodney W. Grimes  */
289b50d902SRodney W. Grimes 
29b457a3e1SOlivier Houchard #include <sys/param.h>
309b50d902SRodney W. Grimes #include <sys/types.h>
3138a99942STim J. Robbins #include <sys/ioctl.h>
3238a99942STim J. Robbins #include <sys/stat.h>
332c062c85SMark Murray 
340abcff76SPhilippe Charnier #include <err.h>
358358edb6STim J. Robbins #include <errno.h>
36e36de81eSAndrey A. Chernov #include <langinfo.h>
378358edb6STim J. Robbins #include <limits.h>
38965f310cSAndrey A. Chernov #include <locale.h>
3938a99942STim J. Robbins #include <paths.h>
400abcff76SPhilippe Charnier #include <pwd.h>
410abcff76SPhilippe Charnier #include <stdio.h>
427a19d1bbSDima Dorfman #include <stdlib.h>
430abcff76SPhilippe Charnier #include <string.h>
440abcff76SPhilippe Charnier #include <time.h>
45821df508SXin LI #include <timeconv.h>
460abcff76SPhilippe Charnier #include <unistd.h>
47ab90a4d1SEd Schouten #include <utmpx.h>
489b50d902SRodney W. Grimes 
4938a99942STim J. Robbins static void	heading(void);
50b911cea8SEd Schouten static void	process_utmp(void);
51b911cea8SEd Schouten static void	quick(void);
5285714224SSergey Kandaurov static void	row(const struct utmpx *);
5338a99942STim J. Robbins static int	ttywidth(void);
543f330d7dSWarner Losh static void	usage(void);
55b911cea8SEd Schouten static void	whoami(void);
5638a99942STim J. Robbins 
5738a99942STim J. Robbins static int	Hflag;			/* Write column headings */
5879625d00SEd Schouten static int	aflag;			/* Print all entries */
5985714224SSergey Kandaurov static int	bflag;			/* Show date of the last reboot */
6038a99942STim J. Robbins static int	mflag;			/* Show info about current terminal */
6138a99942STim J. Robbins static int	qflag;			/* "Quick" mode */
6238a99942STim J. Robbins static int	sflag;			/* Show name, line, time */
6338a99942STim J. Robbins static int	Tflag;			/* Show terminal state */
6438a99942STim J. Robbins static int	uflag;			/* Show idle time */
650abcff76SPhilippe Charnier 
660abcff76SPhilippe Charnier int
main(int argc,char * argv[])6738a99942STim J. Robbins main(int argc, char *argv[])
689b50d902SRodney W. Grimes {
6938a99942STim J. Robbins 	int ch;
709b50d902SRodney W. Grimes 
7138a99942STim J. Robbins 	setlocale(LC_TIME, "");
72965f310cSAndrey A. Chernov 
7379625d00SEd Schouten 	while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
7438a99942STim J. Robbins 		switch (ch) {
7538a99942STim J. Robbins 		case 'H':		/* Write column headings */
7638a99942STim J. Robbins 			Hflag = 1;
779b50d902SRodney W. Grimes 			break;
7838a99942STim J. Robbins 		case 'T':		/* Show terminal state */
7938a99942STim J. Robbins 			Tflag = 1;
809b50d902SRodney W. Grimes 			break;
8179625d00SEd Schouten 		case 'a':		/* Same as -bdlprtTu */
8279625d00SEd Schouten 			aflag = bflag = Tflag = uflag = 1;
8379625d00SEd Schouten 			break;
8485714224SSergey Kandaurov 		case 'b':		/* Show date of the last reboot */
8585714224SSergey Kandaurov 			bflag = 1;
8685714224SSergey Kandaurov 			break;
8738a99942STim J. Robbins 		case 'm':		/* Show info about current terminal */
8838a99942STim J. Robbins 			mflag = 1;
8938a99942STim J. Robbins 			break;
9038a99942STim J. Robbins 		case 'q':		/* "Quick" mode */
9138a99942STim J. Robbins 			qflag = 1;
9238a99942STim J. Robbins 			break;
9338a99942STim J. Robbins 		case 's':		/* Show name, line, time */
9438a99942STim J. Robbins 			sflag = 1;
9538a99942STim J. Robbins 			break;
9638a99942STim J. Robbins 		case 'u':		/* Show idle time */
9738a99942STim J. Robbins 			uflag = 1;
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		default:
1000abcff76SPhilippe Charnier 			usage();
10138a99942STim J. Robbins 			/*NOTREACHED*/
1029b50d902SRodney W. Grimes 		}
10338a99942STim J. Robbins 	}
10438a99942STim J. Robbins 	argc -= optind;
10538a99942STim J. Robbins 	argv += optind;
10638a99942STim J. Robbins 
10738a99942STim J. Robbins 	if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
10838a99942STim J. Robbins 	    (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
10938a99942STim J. Robbins 		/* "who am i" or "who am I", equivalent to -m */
11038a99942STim J. Robbins 		mflag = 1;
11138a99942STim J. Robbins 		argc -= 2;
11238a99942STim J. Robbins 		argv += 2;
11338a99942STim J. Robbins 	}
11438a99942STim J. Robbins 	if (argc > 1)
11538a99942STim J. Robbins 		usage();
11638a99942STim J. Robbins 
117b911cea8SEd Schouten 	if (*argv != NULL) {
118ab90a4d1SEd Schouten 		if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
119b911cea8SEd Schouten 			err(1, "%s", *argv);
120b911cea8SEd Schouten 	}
12138a99942STim J. Robbins 
12238a99942STim J. Robbins 	if (qflag)
123b911cea8SEd Schouten 		quick();
12438a99942STim J. Robbins 	else {
12538a99942STim J. Robbins 		if (sflag)
12638a99942STim J. Robbins 			Tflag = uflag = 0;
12738a99942STim J. Robbins 		if (Hflag)
12838a99942STim J. Robbins 			heading();
12938a99942STim J. Robbins 		if (mflag)
130b911cea8SEd Schouten 			whoami();
13138a99942STim J. Robbins 		else
132b911cea8SEd Schouten 			process_utmp();
13338a99942STim J. Robbins 	}
13438a99942STim J. Robbins 
135b911cea8SEd Schouten 	endutxent();
13638a99942STim J. Robbins 
1379b50d902SRodney W. Grimes 	exit(0);
1389b50d902SRodney W. Grimes }
1399b50d902SRodney W. Grimes 
140d2979f56STim J. Robbins static void
usage(void)14138a99942STim J. Robbins usage(void)
1420abcff76SPhilippe Charnier {
14338a99942STim J. Robbins 
144bab1d5b8SEd Schouten 	fprintf(stderr, "usage: who [-abHmqsTu] [am I] [file]\n");
1450abcff76SPhilippe Charnier 	exit(1);
1460abcff76SPhilippe Charnier }
1470abcff76SPhilippe Charnier 
148d2979f56STim J. Robbins static void
heading(void)14938a99942STim J. Robbins heading(void)
1509b50d902SRodney W. Grimes {
15138a99942STim J. Robbins 
152b911cea8SEd Schouten 	printf("%-16s ", "NAME");
15338a99942STim J. Robbins 	if (Tflag)
15438a99942STim J. Robbins 		printf("S ");
15585714224SSergey Kandaurov 	printf("%-12s %-12s ", "LINE", "TIME");
15638a99942STim J. Robbins 	if (uflag)
15738a99942STim J. Robbins 		printf("IDLE  ");
158b911cea8SEd Schouten 	printf("%-16s\n", "FROM");
15938a99942STim J. Robbins }
16038a99942STim J. Robbins 
161d2979f56STim J. Robbins static void
row(const struct utmpx * ut)16285714224SSergey Kandaurov row(const struct utmpx *ut)
16338a99942STim J. Robbins {
164b911cea8SEd Schouten 	char buf[80], tty[PATH_MAX];
16538a99942STim J. Robbins 	struct stat sb;
1666ceeb690SPeter Wemm 	time_t idle, t;
167e36de81eSAndrey A. Chernov 	static int d_first = -1;
16838a99942STim J. Robbins 	struct tm *tm;
16938a99942STim J. Robbins 	char state;
170e36de81eSAndrey A. Chernov 
171e36de81eSAndrey A. Chernov 	if (d_first < 0)
172e36de81eSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1739b50d902SRodney W. Grimes 
1748a9d84b4SPhilippe Charnier 	state = '?';
1758a9d84b4SPhilippe Charnier 	idle = 0;
17638a99942STim J. Robbins 	if (Tflag || uflag) {
177b911cea8SEd Schouten 		snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, ut->ut_line);
17838a99942STim J. Robbins 		if (stat(tty, &sb) == 0) {
17938a99942STim J. Robbins 			state = sb.st_mode & (S_IWOTH|S_IWGRP) ?
18038a99942STim J. Robbins 			    '+' : '-';
18138a99942STim J. Robbins 			idle = time(NULL) - sb.st_mtime;
18238a99942STim J. Robbins 		}
1839b50d902SRodney W. Grimes 	}
1849b50d902SRodney W. Grimes 
185b911cea8SEd Schouten 	printf("%-16s ", ut->ut_user);
18638a99942STim J. Robbins 	if (Tflag)
18738a99942STim J. Robbins 		printf("%c ", state);
18885714224SSergey Kandaurov 	if (ut->ut_type == BOOT_TIME)
18985714224SSergey Kandaurov 		printf("%-12s ", "system boot");
19085714224SSergey Kandaurov 	else
19185714224SSergey Kandaurov 		printf("%-12s ", ut->ut_line);
192b911cea8SEd Schouten 	t = ut->ut_tv.tv_sec;
1936ceeb690SPeter Wemm 	tm = localtime(&t);
19438a99942STim J. Robbins 	strftime(buf, sizeof(buf), d_first ? "%e %b %R" : "%b %e %R", tm);
19538a99942STim J. Robbins 	printf("%-*s ", 12, buf);
19638a99942STim J. Robbins 	if (uflag) {
19738a99942STim J. Robbins 		if (idle < 60)
19838a99942STim J. Robbins 			printf("  .   ");
19938a99942STim J. Robbins 		else if (idle < 24 * 60 * 60)
20038a99942STim J. Robbins 			printf("%02d:%02d ", (int)(idle / 60 / 60),
20138a99942STim J. Robbins 			    (int)(idle / 60 % 60));
20238a99942STim J. Robbins 		else
20338a99942STim J. Robbins 			printf(" old  ");
20438a99942STim J. Robbins 	}
20538a99942STim J. Robbins 	if (*ut->ut_host != '\0')
206b911cea8SEd Schouten 		printf("(%s)", ut->ut_host);
20738a99942STim J. Robbins 	putchar('\n');
20838a99942STim J. Robbins }
2099b50d902SRodney W. Grimes 
210b457a3e1SOlivier Houchard static int
ttystat(char * line)211b911cea8SEd Schouten ttystat(char *line)
212b457a3e1SOlivier Houchard {
213b457a3e1SOlivier Houchard 	struct stat sb;
214b457a3e1SOlivier Houchard 	char ttybuf[MAXPATHLEN];
215b457a3e1SOlivier Houchard 
216db11c57aSSteve Wills 	if (line == NULL)
217db11c57aSSteve Wills 		return (0);
218b911cea8SEd Schouten 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
219b457a3e1SOlivier Houchard 	if (stat(ttybuf, &sb) == 0) {
220b457a3e1SOlivier Houchard 		return (0);
221b457a3e1SOlivier Houchard 	} else
222b457a3e1SOlivier Houchard 		return (-1);
223b457a3e1SOlivier Houchard }
224b457a3e1SOlivier Houchard 
225d2979f56STim J. Robbins static void
process_utmp(void)226b911cea8SEd Schouten process_utmp(void)
22738a99942STim J. Robbins {
228b911cea8SEd Schouten 	struct utmpx *utx;
22938a99942STim J. Robbins 
230b911cea8SEd Schouten 	while ((utx = getutxent()) != NULL) {
231db11c57aSSteve Wills 		if ((aflag || !bflag) && utx->ut_type == USER_PROCESS) {
23279625d00SEd Schouten 			if (ttystat(utx->ut_line) == 0)
233b911cea8SEd Schouten 				row(utx);
234db11c57aSSteve Wills 		} else if (bflag && utx->ut_type == BOOT_TIME)
235db11c57aSSteve Wills 				row(utx);
23638a99942STim J. Robbins 	}
237b457a3e1SOlivier Houchard }
23838a99942STim J. Robbins 
239d2979f56STim J. Robbins static void
quick(void)240b911cea8SEd Schouten quick(void)
24138a99942STim J. Robbins {
242b911cea8SEd Schouten 	struct utmpx *utx;
24338a99942STim J. Robbins 	int col, ncols, num;
24438a99942STim J. Robbins 
24538a99942STim J. Robbins 	ncols = ttywidth();
24638a99942STim J. Robbins 	col = num = 0;
247b911cea8SEd Schouten 	while ((utx = getutxent()) != NULL) {
248b911cea8SEd Schouten 		if (utx->ut_type != USER_PROCESS)
24938a99942STim J. Robbins 			continue;
250b911cea8SEd Schouten 		printf("%-16s", utx->ut_user);
251b911cea8SEd Schouten 		if (++col < ncols / (16 + 1))
25238a99942STim J. Robbins 			putchar(' ');
25338a99942STim J. Robbins 		else {
25438a99942STim J. Robbins 			col = 0;
25538a99942STim J. Robbins 			putchar('\n');
25638a99942STim J. Robbins 		}
25738a99942STim J. Robbins 		num++;
25838a99942STim J. Robbins 	}
25938a99942STim J. Robbins 	if (col != 0)
26038a99942STim J. Robbins 		putchar('\n');
26138a99942STim J. Robbins 
26238a99942STim J. Robbins 	printf("# users = %d\n", num);
26338a99942STim J. Robbins }
26438a99942STim J. Robbins 
265d2979f56STim J. Robbins static void
whoami(void)266b911cea8SEd Schouten whoami(void)
26738a99942STim J. Robbins {
268b911cea8SEd Schouten 	struct utmpx ut, *utx;
26938a99942STim J. Robbins 	struct passwd *pwd;
270b911cea8SEd Schouten 	const char *name, *tty;
27138a99942STim J. Robbins 
27238a99942STim J. Robbins 	if ((tty = ttyname(STDIN_FILENO)) == NULL)
27338a99942STim J. Robbins 		tty = "tty??";
274b911cea8SEd Schouten 	else if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
275b911cea8SEd Schouten 		tty += sizeof _PATH_DEV - 1;
276b911cea8SEd Schouten 	strlcpy(ut.ut_line, tty, sizeof ut.ut_line);
27738a99942STim J. Robbins 
27838a99942STim J. Robbins 	/* Search utmp for our tty, dump first matching record. */
279b911cea8SEd Schouten 	if ((utx = getutxline(&ut)) != NULL && utx->ut_type == USER_PROCESS) {
280b911cea8SEd Schouten 		row(utx);
28138a99942STim J. Robbins 		return;
28238a99942STim J. Robbins 	}
28338a99942STim J. Robbins 
28438a99942STim J. Robbins 	/* Not found; fill the utmp structure with the information we have. */
28538a99942STim J. Robbins 	memset(&ut, 0, sizeof(ut));
28638a99942STim J. Robbins 	if ((pwd = getpwuid(getuid())) != NULL)
28738a99942STim J. Robbins 		name = pwd->pw_name;
28838a99942STim J. Robbins 	else
28938a99942STim J. Robbins 		name = "?";
290b911cea8SEd Schouten 	strlcpy(ut.ut_user, name, sizeof ut.ut_user);
291b911cea8SEd Schouten 	gettimeofday(&ut.ut_tv, NULL);
29238a99942STim J. Robbins 	row(&ut);
29338a99942STim J. Robbins }
29438a99942STim J. Robbins 
295d2979f56STim J. Robbins static int
ttywidth(void)29638a99942STim J. Robbins ttywidth(void)
29738a99942STim J. Robbins {
29838a99942STim J. Robbins 	struct winsize ws;
2998358edb6STim J. Robbins 	long width;
3008358edb6STim J. Robbins 	char *cols, *ep;
30138a99942STim J. Robbins 
3028358edb6STim J. Robbins 	if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
3038358edb6STim J. Robbins 		errno = 0;
3048358edb6STim J. Robbins 		width = strtol(cols, &ep, 10);
3058358edb6STim J. Robbins 		if (errno || width <= 0 || width > INT_MAX || ep == cols ||
3068358edb6STim J. Robbins 		    *ep != '\0')
3078358edb6STim J. Robbins 			warnx("invalid COLUMNS environment variable ignored");
30838a99942STim J. Robbins 		else
30910ebb45aSPeter Wemm 			return (width);
3108358edb6STim J. Robbins 	}
3118358edb6STim J. Robbins 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
3128358edb6STim J. Robbins 		return (ws.ws_col);
31338a99942STim J. Robbins 
3148358edb6STim J. Robbins 	return (80);
3159b50d902SRodney W. Grimes }
316