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, see
18 <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
29 # define utmpname utmpxname
30 # define setutent setutxent
31 # define getutent getutxent
32 # define endutent endutxent
33 # define getutline getutxline
34 # define getutid getutxid
35 # define pututline pututxline
41 #if _HAVE_UT_TYPE || defined UTMPX
43 /* Prototype for our test function. */
44 static int do_test (int argc, char *argv[]);
46 /* We have a preparation function. */
47 static void do_prepare (int argc, char *argv[]);
48 #define PREPARE do_prepare
50 /* This defines the `main' function and some more. */
51 #include "../test-skeleton.c"
54 /* These are for the temporary file we generate. */
59 do_prepare (int argc, char *argv[])
63 name_len = strlen (test_dir);
64 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
65 mempcpy (mempcpy (name, test_dir, name_len),
66 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
69 /* Open our test file. */
72 fprintf (stderr, "cannot open test file `%s': ", name);
80 #if _HAVE_UT_TV || defined UTMPX
81 #define UT(a) .ut_tv = { .tv_sec = (a)}
83 #define UT(a) .ut_time = (a)
86 { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
87 { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
88 { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
89 { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
90 .ut_user = "LOGIN", UT(4000) },
91 { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
92 .ut_user = "albert", UT(8000) },
93 { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
94 .ut_user = "niels", UT(10000) },
95 { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
99 int num_entries = sizeof entry / sizeof (struct utmp);
101 time_t entry_time = 20000;
102 pid_t entry_pid = 234;
111 for (n = 0; n < num_entries; n++)
113 if (pututline (&entry[n]) == NULL)
115 perror ("cannot write UTMP entry");
135 while ((ut = getutent ()))
137 if (n < num_entries &&
138 memcmp (ut, &entry[n], sizeof (struct utmp)))
140 fprintf (stderr, "UTMP entry does not match\n");
147 if (n != num_entries)
149 fprintf (stderr, "number of UTMP entries is incorrect\n");
159 simulate_login (const char *line, const char *user)
163 for (n = 0; n < num_entries; n++)
165 if (strcmp (line, entry[n].ut_line) == 0 ||
166 entry[n].ut_type == DEAD_PROCESS)
168 if (entry[n].ut_pid == DEAD_PROCESS)
169 entry[n].ut_pid = (entry_pid += 27);
170 entry[n].ut_type = USER_PROCESS;
171 strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
172 #if _HAVE_UT_TV - 0 || defined UTMPX
173 entry[n].ut_tv.tv_sec = (entry_time += 1000);
175 entry[n].ut_time = (entry_time += 1000);
179 if (pututline (&entry[n]) == NULL)
181 perror ("cannot write UTMP entry");
191 fprintf (stderr, "no entries available\n");
196 simulate_logout (const char *line)
200 for (n = 0; n < num_entries; n++)
202 if (strcmp (line, entry[n].ut_line) == 0)
204 entry[n].ut_type = DEAD_PROCESS;
205 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
206 #if _HAVE_UT_TV - 0 || defined UTMPX
207 entry[n].ut_tv.tv_sec = (entry_time += 1000);
209 entry[n].ut_time = (entry_time += 1000);
213 if (pututline (&entry[n]) == NULL)
215 perror ("cannot write UTMP entry");
225 fprintf (stderr, "no entry found for `%s'\n", line);
230 check_login (const char *line)
238 strcpy (ut.ut_line, line);
239 up = getutline (&ut);
242 fprintf (stderr, "cannot get entry for line `%s': ", line);
249 for (n = 0; n < num_entries; n++)
251 if (strcmp (line, entry[n].ut_line) == 0)
253 if (memcmp (up, &entry[n], sizeof (struct utmp)))
255 fprintf (stderr, "UTMP entry does not match\n");
263 fprintf (stderr, "bogus entry for line `%s'\n", line);
268 check_logout (const char *line)
274 strcpy (ut.ut_line, line);
275 if (getutline (&ut) != NULL)
277 fprintf (stderr, "bogus login entry for `%s'\n", line);
287 check_id (const char *id)
295 ut.ut_type = USER_PROCESS;
296 strcpy (ut.ut_id, id);
300 fprintf (stderr, "cannot get entry for ID `%s': ", id);
307 for (n = 0; n < num_entries; n++)
309 if (strcmp (id, entry[n].ut_id) == 0)
311 if (memcmp (up, &entry[n], sizeof (struct utmp)))
313 fprintf (stderr, "UTMP entry does not match\n");
321 fprintf (stderr, "bogus entry for ID `%s'\n", id);
326 check_type (int type)
338 fprintf (stderr, "cannot get entry for type `%d': ", type);
345 for (n = 0; n < num_entries; n++)
347 if (type == entry[n].ut_type)
349 if (memcmp (up, &entry[n], sizeof (struct utmp)))
351 fprintf (stderr, "UTMP entry does not match\n");
359 fprintf (stderr, "bogus entry for type `%d'\n", type);
364 do_test (int argc, char *argv[])
370 result |= do_init ();
371 result |= do_check ();
373 result |= simulate_login ("tty1", "erwin");
374 result |= do_check ();
376 result |= simulate_login ("ttyp1", "paul");
377 result |= do_check ();
379 result |= simulate_logout ("tty2");
380 result |= do_check ();
382 result |= simulate_logout ("ttyp0");
383 result |= do_check ();
385 result |= simulate_login ("ttyp2", "richard");
386 result |= do_check ();
388 result |= check_login ("tty1");
389 result |= check_logout ("ttyp0");
390 result |= check_id ("p1");
391 result |= check_id ("2");
392 result |= check_id ("si");
393 result |= check_type (BOOT_TIME);
394 result |= check_type (RUN_LVL);
401 /* No field 'ut_type' in struct utmp. */