1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <sys/types.h>
31 # define utmpname utmpxname
32 # define setutent setutxent
33 # define getutent getutxent
34 # define endutent endutxent
35 # define getutline getutxline
36 # define getutid getutxid
37 # define pututline pututxline
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 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
77 #if _HAVE_UT_TV || defined UTMPX
78 #define UT(a) ut_tv:{tv_sec:(a)}
80 #define UT(a) ut_time:(a)
83 { ut_type
: BOOT_TIME
, ut_pid
: 1, UT(1000) },
84 { ut_type
: RUN_LVL
, ut_pid
: 1, UT(2000) },
85 { ut_type
: INIT_PROCESS
, ut_pid
: 5, ut_id
: "si", UT(3000) },
86 { ut_type
: LOGIN_PROCESS
, ut_pid
: 23, ut_line
: "tty1", ut_id
: "1",
87 ut_user
: "LOGIN", UT(4000) },
88 { ut_type
: USER_PROCESS
, ut_pid
: 24, ut_line
: "tty2", ut_id
: "2",
89 ut_user
: "albert", UT(8000) },
90 { ut_type
: USER_PROCESS
, ut_pid
: 196, ut_line
: "ttyp0", ut_id
: "p0",
91 ut_user
: "niels", UT(10000) },
92 { ut_type
: DEAD_PROCESS
, ut_line
: "ttyp1", ut_id
: "p1", UT(16000) },
96 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
98 time_t entry_time
= 20000;
99 pid_t entry_pid
= 234;
108 for (n
= 0; n
< num_entries
; n
++)
110 if (pututline (&entry
[n
]) == NULL
)
112 error (0, errno
, "cannot write UTMP entry");
132 while ((ut
= getutent ()))
134 if (n
< num_entries
&&
135 memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
137 error (0, 0, "UTMP entry does not match");
144 if (n
!= num_entries
)
146 error (0, 0, "number of UTMP entries is incorrect");
156 simulate_login (const char *line
, const char *user
)
160 for (n
= 0; n
< num_entries
; n
++)
162 if (strcmp (line
, entry
[n
].ut_line
) == 0 ||
163 entry
[n
].ut_type
== DEAD_PROCESS
)
165 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
166 entry
[n
].ut_pid
= (entry_pid
+= 27);
167 entry
[n
].ut_type
= USER_PROCESS
;
168 strcpy (entry
[n
].ut_user
, user
);
169 #if _HAVE_UT_TV - 0 || defined UTMPX
170 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
172 entry
[n
].ut_time
= (entry_time
+= 1000);
176 if (pututline (&entry
[n
]) == NULL
)
178 error (0, errno
, "cannot write UTMP entry");
188 error (0, 0, "no entries available");
193 simulate_logout (const char *line
)
197 for (n
= 0; n
< num_entries
; n
++)
199 if (strcmp (line
, entry
[n
].ut_line
) == 0)
201 entry
[n
].ut_type
= DEAD_PROCESS
;
202 entry
[n
].ut_user
[0] = '\0';
203 #if _HAVE_UT_TV - 0 || defined UTMPX
204 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
206 entry
[n
].ut_time
= (entry_time
+= 1000);
210 if (pututline (&entry
[n
]) == NULL
)
212 error (0, errno
, "cannot write UTMP entry");
222 error (0, 0, "no entry found for `%s'", line
);
227 check_login (const char *line
)
235 strcpy (ut
.ut_line
, line
);
236 up
= getutline (&ut
);
239 error (0, errno
, "cannot get entry for line `%s'", line
);
245 for (n
= 0; n
< num_entries
; n
++)
247 if (strcmp (line
, entry
[n
].ut_line
) == 0)
249 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
251 error (0, 0, "UTMP entry does not match");
259 error (0, 0, "bogus entry for line `%s'", line
);
264 check_logout (const char *line
)
270 strcpy (ut
.ut_line
, line
);
271 if (getutline (&ut
) != NULL
)
273 error (0, 0, "bogus login entry for `%s'", line
);
283 check_id (const char *id
)
291 ut
.ut_type
= USER_PROCESS
;
292 strcpy (ut
.ut_id
, id
);
296 error (0, errno
, "cannot get entry for ID `%s'", id
);
302 for (n
= 0; n
< num_entries
; n
++)
304 if (strcmp (id
, entry
[n
].ut_id
) == 0)
306 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
308 error (0, 0, "UTMP entry does not match");
316 error (0, 0, "bogus entry for ID `%s'", id
);
321 check_type (int type
)
333 error (0, errno
, "cannot get entry for type `%d'", type
);
339 for (n
= 0; n
< num_entries
; n
++)
341 if (type
== entry
[n
].ut_type
)
343 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
345 error (0, 0, "UTMP entry does not match");
353 error (0, 0, "bogus entry for type `%d'", type
);
358 do_test (int argc
, char *argv
[])
364 result
|= do_init ();
365 result
|= do_check ();
367 result
|= simulate_login ("tty1", "erwin");
368 result
|= do_check ();
370 result
|= simulate_login ("ttyp1", "paul");
371 result
|= do_check ();
373 result
|= simulate_logout ("tty2");
374 result
|= do_check ();
376 result
|= simulate_logout ("ttyp0");
377 result
|= do_check ();
379 result
|= simulate_login ("ttyp2", "richard");
380 result
|= do_check ();
382 result
|= check_login ("tty1");
383 result
|= check_logout ("ttyp0");
384 result
|= check_id ("p1");
385 result
|= check_id ("2");
386 result
|= check_id ("si");
387 result
|= check_type (BOOT_TIME
);
388 result
|= check_type (RUN_LVL
);