1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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
= malloc (name_len
+ sizeof ("/utmpXXXXXX"));
64 mempcpy (mempcpy (name
, test_dir
, name_len
),
65 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
68 /* Open our test file. */
71 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
76 #if _HAVE_UT_TV || defined UTMPX
77 #define UT(a) ut_tv:{tv_sec:(a)}
79 #define UT(a) ut_time:(a)
82 { ut_type
: BOOT_TIME
, ut_pid
: 1, UT(1000) },
83 { ut_type
: RUN_LVL
, ut_pid
: 1, UT(2000) },
84 { ut_type
: INIT_PROCESS
, ut_pid
: 5, ut_id
: "si", UT(3000) },
85 { ut_type
: LOGIN_PROCESS
, ut_pid
: 23, ut_line
: "tty1", ut_id
: "1",
86 ut_user
: "LOGIN", UT(4000) },
87 { ut_type
: USER_PROCESS
, ut_pid
: 24, ut_line
: "tty2", ut_id
: "2",
88 ut_user
: "albert", UT(8000) },
89 { ut_type
: USER_PROCESS
, ut_pid
: 196, ut_line
: "ttyp0", ut_id
: "p0",
90 ut_user
: "niels", UT(10000) },
91 { ut_type
: DEAD_PROCESS
, ut_line
: "ttyp1", ut_id
: "p1", UT(16000) },
95 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
97 time_t entry_time
= 20000;
98 pid_t entry_pid
= 234;
107 for (n
= 0; n
< num_entries
; n
++)
109 if (pututline (&entry
[n
]) == NULL
)
111 error (0, errno
, "cannot write UTMP entry");
131 while ((ut
= getutent ()))
133 if (n
< num_entries
&&
134 memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
136 error (0, 0, "UTMP entry does not match");
143 if (n
!= num_entries
)
145 error (0, 0, "number of UTMP entries is incorrect");
155 simulate_login (const char *line
, const char *user
)
159 for (n
= 0; n
< num_entries
; n
++)
161 if (strcmp (line
, entry
[n
].ut_line
) == 0 ||
162 entry
[n
].ut_type
== DEAD_PROCESS
)
164 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
165 entry
[n
].ut_pid
= (entry_pid
+= 27);
166 entry
[n
].ut_type
= USER_PROCESS
;
167 strcpy (entry
[n
].ut_user
, user
);
168 #if _HAVE_UT_TV - 0 || defined UTMPX
169 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
171 entry
[n
].ut_time
= (entry_time
+= 1000);
175 if (pututline (&entry
[n
]) == NULL
)
177 error (0, errno
, "cannot write UTMP entry");
187 error (0, 0, "no entries available");
192 simulate_logout (const char *line
)
196 for (n
= 0; n
< num_entries
; n
++)
198 if (strcmp (line
, entry
[n
].ut_line
) == 0)
200 entry
[n
].ut_type
= DEAD_PROCESS
;
201 entry
[n
].ut_user
[0] = '\0';
202 #if _HAVE_UT_TV - 0 || defined UTMPX
203 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
205 entry
[n
].ut_time
= (entry_time
+= 1000);
209 if (pututline (&entry
[n
]) == NULL
)
211 error (0, errno
, "cannot write UTMP entry");
221 error (0, 0, "no entry found for `%s'", line
);
226 check_login (const char *line
)
234 strcpy (ut
.ut_line
, line
);
235 up
= getutline (&ut
);
238 error (0, errno
, "cannot get entry for line `%s'", line
);
244 for (n
= 0; n
< num_entries
; n
++)
246 if (strcmp (line
, entry
[n
].ut_line
) == 0)
248 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
250 error (0, 0, "UTMP entry does not match");
258 error (0, 0, "bogus entry for line `%s'", line
);
263 check_logout (const char *line
)
269 strcpy (ut
.ut_line
, line
);
270 if (getutline (&ut
) != NULL
)
272 error (0, 0, "bogus login entry for `%s'", line
);
282 check_id (const char *id
)
290 ut
.ut_type
= USER_PROCESS
;
291 strcpy (ut
.ut_id
, id
);
295 error (0, errno
, "cannot get entry for ID `%s'", id
);
301 for (n
= 0; n
< num_entries
; n
++)
303 if (strcmp (id
, entry
[n
].ut_id
) == 0)
305 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
307 error (0, 0, "UTMP entry does not match");
315 error (0, 0, "bogus entry for ID `%s'", id
);
320 check_type (int type
)
332 error (0, errno
, "cannot get entry for type `%d'", type
);
338 for (n
= 0; n
< num_entries
; n
++)
340 if (type
== entry
[n
].ut_type
)
342 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
344 error (0, 0, "UTMP entry does not match");
352 error (0, 0, "bogus entry for type `%d'", type
);
357 do_test (int argc
, char *argv
[])
363 result
|= do_init ();
364 result
|= do_check ();
366 result
|= simulate_login ("tty1", "erwin");
367 result
|= do_check ();
369 result
|= simulate_login ("ttyp1", "paul");
370 result
|= do_check ();
372 result
|= simulate_logout ("tty2");
373 result
|= do_check ();
375 result
|= simulate_logout ("ttyp0");
376 result
|= do_check ();
378 result
|= simulate_login ("ttyp2", "richard");
379 result
|= do_check ();
381 result
|= check_login ("tty1");
382 result
|= check_logout ("ttyp0");
383 result
|= check_id ("p1");
384 result
|= check_id ("2");
385 result
|= check_id ("si");
386 result
|= check_type (BOOT_TIME
);
387 result
|= check_type (RUN_LVL
);