]> git.ipfire.org Git - thirdparty/glibc.git/blob - login/tst-utmp.c
iconv, localedef: avoid floating point rounding differences [BZ #24372]
[thirdparty/glibc.git] / login / tst-utmp.c
1 /* Tests for UTMP functions.
2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
5
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.
10
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.
15
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/>. */
19
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>
26
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
40
41
42 #if defined UTMPX || _HAVE_UT_TYPE
43
44 /* Prototype for our test function. */
45 static int do_test (int argc, char *argv[]);
46
47 /* We have a preparation function. */
48 static void do_prepare (int argc, char *argv[]);
49 #define PREPARE do_prepare
50
51 /* This defines the `main' function and some more. */
52 #include <test-skeleton.c>
53
54
55 /* These are for the temporary file we generate. */
56 char *name;
57 int fd;
58
59 static void
60 do_prepare (int argc, char *argv[])
61 {
62 size_t name_len;
63
64 name_len = strlen (test_dir);
65 name = xmalloc (name_len + sizeof ("/utmpXXXXXX"));
66 mempcpy (mempcpy (name, test_dir, name_len),
67 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
68
69 /* Open our test file. */
70 fd = mkstemp (name);
71 if (fd == -1)
72 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
73 add_temp_file (name);
74 }
75
76 struct utmp entry[] =
77 {
78 #if defined UTMPX || _HAVE_UT_TV
79 #define UT(a) .ut_tv = { .tv_sec = (a)}
80 #else
81 #define UT(a) .ut_time = (a)
82 #endif
83
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 }
96 };
97 int num_entries = sizeof entry / sizeof (struct utmp);
98
99 time_t entry_time = 20000;
100 pid_t entry_pid = 234;
101
102 static int
103 do_init (void)
104 {
105 int n;
106
107 setutent ();
108
109 for (n = 0; n < num_entries; n++)
110 {
111 if (pututline (&entry[n]) == NULL)
112 {
113 error (0, errno, "cannot write UTMP entry");
114 return 1;
115 }
116 }
117
118 endutent ();
119
120 return 0;
121 }
122
123
124 static int
125 do_check (void)
126 {
127 struct utmp *ut;
128 int n;
129
130 setutent ();
131
132 n = 0;
133 while ((ut = getutent ()))
134 {
135 if (n < num_entries
136 && memcmp (ut, &entry[n], sizeof (struct utmp)))
137 {
138 error (0, 0, "UTMP entry does not match");
139 return 1;
140 }
141
142 n++;
143 }
144
145 if (n != num_entries)
146 {
147 error (0, 0, "number of UTMP entries is incorrect");
148 return 1;
149 }
150
151 endutent ();
152
153 return 0;
154 }
155
156 static int
157 simulate_login (const char *line, const char *user)
158 {
159 int n;
160
161 for (n = 0; n < num_entries; n++)
162 {
163 if (strcmp (line, entry[n].ut_line) == 0
164 || entry[n].ut_type == DEAD_PROCESS)
165 {
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 defined UTMPX || _HAVE_UT_TV - 0
171 entry[n].ut_tv.tv_sec = (entry_time += 1000);
172 #else
173 entry[n].ut_time = (entry_time += 1000);
174 #endif
175 setutent ();
176
177 if (pututline (&entry[n]) == NULL)
178 {
179 error (0, errno, "cannot write UTMP entry");
180 return 1;
181 }
182
183 endutent ();
184
185 return 0;
186 }
187 }
188
189 error (0, 0, "no entries available");
190 return 1;
191 }
192
193 static int
194 simulate_logout (const char *line)
195 {
196 int n;
197
198 for (n = 0; n < num_entries; n++)
199 {
200 if (strcmp (line, entry[n].ut_line) == 0)
201 {
202 entry[n].ut_type = DEAD_PROCESS;
203 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
204 #if defined UTMPX || _HAVE_UT_TV - 0
205 entry[n].ut_tv.tv_sec = (entry_time += 1000);
206 #else
207 entry[n].ut_time = (entry_time += 1000);
208 #endif
209 setutent ();
210
211 if (pututline (&entry[n]) == NULL)
212 {
213 error (0, errno, "cannot write UTMP entry");
214 return 1;
215 }
216
217 endutent ();
218
219 return 0;
220 }
221 }
222
223 error (0, 0, "no entry found for `%s'", line);
224 return 1;
225 }
226
227 static int
228 check_login (const char *line)
229 {
230 struct utmp *up;
231 struct utmp ut;
232 int n;
233
234 setutent ();
235
236 strcpy (ut.ut_line, line);
237 up = getutline (&ut);
238 if (up == NULL)
239 {
240 error (0, errno, "cannot get entry for line `%s'", line);
241 return 1;
242 }
243
244 endutent ();
245
246 for (n = 0; n < num_entries; n++)
247 {
248 if (strcmp (line, entry[n].ut_line) == 0)
249 {
250 if (memcmp (up, &entry[n], sizeof (struct utmp)))
251 {
252 error (0, 0, "UTMP entry does not match");
253 return 1;
254 }
255
256 return 0;
257 }
258 }
259
260 error (0, 0, "bogus entry for line `%s'", line);
261 return 1;
262 }
263
264 static int
265 check_logout (const char *line)
266 {
267 struct utmp ut;
268
269 setutent ();
270
271 strcpy (ut.ut_line, line);
272 if (getutline (&ut) != NULL)
273 {
274 error (0, 0, "bogus login entry for `%s'", line);
275 return 1;
276 }
277
278 endutent ();
279
280 return 0;
281 }
282
283 static int
284 check_id (const char *id)
285 {
286 struct utmp *up;
287 struct utmp ut;
288 int n;
289
290 setutent ();
291
292 ut.ut_type = USER_PROCESS;
293 strcpy (ut.ut_id, id);
294 up = getutid (&ut);
295 if (up == NULL)
296 {
297 error (0, errno, "cannot get entry for ID `%s'", id);
298 return 1;
299 }
300
301 endutent ();
302
303 for (n = 0; n < num_entries; n++)
304 {
305 if (strcmp (id, entry[n].ut_id) == 0)
306 {
307 if (memcmp (up, &entry[n], sizeof (struct utmp)))
308 {
309 error (0, 0, "UTMP entry does not match");
310 return 1;
311 }
312
313 return 0;
314 }
315 }
316
317 error (0, 0, "bogus entry for ID `%s'", id);
318 return 1;
319 }
320
321 static int
322 check_type (int type)
323 {
324 struct utmp *up;
325 struct utmp ut;
326 int n;
327
328 setutent ();
329
330 ut.ut_type = type;
331 up = getutid (&ut);
332 if (up == NULL)
333 {
334 error (0, errno, "cannot get entry for type `%d'", type);
335 return 1;
336 }
337
338 endutent ();
339
340 for (n = 0; n < num_entries; n++)
341 {
342 if (type == entry[n].ut_type)
343 {
344 if (memcmp (up, &entry[n], sizeof (struct utmp)))
345 {
346 error (0, 0, "UTMP entry does not match");
347 return 1;
348 }
349
350 return 0;
351 }
352 }
353
354 error (0, 0, "bogus entry for type `%d'", type);
355 return 1;
356 }
357
358 static int
359 do_test (int argc, char *argv[])
360 {
361 int result = 0;
362
363 utmpname (name);
364
365 result |= do_init ();
366 result |= do_check ();
367
368 result |= simulate_login ("tty1", "erwin");
369 result |= do_check ();
370
371 result |= simulate_login ("ttyp1", "paul");
372 result |= do_check ();
373
374 result |= simulate_logout ("tty2");
375 result |= do_check ();
376
377 result |= simulate_logout ("ttyp0");
378 result |= do_check ();
379
380 result |= simulate_login ("ttyp2", "richard");
381 result |= do_check ();
382
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);
390
391 return result;
392 }
393
394 #else
395
396 /* No field 'ut_type' in struct utmp. */
397 int
398 main (void)
399 {
400 return 0;
401 }
402
403 #endif