1
/* Tests for UTMP functions.
2
Copyright (C) 1998, 2001-2003 Free Software Foundation, Inc.
3
This file is part of the GNU C Library.
4
Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
6
The GNU C Library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
The GNU C Library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with the GNU C Library; if not, write to the Free
18
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25
#include <sys/types.h>
31
# define utmpname utmpxname
32
# define setutent setutxent
33
# define getutent getutxent
34
# define endutent endutxent
35
# define getutline getutxline
36
# define getutid getutxid
37
# define pututline pututxline
43
#if _HAVE_UT_TYPE || defined UTMPX
45
/* Prototype for our test function. */
46
static int do_test (int argc, char *argv[]);
48
/* We have a preparation function. */
49
static void do_prepare (int argc, char *argv[]);
50
#define PREPARE do_prepare
52
/* This defines the `main' function and some more. */
53
#include <test-skeleton.c>
56
/* These are for the temporary file we generate. */
61
do_prepare (int argc, char *argv[])
65
name_len = strlen (test_dir);
66
name = malloc (name_len + sizeof ("/utmpXXXXXX"));
67
mempcpy (mempcpy (name, test_dir, name_len),
68
"/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
71
/* Open our test file. */
74
error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
79
#if _HAVE_UT_TV || defined UTMPX
80
#define UT(a) .ut_tv = { .tv_sec = (a)}
82
#define UT(a) .ut_time = (a)
85
{ .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
86
{ .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
87
{ .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
88
{ .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
89
.ut_user = "LOGIN", UT(4000) },
90
{ .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
91
.ut_user = "albert", UT(8000) },
92
{ .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
93
.ut_user = "niels", UT(10000) },
94
{ .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
98
int num_entries = sizeof entry / sizeof (struct utmp);
100
time_t entry_time = 20000;
101
pid_t entry_pid = 234;
110
for (n = 0; n < num_entries; n++)
112
if (pututline (&entry[n]) == NULL)
114
error (0, errno, "cannot write UTMP entry");
134
while ((ut = getutent ()))
136
if (n < num_entries &&
137
memcmp (ut, &entry[n], sizeof (struct utmp)))
139
error (0, 0, "UTMP entry does not match");
146
if (n != num_entries)
148
error (0, 0, "number of UTMP entries is incorrect");
158
simulate_login (const char *line, const char *user)
162
for (n = 0; n < num_entries; n++)
164
if (strcmp (line, entry[n].ut_line) == 0 ||
165
entry[n].ut_type == DEAD_PROCESS)
167
if (entry[n].ut_pid == DEAD_PROCESS)
168
entry[n].ut_pid = (entry_pid += 27);
169
entry[n].ut_type = USER_PROCESS;
170
strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
171
#if _HAVE_UT_TV - 0 || defined UTMPX
172
entry[n].ut_tv.tv_sec = (entry_time += 1000);
174
entry[n].ut_time = (entry_time += 1000);
178
if (pututline (&entry[n]) == NULL)
180
error (0, errno, "cannot write UTMP entry");
190
error (0, 0, "no entries available");
195
simulate_logout (const char *line)
199
for (n = 0; n < num_entries; n++)
201
if (strcmp (line, entry[n].ut_line) == 0)
203
entry[n].ut_type = DEAD_PROCESS;
204
strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
205
#if _HAVE_UT_TV - 0 || defined UTMPX
206
entry[n].ut_tv.tv_sec = (entry_time += 1000);
208
entry[n].ut_time = (entry_time += 1000);
212
if (pututline (&entry[n]) == NULL)
214
error (0, errno, "cannot write UTMP entry");
224
error (0, 0, "no entry found for `%s'", line);
229
check_login (const char *line)
237
strcpy (ut.ut_line, line);
238
up = getutline (&ut);
241
error (0, errno, "cannot get entry for line `%s'", line);
247
for (n = 0; n < num_entries; n++)
249
if (strcmp (line, entry[n].ut_line) == 0)
251
if (memcmp (up, &entry[n], sizeof (struct utmp)))
253
error (0, 0, "UTMP entry does not match");
261
error (0, 0, "bogus entry for line `%s'", line);
266
check_logout (const char *line)
272
strcpy (ut.ut_line, line);
273
if (getutline (&ut) != NULL)
275
error (0, 0, "bogus login entry for `%s'", line);
285
check_id (const char *id)
293
ut.ut_type = USER_PROCESS;
294
strcpy (ut.ut_id, id);
298
error (0, errno, "cannot get entry for ID `%s'", id);
304
for (n = 0; n < num_entries; n++)
306
if (strcmp (id, entry[n].ut_id) == 0)
308
if (memcmp (up, &entry[n], sizeof (struct utmp)))
310
error (0, 0, "UTMP entry does not match");
318
error (0, 0, "bogus entry for ID `%s'", id);
323
check_type (int type)
335
error (0, errno, "cannot get entry for type `%d'", type);
341
for (n = 0; n < num_entries; n++)
343
if (type == entry[n].ut_type)
345
if (memcmp (up, &entry[n], sizeof (struct utmp)))
347
error (0, 0, "UTMP entry does not match");
355
error (0, 0, "bogus entry for type `%d'", type);
360
do_test (int argc, char *argv[])
366
result |= do_init ();
367
result |= do_check ();
369
result |= simulate_login ("tty1", "erwin");
370
result |= do_check ();
372
result |= simulate_login ("ttyp1", "paul");
373
result |= do_check ();
375
result |= simulate_logout ("tty2");
376
result |= do_check ();
378
result |= simulate_logout ("ttyp0");
379
result |= do_check ();
381
result |= simulate_login ("ttyp2", "richard");
382
result |= do_check ();
384
result |= check_login ("tty1");
385
result |= check_logout ("ttyp0");
386
result |= check_id ("p1");
387
result |= check_id ("2");
388
result |= check_id ("si");
389
result |= check_type (BOOT_TIME);
390
result |= check_type (RUN_LVL);
397
/* No field 'ut_type' in struct utmp. */