]> sourceware.org Git - glibc.git/blame - login/tst-utmp.c
nss: tst-nss-files-hosts-long: Add host.conf [BZ #21915]
[glibc.git] / login / tst-utmp.c
CommitLineData
7ba4fcfc 1/* Tests for UTMP functions.
04277e02 2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
7ba4fcfc
UD
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
41bdb6e2
AJ
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.
7ba4fcfc
UD
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
41bdb6e2 14 Lesser General Public License for more details.
7ba4fcfc 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
7ba4fcfc
UD
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
ccb729df 42#if defined UTMPX || _HAVE_UT_TYPE
ffa8d2a0 43
7ba4fcfc
UD
44/* Prototype for our test function. */
45static int do_test (int argc, char *argv[]);
46
47/* We have a preparation function. */
48static 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. */
56char *name;
57int fd;
58
59static void
60do_prepare (int argc, char *argv[])
61{
62 size_t name_len;
63
64 name_len = strlen (test_dir);
850c6760 65 name = xmalloc (name_len + sizeof ("/utmpXXXXXX"));
7ba4fcfc
UD
66 mempcpy (mempcpy (name, test_dir, name_len),
67 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
7ba4fcfc
UD
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);
cf145565 73 add_temp_file (name);
7ba4fcfc
UD
74}
75
7ba4fcfc
UD
76struct utmp entry[] =
77{
ccb729df 78#if defined UTMPX || _HAVE_UT_TV
b0c9067d 79#define UT(a) .ut_tv = { .tv_sec = (a)}
46894673 80#else
b0c9067d 81#define UT(a) .ut_time = (a)
46894673
UD
82#endif
83
b0c9067d
UD
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 }
7ba4fcfc
UD
96};
97int num_entries = sizeof entry / sizeof (struct utmp);
98
99time_t entry_time = 20000;
100pid_t entry_pid = 234;
101
102static int
103do_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
124static int
125do_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
156static int
157simulate_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;
ffa8d2a0 169 strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
ccb729df 170#if defined UTMPX || _HAVE_UT_TV - 0
7ba4fcfc 171 entry[n].ut_tv.tv_sec = (entry_time += 1000);
46894673
UD
172#else
173 entry[n].ut_time = (entry_time += 1000);
174#endif
7ba4fcfc
UD
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
193static int
194simulate_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;
ffa8d2a0 203 strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
ccb729df 204#if defined UTMPX || _HAVE_UT_TV - 0
46894673
UD
205 entry[n].ut_tv.tv_sec = (entry_time += 1000);
206#else
207 entry[n].ut_time = (entry_time += 1000);
208#endif
7ba4fcfc
UD
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
227static int
228check_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
264static int
265check_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
283static int
284check_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
321static int
322check_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
358static int
359do_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}
ffa8d2a0
RM
393
394#else
395
396/* No field 'ut_type' in struct utmp. */
397int
60d2f8f3 398main (void)
ffa8d2a0
RM
399{
400 return 0;
401}
402
403#endif
This page took 0.50441 seconds and 5 git commands to generate.