| 1 |
/* |
| 2 |
Copyright (C) 1999-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1995 Wietse Venema. All rights reserved. |
| 21 |
* |
| 22 |
* Individual files may be covered by other copyrights (as noted in |
| 23 |
* the file itself.) |
| 24 |
* |
| 25 |
* This material was originally written and compiled by Wietse Venema |
| 26 |
* at Eindhoven University of Technology, The Netherlands, in 1990, |
| 27 |
* 1991, 1992, 1993, 1994 and 1995. |
| 28 |
* |
| 29 |
* Redistribution and use in source and binary forms are permitted |
| 30 |
* provided that this entire copyright notice is duplicated in all |
| 31 |
* such copies. |
| 32 |
* |
| 33 |
* This software is provided "as is" and without any expressed or |
| 34 |
* implied warranties, including, without limitation, the implied |
| 35 |
* warranties of merchantibility and fitness for any particular |
| 36 |
* purpose. |
| 37 |
*/ |
| 38 |
|
| 39 |
/* Written by Wietse Venema. With port to GNU Inetutils done by Alain |
| 40 |
Magloire. Reorganized to cope with full variation between utmpx |
| 41 |
and utmp, with different API sets, by Mats Erik Andersson. */ |
| 42 |
|
| 43 |
#include <config.h> |
| 44 |
|
| 45 |
#include <sys/types.h> |
| 46 |
#include <sys/time.h> |
| 47 |
#include <time.h> |
| 48 |
#ifdef HAVE_UTMPX_H |
| 49 |
# ifndef __USE_GNU |
| 50 |
# define __USE_GNU 1 |
| 51 |
# endif |
| 52 |
# include <utmpx.h> |
| 53 |
#else /* !HAVE_UTMPX_H */ |
| 54 |
# ifdef HAVE_UTIL_H |
| 55 |
# include <util.h> |
| 56 |
# endif |
| 57 |
# ifdef HAVE_LIBUTIL_H |
| 58 |
# include <libutil.h> |
| 59 |
# endif |
| 60 |
# include <utmp.h> |
| 61 |
#endif |
| 62 |
#include <string.h> |
| 63 |
|
| 64 |
/* utmp_logout - update utmp and wtmp after logout */ |
| 65 |
|
| 66 |
void |
| 67 |
utmp_logout (char *line) |
| 68 |
{ |
| 69 |
#ifdef HAVE_UTMPX_H |
| 70 |
struct utmpx utx; |
| 71 |
struct utmpx *ut; |
| 72 |
|
| 73 |
strncpy (utx.ut_line, line, sizeof (utx.ut_line)); |
| 74 |
|
| 75 |
# ifdef HAVE_PUTUTXLINE |
| 76 |
setutxent (); |
| 77 |
ut = getutxline (&utx); |
| 78 |
if (ut) |
| 79 |
{ |
| 80 |
struct timeval tv; |
| 81 |
|
| 82 |
ut->ut_type = DEAD_PROCESS; |
| 83 |
# ifdef HAVE_STRUCT_UTMPX_UT_EXIT |
| 84 |
memset (&ut->ut_exit, 0, sizeof (ut->ut_exit)); |
| 85 |
# endif |
| 86 |
gettimeofday (&tv, 0); |
| 87 |
ut->ut_tv.tv_sec = tv.tv_sec; |
| 88 |
ut->ut_tv.tv_usec = tv.tv_usec; |
| 89 |
# ifdef HAVE_STRUCT_UTMPX_UT_USER |
| 90 |
memset (&ut->ut_user, 0, sizeof (ut->ut_user)); |
| 91 |
# elif defined HAVE_STRUCT_UTMPX_UT_NAME |
| 92 |
memset (&ut->ut_name, 0, sizeof (ut->ut_name)); |
| 93 |
# endif |
| 94 |
# ifdef HAVE_STRUCT_UTMPX_UT_HOST |
| 95 |
memset (ut->ut_host, 0, sizeof (ut->ut_host)); |
| 96 |
# ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN |
| 97 |
ut->ut_syslen = 1; /* Counting NUL. */ |
| 98 |
# endif |
| 99 |
# endif |
| 100 |
/* UT_HOST */ |
| 101 |
pututxline (ut); |
| 102 |
/* Some systems perform wtmp updating |
| 103 |
* already in calling pututxline(). |
| 104 |
*/ |
| 105 |
# ifdef HAVE_UPDWTMPX |
| 106 |
updwtmpx (PATH_WTMPX, ut); |
| 107 |
# elif defined HAVE_LOGWTMPX |
| 108 |
logwtmpx (ut->ut_line, "", "", 0, DEAD_PROCESS); |
| 109 |
# endif |
| 110 |
} |
| 111 |
endutxent (); |
| 112 |
# elif defined HAVE_LOGOUTX /* !HAVE_PUTUTXLINE */ |
| 113 |
if (logoutx (line, 0, DEAD_PROCESS)) |
| 114 |
logwtmpx (line, "", "", 0, DEAD_PROCESS); |
| 115 |
# endif/* HAVE_LOGOUTX */ |
| 116 |
|
| 117 |
#else /* !HAVE_UTMPX_H */ |
| 118 |
struct utmp utx; |
| 119 |
# ifdef HAVE_PUTUTLINE |
| 120 |
struct utmp *ut; |
| 121 |
# endif |
| 122 |
|
| 123 |
strncpy (utx.ut_line, line, sizeof (utx.ut_line)); |
| 124 |
|
| 125 |
# ifdef HAVE_PUTUTLINE |
| 126 |
setutent (); |
| 127 |
ut = getutline (&utx); |
| 128 |
if (ut) |
| 129 |
{ |
| 130 |
# ifdef HAVE_STRUCT_UTMP_UT_TV |
| 131 |
struct timeval tv; |
| 132 |
# endif |
| 133 |
|
| 134 |
# ifdef HAVE_STRUCT_UTMP_UT_TYPE |
| 135 |
ut->ut_type = DEAD_PROCESS; |
| 136 |
# endif |
| 137 |
# ifdef HAVE_STRUCT_UTMP_UT_EXIT |
| 138 |
memset (&ut->ut_exit, 0, sizeof (ut->ut_exit)); |
| 139 |
# endif |
| 140 |
# ifdef HAVE_STRUCT_UTMP_UT_TV |
| 141 |
gettimeofday (&tv, 0); |
| 142 |
ut->ut_tv.tv_sec = tv.tv_sec; |
| 143 |
ut->ut_tv.tv_usec = tv.tv_usec; |
| 144 |
# else |
| 145 |
/* !HAVE_STRUCT_UTMP_UT_TV */ |
| 146 |
time (&(ut->ut_time)); |
| 147 |
# endif |
| 148 |
# ifdef HAVE_STRUCT_UTMP_UT_USER |
| 149 |
memset (&ut->ut_user, 0, sizeof (ut->ut_user)); |
| 150 |
# elif defined HAVE_STRUCT_UTMP_UT_NAME |
| 151 |
memset (&ut->ut_name, 0, sizeof (ut->ut_name)); |
| 152 |
# endif |
| 153 |
# ifdef HAVE_STRUCT_UTMP_UT_HOST |
| 154 |
memset (ut->ut_host, 0, sizeof (ut->ut_host)); |
| 155 |
# endif |
| 156 |
pututline (ut); |
| 157 |
# ifdef HAVE_UPDWTMP |
| 158 |
updwtmp (WTMP_FILE, ut); |
| 159 |
# elif defined HAVE_LOGWTMP /* !HAVE_UPDWTMP */ |
| 160 |
logwtmp (ut->ut_line, "", ""); |
| 161 |
# endif |
| 162 |
} |
| 163 |
endutent (); |
| 164 |
# elif defined HAVE_LOGOUT /* !HAVE_PUTUTLINE */ |
| 165 |
if (logout (line)) |
| 166 |
logwtmp (line, "", ""); |
| 167 |
# endif/* HAVE_LOGOUT */ |
| 168 |
#endif |
| 169 |
} |