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