Home | History | Annotate | Line # | Download | only in who
who.c revision 1.17
      1 /*	$NetBSD: who.c,v 1.17 2006/09/19 14:35:25 hubertf 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.17 2006/09/19 14:35:25 hubertf 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);
     72 static void process(const char *, int);
     73 static void print(const char *, const char *, time_t, const char *, pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type);
     74 static void quick(const char *);
     75 
     76 static int show_term;			/* show term state */
     77 static int show_idle;			/* show idle time */
     78 static int show_details;		/* show exit status etc. */
     79 
     80 extern int maxname, maxline, maxhost;
     81 
     82 struct ut_type_names {
     83   int type;
     84   char name[30];
     85 } ut_type_names[] = {
     86 #ifdef SUPPORT_UTMPX
     87   { EMPTY, "EMPTY" },
     88   { RUN_LVL, "RUN_LVL" },
     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 
    109 	setlocale(LC_ALL, "");
    110 
    111 	only_current_term = show_term = show_idle = show_labels = 0;
    112 	quick_mode = default_mode = 0;
    113 
    114 	while ((c = getopt(argc, argv, "dHmqsTu")) != -1) {
    115 		switch (c) {
    116 		case 'd':
    117 			show_details = 1;
    118 			break;
    119 		case 'H':
    120 			show_labels = 1;
    121 			break;
    122 		case 'm':
    123 			only_current_term = 1;
    124 			break;
    125 		case 'q':
    126 			quick_mode = 1;
    127 			break;
    128 		case 's':
    129 			default_mode = 1;
    130 			break;
    131 		case 'T':
    132 			show_term = 1;
    133 			break;
    134 		case 'u':
    135 			show_idle = 1;
    136 			break;
    137 		default:
    138 			usage();
    139 			/* NOTREACHED */
    140 		}
    141 	}
    142 	argc -= optind;
    143 	argv += optind;
    144 
    145 	if (chdir("/dev")) {
    146 		err(EXIT_FAILURE, "cannot change directory to /dev");
    147 		/* NOTREACHED */
    148 	}
    149 
    150 	if (default_mode)
    151 		only_current_term = show_term = show_idle = 0;
    152 
    153 	switch (argc) {
    154 	case 0:					/* who */
    155 		if (quick_mode) {
    156 			quick(NULL);
    157 		} else if (only_current_term) {
    158 			who_am_i(NULL, show_labels);
    159 		} else {
    160 			process(NULL, show_labels);
    161 		}
    162 		break;
    163 	case 1:					/* who utmp_file */
    164 		if (quick_mode) {
    165 			quick(*argv);
    166 		} else if (only_current_term) {
    167 			who_am_i(*argv, show_labels);
    168 		} else {
    169 			process(*argv, show_labels);
    170 		}
    171 		break;
    172 	case 2:					/* who am i */
    173 		who_am_i(NULL, show_labels);
    174 		break;
    175 	default:
    176 		usage();
    177 		/* NOTREACHED */
    178 	}
    179 
    180 	return 0;
    181 }
    182 
    183 static void
    184 who_am_i(const char *fname, int show_labels)
    185 {
    186 	struct passwd *pw;
    187 	const char *p;
    188 	char *t;
    189 	time_t now;
    190 	struct utmpentry *ehead, *ep;
    191 
    192 	/* search through the utmp and find an entry for this tty */
    193 	if ((p = ttyname(STDIN_FILENO)) != NULL) {
    194 
    195 		/* strip any directory component */
    196 		if ((t = strrchr(p, '/')) != NULL)
    197 			p = t + 1;
    198 
    199 		(void)getutentries(fname, &ehead);
    200 		for (ep = ehead; ep; ep = ep->next)
    201 			if (strcmp(ep->line, p) == 0) {
    202 				if (show_labels)
    203 					output_labels();
    204 				print(ep->name, ep->line,
    205 				      (time_t)ep->tv.tv_sec,
    206 				      ep->host,
    207 				      ep->pid,
    208 				      ep->term,
    209 				      ep->exit,
    210 				      ep->sess,
    211 				      ep->type );
    212 				return;
    213 			}
    214 	} else
    215 		p = "tty??";
    216 
    217 	(void)time(&now);
    218 	pw = getpwuid(getuid());
    219 	if (show_labels)
    220 		output_labels();
    221 	print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
    222 }
    223 
    224 static void
    225 process(const char *fname, int show_labels)
    226 {
    227 	struct utmpentry *ehead, *ep;
    228 	(void)getutentries(fname, &ehead);
    229 	if (show_labels)
    230 		output_labels();
    231 	for (ep = ehead; ep != NULL; ep = ep->next)
    232 		print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
    233 		      ep->host, ep->pid, ep->term, ep->exit,
    234 		      ep->sess, ep->type);
    235 }
    236 
    237 static void
    238 print(const char *name, const char *line, time_t t, const char *host, pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type)
    239 {
    240 	struct stat sb;
    241 	char state;
    242 	static time_t now = 0;
    243 	time_t idle;
    244 	char *types = NULL;
    245 	int i;
    246 
    247 	state = '?';
    248 	idle = 0;
    249 
    250 	for (i=0; ut_type_names[i].type >= 0; i++) {
    251 		types = ut_type_names[i].name;
    252 		if ( ut_type_names[i].type == type )
    253 			break;
    254 	}
    255 
    256 	if (show_term || show_idle) {
    257 		if (now == 0)
    258 			time(&now);
    259 
    260 		if (stat(line, &sb) == 0) {
    261 			state = (sb.st_mode & 020) ? '+' : '-';
    262 			idle = now - sb.st_atime;
    263 		}
    264 
    265 	}
    266 
    267 	(void)printf("%-*.*s ", maxname, maxname, name);
    268 
    269 	if (show_term)
    270 		(void)printf("%c ", state);
    271 
    272 	(void)printf("%-*.*s ", maxline, maxline, line);
    273 	(void)printf("%.12s ", ctime(&t) + 4);
    274 
    275 	if (show_idle) {
    276 		if (idle < 60)
    277 			(void)printf("  .   ");
    278 		else if (idle < (24 * 60 * 60))
    279 			(void)printf("%02ld:%02ld ",
    280 				     (long)(idle / (60 * 60)),
    281 				     (long)(idle % (60 * 60)) / 60);
    282 		else
    283 			(void)printf(" old  ");
    284 
    285 		(void)printf("\t%6d", pid);
    286 
    287 		if (show_details) {
    288 			(void)printf("\tterm=%d exit=%d", term, xit);
    289 			(void)printf(" sess=%d", sess);
    290 			(void)printf(" type=%s ", types);
    291 		}
    292 	}
    293 
    294 	if (*host)
    295 		(void)printf("\t(%.*s)", maxhost, host);
    296 	(void)putchar('\n');
    297 }
    298 
    299 static void
    300 output_labels(void)
    301 {
    302 	(void)printf("%-*.*s ", maxname, maxname, "USER");
    303 
    304 	if (show_term)
    305 		(void)printf("S ");
    306 
    307 	(void)printf("%-*.*s ", maxline, maxline, "LINE");
    308 	(void)printf("WHEN         ");
    309 
    310 	if (show_idle) {
    311 		(void)printf("IDLE  ");
    312 		(void)printf("\t   PID");
    313 
    314 		(void)printf("\tCOMMENT");
    315 	}
    316 
    317 	(void)putchar('\n');
    318 }
    319 
    320 static void
    321 quick(const char *fname)
    322 {
    323 	struct utmpentry *ehead, *ep;
    324 	int num = 0;
    325 
    326 	(void)getutentries(fname, &ehead);
    327 	for (ep = ehead; ep != NULL; ep = ep->next) {
    328 		(void)printf("%-*s ", maxname, ep->name);
    329 		if ((++num % 8) == 0)
    330 			(void)putchar('\n');
    331 	}
    332 	if (num % 8)
    333 		(void)putchar('\n');
    334 
    335 	(void)printf("# users = %d\n", num);
    336 }
    337 
    338 static void
    339 usage(void)
    340 {
    341 	(void)fprintf(stderr, "usage: %s [-dHmqsTu] [file]\n       %s am i\n",
    342 	    getprogname(), getprogname());
    343 	exit(EXIT_FAILURE);
    344 }
    345