1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://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 /* Prototype for our test function. */
42 static int do_test (int argc
, char *argv
[]);
44 /* We have a preparation function. */
45 static void do_prepare (int argc
, char *argv
[]);
46 #define PREPARE do_prepare
48 /* This defines the `main' function and some more. */
49 #include <test-skeleton.c>
52 /* These are for the temporary file we generate. */
57 do_prepare (int argc
, char *argv
[])
61 name_len
= strlen (test_dir
);
62 name
= xmalloc (name_len
+ sizeof ("/utmpXXXXXX"));
63 mempcpy (mempcpy (name
, test_dir
, name_len
),
64 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
66 /* Open our test file. */
69 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
75 #define UT(a) .ut_tv = { .tv_sec = (a)}
77 { .ut_type
= BOOT_TIME
, .ut_pid
= 1, UT(1000) },
78 { .ut_type
= RUN_LVL
, .ut_pid
= 1, UT(2000) },
79 { .ut_type
= INIT_PROCESS
, .ut_pid
= 5, .ut_id
= "si", UT(3000) },
80 { .ut_type
= LOGIN_PROCESS
, .ut_pid
= 23, .ut_line
= "tty1", .ut_id
= "1",
81 .ut_user
= "LOGIN", UT(4000) },
82 { .ut_type
= USER_PROCESS
, .ut_pid
= 24, .ut_line
= "tty2", .ut_id
= "2",
83 .ut_user
= "albert", UT(8000) },
84 { .ut_type
= USER_PROCESS
, .ut_pid
= 196, .ut_line
= "ttyp0", .ut_id
= "p0",
85 .ut_user
= "niels", UT(10000) },
86 { .ut_type
= DEAD_PROCESS
, .ut_line
= "ttyp1", .ut_id
= "p1", UT(16000) },
90 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
92 time_t entry_time
= 20000;
93 pid_t entry_pid
= 234;
102 for (n
= 0; n
< num_entries
; n
++)
104 if (pututline (&entry
[n
]) == NULL
)
106 error (0, errno
, "cannot write UTMP entry");
126 while ((ut
= getutent ()))
129 && memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
131 error (0, 0, "UTMP entry does not match");
138 if (n
!= num_entries
)
140 error (0, 0, "number of UTMP entries is incorrect");
150 simulate_login (const char *line
, const char *user
)
154 for (n
= 0; n
< num_entries
; n
++)
156 if (strcmp (line
, entry
[n
].ut_line
) == 0
157 || entry
[n
].ut_type
== DEAD_PROCESS
)
159 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
160 entry
[n
].ut_pid
= (entry_pid
+= 27);
161 entry
[n
].ut_type
= USER_PROCESS
;
162 strncpy (entry
[n
].ut_user
, user
, sizeof (entry
[n
].ut_user
));
163 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
166 if (pututline (&entry
[n
]) == NULL
)
168 error (0, errno
, "cannot write UTMP entry");
178 error (0, 0, "no entries available");
183 simulate_logout (const char *line
)
187 for (n
= 0; n
< num_entries
; n
++)
189 if (strcmp (line
, entry
[n
].ut_line
) == 0)
191 entry
[n
].ut_type
= DEAD_PROCESS
;
192 strncpy (entry
[n
].ut_user
, "", sizeof (entry
[n
].ut_user
));
193 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
196 if (pututline (&entry
[n
]) == NULL
)
198 error (0, errno
, "cannot write UTMP entry");
208 error (0, 0, "no entry found for `%s'", line
);
213 check_login (const char *line
)
221 strcpy (ut
.ut_line
, line
);
222 up
= getutline (&ut
);
225 error (0, errno
, "cannot get entry for line `%s'", line
);
231 for (n
= 0; n
< num_entries
; n
++)
233 if (strcmp (line
, entry
[n
].ut_line
) == 0)
235 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
237 error (0, 0, "UTMP entry does not match");
245 error (0, 0, "bogus entry for line `%s'", line
);
250 check_logout (const char *line
)
256 strcpy (ut
.ut_line
, line
);
257 if (getutline (&ut
) != NULL
)
259 error (0, 0, "bogus login entry for `%s'", line
);
269 check_id (const char *id
)
277 ut
.ut_type
= USER_PROCESS
;
278 strcpy (ut
.ut_id
, id
);
282 error (0, errno
, "cannot get entry for ID `%s'", id
);
288 for (n
= 0; n
< num_entries
; n
++)
290 if (strcmp (id
, entry
[n
].ut_id
) == 0)
292 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
294 error (0, 0, "UTMP entry does not match");
302 error (0, 0, "bogus entry for ID `%s'", id
);
307 check_type (int type
)
319 error (0, errno
, "cannot get entry for type `%d'", type
);
325 for (n
= 0; n
< num_entries
; n
++)
327 if (type
== entry
[n
].ut_type
)
329 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
331 error (0, 0, "UTMP entry does not match");
339 error (0, 0, "bogus entry for type `%d'", type
);
344 do_test (int argc
, char *argv
[])
350 result
|= do_init ();
351 result
|= do_check ();
353 result
|= simulate_login ("tty1", "erwin");
354 result
|= do_check ();
356 result
|= simulate_login ("ttyp1", "paul");
357 result
|= do_check ();
359 result
|= simulate_logout ("tty2");
360 result
|= do_check ();
362 result
|= simulate_logout ("ttyp0");
363 result
|= do_check ();
365 result
|= simulate_login ("ttyp2", "richard");
366 result
|= do_check ();
368 result
|= check_login ("tty1");
369 result
|= check_logout ("ttyp0");
370 result
|= check_id ("p1");
371 result
|= check_id ("2");
372 result
|= check_id ("si");
373 result
|= check_type (BOOT_TIME
);
374 result
|= check_type (RUN_LVL
);