2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 1999, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2001 - 2005, Tomasz Kłoczko
|
|
|
|
* Copyright (c) 2008 , Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "defines.h"
|
2008-01-05 18:53:22 +05:30
|
|
|
#include "prototypes.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include <utmp.h>
|
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
|
|
|
#include <utmpx.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:14 +05:30
|
|
|
struct utmpx utxent;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:14 +05:30
|
|
|
struct utmp utent;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#define NO_UTENT \
|
2007-10-07 17:17:01 +05:30
|
|
|
_("No utmp entry. You must exec \"login\" from the lowest level \"sh\"")
|
2007-10-07 17:14:02 +05:30
|
|
|
#define NO_TTY \
|
2007-10-07 17:17:01 +05:30
|
|
|
_("Unable to determine your tty name.")
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-11-23 05:26:11 +05:30
|
|
|
/*
|
|
|
|
* is_my_tty -- determine if "tty" is the same TTY stdin is using
|
|
|
|
*/
|
|
|
|
static bool is_my_tty (const char *tty)
|
|
|
|
{
|
|
|
|
char full_tty[200];
|
|
|
|
struct stat by_name, by_fd;
|
|
|
|
|
|
|
|
if ('/' != *tty) {
|
|
|
|
snprintf (full_tty, sizeof full_tty, "/dev/%s", tty);
|
|
|
|
tty = full_tty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (stat (tty, &by_name) != 0)
|
|
|
|
|| (fstat (STDIN_FILENO, &by_fd) != 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (by_name.st_rdev != by_fd.st_rdev) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* checkutmp - see if utmp file is correct for this process
|
|
|
|
*
|
|
|
|
* System V is very picky about the contents of the utmp file
|
|
|
|
* and requires that a slot for the current process exist.
|
|
|
|
* The utmp file is scanned for an entry with the same process
|
|
|
|
* ID. If no entry exists the process exits with a message.
|
|
|
|
*
|
|
|
|
* The "picky" flag is for network and other logins that may
|
|
|
|
* use special flags. It allows the pid checks to be overridden.
|
|
|
|
* This means that getty should never invoke login with any
|
|
|
|
* command line flags.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
#if defined(__linux__) /* XXX */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 05:56:33 +05:30
|
|
|
void checkutmp (bool picky)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
struct utmp *ut;
|
2007-10-07 17:15:23 +05:30
|
|
|
pid_t pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/* First, try to find a valid utmp entry for this process. */
|
2008-11-23 05:26:11 +05:30
|
|
|
while ((ut = getutent ()) != NULL) {
|
2008-06-18 02:43:30 +05:30
|
|
|
if ( (ut->ut_pid == pid)
|
|
|
|
&& ('\0' != ut->ut_line[0])
|
|
|
|
&& ('\0' != ut->ut_id[0])
|
|
|
|
&& ( (LOGIN_PROCESS == ut->ut_type)
|
2008-11-23 05:26:11 +05:30
|
|
|
|| (USER_PROCESS == ut->ut_type))
|
|
|
|
/* A process may have failed to close an entry
|
|
|
|
* Check if this entry refers to this tty */
|
|
|
|
&& is_my_tty (ut->ut_line)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2008-11-23 05:26:11 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-11-23 05:26:11 +05:30
|
|
|
/* If there is one, just use it, otherwise create a new one. */
|
2008-06-18 02:43:30 +05:30
|
|
|
if (NULL != ut) {
|
2007-10-07 17:14:02 +05:30
|
|
|
utent = *ut;
|
|
|
|
} else {
|
|
|
|
if (picky) {
|
2008-06-18 02:43:30 +05:30
|
|
|
(void) puts (NO_UTENT);
|
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
line = ttyname (0);
|
2008-06-18 02:43:30 +05:30
|
|
|
if (NULL == line) {
|
|
|
|
(void) puts (NO_TTY);
|
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-18 02:43:30 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
memset ((void *) &utent, 0, sizeof utent);
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_type = LOGIN_PROCESS;
|
|
|
|
utent.ut_pid = pid;
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
2008-11-23 05:26:11 +05:30
|
|
|
/* XXX - assumes /dev/tty?? or /dev/pts/?? */
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id);
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utent.ut_user, "LOGIN");
|
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(LOGIN_PROCESS)
|
|
|
|
|
2008-05-26 03:41:12 +05:30
|
|
|
void checkutmp (bool picky)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
struct utmp *ut;
|
2007-10-07 17:15:23 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
|
|
|
struct utmpx *utx;
|
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
pid_t pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
setutxent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (picky) {
|
|
|
|
#if HAVE_UTMPX_H
|
2008-06-18 02:43:30 +05:30
|
|
|
while ((utx = getutxent ()) != NULL) {
|
|
|
|
if (utx->ut_pid == pid) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if (NULL != utx) {
|
2007-10-07 17:14:02 +05:30
|
|
|
utxent = *utx;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-06-18 02:43:30 +05:30
|
|
|
while ((ut = getutent ()) != NULL) {
|
|
|
|
if (ut->ut_pid == pid) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if (NULL != ut) {
|
2007-10-07 17:14:02 +05:30
|
|
|
utent = *ut;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
endutxent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:15:23 +05:30
|
|
|
endutent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if (NULL == ut) {
|
|
|
|
(void) puts (NO_UTENT);
|
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#ifndef UNIXPC
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no ut_line value in this record, fill
|
|
|
|
* it in by getting the TTY name and stuffing it in
|
|
|
|
* the structure. The UNIX/PC is broken in this regard
|
|
|
|
* and needs help ...
|
|
|
|
*/
|
2008-11-23 05:26:11 +05:30
|
|
|
/* XXX: The ut_line may not match with the current tty.
|
|
|
|
* ut_line will be set by setutmp anyway, but ut_id
|
|
|
|
* will not be set, and the wrong utmp entry may be
|
|
|
|
* updated. -- nekral */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (utent.ut_line[0] == '\0')
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* !UNIXPC */
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2008-06-18 02:43:30 +05:30
|
|
|
line = ttyname (0);
|
|
|
|
if (NULL == line) {
|
|
|
|
(void) puts (NO_TTY);
|
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-18 02:43:30 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
2008-06-18 02:43:30 +05:30
|
|
|
line = ttyname (0);
|
|
|
|
if (NULL == line) {
|
|
|
|
(void) puts (NO_TTY);
|
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-18 02:43:30 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_line, line, sizeof utent.ut_line);
|
2008-06-18 02:43:30 +05:30
|
|
|
ut = getutline (&utent);
|
|
|
|
if (NULL != ut) {
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id);
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utent.ut_user, "LOGIN");
|
|
|
|
utent.ut_pid = getpid ();
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_type = LOGIN_PROCESS;
|
2007-10-07 17:15:23 +05:30
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_UTMPX_H
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
|
2008-06-18 02:43:30 +05:30
|
|
|
utx = getutxline (&utxent);
|
|
|
|
if (NULL != utx) {
|
2007-10-07 17:16:07 +05:30
|
|
|
strncpy (utxent.ut_id, utx->ut_id, sizeof utxent.ut_id);
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (utxent.ut_user, "LOGIN");
|
2007-10-07 17:14:02 +05:30
|
|
|
utxent.ut_pid = utent.ut_pid;
|
|
|
|
utxent.ut_type = utent.ut_type;
|
2008-06-18 02:43:30 +05:30
|
|
|
if (sizeof (utxent.ut_tv) == sizeof (struct timeval)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
gettimeofday ((struct timeval *) &utxent.ut_tv, NULL);
|
2008-06-18 02:43:30 +05:30
|
|
|
} else {
|
2007-10-07 17:15:23 +05:30
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
utxent.ut_tv.tv_sec = tv.tv_sec;
|
|
|
|
utxent.ut_tv.tv_usec = tv.tv_usec;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_time = utxent.ut_tv.tv_sec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:34 +05:30
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some systems already have updwtmp() and possibly updwtmpx(). Others
|
|
|
|
* don't, so we re-implement these functions if necessary. --marekm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HAVE_UPDWTMP
|
2007-10-07 17:15:23 +05:30
|
|
|
static void updwtmp (const char *filename, const struct utmp *ut)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fd = open (filename, O_APPEND | O_WRONLY, 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fd >= 0) {
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (const char *) ut, sizeof (*ut));
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* ! HAVE_UPDWTMP */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_UTMPX_H
|
|
|
|
#ifndef HAVE_UPDWTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
static void updwtmpx (const char *filename, const struct utmpx *utx)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
fd = open (filename, O_APPEND | O_WRONLY, 0);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fd >= 0) {
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (const char *) utx, sizeof (*utx));
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* ! HAVE_UPDWTMPX */
|
|
|
|
#endif /* ! HAVE_UTMPX_H */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setutmp - put a USER_PROCESS entry in the utmp file
|
|
|
|
*
|
|
|
|
* setutmp changes the type of the current utmp entry to
|
|
|
|
* USER_PROCESS. the wtmp file will be updated as well.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
#if defined(__linux__) /* XXX */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-08-31 22:57:37 +05:30
|
|
|
int setutmp (const char *name, const char unused(*line), const char unused(*host))
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2008-06-18 02:43:30 +05:30
|
|
|
int err = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
utent.ut_type = USER_PROCESS;
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (utent.ut_user, name, sizeof utent.ut_user);
|
|
|
|
utent.ut_time = time (NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
/* other fields already filled in by checkutmp above */
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2008-06-18 02:43:30 +05:30
|
|
|
if (pututline (&utent) == NULL) {
|
|
|
|
err = 1;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
endutent ();
|
|
|
|
updwtmp (_WTMP_FILE, &utent);
|
2008-06-18 02:43:30 +05:30
|
|
|
|
|
|
|
return err;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#elif HAVE_UTMPX_H
|
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
int setutmp (const char *name, const char *line, const char *host)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct utmp *utmp, utline;
|
|
|
|
struct utmpx *utmpx, utxline;
|
|
|
|
pid_t pid = getpid ();
|
2008-06-18 02:43:30 +05:30
|
|
|
bool found_utmpx = false;
|
|
|
|
bool found_utmp = false;
|
|
|
|
int err = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* The canonical device name doesn't include "/dev/"; skip it
|
|
|
|
* if it is already there.
|
|
|
|
*/
|
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if (strncmp (line, "/dev/", 5) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
line += 5;
|
2008-06-18 02:43:30 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Update utmpx. We create an empty entry in case there is
|
|
|
|
* no matching entry in the utmpx file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
setutxent ();
|
|
|
|
setutent ();
|
|
|
|
|
2008-06-13 23:47:10 +05:30
|
|
|
while ((utmpx = getutxent ()) != NULL) {
|
2007-10-07 17:14:02 +05:30
|
|
|
if (utmpx->ut_pid == pid) {
|
2008-05-26 03:41:12 +05:30
|
|
|
found_utmpx = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-06-13 23:47:10 +05:30
|
|
|
while ((utmp = getutent ()) != NULL) {
|
2007-10-07 17:14:02 +05:30
|
|
|
if (utmp->ut_pid == pid) {
|
2008-05-26 03:41:12 +05:30
|
|
|
found_utmp = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the entry matching `pid' cannot be found, create a new
|
|
|
|
* entry with the device name in it.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!found_utmpx) {
|
2007-10-07 17:14:02 +05:30
|
|
|
memset ((void *) &utxline, 0, sizeof utxline);
|
|
|
|
strncpy (utxline.ut_line, line, sizeof utxline.ut_line);
|
|
|
|
utxline.ut_pid = getpid ();
|
|
|
|
} else {
|
|
|
|
utxline = *utmpx;
|
|
|
|
if (strncmp (utxline.ut_line, "/dev/", 5) == 0) {
|
|
|
|
memmove (utxline.ut_line, utxline.ut_line + 5,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utxline.ut_line - 5);
|
2007-10-07 17:14:02 +05:30
|
|
|
utxline.ut_line[sizeof utxline.ut_line - 5] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
if (!found_utmp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
memset ((void *) &utline, 0, sizeof utline);
|
|
|
|
strncpy (utline.ut_line, utxline.ut_line,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utline.ut_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_pid = utxline.ut_pid;
|
|
|
|
} else {
|
|
|
|
utline = *utmp;
|
|
|
|
if (strncmp (utline.ut_line, "/dev/", 5) == 0) {
|
|
|
|
memmove (utline.ut_line, utline.ut_line + 5,
|
2007-10-07 17:15:23 +05:30
|
|
|
sizeof utline.ut_line - 5);
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_line[sizeof utline.ut_line - 5] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the fields in the utmpx entry and write it out. Do
|
|
|
|
* the utmp entry at the same time to make sure things don't
|
|
|
|
* get messed up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
strncpy (utxline.ut_user, name, sizeof utxline.ut_user);
|
|
|
|
strncpy (utline.ut_user, name, sizeof utline.ut_user);
|
|
|
|
|
|
|
|
utline.ut_type = utxline.ut_type = USER_PROCESS;
|
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if (sizeof (utxline.ut_tv) == sizeof (struct timeval)) {
|
2007-10-07 17:15:23 +05:30
|
|
|
gettimeofday ((struct timeval *) &utxline.ut_tv, NULL);
|
2008-06-18 02:43:30 +05:30
|
|
|
} else {
|
2007-10-07 17:15:23 +05:30
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
utxline.ut_tv.tv_sec = tv.tv_sec;
|
|
|
|
utxline.ut_tv.tv_usec = tv.tv_usec;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
utline.ut_time = utxline.ut_tv.tv_sec;
|
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
strncpy (utxline.ut_host, (NULL != host) ? host : "",
|
|
|
|
sizeof utxline.ut_host);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 02:43:30 +05:30
|
|
|
if ( (pututxline (&utxline) == NULL)
|
|
|
|
|| (pututline (&utline) == NULL)) {
|
|
|
|
err = 1;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
updwtmpx (_WTMP_FILE "x", &utxline);
|
|
|
|
updwtmp (_WTMP_FILE, &utline);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
utxent = utxline;
|
|
|
|
utent = utline;
|
2008-06-18 02:43:30 +05:30
|
|
|
|
|
|
|
return err;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|