hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame login/tst-utmp.c

Packit 6c4009
/* Tests for UTMP functions.
Packit 6c4009
   Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
#ifdef UTMPX
Packit 6c4009
# include <utmpx.h>
Packit 6c4009
# define utmp utmpx
Packit 6c4009
# define utmpname utmpxname
Packit 6c4009
# define setutent setutxent
Packit 6c4009
# define getutent getutxent
Packit 6c4009
# define endutent endutxent
Packit 6c4009
# define getutline getutxline
Packit 6c4009
# define getutid getutxid
Packit 6c4009
# define pututline pututxline
Packit 6c4009
#else
Packit 6c4009
# include <utmp.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Prototype for our test function.  */
Packit 6c4009
static int do_test (int argc, char *argv[]);
Packit 6c4009
Packit 6c4009
/* We have a preparation function.  */
Packit 6c4009
static void do_prepare (int argc, char *argv[]);
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
/* This defines the `main' function and some more.  */
Packit 6c4009
#include <test-skeleton.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* These are for the temporary file we generate.  */
Packit 6c4009
char *name;
Packit 6c4009
int fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  size_t name_len;
Packit 6c4009
Packit 6c4009
  name_len = strlen (test_dir);
Packit 6c4009
  name = xmalloc (name_len + sizeof ("/utmpXXXXXX"));
