Change -u behaviour to also print PID of process on terminal, and

add -d to print more information from utmpx.

OK'd by christos@
This commit is contained in:
hubertf 2006-09-19 14:35:25 +00:00
parent 47ac97884a
commit d7f002b78d
4 changed files with 132 additions and 27 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: utmpentry.c,v 1.7 2006/03/17 20:44:28 elad Exp $ */
/* $NetBSD: utmpentry.c,v 1.8 2006/09/19 14:35:25 hubertf Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: utmpentry.c,v 1.7 2006/03/17 20:44:28 elad Exp $");
__RCSID("$NetBSD: utmpentry.c,v 1.8 2006/09/19 14:35:25 hubertf Exp $");
#endif
#include <sys/stat.h>
@ -277,6 +277,11 @@ getentry(struct utmpentry *e, struct utmp *up)
e->name[sizeof(e->name) - 1] = '\0';
e->tv.tv_sec = up->ut_time;
e->tv.tv_usec = 0;
e->pid = 0;
e->term = 0;
e->exit = 0;
e->sess = 0;
e->type = 0;
adjust_size(e);
}
#endif
@ -292,6 +297,11 @@ getentryx(struct utmpentry *e, struct utmpx *up)
(void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
e->name[sizeof(e->name) - 1] = '\0';
e->tv = up->ut_tv;
e->pid = up->ut_pid;
e->term = up->ut_exit.e_termination;
e->exit = up->ut_exit.e_exit;
e->sess = up->ut_session;
e->type = up->ut_type;
adjust_size(e);
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: utmpentry.h,v 1.2 2003/11/28 23:52:34 wiz Exp $ */
/* $NetBSD: utmpentry.h,v 1.3 2006/09/19 14:35:25 hubertf Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -36,11 +36,29 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(SUPPORT_UTMPX)
# define WHO_NAME_LEN _UTX_USERSIZE
# define WHO_LINE_LEN _UTX_LINESIZE
# define WHO_HOST_LEN _UTX_HOSTSIZE
#elif defined(SUPPORT_UTMP)
# define WHO_NAME_LEN UT_NAMESIZE
# define WHO_LINE_LEN UT_LINESIZE
# define WHO_HOST_LEN UT_HOSTSIZE
#else
# error Either SUPPORT_UTMPX or SUPPORT_UTMP must be defined!
#endif
struct utmpentry {
char name[65];
char line[65];
char host[257];
char name[WHO_NAME_LEN + 1];
char line[WHO_LINE_LEN + 1];
char host[WHO_HOST_LEN + 1];
struct timeval tv;
pid_t pid;
uint16_t term;
uint16_t exit;
uint16_t sess;
uint16_t type;
struct utmpentry *next;
};

View File

@ -1,4 +1,4 @@
.\" $NetBSD: who.1,v 1.16 2005/07/22 14:23:05 peter Exp $
.\" $NetBSD: who.1,v 1.17 2006/09/19 14:35:25 hubertf Exp $
.\"
.\" Copyright (c) 1986, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)who.1 8.2 (Berkeley) 12/30/93
.\"
.Dd July 22, 2005
.Dd September 19, 2006
.Dt WHO 1
.Os
.Sh NAME
@ -37,7 +37,7 @@
.Nd display who is logged in
.Sh SYNOPSIS
.Nm
.Op Fl HmqsTu
.Op Fl dHmqsTu
.Op Ar file
.Nm
.Ar am i
@ -51,6 +51,15 @@ hostname if not local.
Available options:
.Pp
.Bl -tag -width file
.It Fl d
When printing of more information is requested with
.Fl u ,
this switch can be used to also printed
process termination signals,
process exit status,
session id for windowing
and the type of the entry, see documentation of ut_type in
.Xr getutxent 3 .
.It Fl H
Write column headings above the regular output.
.It Fl m
@ -78,38 +87,46 @@ and
.Sq \&?
if a bad line is encountered.
.It Fl u
Print the idle time for each user.
Print the idle time for each user, and the associated process ID.
.It Ar \&am I
Returns the invoker's real user name.
.It Ar file
By default,
.Nm
gathers information from the file
.Pa /var/run/utmp .
.Pa /var/run/utmpx .
An alternative
.Ar file
may be specified which is usually
.Pa /var/log/wtmp
.Pa /var/log/wtmpx
(or
.Pa /var/log/wtmp
or
.Pa /var/log/wtmp.[0-6]
depending on site policy as
.Pa wtmp
.Pa wtmpx
can grow quite large and daily versions may or may not
be kept around after compression by
.Xr ac 8 ) .
The
.Pa wtmpx
and
.Pa wtmp
file contains a record of every login, logout,
crash, shutdown and date change
since
.Pa wtmpx
and
.Pa wtmp
was last truncated or
were last truncated or
created.
.El
.Pp
If
.Pa /var/log/wtmpx
or
.Pa /var/log/wtmp
is being used as the file, the user name may be empty
are being used as the file, the user name may be empty
or one of the special characters '|', '}' and '~'.
Logouts produce an output line without any user name.
For more information on the
@ -118,8 +135,10 @@ special characters, see
.Sh FILES
.Bl -tag -width /var/log/wtmp.[0-6] -compact
.It Pa /var/run/utmp
.It Pa /var/run/utmpx
.It Pa /var/log/wtmp
.It Pa /var/log/wtmp.[0-6]
.It Pa /var/log/wtmpx
.El
.Sh SEE ALSO
.Xr last 1 ,

View File

@ -1,4 +1,4 @@
/* $NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $ */
/* $NetBSD: who.c,v 1.17 2006/09/19 14:35:25 hubertf Exp $ */
/*
* Copyright (c) 1989, 1993
@ -43,7 +43,7 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $");
__RCSID("$NetBSD: who.c,v 1.17 2006/09/19 14:35:25 hubertf Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -57,6 +57,12 @@ __RCSID("$NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $");
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef SUPPORT_UTMP
#include <utmp.h>
#endif
#ifdef SUPPORT_UTMPX
#include <utmpx.h>
#endif
#include "utmpentry.h"
@ -64,14 +70,37 @@ static void output_labels(void);
static void who_am_i(const char *, int);
static void usage(void);
static void process(const char *, int);
static void print(const char *, const char *, time_t, const char *);
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);
static void quick(const char *);
static int show_term; /* show term state */
static int show_idle; /* show idle time */
static int show_details; /* show exit status etc. */
extern int maxname, maxline, maxhost;
struct ut_type_names {
int type;
char name[30];
} ut_type_names[] = {
#ifdef SUPPORT_UTMPX
{ EMPTY, "EMPTY" },
{ RUN_LVL, "RUN_LVL" },
{ BOOT_TIME, "BOOT_TIME" },
{ OLD_TIME, "OLD_TIME" },
{ NEW_TIME, "NEW_TIME" },
{ INIT_PROCESS, "INIT_PROCESS" },
{ LOGIN_PROCESS, "LOGIN_PROCESS" },
{ USER_PROCESS, "USER_PROCESS" },
{ DEAD_PROCESS, "DEAD_PROCESS" },
#if defined(_NETBSD_SOURCE)
{ ACCOUNTING, "ACCOUNTING" },
{ SIGNATURE, "SIGNATURE" },
#endif /* _NETBSD_SOURCE */
#endif /* SUPPORT_UTMPX */
{ -1, "unknown" }
};
int
main(int argc, char *argv[])
{
@ -82,8 +111,11 @@ main(int argc, char *argv[])
only_current_term = show_term = show_idle = show_labels = 0;
quick_mode = default_mode = 0;
while ((c = getopt(argc, argv, "HmqsTu")) != -1) {
while ((c = getopt(argc, argv, "dHmqsTu")) != -1) {
switch (c) {
case 'd':
show_details = 1;
break;
case 'H':
show_labels = 1;
break;
@ -169,8 +201,14 @@ who_am_i(const char *fname, int show_labels)
if (strcmp(ep->line, p) == 0) {
if (show_labels)
output_labels();
print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
ep->host);
print(ep->name, ep->line,
(time_t)ep->tv.tv_sec,
ep->host,
ep->pid,
ep->term,
ep->exit,
ep->sess,
ep->type );
return;
}
} else
@ -180,7 +218,7 @@ who_am_i(const char *fname, int show_labels)
pw = getpwuid(getuid());
if (show_labels)
output_labels();
print(pw ? pw->pw_name : "?", p, now, "");
print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
}
static void
@ -191,20 +229,30 @@ process(const char *fname, int show_labels)
if (show_labels)
output_labels();
for (ep = ehead; ep != NULL; ep = ep->next)
print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host);
print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
ep->host, ep->pid, ep->term, ep->exit,
ep->sess, ep->type);
}
static void
print(const char *name, const char *line, time_t t, const char *host)
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)
{
struct stat sb;
char state;
static time_t now = 0;
time_t idle;
char *types = NULL;
int i;
state = '?';
idle = 0;
for (i=0; ut_type_names[i].type >= 0; i++) {
types = ut_type_names[i].name;
if ( ut_type_names[i].type == type )
break;
}
if (show_term || show_idle) {
if (now == 0)
time(&now);
@ -233,6 +281,14 @@ print(const char *name, const char *line, time_t t, const char *host)
(long)(idle % (60 * 60)) / 60);
else
(void)printf(" old ");
(void)printf("\t%6d", pid);
if (show_details) {
(void)printf("\tterm=%d exit=%d", term, xit);
(void)printf(" sess=%d", sess);
(void)printf(" type=%s ", types);
}
}
if (*host)
@ -251,10 +307,12 @@ output_labels(void)
(void)printf("%-*.*s ", maxline, maxline, "LINE");
(void)printf("WHEN ");
if (show_idle)
if (show_idle) {
(void)printf("IDLE ");
(void)printf("\t PID");
(void)printf("\t%.*s", maxhost, "FROM");
(void)printf("\tCOMMENT");
}
(void)putchar('\n');
}
@ -280,7 +338,7 @@ quick(const char *fname)
static void
usage(void)
{
(void)fprintf(stderr, "usage: %s [-HmqsTu] [file]\n %s am i\n",
(void)fprintf(stderr, "usage: %s [-dHmqsTu] [file]\n %s am i\n",
getprogname(), getprogname());
exit(EXIT_FAILURE);
}