Home | History | Annotate | Line # | Download | only in who
      1  1.25  christos /*	$NetBSD: who.c,v 1.25 2015/11/21 15:01:43 christos Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1989, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Michael Fischbein.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.5    kleink #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.22     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     38  1.22     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42   1.3       jtc #if 0
     43   1.3       jtc static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 6/6/93";
     44   1.3       jtc #endif
     45  1.25  christos __RCSID("$NetBSD: who.c,v 1.25 2015/11/21 15:01:43 christos Exp $");
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48   1.1       cgd #include <sys/types.h>
     49   1.4       jtc #include <sys/stat.h>
     50  1.16     peter 
     51   1.5    kleink #include <err.h>
     52   1.5    kleink #include <locale.h>
     53   1.1       cgd #include <pwd.h>
     54   1.1       cgd #include <stdio.h>
     55   1.5    kleink #include <stdlib.h>
     56   1.4       jtc #include <string.h>
     57   1.5    kleink #include <time.h>
     58   1.4       jtc #include <unistd.h>
     59  1.17   hubertf #ifdef SUPPORT_UTMP
     60  1.17   hubertf #include <utmp.h>
     61  1.17   hubertf #endif
     62  1.17   hubertf #ifdef SUPPORT_UTMPX
     63  1.17   hubertf #include <utmpx.h>
     64  1.17   hubertf #endif
     65  1.16     peter 
     66  1.10  christos #include "utmpentry.h"
     67   1.9  christos 
     68   1.9  christos static void output_labels(void);
     69   1.9  christos static void who_am_i(const char *, int);
     70  1.21     perry static void usage(void) __dead;
     71   1.9  christos static void process(const char *, int);
     72  1.18  christos static void eprint(const struct utmpentry *);
     73  1.18  christos static void print(const char *, const char *, time_t, const char *, pid_t pid,
     74  1.18  christos     uint16_t term, uint16_t xit, uint16_t sess, uint16_t type);
     75  1.16     peter static void quick(const char *);
     76   1.1       cgd 
     77   1.9  christos static int show_term;			/* show term state */
     78   1.9  christos static int show_idle;			/* show idle time */
     79  1.17   hubertf static int show_details;		/* show exit status etc. */
     80   1.9  christos 
     81  1.17   hubertf struct ut_type_names {
     82  1.17   hubertf   int type;
     83  1.18  christos   const char *name;
     84  1.17   hubertf } ut_type_names[] = {
     85  1.17   hubertf #ifdef SUPPORT_UTMPX
     86  1.18  christos   { EMPTY, "empty" },
     87  1.18  christos   { RUN_LVL, "run level" },
     88  1.18  christos   { BOOT_TIME, "boot time" },
     89  1.18  christos   { OLD_TIME, "old time" },
     90  1.18  christos   { NEW_TIME, "new time" },
     91  1.18  christos   { INIT_PROCESS, "init process" },
     92  1.18  christos   { LOGIN_PROCESS, "login process" },
     93  1.18  christos   { USER_PROCESS, "user process" },
     94  1.18  christos   { DEAD_PROCESS, "dead process" },
     95  1.17   hubertf #if defined(_NETBSD_SOURCE)
     96  1.18  christos   { ACCOUNTING, "accounting" },
     97  1.18  christos   { SIGNATURE, "signature" },
     98  1.19  christos   { DOWN_TIME, "down time" },
     99  1.17   hubertf #endif /* _NETBSD_SOURCE */
    100  1.17   hubertf #endif /* SUPPORT_UTMPX */
    101  1.17   hubertf   { -1, "unknown" }
    102  1.17   hubertf };
    103  1.17   hubertf 
    104   1.4       jtc int
    105  1.16     peter main(int argc, char *argv[])
    106   1.1       cgd {
    107  1.16     peter 	int c, only_current_term, show_labels, quick_mode, default_mode;
    108  1.18  christos 	int et = 0;
    109   1.5    kleink 
    110   1.5    kleink 	setlocale(LC_ALL, "");
    111   1.4       jtc 
    112   1.5    kleink 	only_current_term = show_term = show_idle = show_labels = 0;
    113  1.16     peter 	quick_mode = default_mode = 0;
    114  1.16     peter 
    115  1.18  christos 	while ((c = getopt(argc, argv, "abdHlmpqrsTtuv")) != -1) {
    116   1.4       jtc 		switch (c) {
    117  1.18  christos 		case 'a':
    118  1.18  christos 			et = -1;
    119  1.18  christos 			show_idle = show_details = 1;
    120  1.18  christos 			break;
    121  1.18  christos 		case 'b':
    122  1.18  christos 			et |= (1 << BOOT_TIME);
    123  1.18  christos 			break;
    124  1.17   hubertf 		case 'd':
    125  1.18  christos 			et |= (1 << DEAD_PROCESS);
    126  1.17   hubertf 			break;
    127  1.14     peter 		case 'H':
    128  1.14     peter 			show_labels = 1;
    129  1.14     peter 			break;
    130  1.18  christos 		case 'l':
    131  1.18  christos 			et |= (1 << LOGIN_PROCESS);
    132  1.18  christos 			break;
    133   1.4       jtc 		case 'm':
    134   1.4       jtc 			only_current_term = 1;
    135   1.4       jtc 			break;
    136  1.18  christos 		case 'p':
    137  1.18  christos 			et |= (1 << INIT_PROCESS);
    138  1.18  christos 			break;
    139  1.16     peter 		case 'q':
    140  1.16     peter 			quick_mode = 1;
    141  1.16     peter 			break;
    142  1.18  christos 		case 'r':
    143  1.18  christos 			et |= (1 << RUN_LVL);
    144  1.18  christos 			break;
    145  1.16     peter 		case 's':
    146  1.16     peter 			default_mode = 1;
    147  1.16     peter 			break;
    148   1.4       jtc 		case 'T':
    149   1.4       jtc 			show_term = 1;
    150   1.4       jtc 			break;
    151  1.18  christos 		case 't':
    152  1.18  christos 			et |= (1 << NEW_TIME);
    153  1.18  christos 			break;
    154   1.4       jtc 		case 'u':
    155   1.4       jtc 			show_idle = 1;
    156   1.4       jtc 			break;
    157  1.18  christos 		case 'v':
    158  1.18  christos 			show_details = 1;
    159  1.18  christos 			break;
    160   1.4       jtc 		default:
    161   1.4       jtc 			usage();
    162   1.4       jtc 			/* NOTREACHED */
    163   1.4       jtc 		}
    164   1.4       jtc 	}
    165   1.4       jtc 	argc -= optind;
    166   1.4       jtc 	argv += optind;
    167   1.4       jtc 
    168  1.18  christos 	if (et != 0)
    169  1.18  christos 		etype = et;
    170  1.18  christos 
    171   1.4       jtc 	if (chdir("/dev")) {
    172  1.16     peter 		err(EXIT_FAILURE, "cannot change directory to /dev");
    173   1.4       jtc 		/* NOTREACHED */
    174   1.4       jtc 	}
    175   1.1       cgd 
    176  1.16     peter 	if (default_mode)
    177  1.16     peter 		only_current_term = show_term = show_idle = 0;
    178  1.16     peter 
    179   1.1       cgd 	switch (argc) {
    180   1.4       jtc 	case 0:					/* who */
    181  1.16     peter 		if (quick_mode) {
    182  1.16     peter 			quick(NULL);
    183  1.16     peter 		} else if (only_current_term) {
    184   1.9  christos 			who_am_i(NULL, show_labels);
    185   1.4       jtc 		} else {
    186   1.9  christos 			process(NULL, show_labels);
    187   1.4       jtc 		}
    188   1.4       jtc 		break;
    189   1.4       jtc 	case 1:					/* who utmp_file */
    190  1.16     peter 		if (quick_mode) {
    191  1.16     peter 			quick(*argv);
    192  1.16     peter 		} else if (only_current_term) {
    193   1.9  christos 			who_am_i(*argv, show_labels);
    194   1.4       jtc 		} else {
    195   1.9  christos 			process(*argv, show_labels);
    196   1.4       jtc 		}
    197   1.1       cgd 		break;
    198   1.4       jtc 	case 2:					/* who am i */
    199   1.9  christos 		who_am_i(NULL, show_labels);
    200   1.1       cgd 		break;
    201   1.1       cgd 	default:
    202   1.4       jtc 		usage();
    203   1.4       jtc 		/* NOTREACHED */
    204   1.1       cgd 	}
    205  1.16     peter 
    206  1.16     peter 	return 0;
    207   1.1       cgd }
    208   1.1       cgd 
    209  1.23  christos static char *
    210  1.23  christos strrstr(const char *str, const char *pat)
    211  1.23  christos {
    212  1.23  christos 	const char *estr;
    213  1.23  christos 	size_t len;
    214  1.23  christos 	if (*pat == '\0')
    215  1.23  christos 		return __UNCONST(str);
    216  1.23  christos 
    217  1.23  christos 	len = strlen(pat);
    218  1.23  christos 
    219  1.23  christos 	for (estr = str + strlen(str); str < estr; estr--)
    220  1.23  christos 		if (strncmp(estr, pat, len) == 0)
    221  1.23  christos 			return __UNCONST(estr);
    222  1.23  christos 	return NULL;
    223  1.23  christos }
    224  1.23  christos 
    225   1.9  christos static void
    226   1.9  christos who_am_i(const char *fname, int show_labels)
    227   1.4       jtc {
    228   1.4       jtc 	struct passwd *pw;
    229  1.15  christos 	const char *p;
    230   1.4       jtc 	char *t;
    231   1.9  christos 	time_t now;
    232  1.10  christos 	struct utmpentry *ehead, *ep;
    233   1.4       jtc 
    234   1.4       jtc 	/* search through the utmp and find an entry for this tty */
    235   1.9  christos 	if ((p = ttyname(STDIN_FILENO)) != NULL) {
    236   1.9  christos 
    237  1.23  christos 		/* strip directory prefixes for ttys */
    238  1.23  christos 		if ((t = strrstr(p, "/pts/")) != NULL ||
    239  1.23  christos 		    (t = strrchr(p, '/')) != NULL)
    240   1.4       jtc 			p = t + 1;
    241  1.10  christos 
    242  1.10  christos 		(void)getutentries(fname, &ehead);
    243  1.10  christos 		for (ep = ehead; ep; ep = ep->next)
    244  1.10  christos 			if (strcmp(ep->line, p) == 0) {
    245   1.9  christos 				if (show_labels)
    246   1.9  christos 					output_labels();
    247  1.18  christos 				eprint(ep);
    248   1.4       jtc 				return;
    249   1.4       jtc 			}
    250   1.4       jtc 	} else
    251   1.9  christos 		p = "tty??";
    252   1.4       jtc 
    253   1.9  christos 	(void)time(&now);
    254   1.4       jtc 	pw = getpwuid(getuid());
    255   1.9  christos 	if (show_labels)
    256   1.9  christos 		output_labels();
    257  1.17   hubertf 	print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
    258   1.9  christos }
    259   1.9  christos 
    260   1.9  christos static void
    261   1.9  christos process(const char *fname, int show_labels)
    262   1.9  christos {
    263  1.10  christos 	struct utmpentry *ehead, *ep;
    264  1.10  christos 	(void)getutentries(fname, &ehead);
    265   1.9  christos 	if (show_labels)
    266   1.9  christos 		output_labels();
    267   1.9  christos 	for (ep = ehead; ep != NULL; ep = ep->next)
    268  1.18  christos 		eprint(ep);
    269  1.18  christos }
    270  1.18  christos 
    271  1.18  christos static void
    272  1.18  christos eprint(const struct utmpentry *ep)
    273  1.18  christos {
    274  1.18  christos 	print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host, ep->pid,
    275  1.18  christos 	    ep->term, ep->exit, ep->sess, ep->type);
    276   1.9  christos }
    277   1.4       jtc 
    278   1.9  christos static void
    279  1.18  christos print(const char *name, const char *line, time_t t, const char *host,
    280  1.18  christos     pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type)
    281   1.1       cgd {
    282   1.4       jtc 	struct stat sb;
    283   1.4       jtc 	char state;
    284   1.4       jtc 	static time_t now = 0;
    285   1.4       jtc 	time_t idle;
    286  1.18  christos 	const char *types = NULL;
    287  1.18  christos 	size_t i;
    288  1.24   mlelstv 	char *tstr;
    289   1.4       jtc 
    290   1.5    kleink 	state = '?';
    291   1.5    kleink 	idle = 0;
    292   1.5    kleink 
    293  1.18  christos 	for (i = 0; ut_type_names[i].type >= 0; i++) {
    294  1.17   hubertf 		types = ut_type_names[i].name;
    295  1.18  christos 		if (ut_type_names[i].type == type)
    296  1.17   hubertf 			break;
    297  1.17   hubertf 	}
    298  1.17   hubertf 
    299   1.4       jtc 	if (show_term || show_idle) {
    300   1.4       jtc 		if (now == 0)
    301   1.4       jtc 			time(&now);
    302   1.4       jtc 
    303   1.4       jtc 		if (stat(line, &sb) == 0) {
    304   1.4       jtc 			state = (sb.st_mode & 020) ? '+' : '-';
    305   1.4       jtc 			idle = now - sb.st_atime;
    306   1.4       jtc 		}
    307   1.4       jtc 
    308   1.4       jtc 	}
    309   1.4       jtc 
    310  1.25  christos 	(void)printf("%-*.*s ", (int)maxname, (int)maxname, name);
    311   1.4       jtc 
    312  1.16     peter 	if (show_term)
    313   1.4       jtc 		(void)printf("%c ", state);
    314   1.4       jtc 
    315  1.25  christos 	(void)printf("%-*.*s ", (int)maxline, (int)maxline, line);
    316  1.24   mlelstv 	tstr = ctime(&t);
    317  1.24   mlelstv 	(void)printf("%.12s ", tstr ? tstr + 4 : "?");
    318   1.1       cgd 
    319   1.4       jtc 	if (show_idle) {
    320   1.4       jtc 		if (idle < 60)
    321   1.4       jtc 			(void)printf("  .   ");
    322   1.4       jtc 		else if (idle < (24 * 60 * 60))
    323   1.5    kleink 			(void)printf("%02ld:%02ld ",
    324   1.5    kleink 				     (long)(idle / (60 * 60)),
    325   1.5    kleink 				     (long)(idle % (60 * 60)) / 60);
    326   1.4       jtc 		else
    327   1.4       jtc 			(void)printf(" old  ");
    328  1.17   hubertf 
    329  1.17   hubertf 		(void)printf("\t%6d", pid);
    330  1.17   hubertf 
    331  1.17   hubertf 		if (show_details) {
    332  1.20  christos 			if (type == RUN_LVL)
    333  1.20  christos 				(void)printf("\tnew=%c old=%c", term, xit);
    334  1.20  christos 			else
    335  1.20  christos 				(void)printf("\tterm=%d exit=%d", term, xit);
    336  1.17   hubertf 			(void)printf(" sess=%d", sess);
    337  1.17   hubertf 			(void)printf(" type=%s ", types);
    338  1.17   hubertf 		}
    339   1.4       jtc 	}
    340   1.4       jtc 
    341   1.9  christos 	if (*host)
    342  1.25  christos 		(void)printf("\t(%.*s)", (int)maxhost, host);
    343   1.1       cgd 	(void)putchar('\n');
    344   1.1       cgd }
    345   1.1       cgd 
    346   1.9  christos static void
    347  1.16     peter output_labels(void)
    348   1.5    kleink {
    349  1.25  christos 	(void)printf("%-*.*s ", (int)maxname, (int)maxname, "USER");
    350   1.5    kleink 
    351   1.5    kleink 	if (show_term)
    352   1.5    kleink 		(void)printf("S ");
    353   1.5    kleink 
    354  1.25  christos 	(void)printf("%-*.*s ", (int)maxline, (int)maxline, "LINE");
    355   1.5    kleink 	(void)printf("WHEN         ");
    356   1.5    kleink 
    357  1.17   hubertf 	if (show_idle) {
    358   1.5    kleink 		(void)printf("IDLE  ");
    359  1.17   hubertf 		(void)printf("\t   PID");
    360   1.5    kleink 
    361  1.17   hubertf 		(void)printf("\tCOMMENT");
    362  1.17   hubertf 	}
    363   1.5    kleink 
    364   1.5    kleink 	(void)putchar('\n');
    365   1.5    kleink }
    366   1.5    kleink 
    367   1.9  christos static void
    368  1.16     peter quick(const char *fname)
    369  1.16     peter {
    370  1.16     peter 	struct utmpentry *ehead, *ep;
    371  1.16     peter 	int num = 0;
    372  1.16     peter 
    373  1.16     peter 	(void)getutentries(fname, &ehead);
    374  1.16     peter 	for (ep = ehead; ep != NULL; ep = ep->next) {
    375  1.25  christos 		(void)printf("%-*s ", (int)maxname, ep->name);
    376  1.16     peter 		if ((++num % 8) == 0)
    377  1.16     peter 			(void)putchar('\n');
    378  1.16     peter 	}
    379  1.16     peter 	if (num % 8)
    380  1.16     peter 		(void)putchar('\n');
    381  1.16     peter 
    382  1.16     peter 	(void)printf("# users = %d\n", num);
    383  1.16     peter }
    384  1.16     peter 
    385  1.16     peter static void
    386  1.16     peter usage(void)
    387   1.4       jtc {
    388  1.18  christos 	(void)fprintf(stderr, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
    389   1.9  christos 	    getprogname(), getprogname());
    390  1.16     peter 	exit(EXIT_FAILURE);
    391   1.1       cgd }
    392