1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2013 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/>. */
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 #if _HAVE_UT_TYPE || defined UTMPX
44 /* Prototype for our test function. */
45 static int do_test (int argc
, char *argv
[]);
47 /* We have a preparation function. */
48 static void do_prepare (int argc
, char *argv
[]);
49 #define PREPARE do_prepare
51 /* This defines the `main' function and some more. */
52 #include <test-skeleton.c>
55 /* These are for the temporary file we generate. */
60 do_prepare (int argc
, char *argv
[])
64 name_len
= strlen (test_dir
);
65 name
= malloc (name_len
+ sizeof ("/utmpXXXXXX"));
66 mempcpy (mempcpy (name
, test_dir
, name_len
),
67 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
70 /* Open our test file. */
73 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
78 #if _HAVE_UT_TV || defined UTMPX
79 #define UT(a) .ut_tv = { .tv_sec = (a)}
81 #define UT(a) .ut_time = (a)
84 { .ut_type
= BOOT_TIME
, .ut_pid
= 1, UT(1000) },
85 { .ut_type
= RUN_LVL
, .ut_pid
= 1, UT(2000) },
86 { .ut_type
= INIT_PROCESS
, .ut_pid
= 5, .ut_id
= "si", UT(3000) },
87 { .ut_type
= LOGIN_PROCESS
, .ut_pid
= 23, .ut_line
= "tty1", .ut_id
= "1",
88 .ut_user
= "LOGIN", UT(4000) },
89 { .ut_type
= USER_PROCESS
, .ut_pid
= 24, .ut_line
= "tty2", .ut_id
= "2",
90 .ut_user
= "albert", UT(8000) },
91 { .ut_type
= USER_PROCESS
, .ut_pid
= 196, .ut_line
= "ttyp0", .ut_id
= "p0",
92 .ut_user
= "niels", UT(10000) },
93 { .ut_type
= DEAD_PROCESS
, .ut_line
= "ttyp1", .ut_id
= "p1", UT(16000) },
97 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
99 time_t entry_time
= 20000;
100 pid_t entry_pid
= 234;
109 for (n
= 0; n
< num_entries
; n
++)
111 if (pututline (&entry
[n
]) == NULL
)
113 error (0, errno
, "cannot write UTMP entry");
133 while ((ut
= getutent ()))
135 if (n
< num_entries
&&
136 memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
138 error (0, 0, "UTMP entry does not match");
145 if (n
!= num_entries
)
147 error (0, 0, "number of UTMP entries is incorrect");
157 simulate_login (const char *line
, const char *user
)
161 for (n
= 0; n
< num_entries
; n
++)
163 if (strcmp (line
, entry
[n
].ut_line
) == 0 ||
164 entry
[n
].ut_type
== DEAD_PROCESS
)
166 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
167 entry
[n
].ut_pid
= (entry_pid
+= 27);
168 entry
[n
].ut_type
= USER_PROCESS
;
169 strncpy (entry
[n
].ut_user
, user
, sizeof (entry
[n
].ut_user
));
170 #if _HAVE_UT_TV - 0 || defined UTMPX
171 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
173 entry
[n
].ut_time
= (entry_time
+= 1000);
177 if (pututline (&entry
[n
]) == NULL
)
179 error (0, errno
, "cannot write UTMP entry");
189 error (0, 0, "no entries available");
194 simulate_logout (const char *line
)
198 for (n
= 0; n
< num_entries
; n
++)
200 if (strcmp (line
, entry
[n
].ut_line
) == 0)
202 entry
[n
].ut_type
= DEAD_PROCESS
;
203 strncpy (entry
[n
].ut_user
, "", sizeof (entry
[n
].ut_user
));
204 #if _HAVE_UT_TV - 0 || defined UTMPX
205 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
207 entry
[n
].ut_time
= (entry_time
+= 1000);
211 if (pututline (&entry
[n
]) == NULL
)
213 error (0, errno
, "cannot write UTMP entry");
223 error (0, 0, "no entry found for `%s'", line
);
228 check_login (const char *line
)
236 strcpy (ut
.ut_line
, line
);
237 up
= getutline (&ut
);
240 error (0, errno
, "cannot get entry for line `%s'", line
);
246 for (n
= 0; n
< num_entries
; n
++)
248 if (strcmp (line
, entry
[n
].ut_line
) == 0)
250 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
252 error (0, 0, "UTMP entry does not match");
260 error (0, 0, "bogus entry for line `%s'", line
);
265 check_logout (const char *line
)
271 strcpy (ut
.ut_line
, line
);
272 if (getutline (&ut
) != NULL
)
274 error (0, 0, "bogus login entry for `%s'", line
);
284 check_id (const char *id
)
292 ut
.ut_type
= USER_PROCESS
;
293 strcpy (ut
.ut_id
, id
);
297 error (0, errno
, "cannot get entry for ID `%s'", id
);
303 for (n
= 0; n
< num_entries
; n
++)
305 if (strcmp (id
, entry
[n
].ut_id
) == 0)
307 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
309 error (0, 0, "UTMP entry does not match");
317 error (0, 0, "bogus entry for ID `%s'", id
);
322 check_type (int type
)
334 error (0, errno
, "cannot get entry for type `%d'", type
);
340 for (n
= 0; n
< num_entries
; n
++)
342 if (type
== entry
[n
].ut_type
)
344 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
346 error (0, 0, "UTMP entry does not match");
354 error (0, 0, "bogus entry for type `%d'", type
);
359 do_test (int argc
, char *argv
[])
365 result
|= do_init ();
366 result
|= do_check ();
368 result
|= simulate_login ("tty1", "erwin");
369 result
|= do_check ();
371 result
|= simulate_login ("ttyp1", "paul");
372 result
|= do_check ();
374 result
|= simulate_logout ("tty2");
375 result
|= do_check ();
377 result
|= simulate_logout ("ttyp0");
378 result
|= do_check ();
380 result
|= simulate_login ("ttyp2", "richard");
381 result
|= do_check ();
383 result
|= check_login ("tty1");
384 result
|= check_logout ("ttyp0");
385 result
|= check_id ("p1");
386 result
|= check_id ("2");
387 result
|= check_id ("si");
388 result
|= check_type (BOOT_TIME
);
389 result
|= check_type (RUN_LVL
);
396 /* No field 'ut_type' in struct utmp. */