Back to index

glibc  2.9
Defines | Functions | Variables
utmp_file.c File Reference
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <utmp.h>
#include <not-cancel.h>
#include <kernel-features.h>
#include "utmp-private.h"
#include "utmp-equal.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define TIMEOUT   1
#define LOCK_FILE(fd, type)
#define LOCKING_FAILED()   goto unalarm_return
#define UNLOCK_FILE(fd)
#define TRANSFORM_UTMP_FILE_NAME(file_name)   (file_name)
#define O_flags   O_LARGEFILE

Functions

static void timeout_handler (int signum)
static int setutent_file (void)
static int getutent_r_file (struct utmp *buffer, struct utmp **result)
static int getutid_r_file (const struct utmp *key, struct utmp *buffer, struct utmp **result)
static int getutline_r_file (const struct utmp *key, struct utmp *buffer, struct utmp **result)
static struct utmppututline_file (const struct utmp *data)
static void endutent_file (void)
static int updwtmp_file (const char *file, const struct utmp *utmp)
static int internal_getut_r (const struct utmp *id, struct utmp *buffer, bool *lock_failed)

Variables

static int file_fd = -1
static off64_t file_offset
static struct utmp

Define Documentation

#define LOCK_FILE (   fd,
  type 
)
Value:
{                                                                    \
  struct flock fl;                                                   \
  struct sigaction action, old_action;                                      \
  unsigned int old_timeout;                                          \
                                                                     \
  /* Cancel any existing alarm.  */                                         \
  old_timeout = alarm (0);                                           \
                                                                     \
  /* Establish signal handler.  */                                   \
  action.sa_handler = timeout_handler;                                      \
  __sigemptyset (&action.sa_mask);                                   \
  action.sa_flags = 0;                                                      \
  __sigaction (SIGALRM, &action, &old_action);                              \
									      \
  alarm (TIMEOUT);                                                   \
                                                                     \
  /* Try to get the lock.  */                                               \
  memset (&fl, '\0', sizeof (struct flock));                                \
  fl.l_type = (type);                                                       \
  fl.l_whence = SEEK_SET;                                            \
  if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)

Definition at line 62 of file utmp_file.c.

#define LOCKING_FAILED ( )    goto unalarm_return

Definition at line 85 of file utmp_file.c.

#define O_flags   O_LARGEFILE
#define TIMEOUT   1

Definition at line 47 of file utmp_file.c.

Definition at line 127 of file utmp_file.c.

#define UNLOCK_FILE (   fd)
Value:
/* Unlock the file.  */                                              \
  fl.l_type = F_UNLCK;                                                      \
  fcntl_not_cancel ((fd), F_SETLKW, &fl);                            \
                                                                     \
 unalarm_return:                                                     \
  /* Reset the signal handler and alarm.  We must reset the alarm           \
     before resetting the handler so our alarm does not generate a          \
     spurious SIGALRM seen by the user.  However, we cannot just set        \
     the user's old alarm before restoring the handler, because then        \
     it's possible our handler could catch the user alarm's SIGARLM         \
     and then the user would never see the signal he expected.  */          \
  alarm (0);                                                         \
  __sigaction (SIGALRM, &old_action, NULL);                                 \
  if (old_timeout != 0)                                                     \
    alarm (old_timeout);                                             \
} while (0)

Definition at line 88 of file utmp_file.c.


Function Documentation

static void endutent_file ( void  ) [static]

Definition at line 480 of file utmp_file.c.

{
static int getutent_r_file ( struct utmp buffer,
struct utmp **  result 
) [static]

Definition at line 200 of file utmp_file.c.

{
  ssize_t nbytes;

  assert (file_fd >= 0);

  if (file_offset == -1l)
    {
      /* Not available.  */
      *result = NULL;
      return -1;
    }

  LOCK_FILE (file_fd, F_RDLCK)
    {
      nbytes = 0;
      LOCKING_FAILED ();
    }

  /* Read the next entry.  */
  nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));

  UNLOCK_FILE (file_fd);

  if (nbytes != sizeof (struct utmp))
    {
      if (nbytes != 0)
       file_offset = -1l;
      *result = NULL;
      return -1;
    }

  /* Update position pointer.  */
  file_offset += sizeof (struct utmp);

