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