]>
sourceware.org Git - glibc.git/blob - login/tst-utmp.c
1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2021 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 <https://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
30 # define utmpname utmpxname
31 # define setutent setutxent
32 # define getutent getutxent
33 # define endutent endutxent
34 # define getutline getutxline
35 # define getutid getutxid
36 # define pututline pututxline
42 /* Prototype for our test function. */
43 static int do_test (int argc
, char *argv
[]);
45 /* We have a preparation function. */
46 static void do_prepare (int argc
, char *argv
[]);
47 #define PREPARE do_prepare
49 /* This defines the `main' function and some more. */
50 #include <test-skeleton.c>
53 /* These are for the temporary file we generate. */
58 do_prepare (int argc
, char *argv
[])
62 name_len
= strlen (test_dir
);
63 name
= xmalloc (name_len
+ sizeof ("/utmpXXXXXX"));
64 mempcpy (mempcpy (name
, test_dir
, name_len
),
65 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
67 /* Open our test file. */
70 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
76 #define UT(a) .ut_tv = { .tv_sec = (a)}
78 { .ut_type
= BOOT_TIME
, .ut_pid
= 1, UT(1000) },
79 { .ut_type
= RUN_LVL
, .ut_pid
= 1, UT(2000) },
80 { .ut_type
= INIT_PROCESS
, .ut_pid
= 5, .ut_id
= "si", UT(3000) },
81 { .ut_type
= LOGIN_PROCESS
, .ut_pid
= 23, .ut_line
= "tty1", .ut_id
= "1",
82 .ut_user
= "LOGIN", UT(4000) },
83 { .ut_type
= USER_PROCESS
, .ut_pid
= 24, .ut_line
= "tty2", .ut_id
= "2",
84 .ut_user
= "albert", UT(8000) },
85 { .ut_type
= USER_PROCESS
, .ut_pid
= 196, .ut_line
= "ttyp0", .ut_id
= "p0",
86 .ut_user
= "niels", UT(10000) },
87 { .ut_type
= DEAD_PROCESS
, .ut_line
= "ttyp1", .ut_id
= "p1", UT(16000) },
91 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
93 time_t entry_time
= 20000;
94 pid_t entry_pid
= 234;
103 for (n
= 0; n
< num_entries
; n
++)
105 if (pututline (&entry
[n
]) == NULL
)
107 error (0, errno
, "cannot write UTMP entry");
127 while ((ut
= getutent ()))
130 && memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
132 error (0, 0, "UTMP entry does not match");
139 if (n
!= num_entries
)
141 error (0, 0, "number of UTMP entries is incorrect");
151 simulate_login (const char *line
, const char *user
)
155 for (n
= 0; n
< num_entries
; n
++)
157 if (strcmp (line
, entry
[n
].ut_line
) == 0
158 || entry
[n
].ut_type
== DEAD_PROCESS
)
160 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
161 entry
[n
].ut_pid
= (entry_pid
+= 27);
162 entry
[n
].ut_type
= USER_PROCESS
;
163 strncpy (entry
[n
].ut_user
, user
, sizeof (entry
[n
].ut_user
));
164 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
167 if (pututline (&entry
[n
]) == NULL
)
169 error (0, errno
, "cannot write UTMP entry");
179 error (0, 0, "no entries available");
184 simulate_logout (const char *line
)
188 for (n
= 0; n
< num_entries
; n
++)
190 if (strcmp (line
, entry
[n
].ut_line
) == 0)
192 entry
[n
].ut_type
= DEAD_PROCESS
;
193 strncpy (entry
[n
].ut_user
, "", sizeof (entry
[n
].ut_user
));
194 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
197 if (pututline (&entry
[n
]) == NULL
)
199 error (0, errno
, "cannot write UTMP entry");
209 error (0, 0, "no entry found for `%s'", line
);
214 check_login (const char *line
)
222 strcpy (ut
.ut_line
, line
);
223 up
= getutline (&ut
);
226 error (0, errno
, "cannot get entry for line `%s'", line
);
232 for (n
= 0; n
< num_entries
; n
++)
234 if (strcmp (line
, entry
[n
].ut_line
) == 0)
236 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
238 error (0, 0, "UTMP entry does not match");
246 error (0, 0, "bogus entry for line `%s'", line
);
251 check_logout (const char *line
)
257 strcpy (ut
.ut_line
, line
);
258 if (getutline (&ut
) != NULL
)
260 error (0, 0, "bogus login entry for `%s'", line
);
270 check_id (const char *id
)
278 ut
.ut_type
= USER_PROCESS
;
279 strcpy (ut
.ut_id
, id
);
283 error (0, errno
, "cannot get entry for ID `%s'", id
);
289 for (n
= 0; n
< num_entries
; n
++)
291 if (strcmp (id
, entry
[n
].ut_id
) == 0)
293 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
295 error (0, 0, "UTMP entry does not match");
303 error (0, 0, "bogus entry for ID `%s'", id
);
308 check_type (int type
)
320 error (0, errno
, "cannot get entry for type `%d'", type
);
326 for (n
= 0; n
< num_entries
; n
++)
328 if (type
== entry
[n
].ut_type
)
330 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
332 error (0, 0, "UTMP entry does not match");
340 error (0, 0, "bogus entry for type `%d'", type
);
345 do_test (int argc
, char *argv
[])
351 result
|= do_init ();
352 result
|= do_check ();
354 result
|= simulate_login ("tty1", "erwin");
355 result
|= do_check ();
357 result
|= simulate_login ("ttyp1", "paul");
358 result
|= do_check ();
360 result
|= simulate_logout ("tty2");
361 result
|= do_check ();
363 result
|= simulate_logout ("ttyp0");
364 result
|= do_check ();
366 result
|= simulate_login ("ttyp2", "richard");
367 result
|= do_check ();
369 result
|= check_login ("tty1");
370 result
|= check_logout ("ttyp0");
371 result
|= check_id ("p1");
372 result
|= check_id ("2");
373 result
|= check_id ("si");
374 result
|= check_type (BOOT_TIME
);
375 result
|= check_type (RUN_LVL
);
This page took 0.050486 seconds and 5 git commands to generate.