static int getutid_r_file ( const struct utmp key,
struct utmp buffer,
struct utmp **  result 
) [static]

Definition at line 312 of file utmp_file.c.

{
  assert (file_fd >= 0);

  if (file_offset == -1l)
    {
      *result = NULL;
      return -1;
    }

  /* We don't have to distinguish whether we can lock the file or
     whether there is no entry.  */
  bool lock_failed = false;
  if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
    {
      *result = NULL;
      return -1;
    }

Here is the call graph for this function:

static int getutline_r_file ( const struct utmp key,
struct utmp buffer,
struct utmp **  result 
) [static]

Definition at line 342 of file utmp_file.c.

{
  assert (file_fd >= 0);

  if (file_offset == -1l)
    {
      *result = NULL;
      return -1;
    }

  LOCK_FILE (file_fd, F_RDLCK)
    {
      *result = NULL;
      LOCKING_FAILED ();
    }

  while (1)
    {
      /* Read the next entry.  */
      if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
         != sizeof (struct utmp))
       {
         __set_errno (ESRCH);
         file_offset = -1l;
         *result = NULL;
         goto unlock_return;
       }
      file_offset += sizeof (struct utmp);

      /* Stop if we found a user or login entry.  */
      if (
#if _HAVE_UT_TYPE - 0
         (last_entry.ut_type == USER_PROCESS
          || last_entry.ut_type == LOGIN_PROCESS)
         &&
#endif
         !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
       break;
    }

  memcpy (buffer, &last_entry, sizeof (struct utmp));
  *result = buffer;

static int internal_getut_r ( const struct utmp id,
struct utmp buffer,
bool *  lock_failed 
) [static]

Definition at line 243 of file utmp_file.c.

{
  int result = -1;

  LOCK_FILE (file_fd, F_RDLCK)
    {
      *lock_failed = true;
      LOCKING_FAILED ();
    }

#if _HAVE_UT_TYPE - 0
  if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
      || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
    {
      /* Search for next entry with type RUN_LVL, BOOT_TIME,
        OLD_TIME, or NEW_TIME.  */

      while (1)
       {
         /* Read the next entry.  */
         if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
             != sizeof (struct utmp))
           {
             __set_errno (ESRCH);
             file_offset = -1l;
             goto unlock_return;
           }
         file_offset += sizeof (struct utmp);

         if (id->ut_type == buffer->ut_type)
           break;
       }
    }
  else
#endif /* _HAVE_UT_TYPE */
    {
      /* Search for the next entry with the specified ID and with type
        INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS.  */

      while (1)
       {
         /* Read the next entry.  */
         if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
             != sizeof (struct utmp))
           {
             __set_errno (ESRCH);
             file_offset = -1l;
             goto unlock_return;
           }
         file_offset += sizeof (struct utmp);

         if (__utmp_equal (buffer, id))
           break;
       }
    }

  result = 0;

Here is the caller graph for this function:

static struct utmp * pututline_file ( const struct utmp data) [static, read]

Definition at line 394 of file utmp_file.c.

                                 : 0);
}


