ia64: ioperm: clean up long dead code
[glibc.git] / login / tst-utmp.c
bloba61b54305e7ae5c5bdce0b79039dd366e6df153b
1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2013 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 <error.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <time.h>
27 #ifdef UTMPX
28 # include <utmpx.h>
29 # define utmp utmpx
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
37 #else
38 # include <utmp.h>
39 #endif
42 #if _HAVE_UT_TYPE || defined UTMPX
44 /* Prototype for our test function. */
45 static int do_test (int argc, char *argv[]);
47 /* We have a preparation function. */
48 static void do_prepare (int argc, char *argv[]);
49 #define PREPARE do_prepare
51 /* This defines the `main' function and some more. */
52 #include <test-skeleton.c>
55 /* These are for the temporary file we generate. */
56 char *name;
57 int fd;
59 static void
60 do_prepare (int argc, char *argv[])
62 size_t name_len;
64 name_len = strlen (test_dir);
65 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
66 mempcpy (mempcpy (name, test_dir, name_len),
67 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
68 add_temp_file (name);
70 /* Open our test file. */
71 fd = mkstemp (name);
72 if (fd == -1)
73 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
76 struct utmp entry[] =
78 #if _HAVE_UT_TV || defined UTMPX
79 #define UT(a) .ut_tv = { .tv_sec = (a)}
80 #else
81 #define UT(a) .ut_time = (a)
82 #endif
84 { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
85 { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
86 { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
87 { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
88 .ut_user = "LOGIN", UT(4000) },
89 { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
90 .ut_user = "albert", UT(8000) },
91 { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
92 .ut_user = "niels", UT(10000) },
93 { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
94 { .ut_type = EMPTY },
95 { .ut_type = EMPTY }
97 int num_entries = sizeof entry / sizeof (struct utmp);
99 time_t entry_time = 20000;
100 pid_t entry_pid = 234;
102 static int
103 do_init (void)
105 int n;
107 setutent ();
109 for (n = 0; n < num_entries; n++)
111 if (pututline (&entry[n]) == NULL)
113 error (0, errno, "cannot write UTMP entry");
114 return 1;
118 endutent ();
120 return 0;
124 static int
125 do_check (void)
127 struct utmp *ut;
128 int n;
130 setutent ();
132 n = 0;
133 while ((ut = getutent ()))
135 if (n < num_entries &&
136 memcmp (ut, &entry[n], sizeof (struct utmp)))
138 error (0, 0, "UTMP entry does not match");
139 return 1;
142 n++;
145 if (n != num_entries)
147 error (0, 0, "number of UTMP entries is incorrect");
148 return 1;
151 endutent ();
153 return 0;
156 static int
157 simulate_login (const char *line, const char *user)
159 int n;
161 for (n = 0; n < num_entries; n++)
163 if (strcmp (line, entry[n].ut_line) == 0 ||
164 entry[n].ut_type == DEAD_PROCESS)
166 if (entry[n].ut_pid == DEAD_PROCESS)
167 entry[n].ut_pid = (entry_pid += 27);
168 entry[n].ut_type = USER_PROCESS;
169 strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
170 #if _HAVE_UT_TV - 0 || defined UTMPX
171 entry[n].ut_tv.tv_sec = (entry_time += 1000);
172 #else
173 entry[n].ut_time = (entry_time += 1000);
174 #endif
175 setutent ();
177 if (pututline (&entry[n]) == NULL)
179 error (0, errno, "cannot write UTMP entry");
180 return 1;
183 endutent ();
185 return 0;
189 error (0, 0, "no entries available");
190 return 1;
193 static int
194 simulate_logout (const char *line)
196 int n;
198 for (n = 0; n < num_entries; n++)
200 if (strcmp (line, entry[n].ut_line) == 0)
202 entry[n].ut_type = DEAD_PROCESS;
203 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
204 #if _HAVE_UT_TV - 0 || defined UTMPX
205 entry[n].ut_tv.tv_sec = (entry_time += 1000);
206 #else
207 entry[n].ut_time = (entry_time += 1000);
208 #endif
209 setutent ();
211 if (pututline (&entry[n]) == NULL)
213 error (0, errno, "cannot write UTMP entry");
214 return 1;
217 endutent ();
219 return 0;
223 error (0, 0, "no entry found for `%s'", line);
224 return 1;
227 static int
228 check_login (const char *line)
230 struct utmp *up;
231 struct utmp ut;
232 int n;
234 setutent ();
236 strcpy (ut.ut_line, line);
237 up = getutline (&ut);
238 if (up == NULL)
240 error (0, errno, "cannot get entry for line `%s'", line);
241 return 1;
244 endutent ();
246 for (n = 0; n < num_entries; n++)
248 if (strcmp (line, entry[n].ut_line) == 0)
250 if (memcmp (up, &entry[n], sizeof (struct utmp)))
252 error (0, 0, "UTMP entry does not match");
253 return 1;
256 return 0;
260 error (0, 0, "bogus entry for line `%s'", line);
261 return 1;
264 static int
265 check_logout (const char *line)
267 struct utmp ut;
269 setutent ();
271 strcpy (ut.ut_line, line);
272 if (getutline (&ut) != NULL)
274 error (0, 0, "bogus login entry for `%s'", line);
275 return 1;
278 endutent ();
280 return 0;
283 static int
284 check_id (const char *id)
286 struct utmp *up;
287 struct utmp ut;
288 int n;
290 setutent ();
292 ut.ut_type = USER_PROCESS;
293 strcpy (ut.ut_id, id);
294 up = getutid (&ut);
295 if (up == NULL)
297 error (0, errno, "cannot get entry for ID `%s'", id);
298 return 1;
301 endutent ();
303 for (n = 0; n < num_entries; n++)
305 if (strcmp (id, entry[n].ut_id) == 0)
307 if (memcmp (up, &entry[n], sizeof (struct utmp)))
309 error (0, 0, "UTMP entry does not match");
310 return 1;
313 return 0;
317 error (0, 0, "bogus entry for ID `%s'", id);
318 return 1;
321 static int
322 check_type (int type)
324 struct utmp *up;
325 struct utmp ut;
326 int n;
328 setutent ();
330 ut.ut_type = type;
331 up = getutid (&ut);
332 if (up == NULL)
334 error (0, errno, "cannot get entry for type `%d'", type);
335 return 1;
338 endutent ();
340 for (n = 0; n < num_entries; n++)
342 if (type == entry[n].ut_type)
344 if (memcmp (up, &entry[n], sizeof (struct utmp)))
346 error (0, 0, "UTMP entry does not match");
347 return 1;
350 return 0;
354 error (0, 0, "bogus entry for type `%d'", type);
355 return 1;
358 static int
359 do_test (int argc, char *argv[])
361 int result = 0;
363 utmpname (name);
365 result |= do_init ();
366 result |= do_check ();
368 result |= simulate_login ("tty1", "erwin");
369 result |= do_check ();
371 result |= simulate_login ("ttyp1", "paul");
372 result |= do_check ();
374 result |= simulate_logout ("tty2");
375 result |= do_check ();
377 result |= simulate_logout ("ttyp0");
378 result |= do_check ();
380 result |= simulate_login ("ttyp2", "richard");
381 result |= do_check ();
383 result |= check_login ("tty1");
384 result |= check_logout ("ttyp0");
385 result |= check_id ("p1");
386 result |= check_id ("2");
387 result |= check_id ("si");
388 result |= check_type (BOOT_TIME);
389 result |= check_type (RUN_LVL);
391 return result;
394 #else
396 /* No field 'ut_type' in struct utmp. */
398 main (void)
400 return 0;
403 #endif