1 /* $NetBSD: want.c,v 1.12 2008/12/29 01:25:04 christos Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 static struct utmp
*buf
;
33 static void onintr(int);
34 static int want(struct utmp
*, int);
35 static const char *gethost(struct utmp
*, const char *, int);
39 gethost(struct utmp
*ut
, const char *host
, int numeric
)
42 return numeric
? "" : host
;
45 static char hbuf
[512];
47 (void)sockaddr_snprintf(hbuf
, sizeof(hbuf
), "%a",
48 (struct sockaddr
*)&ut
->ut_ss
);
55 #define NULTERM(what) \
57 (void)strlcpy(what ## p = what ## buf, bp->ut_ ## what, \
58 sizeof(what ## buf)); \
60 what ## p = bp->ut_ ## what
64 * read through the wtmp file
67 wtmp(const char *file
, int namesz
, int linesz
, int hostsz
, int numeric
)
69 struct utmp
*bp
; /* current structure */
70 TTY
*T
; /* tty list entry */
71 struct stat stb
; /* stat of file for sz */
76 size_t len
= sizeof(*buf
) * MAXUTMP
;
77 char namebuf
[sizeof(bp
->ut_name
) + 1], *namep
;
78 char linebuf
[sizeof(bp
->ut_line
) + 1], *linep
;
79 char hostbuf
[sizeof(bp
->ut_host
) + 1], *hostp
;
80 int checkname
= namesz
> (int)sizeof(bp
->ut_name
);
81 int checkline
= linesz
> (int)sizeof(bp
->ut_line
);
82 int checkhost
= hostsz
> (int)sizeof(bp
->ut_host
);
84 if ((buf
= malloc(len
)) == NULL
)
85 err(EXIT_FAILURE
, "Cannot allocate utmp buffer");
89 if (!strcmp(file
, "-")) {
92 } else if ((wfd
= open(file
, O_RDONLY
, 0)) < 0) {
93 err(EXIT_FAILURE
, "%s", file
);
96 if (lseek(wfd
, 0, SEEK_CUR
) < 0) {
102 if (ESPIPE
!= errno
) {
103 err(EXIT_FAILURE
, "lseek");
105 dir
= getenv("TMPDIR");
106 if (asprintf(&tfile
, "%s/last.XXXXXX", dir
? dir
: _PATH_TMP
) == -1)
107 err(EXIT_FAILURE
, "asprintf");
108 tempfd
= mkstemp(tfile
);
110 err(EXIT_FAILURE
, "mkstemp");
114 tlen
= read(wfd
, buf
, len
);
116 err(1, "%s: read", file
);
121 if (write(tempfd
, buf
, tlen
) != tlen
) {
122 err(1, "%s: write", tfile
);
128 if (fstat(wfd
, &stb
) == -1)
129 err(EXIT_FAILURE
, "%s: fstat", file
);
130 if (!S_ISREG(stb
.st_mode
))
131 errx(EXIT_FAILURE
, "%s: Not a regular file", file
);
133 buf
[FIRSTVALID
].ut_timefld
= time(NULL
);
134 (void)signal(SIGINT
, onintr
);
135 (void)signal(SIGQUIT
, onintr
);
137 offset
= stb
.st_size
;
138 /* Ignore trailing garbage or partial record */
139 offset
-= offset
% (off_t
) sizeof(*buf
);
141 while (offset
>= (off_t
) sizeof(*buf
)) {
145 size
= MIN((off_t
)len
, offset
);
146 offset
-= size
; /* Always a multiple of sizeof(*buf) */
147 ret
= pread(wfd
, buf
, size
, offset
);
149 err(EXIT_FAILURE
, "%s: pread", file
);
150 } else if ((size_t) ret
< size
) {
151 err(EXIT_FAILURE
, "%s: Unexpected end of file", file
);
154 for (i
= ret
/ sizeof(*buf
) - 1; i
>= 0; i
--) {
161 * if the terminal line is '~', the machine stopped.
162 * see utmp(5) for more info.
164 if (linep
[0] == '~' && !linep
[1]) {
165 /* everybody just logged out */
166 for (T
= ttylist
; T
; T
= T
->next
)
167 T
->logout
= -bp
->ut_timefld
;
168 currentout
= -bp
->ut_timefld
;
169 crmsg
= strncmp(namep
, "shutdown",
170 namesz
) ? "crash" : "shutdown";
172 ct
= fmttime(bp
->ut_timefld
, fulltime
);
173 printf("%-*.*s %-*.*s %-*.*s %s\n",
174 namesz
, namesz
, namep
,
175 linesz
, linesz
, linep
,
177 gethost(bp
, hostp
, numeric
), ct
);
178 if (maxrec
!= -1 && !--maxrec
)
184 * if the line is '{' or '|', date got set; see
185 * utmp(5) for more info.
187 if ((linep
[0] == '{' || linep
[0] == '|') && !linep
[1]) {
189 ct
= fmttime(bp
->ut_timefld
, fulltime
);
190 printf("%-*.*s %-*.*s %-*.*s %s\n",
191 namesz
, namesz
, namep
,
192 linesz
, linesz
, linep
,
194 gethost(bp
, hostp
, numeric
),
196 if (maxrec
&& !--maxrec
)
201 /* find associated tty */
202 for (T
= ttylist
;; T
= T
->next
) {
208 if (!strncmp(T
->tty
, linep
, LINESIZE
))
211 if (TYPE(bp
) == SIGNATURE
)
213 if (namep
[0] && want(bp
, YES
)) {
214 ct
= fmttime(bp
->ut_timefld
, fulltime
);
215 printf("%-*.*s %-*.*s %-*.*s %s ",
216 namesz
, namesz
, namep
,
217 linesz
, linesz
, linep
,
219 gethost(bp
, hostp
, numeric
),
222 puts(" still logged in");
224 time_t delta
; /* time difference */
227 T
->logout
= -T
->logout
;
228 printf("- %s", crmsg
);
233 fulltime
| TIMEONLY
));
234 delta
= T
->logout
- bp
->ut_timefld
;
235 if (delta
< SECSPERDAY
)
238 fulltime
| TIMEONLY
| GMT
));
240 printf(" (%lld+%s)\n",
244 fulltime
| TIMEONLY
| GMT
));
246 if (maxrec
!= -1 && !--maxrec
)
249 T
->logout
= bp
->ut_timefld
;
252 fulltime
= 1; /* show full time */
253 crmsg
= fmttime(buf
[FIRSTVALID
].ut_timefld
, FULLTIME
);
254 if ((ct
= strrchr(file
, '/')) != NULL
)
256 printf("\n%s begins %s\n", ct
? ct
: file
, crmsg
);
261 * see if want this entry
264 want(struct utmp
*bp
, int check
)
270 * when uucp and ftp log in over a network, the entry in
271 * the utmp file is the name plus their process id. See
272 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
274 if (!strncmp(bp
->ut_line
, "ftp", sizeof("ftp") - 1))
275 bp
->ut_line
[3] = '\0';
276 else if (!strncmp(bp
->ut_line
, "uucp", sizeof("uucp") - 1))
277 bp
->ut_line
[4] = '\0';
282 for (step
= arglist
; step
; step
= step
->next
)
285 if (!strncasecmp(step
->name
, bp
->ut_host
, HOSTSIZE
))
289 if (!strncmp(step
->name
, bp
->ut_line
, LINESIZE
))
293 if (!strncmp(step
->name
, bp
->ut_name
, NAMESIZE
))
302 * on interrupt, we inform the user how far we've gotten
307 /* FIXME: None of this is allowed in a signal handler */
308 printf("\ninterrupted %s\n", fmttime(buf
[FIRSTVALID
].ut_timefld
,
310 if (signo
== SIGINT
) {
311 (void)raise_default_signal(signo
);
314 (void)fflush(stdout
); /* fix required for rsh */