powerpc64le: ifunc select *f128 routines in multiarch mode
[glibc.git] / login / tst-utmp.c
blob5c2cdfee5068f43cd779b3999cfd71432a4ef2b8
1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2020 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 <https://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 /* Prototype for our test function. */
43 static int do_test (int argc, char *argv[]);
45 /* We have a preparation function. */
46 static void do_prepare (int argc, char *argv[]);
47 #define PREPARE do_prepare
49 /* This defines the `main' function and some more. */
50 #include <test-skeleton.c>
53 /* These are for the temporary file we generate. */
54 char *name;
55 int fd;
57 static void
58 do_prepare (int argc, char *argv[])
60 size_t name_len;
62 name_len = strlen (test_dir);
63 name = xmalloc (name_len + sizeof ("/utmpXXXXXX"));
64 mempcpy (mempcpy (name, test_dir, name_len),
65 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
67 /* Open our test file. */
68 fd = mkstemp (name);
69 if (fd == -1)
70 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
71 add_temp_file (name);
74 struct utmp entry[] =
76 #define UT(a) .ut_tv = { .tv_sec = (a)}
78 { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
79 { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
80 { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
81 { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
82 .ut_user = "LOGIN", UT(4000) },
83 { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
84 .ut_user = "albert", UT(8000) },
85 { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
86 .ut_user = "niels", UT(10000) },
87 { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
88 { .ut_type = EMPTY },
89 { .ut_type = EMPTY }
91 int num_entries = sizeof entry / sizeof (struct utmp);
93 time_t entry_time = 20000;
94 pid_t entry_pid = 234;
96 static int
97 do_init (void)
99 int n;
101 setutent ();
103 for (n = 0; n < num_entries; n++)
105 if (pututline (&entry[n]) == NULL)
107 error (0, errno, "cannot write UTMP entry");
108 return 1;
112 endutent ();
114 return 0;
118 static int
119 do_check (void)
121 struct utmp *ut;
122 int n;
124 setutent ();
126 n = 0;
127 while ((ut = getutent ()))
129 if (n < num_entries
130 && memcmp (ut, &entry[n], sizeof (struct utmp)))
132 error (0, 0, "UTMP entry does not match");
133 return 1;
136 n++;
139 if (n != num_entries)
141 error (0, 0, "number of UTMP entries is incorrect");
142 return 1;
145 endutent ();
147 return 0;
150 static int
151 simulate_login (const char *line, const char *user)
153 int n;
155 for (n = 0; n < num_entries; n++)
157 if (strcmp (line, entry[n].ut_line) == 0
158 || entry[n].ut_type == DEAD_PROCESS)
160 if (entry[n].ut_pid == DEAD_PROCESS)
161 entry[n].ut_pid = (entry_pid += 27);
162 entry[n].ut_type = USER_PROCESS;
163 strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
164 entry[n].ut_tv.tv_sec = (entry_time += 1000);
165 setutent ();
167 if (pututline (&entry[n]) == NULL)
169 error (0, errno, "cannot write UTMP entry");
170 return 1;
173 endutent ();
175 return 0;
179 error (0, 0, "no entries available");
180 return 1;
183 static int
184 simulate_logout (const char *line)
186 int n;
188 for (n = 0; n < num_entries; n++)
190 if (strcmp (line, entry[n].ut_line) == 0)
192 entry[n].ut_type = DEAD_PROCESS;
193 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
194 entry[n].ut_tv.tv_sec = (entry_time += 1000);
195 setutent ();
197 if (pututline (&entry[n]) == NULL)
199 error (0, errno, "cannot write UTMP entry");
200 return 1;
203 endutent ();
205 return 0;
209 error (0, 0, "no entry found for `%s'", line);
210 return 1;
213 static int
214 check_login (const char *line)
216 struct utmp *up;
217 struct utmp ut;
218 int n;
220 setutent ();
222 strcpy (ut.ut_line, line);
223 up = getutline (&ut);
224 if (up == NULL)
226 error (0, errno, "cannot get entry for line `%s'", line);
227 return 1;
230 endutent ();
232 for (n = 0; n < num_entries; n++)
234 if (strcmp (line, entry[n].ut_line) == 0)
236 if (memcmp (up, &entry[n], sizeof (struct utmp)))
238 error (0, 0, "UTMP entry does not match");
239 return 1;
242 return 0;
246 error (0, 0, "bogus entry for line `%s'", line);
247 return 1;
250 static int
251 check_logout (const char *line)
253 struct utmp ut;
255 setutent ();
257 strcpy (ut.ut_line, line);
258 if (getutline (&ut) != NULL)
260 error (0, 0, "bogus login entry for `%s'", line);
261 return 1;
264 endutent ();
266 return 0;
269 static int
270 check_id (const char *id)
272 struct utmp *up;
273 struct utmp ut;
274 int n;
276 setutent ();
278 ut.ut_type = USER_PROCESS;
279 strcpy (ut.ut_id, id);
280 up = getutid (&ut);
281 if (up == NULL)
283 error (0, errno, "cannot get entry for ID `%s'", id);
284 return 1;
287 endutent ();
289 for (n = 0; n < num_entries; n++)
291 if (strcmp (id, entry[n].ut_id) == 0)
293 if (memcmp (up, &entry[n], sizeof (struct utmp)))
295 error (0, 0, "UTMP entry does not match");
296 return 1;
299 return 0;
303 error (0, 0, "bogus entry for ID `%s'", id);
304 return 1;
307 static int
308 check_type (int type)
310 struct utmp *up;
311 struct utmp ut;
312 int n;
314 setutent ();
316 ut.ut_type = type;
317 up = getutid (&ut);
318 if (up == NULL)
320 error (0, errno, "cannot get entry for type `%d'", type);
321 return 1;
324 endutent ();
326 for (n = 0; n < num_entries; n++)
328 if (type == entry[n].ut_type)
330 if (memcmp (up, &entry[n], sizeof (struct utmp)))
332 error (0, 0, "UTMP entry does not match");
333 return 1;
336 return 0;
340 error (0, 0, "bogus entry for type `%d'", type);
341 return 1;
344 static int
345 do_test (int argc, char *argv[])
347 int result = 0;
349 utmpname (name);
351 result |= do_init ();
352 result |= do_check ();
354 result |= simulate_login ("tty1", "erwin");
355 result |= do_check ();
357 result |= simulate_login ("ttyp1", "paul");
358 result |= do_check ();
360 result |= simulate_logout ("tty2");
361 result |= do_check ();
363 result |= simulate_logout ("ttyp0");
364 result |= do_check ();
366 result |= simulate_login ("ttyp2", "richard");
367 result |= do_check ();
369 result |= check_login ("tty1");
370 result |= check_logout ("ttyp0");
371 result |= check_id ("p1");
372 result |= check_id ("2");
373 result |= check_id ("si");
374 result |= check_type (BOOT_TIME);
375 result |= check_type (RUN_LVL);
377 return result;