buildsys: Tweak pregen wrt headers_dep
[uclibc-ng.git] / test / misc / tst-utmp.c
blob1b0333a32749958bf14970909106b4ae8ddc4134
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/>. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <time.h>
26 #ifdef UTMPX
27 # include <utmpx.h>
28 # define utmp utmpx
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
36 #else
37 # include <utmp.h>
38 #endif
40 #ifndef _HAVE_UT_TYPE
41 # define _HAVE_UT_TYPE 0
42 #endif
43 #ifndef _HAVE_UT_PID
44 # define _HAVE_UT_PID 0
45 #endif
46 #ifndef _HAVE_UT_ID
47 # define _HAVE_UT_ID 0
48 #endif
49 #ifndef _HAVE_UT_TV
50 # define _HAVE_UT_TV 0
51 #endif
52 #ifndef _HAVE_UT_HOST
53 # define _HAVE_UT_HOST 0
54 #endif
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. */
70 char *name;
71 int fd;
73 static void
74 do_prepare (int argc, char *argv[])
76 size_t name_len;
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"));
82 add_temp_file (name);
84 /* Open our test file. */
85 fd = mkstemp (name);
86 if (fd == -1) {
87 fprintf (stderr, "cannot open test file `%s': ", name);
88 perror (NULL);
89 exit (EXIT_FAILURE);
93 struct utmp entry[] =
95 #if _HAVE_UT_TV || defined UTMPX
96 #define UT(a) .ut_tv = { .tv_sec = (a)}
97 #else
98 #define UT(a) .ut_time = (a)
99 #endif
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 },
112 { .ut_type = EMPTY }
114 int num_entries = sizeof entry / sizeof (struct utmp);
116 time_t entry_time = 20000;
117 pid_t entry_pid = 234;
119 static int
120 do_init (void)
122 int n;
124 setutent ();
126 for (n = 0; n < num_entries; n++)
128 if (pututline (&entry[n]) == NULL)
130 perror ("cannot write UTMP entry");
131 return 1;
135 endutent ();
137 return 0;
141 static int
142 do_check (void)
144 struct utmp *ut;
145 int n;
147 setutent ();
149 n = 0;
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");
156 return 1;
159 n++;
162 if (n != num_entries)
164 fprintf (stderr, "number of UTMP entries is incorrect\n");
165 return 1;
168 endutent ();
170 return 0;
173 static int
174 simulate_login (const char *line, const char *user)
176 int n;
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);
189 #else
190 entry[n].ut_time = (entry_time += 1000);
191 #endif
192 setutent ();
194 if (pututline (&entry[n]) == NULL)
196 perror ("cannot write UTMP entry");
197 return 1;
200 endutent ();
202 return 0;
206 fprintf (stderr, "no entries available\n");
207 return 1;
210 static int
211 simulate_logout (const char *line)
213 int n;
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);
223 #else
224 entry[n].ut_time = (entry_time += 1000);
225 #endif
226 setutent ();
228 if (pututline (&entry[n]) == NULL)
230 perror ("cannot write UTMP entry");
231 return 1;
234 endutent ();
236 return 0;
240 fprintf (stderr, "no entry found for `%s'\n", line);
241 return 1;
244 static int
245 check_login (const char *line)
247 struct utmp *up;
248 struct utmp ut;
249 int n;
251 setutent ();
253 strcpy (ut.ut_line, line);
254 up = getutline (&ut);
255 if (up == NULL)
257 fprintf (stderr, "cannot get entry for line `%s': ", line);
258 perror(NULL);
259 return 1;
262 endutent ();
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");
271 return 1;
274 return 0;
278 fprintf (stderr, "bogus entry for line `%s'\n", line);
279 return 1;
282 static int
283 check_logout (const char *line)
285 struct utmp ut;
287 setutent ();
289 strcpy (ut.ut_line, line);
290 if (getutline (&ut) != NULL)
292 fprintf (stderr, "bogus login entry for `%s'\n", line);
293 return 1;
296 endutent ();
298 return 0;
301 static int
302 check_id (const char *id)
304 struct utmp *up;
305 struct utmp ut;
306 int n;
308 setutent ();
310 ut.ut_type = USER_PROCESS;
311 strcpy (ut.ut_id, id);
312 up = getutid (&ut);
313 if (up == NULL)
315 fprintf (stderr, "cannot get entry for ID `%s': ", id);
316 perror (NULL);
317 return 1;
320 endutent ();
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");
329 return 1;
332 return 0;
336 fprintf (stderr, "bogus entry for ID `%s'\n", id);
337 return 1;
340 static int
341 check_type (int type)
343 struct utmp *up;
344 struct utmp ut;
345 int n;
347 setutent ();
349 ut.ut_type = type;
350 up = getutid (&ut);
351 if (up == NULL)
353 fprintf (stderr, "cannot get entry for type `%d': ", type);
354 perror (NULL);
355 return 1;
358 endutent ();
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");
367 return 1;
370 return 0;
374 fprintf (stderr, "bogus entry for type `%d'\n", type);
375 return 1;
378 static int
379 do_test (int argc, char *argv[])
381 int result = 0;
383 utmpname (name);
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);
411 return result;
414 #else
416 /* No field 'ut_type' in struct utmp. */
418 main ()
420 return 0;
423 #endif