aboutsummaryrefslogtreecommitdiff
path: root/login/tst-pututxline-cache.c
blob: 9b24d18f1991ff60d4359ad38942a1e975568c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Test case for cache invalidation after concurrent write (bug 24882).
   Copyright (C) 2019-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If
   not, see <http://www.gnu.org/licenses/>.  */

/* This test writes an entry to the utmpx file, reads it (so that it
   is cached) in process1, and overwrites the same entry in process2
   with something that does not match the search criteria.  At this
   point, the cache of the first process is stale, and when process1
   attempts to write a new record which would have gone to the same
   place (as indicated by the cache), it needs to realize that it has
   to pick a different slot because the old slot is now used for
   something else.  */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <utmp.h>
#include <utmpx.h>

/* Set to the path of the utmp file.  */
static char *utmp_file;

/* Used to synchronize the subprocesses.  The barrier itself is
   allocated in shared memory.  */
static pthread_barrier_t *barrier;

/* setutxent with error checking.  */
static void
xsetutxent (void)
{
  errno = 0;
  setutxent ();
  TEST_COMPARE (errno, 0);
}

/* getutxent with error checking.  */
static struct utmpx *
xgetutxent (void)
{
  errno = 0;
  struct utmpx *result = getutxent ();
  if (result == NULL)
    FAIL_EXIT1 ("getutxent: %m");
  return result;
}

static void
put_entry (const char *id, pid_t pid, const char *user, const char *line)
{
  struct utmpx ut =
    {
     .ut_type = LOGIN_PROCESS,
     .ut_pid = pid,
     .ut_host = "localhost",
    };
  strcpy (ut.ut_id, id);
  strncpy (ut.ut_user, user, sizeof (ut.ut_user));
  strncpy (ut.ut_line, line, sizeof (ut.ut_line));
  TEST_VERIFY (pututxline (&ut) != NULL);
}

/* Use two cooperating subprocesses to avoid issues related to
   unlock-on-close semantics of POSIX advisory locks.  */

static __attribute__ ((noreturn)) void
process1 (void)
{
  TEST_COMPARE (utmpname (utmp_file), 0);

  /* Create an entry.  */
  xsetutxent ();
  put_entry ("1", 101, "root", "process1");

  /* Retrieve the entry.  This will fill the internal cache.  */
  {
    errno = 0;
    setutxent ();
    TEST_COMPARE (errno, 0);
    struct utmpx ut =
      {
       .ut_type = LOGIN_PROCESS,
       .ut_line = "process1",
      };
    struct utmpx *result = getutxline (&ut);
    if (result == NULL)
      FAIL_EXIT1 ("getutxline (\"process1\"): %m");
    TEST_COMPARE (result->ut_pid, 101);
  }

  /* Signal the other process to overwrite the entry.  */
  xpthread_barrier_wait (barrier);

  /* Wait for the other process to complete the write operation.  */
  xpthread_barrier_wait (barrier);

  /* Add another entry.  Note: This time, there is no setutxent call.  */
  put_entry ("1", 103, "root", "process1");

  _exit (0);
}

static void
process2 (void *closure)
{
  /* Wait for the first process to write its entry.  */
  xpthread_barrier_wait (barrier);

  /* Truncate the file.  The glibc interface does not support
     re-purposing records, but an external expiration mechanism may
     trigger this.  */
  TEST_COMPARE (truncate64 (utmp_file, 0), 0);

  /* Write the replacement entry.  */
  TEST_COMPARE (utmpname (utmp_file), 0);
  xsetutxent ();
  put_entry ("2", 102, "user", "process2");

  /* Signal the other process that the entry has been replaced.  */
  xpthread_barrier_wait (barrier);
}

static int
do_test (void)
{
  xclose (create_temp_file ("tst-tumpx-cache-write-", &utmp_file));
  {
    pthread_barrierattr_t attr;
    xpthread_barrierattr_init (&attr);
    xpthread_barrierattr_setpshared (&attr, PTHREAD_SCOPE_PROCESS);
    barrier = support_shared_allocate (sizeof (*barrier));
    xpthread_barrier_init (barrier, &attr, 2);
  }

  /* Run both subprocesses in parallel.  */
  {
    pid_t pid1 = xfork ();
    if (pid1 == 0)
      process1 ();
    support_isolate_in_subprocess (process2, NULL);
    int status;
    xwaitpid (pid1, &status, 0);
    TEST_COMPARE (status, 0);
  }

  /* Check that the utmpx database contains the expected records.  */
  {
    TEST_COMPARE (utmpname (utmp_file), 0);
    xsetutxent ();

    struct utmpx *ut = xgetutxent ();
    TEST_COMPARE_STRING (ut->ut_id, "2");
    TEST_COMPARE (ut->ut_pid, 102);
    TEST_COMPARE_STRING (ut->ut_user, "user");
    TEST_COMPARE_STRING (ut->ut_line, "process2");

    ut = xgetutxent ();
    TEST_COMPARE_STRING (ut->ut_id, "1");
    TEST_COMPARE (ut->ut_pid, 103);
    TEST_COMPARE_STRING (ut->ut_user, "root");
    TEST_COMPARE_STRING (ut->ut_line, "process1");

    if (getutxent () != NULL)
      FAIL_EXIT1 ("additional utmpx entry");
  }

  xpthread_barrier_destroy (barrier);
  support_shared_free (barrier);
  free (utmp_file);

  return 0;
}

#include <support/test-driver.c>