[subdir=io] (sysdep_routines): Add fallocate and fallocate64.
[glibc/history.git] / login / tst-utmp.c
blob12a2088558b21da096dbfaebc1fe3adbac94901b
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <error.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <time.h>
28 #ifdef UTMPX
29 # include <utmpx.h>
30 # define utmp utmpx
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
38 #else
39 # include <utmp.h>
40 #endif
43 #if _HAVE_UT_TYPE || defined UTMPX
45 /* Prototype for our test function. */
46 static int do_test (int argc, char *argv[]);
48 /* We have a preparation function. */
49 static void do_prepare (int argc, char *argv[]);
50 #define PREPARE do_prepare
52 /* This defines the `main' function and some more. */
53 #include <test-skeleton.c>
56 /* These are for the temporary file we generate. */
57 char *name;
58 int fd;
60 static void
61 do_prepare (int argc, char *argv[])
63 size_t name_len;
65 name_len = strlen (test_dir);
66 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
67 mempcpy (mempcpy (name, test_dir, name_len),
68 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
69 add_temp_file (name);
71 /* Open our test file. */
72 fd = mkstemp (name);
73 if (fd == -1)
74 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
77 struct utmp entry[] =
79 #if _HAVE_UT_TV || defined UTMPX
80 #define UT(a) .ut_tv = { .tv_sec = (a)}
81 #else
82 #define UT(a) .ut_time = (a)
83 #endif
85 { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
86 { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
87 { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
88 { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
89 .ut_user = "LOGIN", UT(4000) },
90 { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
91 .ut_user = "albert", UT(8000) },
92 { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
93 .ut_user = "niels", UT(10000) },
94 { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
95 { .ut_type = EMPTY },
96 { .ut_type = EMPTY }
98 int num_entries = sizeof entry / sizeof (struct utmp);
100 time_t entry_time = 20000;
101 pid_t entry_pid = 234;
103 static int
104 do_init (void)
106 int n;
108 setutent ();
110 for (n = 0; n < num_entries; n++)
112 if (pututline (&entry[n]) == NULL)
114 error (0, errno, "cannot write UTMP entry");
115 return 1;
119 endutent ();
121 return 0;
125 static int
126 do_check (void)
128 struct utmp *ut;
129 int n;
131 setutent ();
133 n = 0;
134 while ((ut = getutent ()))
136 if (n < num_entries &&
137 memcmp (ut, &entry[n], sizeof (struct utmp)))
139 error (0, 0, "UTMP entry does not match");
140 return 1;
143 n++;
146 if (n != num_entries)
148 error (0, 0, "number of UTMP entries is incorrect");
149 return 1;
152 endutent ();
154 return 0;
157 static int
158 simulate_login (const char *line, const char *user)
160 int n;
162 for (n = 0; n < num_entries; n++)
164 if (strcmp (line, entry[n].ut_line) == 0 ||
165 entry[n].ut_type == DEAD_PROCESS)
167 if (entry[n].ut_pid == DEAD_PROCESS)
168 entry[n].ut_pid = (entry_pid += 27);
169 entry[n].ut_type = USER_PROCESS;
170 strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
171 #if _HAVE_UT_TV - 0 || defined UTMPX
172 entry[n].ut_tv.tv_sec = (entry_time += 1000);
173 #else
174 entry[n].ut_time = (entry_time += 1000);
175 #endif
176 setutent ();
178 if (pututline (&entry[n]) == NULL)
180 error (0, errno, "cannot write UTMP entry");
181 return 1;
184 endutent ();
186 return 0;
190 error (0, 0, "no entries available");
191 return 1;
194 static int
195 simulate_logout (const char *line)
197 int n;
199 for (n = 0; n < num_entries; n++)
201 if (strcmp (line, entry[n].ut_line) == 0)
203 entry[n].ut_type = DEAD_PROCESS;
204 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
205 #if _HAVE_UT_TV - 0 || defined UTMPX
206 entry[n].ut_tv.tv_sec = (entry_time += 1000);
207 #else
208 entry[n].ut_time = (entry_time += 1000);
209 #endif
210 setutent ();
212 if (pututline (&entry[n]) == NULL)
214 error (0, errno, "cannot write UTMP entry");
215 return 1;
218 endutent ();
220 return 0;
224 error (0, 0, "no entry found for `%s'", line);
225 return 1;
228 static int
229 check_login (const char *line)
231 struct utmp *up;
232 struct utmp ut;
233 int n;
235 setutent ();
237 strcpy (ut.ut_line, line);
238 up = getutline (&ut);
239 if (up == NULL)
241 error (0, errno, "cannot get entry for line `%s'", line);
242 return 1;
245 endutent ();
247 for (n = 0; n < num_entries; n++)
249 if (strcmp (line, entry[n].ut_line) == 0)
251 if (memcmp (up, &entry[n], sizeof (struct utmp)))
253 error (0, 0, "UTMP entry does not match");
254 return 1;
257 return 0;
261 error (0, 0, "bogus entry for line `%s'", line);
262 return 1;
265 static int
266 check_logout (const char *line)
268 struct utmp ut;
270 setutent ();
272 strcpy (ut.ut_line, line);
273 if (getutline (&ut) != NULL)
275 error (0, 0, "bogus login entry for `%s'", line);
276 return 1;
279 endutent ();
281 return 0;
284 static int
285 check_id (const char *id)
287 struct utmp *up;
288 struct utmp ut;
289 int n;
291 setutent ();
293 ut.ut_type = USER_PROCESS;
294 strcpy (ut.ut_id, id);
295 up = getutid (&ut);
296 if (up == NULL)
298 error (0, errno, "cannot get entry for ID `%s'", id);
299 return 1;
302 endutent ();
304 for (n = 0; n < num_entries; n++)
306 if (strcmp (id, entry[n].ut_id) == 0)
308 if (memcmp (up, &entry[n], sizeof (struct utmp)))
310 error (0, 0, "UTMP entry does not match");
311 return 1;
314 return 0;
318 error (0, 0, "bogus entry for ID `%s'", id);
319 return 1;
322 static int
323 check_type (int type)
325 struct utmp *up;
326 struct utmp ut;
327 int n;
329 setutent ();
331 ut.ut_type = type;
332 up = getutid (&ut);
333 if (up == NULL)
335 error (0, errno, "cannot get entry for type `%d'", type);
336 return 1;
339 endutent ();
341 for (n = 0; n < num_entries; n++)
343 if (type == entry[n].ut_type)
345 if (memcmp (up, &entry[n], sizeof (struct utmp)))
347 error (0, 0, "UTMP entry does not match");
348 return 1;
351 return 0;
355 error (0, 0, "bogus entry for type `%d'", type);
356 return 1;
359 static int
360 do_test (int argc, char *argv[])
362 int result = 0;
364 utmpname (name);
366 result |= do_init ();
367 result |= do_check ();
369 result |= simulate_login ("tty1", "erwin");
370 result |= do_check ();
372 result |= simulate_login ("ttyp1", "paul");
373 result |= do_check ();
375 result |= simulate_logout ("tty2");
376 result |= do_check ();
378 result |= simulate_logout ("ttyp0");
379 result |= do_check ();
381 result |= simulate_login ("ttyp2", "richard");
382 result |= do_check ();
384 result |= check_login ("tty1");
385 result |= check_logout ("ttyp0");
386 result |= check_id ("p1");
387 result |= check_id ("2");
388 result |= check_id ("si");
389 result |= check_type (BOOT_TIME);
390 result |= check_type (RUN_LVL);
392 return result;
395 #else
397 /* No field 'ut_type' in struct utmp. */
399 main ()
401 return 0;
404 #endif