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