Home | History | Annotate | Line # | Download | only in last
want.c revision 1.1
      1 /*	$NetBSD: want.c,v 1.1 2003/02/17 15:08:57 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 static struct utmp *buf;
     36 
     37 static void onintr(int);
     38 static int want(struct utmp *, int);
     39 
     40 /*
     41  * wtmp --
     42  *	read through the wtmp file
     43  */
     44 void
     45 wtmp(const char *file, int namesz, int linesz, int hostsz)
     46 {
     47 	struct utmp	*bp;		/* current structure */
     48 	TTY	*T;			/* tty list entry */
     49 	struct stat	stb;		/* stat of file for sz */
     50 	time_t	delta;			/* time difference */
     51 	off_t	bl;
     52 	int	bytes, wfd;
     53 	char	*ct, *crmsg;
     54 	size_t  len = sizeof(*buf) * MAXUTMP;
     55 
     56 	if ((buf = malloc(len)) == NULL)
     57 		err(1, "Cannot allocate utmp buffer");
     58 
     59 	crmsg = NULL;
     60 
     61 	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
     62 		err(1, "%s", file);
     63 	bl = (stb.st_size + len - 1) / len;
     64 
     65 	buf[FIRSTVALID].ut_timefld = time(NULL);
     66 	(void)signal(SIGINT, onintr);
     67 	(void)signal(SIGQUIT, onintr);
     68 
     69 	while (--bl >= 0) {
     70 		if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
     71 		    (bytes = read(wfd, buf, len)) == -1)
     72 			err(1, "%s", file);
     73 		for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
     74 			/*
     75 			 * if the terminal line is '~', the machine stopped.
     76 			 * see utmp(5) for more info.
     77 			 */
     78 			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
     79 				/* everybody just logged out */
     80 				for (T = ttylist; T; T = T->next)
     81 					T->logout = -bp->ut_timefld;
     82 				currentout = -bp->ut_timefld;
     83 				crmsg = strncmp(bp->ut_name, "shutdown",
     84 				    namesz) ? "crash" : "shutdown";
     85 				if (want(bp, NO)) {
     86 					ct = fmttime(bp->ut_timefld, fulltime);
     87 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
     88 					    namesz, namesz, bp->ut_name,
     89 					    linesz, linesz, bp->ut_line,
     90 					    hostsz, hostsz, bp->ut_host, ct);
     91 					if (maxrec != -1 && !--maxrec)
     92 						return;
     93 				}
     94 				continue;
     95 			}
     96 			/*
     97 			 * if the line is '{' or '|', date got set; see
     98 			 * utmp(5) for more info.
     99 			 */
    100 			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
    101 			    && !bp->ut_line[1]) {
    102 				if (want(bp, NO)) {
    103 					ct = fmttime(bp->ut_timefld, fulltime);
    104 				printf("%-*.*s  %-*.*s %-*.*s %s\n",
    105 				    namesz, namesz,
    106 				    bp->ut_name,
    107 				    linesz, linesz,
    108 				    bp->ut_line,
    109 				    hostsz, hostsz,
    110 				    bp->ut_host,
    111 				    ct);
    112 					if (maxrec && !--maxrec)
    113 						return;
    114 				}
    115 				continue;
    116 			}
    117 			/* find associated tty */
    118 			for (T = ttylist;; T = T->next) {
    119 				if (!T) {
    120 					/* add new one */
    121 					T = addtty(bp->ut_line);
    122 					break;
    123 				}
    124 				if (!strncmp(T->tty, bp->ut_line, LINESIZE))
    125 					break;
    126 			}
    127 			if (TYPE(bp) == SIGNATURE)
    128 				continue;
    129 			if (bp->ut_name[0] && want(bp, YES)) {
    130 				ct = fmttime(bp->ut_timefld, fulltime);
    131 				printf("%-*.*s  %-*.*s %-*.*s %s ",
    132 				    namesz, namesz, bp->ut_name,
    133 				    linesz, linesz, bp->ut_line,
    134 				    hostsz, hostsz, bp->ut_host,
    135 				    ct);
    136 				if (!T->logout)
    137 					puts("  still logged in");
    138 				else {
    139 					if (T->logout < 0) {
    140 						T->logout = -T->logout;
    141 						printf("- %s", crmsg);
    142 					}
    143 					else
    144 						printf("- %s",
    145 						    fmttime(T->logout,
    146 						    fulltime | TIMEONLY));
    147 					delta = T->logout - bp->ut_timefld;
    148 					if (delta < SECSPERDAY)
    149 						printf("  (%s)\n",
    150 						    fmttime(delta,
    151 						    fulltime | TIMEONLY | GMT));
    152 					else
    153 						printf(" (%ld+%s)\n",
    154 						    delta / SECSPERDAY,
    155 						    fmttime(delta,
    156 						    fulltime | TIMEONLY | GMT));
    157 				}
    158 				if (maxrec != -1 && !--maxrec)
    159 					return;
    160 			}
    161 			T->logout = bp->ut_timefld;
    162 		}
    163 	}
    164 	fulltime = 1;	/* show full time */
    165 	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
    166 	if ((ct = strrchr(file, '/')) != NULL)
    167 		ct++;
    168 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
    169 }
    170 
    171 /*
    172  * want --
    173  *	see if want this entry
    174  */
    175 static int
    176 want(struct utmp *bp, int check)
    177 {
    178 	ARG *step;
    179 
    180 	if (check) {
    181 		/*
    182 		 * when uucp and ftp log in over a network, the entry in
    183 		 * the utmp file is the name plus their process id.  See
    184 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    185 		 */
    186 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    187 			bp->ut_line[3] = '\0';
    188 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    189 			bp->ut_line[4] = '\0';
    190 	}
    191 	if (!arglist)
    192 		return (YES);
    193 
    194 	for (step = arglist; step; step = step->next)
    195 		switch(step->type) {
    196 		case HOST_TYPE:
    197 			if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
    198 				return (YES);
    199 			break;
    200 		case TTY_TYPE:
    201 			if (!strncmp(step->name, bp->ut_line, LINESIZE))
    202 				return (YES);
    203 			break;
    204 		case USER_TYPE:
    205 			if (!strncmp(step->name, bp->ut_name, NAMESIZE))
    206 				return (YES);
    207 			break;
    208 	}
    209 	return (NO);
    210 }
    211 
    212 /*
    213  * onintr --
    214  *	on interrupt, we inform the user how far we've gotten
    215  */
    216 static void
    217 onintr(int signo)
    218 {
    219 
    220 	printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
    221 	    FULLTIME));
    222 	if (signo == SIGINT)
    223 		exit(1);
    224 	(void)fflush(stdout);		/* fix required for rsh */
    225 }
    226