1*0c3983b2SBen Gras /* $NetBSD: logout.c,v 1.16 2005/08/27 17:07:17 elad Exp $ */
2*0c3983b2SBen Gras
3*0c3983b2SBen Gras /*
4*0c3983b2SBen Gras * Copyright (c) 1988, 1993
5*0c3983b2SBen Gras * The Regents of the University of California. All rights reserved.
6*0c3983b2SBen Gras *
7*0c3983b2SBen Gras * Redistribution and use in source and binary forms, with or without
8*0c3983b2SBen Gras * modification, are permitted provided that the following conditions
9*0c3983b2SBen Gras * are met:
10*0c3983b2SBen Gras * 1. Redistributions of source code must retain the above copyright
11*0c3983b2SBen Gras * notice, this list of conditions and the following disclaimer.
12*0c3983b2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*0c3983b2SBen Gras * notice, this list of conditions and the following disclaimer in the
14*0c3983b2SBen Gras * documentation and/or other materials provided with the distribution.
15*0c3983b2SBen Gras * 3. Neither the name of the University nor the names of its contributors
16*0c3983b2SBen Gras * may be used to endorse or promote products derived from this software
17*0c3983b2SBen Gras * without specific prior written permission.
18*0c3983b2SBen Gras *
19*0c3983b2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0c3983b2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0c3983b2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0c3983b2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0c3983b2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0c3983b2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0c3983b2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0c3983b2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0c3983b2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0c3983b2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0c3983b2SBen Gras * SUCH DAMAGE.
30*0c3983b2SBen Gras */
31*0c3983b2SBen Gras
32*0c3983b2SBen Gras #include <sys/cdefs.h>
33*0c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0c3983b2SBen Gras #if 0
35*0c3983b2SBen Gras static char sccsid[] = "@(#)logout.c 8.1 (Berkeley) 6/4/93";
36*0c3983b2SBen Gras #else
37*0c3983b2SBen Gras __RCSID("$NetBSD: logout.c,v 1.16 2005/08/27 17:07:17 elad Exp $");
38*0c3983b2SBen Gras #endif
39*0c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
40*0c3983b2SBen Gras
41*0c3983b2SBen Gras #include <sys/types.h>
42*0c3983b2SBen Gras #include <sys/time.h>
43*0c3983b2SBen Gras
44*0c3983b2SBen Gras #include <assert.h>
45*0c3983b2SBen Gras #include <fcntl.h>
46*0c3983b2SBen Gras #include <stdlib.h>
47*0c3983b2SBen Gras #include <string.h>
48*0c3983b2SBen Gras #include <time.h>
49*0c3983b2SBen Gras #include <unistd.h>
50*0c3983b2SBen Gras #include <util.h>
51*0c3983b2SBen Gras #include <utmp.h>
52*0c3983b2SBen Gras
53*0c3983b2SBen Gras int
logout(const char * line)54*0c3983b2SBen Gras logout(const char *line)
55*0c3983b2SBen Gras {
56*0c3983b2SBen Gras int fd, rval;
57*0c3983b2SBen Gras struct utmp ut;
58*0c3983b2SBen Gras
59*0c3983b2SBen Gras _DIAGASSERT(line != NULL);
60*0c3983b2SBen Gras
61*0c3983b2SBen Gras if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0)
62*0c3983b2SBen Gras return(0);
63*0c3983b2SBen Gras rval = 0;
64*0c3983b2SBen Gras while (read(fd, &ut, sizeof(ut)) == sizeof(ut)) {
65*0c3983b2SBen Gras if (!ut.ut_name[0] || strncmp(ut.ut_line, line,
66*0c3983b2SBen Gras (size_t)UT_LINESIZE))
67*0c3983b2SBen Gras continue;
68*0c3983b2SBen Gras memset(ut.ut_name, 0, (size_t)UT_NAMESIZE);
69*0c3983b2SBen Gras memset(ut.ut_host, 0, (size_t)UT_HOSTSIZE);
70*0c3983b2SBen Gras (void)time(&ut.ut_time);
71*0c3983b2SBen Gras (void)lseek(fd, -(off_t)sizeof(ut), SEEK_CUR);
72*0c3983b2SBen Gras (void)write(fd, &ut, sizeof(ut));
73*0c3983b2SBen Gras rval = 1;
74*0c3983b2SBen Gras }
75*0c3983b2SBen Gras (void)close(fd);
76*0c3983b2SBen Gras return(rval);
77*0c3983b2SBen Gras }
78