static struct utmp *
pututline_file (const struct utmp *data)
{
  struct utmp buffer;
  struct utmp *pbuf;
  int found;

  assert (file_fd >= 0);

  /* Find the correct place to insert the data.  */
  if (file_offset > 0
      && (
#if _HAVE_UT_TYPE - 0
         (last_entry.ut_type == data->ut_type
          && (last_entry.ut_type == RUN_LVL
              || last_entry.ut_type == BOOT_TIME
              || last_entry.ut_type == OLD_TIME
              || last_entry.ut_type == NEW_TIME))
         ||
#endif
         __utmp_equal (&last_entry, data)))
    found = 1;
  else
    {
      bool lock_failed = false;
      found = internal_getut_r (data, &buffer, &lock_failed);

      if (__builtin_expect (lock_failed, false))
       {
         __set_errno (EAGAIN);
         return NULL;
       }
    }

  LOCK_FILE (file_fd, F_WRLCK)
    {
      pbuf = NULL;
      LOCKING_FAILED ();
    }

  if (found < 0)
    {
      /* We append the next entry.  */
      file_offset = __lseek64 (file_fd, 0, SEEK_END);
      if (file_offset % sizeof (struct utmp) != 0)
       {
         file_offset -= file_offset % sizeof (struct utmp);
         __ftruncate64 (file_fd, file_offset);

         if (__lseek64 (file_fd, 0, SEEK_END) < 0)
           {
             pbuf = NULL;
             goto unlock_return;
           }
       }
    }
  else
    {
      /* We replace the just read entry.  */
      file_offset -= sizeof (struct utmp);
      __lseek64 (file_fd, file_offset, SEEK_SET);
    }

  /* Write the new data.  */
  if (write_not_cancel (file_fd, data, sizeof (struct utmp))
      != sizeof (struct utmp))
    {
      /* If we appended a new record this is only partially written.
        Remove it.  */
      if (found < 0)
       (void) __ftruncate64 (file_fd, file_offset);
      pbuf = NULL;
    }
  else
    {
      file_offset += sizeof (struct utmp);
      pbuf = (struct utmp *) data;
    }

static int setutent_file ( void  ) [static]

Definition at line 131 of file utmp_file.c.

{
  if (file_fd < 0)
    {
      const char *file_name;
      int result;

      file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);

#ifdef O_CLOEXEC
# define O_flags O_LARGEFILE | O_CLOEXEC
#else
# define O_flags O_LARGEFILE
#endif
      file_fd = open_not_cancel_2 (file_name, O_RDWR | O_flags);
      if (file_fd == -1)
       {
         /* Hhm, read-write access did not work.  Try read-only.  */
         file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_flags);
         if (file_fd == -1)
           return 0;
       }

#ifndef __ASSUME_O_CLOEXEC
# ifdef O_CLOEXEC
      if (__have_o_cloexec <= 0)
# endif
       {
         /* We have to make sure the file is `closed on exec'.  */
         result = fcntl_not_cancel (file_fd, F_GETFD, 0);
         if (result >= 0)
           {
# ifdef O_CLOEXEC
             if (__have_o_cloexec == 0)
              __have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;

             if (__have_o_cloexec < 0)
# endif
              result = fcntl_not_cancel (file_fd, F_SETFD,
                                      result | FD_CLOEXEC);
           }

         if (result == -1)
           {
             close_not_cancel_no_status (file_fd);
             return 0;
           }
       }
#endif
    }

  __lseek64 (file_fd, 0, SEEK_SET);
  file_offset = 0;

  /* Make sure the entry won't match.  */
#if _HAVE_UT_TYPE - 0
  last_entry.ut_type = -1;
#else
  last_entry.ut_line[0] = '\177';
# if _HAVE_UT_ID - 0
  last_entry.ut_id[0] = '\0';

Here is the call graph for this function:

static void timeout_handler ( int  signum) [static]

Definition at line 51 of file utmp_file.c.

{};

Here is the caller graph for this function:

static int updwtmp_file ( const char *  file,
const struct utmp utmp 
) [static]

Definition at line 490 of file utmp_file.c.

{
  int result = -1;
  off64_t offset;
  int fd;

  /* Open WTMP file.  */
  fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE);
  if (fd < 0)
    return -1;

  LOCK_FILE (fd, F_WRLCK)
    LOCKING_FAILED ();

  /* Remember original size of log file.  */
  offset = __lseek64 (fd, 0, SEEK_END);
  if (offset % sizeof (struct utmp) != 0)
    {
      offset -= offset % sizeof (struct utmp);
      __ftruncate64 (fd, offset);

      if (__lseek64 (fd, 0, SEEK_END) < 0)
       goto unlock_return;
    }

  /* Write the entry.  If we can't write all the bytes, reset the file
     size back to the original size.  That way, no partial entries
     will remain.  */
  if (write_not_cancel (fd, utmp, sizeof (struct utmp))
      != sizeof (struct utmp))
    {
      __ftruncate64 (fd, offset);
      goto unlock_return;
    }

  result = 0;

unlock_return:
  UNLOCK_FILE (fd);


Variable Documentation

int file_fd = -1 [static]

Definition at line 38 of file utmp_file.c.

off64_t file_offset [static]

Definition at line 39 of file utmp_file.c.

struct utmp [static]

Definition at line 42 of file utmp_file.c.