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