1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001-2003 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/>. */
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 # define _HAVE_UT_TYPE 0
44 # define _HAVE_UT_PID 0
47 # define _HAVE_UT_ID 0
50 # define _HAVE_UT_TV 0
53 # define _HAVE_UT_HOST 0
56 #if _HAVE_UT_TYPE || defined UTMPX
58 /* Prototype for our test function. */
59 static int do_test (int argc
, char *argv
[]);
61 /* We have a preparation function. */
62 static void do_prepare (int argc
, char *argv
[]);
63 #define PREPARE do_prepare
65 /* This defines the `main' function and some more. */
66 #include "../test-skeleton.c"
69 /* These are for the temporary file we generate. */
74 do_prepare (int argc
, char *argv
[])
78 name_len
= strlen (test_dir
);
79 name
= malloc (name_len
+ sizeof ("/utmpXXXXXX"));
80 mempcpy (mempcpy (name
, test_dir
, name_len
),
81 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
84 /* Open our test file. */
87 fprintf (stderr
, "cannot open test file `%s': ", name
);
95 #if _HAVE_UT_TV || defined UTMPX
96 #define UT(a) .ut_tv = { .tv_sec = (a)}
98 #define UT(a) .ut_time = (a)
101 { .ut_type
= BOOT_TIME
, .ut_pid
= 1, UT(1000) },
102 { .ut_type
= RUN_LVL
, .ut_pid
= 1, UT(2000) },
103 { .ut_type
= INIT_PROCESS
, .ut_pid
= 5, .ut_id
= "si", UT(3000) },
104 { .ut_type
= LOGIN_PROCESS
, .ut_pid
= 23, .ut_line
= "tty1", .ut_id
= "1",
105 .ut_user
= "LOGIN", UT(4000) },
106 { .ut_type
= USER_PROCESS
, .ut_pid
= 24, .ut_line
= "tty2", .ut_id
= "2",
107 .ut_user
= "albert", UT(8000) },
108 { .ut_type
= USER_PROCESS
, .ut_pid
= 196, .ut_line
= "ttyp0", .ut_id
= "p0",
109 .ut_user
= "niels", UT(10000) },
110 { .ut_type
= DEAD_PROCESS
, .ut_line
= "ttyp1", .ut_id
= "p1", UT(16000) },
111 { .ut_type
= EMPTY
},
114 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
116 time_t entry_time
= 20000;
117 pid_t entry_pid
= 234;
126 for (n
= 0; n
< num_entries
; n
++)
128 if (pututline (&entry
[n
]) == NULL
)
130 perror ("cannot write UTMP entry");
150 while ((ut
= getutent ()))
152 if (n
< num_entries
&&
153 memcmp (ut
, &entry
[n
], sizeof (struct utmp
)))
155 fprintf (stderr
, "UTMP entry does not match\n");
162 if (n
!= num_entries
)
164 fprintf (stderr
, "number of UTMP entries is incorrect\n");
174 simulate_login (const char *line
, const char *user
)
178 for (n
= 0; n
< num_entries
; n
++)
180 if (strcmp (line
, entry
[n
].ut_line
) == 0 ||
181 entry
[n
].ut_type
== DEAD_PROCESS
)
183 if (entry
[n
].ut_pid
== DEAD_PROCESS
)
184 entry
[n
].ut_pid
= (entry_pid
+= 27);
185 entry
[n
].ut_type
= USER_PROCESS
;
186 strncpy (entry
[n
].ut_user
, user
, sizeof (entry
[n
].ut_user
));
187 #if _HAVE_UT_TV - 0 || defined UTMPX
188 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
190 entry
[n
].ut_time
= (entry_time
+= 1000);
194 if (pututline (&entry
[n
]) == NULL
)
196 perror ("cannot write UTMP entry");
206 fprintf (stderr
, "no entries available\n");
211 simulate_logout (const char *line
)
215 for (n
= 0; n
< num_entries
; n
++)
217 if (strcmp (line
, entry
[n
].ut_line
) == 0)
219 entry
[n
].ut_type
= DEAD_PROCESS
;
220 strncpy (entry
[n
].ut_user
, "", sizeof (entry
[n
].ut_user
));
221 #if _HAVE_UT_TV - 0 || defined UTMPX
222 entry
[n
].ut_tv
.tv_sec
= (entry_time
+= 1000);
224 entry
[n
].ut_time
= (entry_time
+= 1000);
228 if (pututline (&entry
[n
]) == NULL
)
230 perror ("cannot write UTMP entry");
240 fprintf (stderr
, "no entry found for `%s'\n", line
);
245 check_login (const char *line
)
253 strcpy (ut
.ut_line
, line
);
254 up
= getutline (&ut
);
257 fprintf (stderr
, "cannot get entry for line `%s': ", line
);
264 for (n
= 0; n
< num_entries
; n
++)
266 if (strcmp (line
, entry
[n
].ut_line
) == 0)
268 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
270 fprintf (stderr
, "UTMP entry does not match\n");
278 fprintf (stderr
, "bogus entry for line `%s'\n", line
);
283 check_logout (const char *line
)
289 strcpy (ut
.ut_line
, line
);
290 if (getutline (&ut
) != NULL
)
292 fprintf (stderr
, "bogus login entry for `%s'\n", line
);
302 check_id (const char *id
)
310 ut
.ut_type
= USER_PROCESS
;
311 strcpy (ut
.ut_id
, id
);
315 fprintf (stderr
, "cannot get entry for ID `%s': ", id
);
322 for (n
= 0; n
< num_entries
; n
++)
324 if (strcmp (id
, entry
[n
].ut_id
) == 0)
326 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
328 fprintf (stderr
, "UTMP entry does not match\n");
336 fprintf (stderr
, "bogus entry for ID `%s'\n", id
);
341 check_type (int type
)
353 fprintf (stderr
, "cannot get entry for type `%d': ", type
);
360 for (n
= 0; n
< num_entries
; n
++)
362 if (type
== entry
[n
].ut_type
)
364 if (memcmp (up
, &entry
[n
], sizeof (struct utmp
)))
366 fprintf (stderr
, "UTMP entry does not match\n");
374 fprintf (stderr
, "bogus entry for type `%d'\n", type
);
379 do_test (int argc
, char *argv
[])
385 result
|= do_init ();
386 result
|= do_check ();
388 result
|= simulate_login ("tty1", "erwin");
389 result
|= do_check ();
391 result
|= simulate_login ("ttyp1", "paul");
392 result
|= do_check ();
394 result
|= simulate_logout ("tty2");
395 result
|= do_check ();
397 result
|= simulate_logout ("ttyp0");
398 result
|= do_check ();
400 result
|= simulate_login ("ttyp2", "richard");
401 result
|= do_check ();
403 result
|= check_login ("tty1");
404 result
|= check_logout ("ttyp0");
405 result
|= check_id ("p1");
406 result
|= check_id ("2");
407 result
|= check_id ("si");
408 result
|= check_type (BOOT_TIME
);
409 result
|= check_type (RUN_LVL
);
416 /* No field 'ut_type' in struct utmp. */