Packit 6c4009
  mempcpy (mempcpy (name, test_dir, name_len),
Packit 6c4009
	   "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
Packit 6c4009
Packit 6c4009
  /* Open our test file.  */
Packit 6c4009
  fd = mkstemp (name);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
Packit 6c4009
  add_temp_file (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct utmp entry[] =
Packit 6c4009
{
Packit 6c4009
#define UT(a)  .ut_tv = { .tv_sec = (a)}
Packit 6c4009
Packit 6c4009
  { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
Packit 6c4009
  { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
Packit 6c4009
  { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
Packit 6c4009
  { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
Packit 6c4009
    .ut_user = "LOGIN", UT(4000) },
Packit 6c4009
  { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
Packit 6c4009
    .ut_user = "albert", UT(8000) },
Packit 6c4009
  { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
Packit 6c4009
    .ut_user = "niels", UT(10000) },
Packit 6c4009
  { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
Packit 6c4009
  { .ut_type = EMPTY },
Packit 6c4009
  { .ut_type = EMPTY }
Packit 6c4009
};
Packit 6c4009
int num_entries = sizeof entry / sizeof (struct utmp);
Packit 6c4009
Packit 6c4009
time_t entry_time = 20000;
Packit 6c4009
pid_t entry_pid = 234;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_init (void)
Packit 6c4009
{
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (pututline (&entry[n]) == NULL)
Packit 6c4009
	{
Packit 6c4009
	  error (0, errno, "cannot write UTMP entry");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_check (void)
Packit 6c4009
{
Packit 6c4009
  struct utmp *ut;
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  n = 0;
Packit 6c4009
  while ((ut = getutent ()))
Packit 6c4009
    {
Packit 6c4009
      if (n < num_entries &&
Packit 6c4009
	  memcmp (ut, &entry[n], sizeof (struct utmp)))
Packit 6c4009
	{
Packit 6c4009
	  error (0, 0, "UTMP entry does not match");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      n++;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (n != num_entries)
Packit 6c4009
    {
Packit 6c4009
      error (0, 0, "number of UTMP entries is incorrect");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
simulate_login (const char *line, const char *user)
Packit 6c4009
{
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (line, entry[n].ut_line) == 0 ||
Packit 6c4009
	  entry[n].ut_type == DEAD_PROCESS)
Packit 6c4009
	{
Packit 6c4009
	  if (entry[n].ut_pid == DEAD_PROCESS)
Packit 6c4009
	    entry[n].ut_pid = (entry_pid += 27);
Packit 6c4009
	  entry[n].ut_type = USER_PROCESS;
Packit 6c4009
	  strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
Packit 6c4009
	  entry[n].ut_tv.tv_sec = (entry_time += 1000);
Packit 6c4009
	  setutent ();
Packit 6c4009
Packit 6c4009
	  if (pututline (&entry[n]) == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      error (0, errno, "cannot write UTMP entry");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  endutent ();
Packit 6c4009
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  error (0, 0, "no entries available");
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
simulate_logout (const char *line)
Packit 6c4009
{
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (line, entry[n].ut_line) == 0)
Packit 6c4009
	{
Packit 6c4009
	  entry[n].ut_type = DEAD_PROCESS;
Packit 6c4009
	  strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
Packit 6c4009
          entry[n].ut_tv.tv_sec = (entry_time += 1000);
Packit 6c4009
	  setutent ();
Packit 6c4009
Packit 6c4009
	  if (pututline (&entry[n]) == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      error (0, errno, "cannot write UTMP entry");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  endutent ();
Packit 6c4009
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  error (0, 0, "no entry found for `%s'", line);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_login (const char *line)
Packit 6c4009
{
Packit 6c4009
  struct utmp *up;
Packit 6c4009
  struct utmp ut;
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  strcpy (ut.ut_line, line);
Packit 6c4009
  up = getutline (&ut);
Packit 6c4009
  if (up == NULL)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "cannot get entry for line `%s'", line);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (line, entry[n].ut_line) == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (memcmp (up, &entry[n], sizeof (struct utmp)))
Packit 6c4009
	    {
Packit 6c4009
	      error (0, 0, "UTMP entry does not match");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  error (0, 0, "bogus entry for line `%s'", line);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_logout (const char *line)
Packit 6c4009
{
Packit 6c4009
  struct utmp ut;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  strcpy (ut.ut_line, line);
Packit 6c4009
  if (getutline (&ut) != NULL)
Packit 6c4009
    {
Packit 6c4009
      error (0, 0, "bogus login entry for `%s'", line);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_id (const char *id)
Packit 6c4009
{
Packit 6c4009
  struct utmp *up;
Packit 6c4009
  struct utmp ut;
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  ut.ut_type = USER_PROCESS;
Packit 6c4009
  strcpy (ut.ut_id, id);
Packit 6c4009
  up = getutid (&ut);
Packit 6c4009
  if (up == NULL)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "cannot get entry for ID `%s'", id);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp (id, entry[n].ut_id) == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (memcmp (up, &entry[n], sizeof (struct utmp)))
Packit 6c4009
	    {
Packit 6c4009
	      error (0, 0, "UTMP entry does not match");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  error (0, 0, "bogus entry for ID `%s'", id);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_type (int type)
Packit 6c4009
{
Packit 6c4009
  struct utmp *up;
Packit 6c4009
  struct utmp ut;
Packit 6c4009
  int n;
Packit 6c4009
Packit 6c4009
  setutent ();
Packit 6c4009
Packit 6c4009
  ut.ut_type = type;
Packit 6c4009
  up = getutid (&ut);
Packit 6c4009
  if (up == NULL)
Packit 6c4009
    {
Packit 6c4009
      error (0, errno, "cannot get entry for type `%d'", type);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  endutent ();
Packit 6c4009
Packit 6c4009
  for (n = 0; n < num_entries; n++)
Packit 6c4009
    {
Packit 6c4009
      if (type == entry[n].ut_type)
Packit 6c4009
	{
Packit 6c4009
	  if (memcmp (up, &entry[n], sizeof (struct utmp)))
Packit 6c4009
	    {
Packit 6c4009
	      error (0, 0, "UTMP entry does not match");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  error (0, 0, "bogus entry for type `%d'", type);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  utmpname (name);
Packit 6c4009
Packit 6c4009
  result |= do_init ();
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= simulate_login ("tty1", "erwin");
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= simulate_login ("ttyp1", "paul");
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= simulate_logout ("tty2");
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= simulate_logout ("ttyp0");
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= simulate_login ("ttyp2", "richard");
Packit 6c4009
  result |= do_check ();
Packit 6c4009
Packit 6c4009
  result |= check_login ("tty1");
Packit 6c4009
  result |= check_logout ("ttyp0");
Packit 6c4009
  result |= check_id ("p1");
Packit 6c4009
  result |= check_id ("2");
Packit 6c4009
  result |= check_id ("si");
Packit 6c4009
  result |= check_type (BOOT_TIME);
Packit 6c4009
  result |= check_type (RUN_LVL);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}