]> sourceware.org Git - glibc.git/blob - login/tst-utmp.c
Update.
[glibc.git] / login / tst-utmp.c
1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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);
63 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
64 mempcpy (mempcpy (name, test_dir, name_len),
65 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
66 add_temp_file (name);
67
68 /* Open our test file. */
69 fd = mkstemp (name);
70 if (fd == -1)
71 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
72 }
73
74 struct utmp entry[] =
75 {
76 #if _HAVE_UT_TV || defined UTMPX
77 #define UT(a) ut_tv:{tv_sec:(a)}
78 #else
79 #define UT(a) ut_time:(a)
80 #endif
81
82 { ut_type: BOOT_TIME, ut_pid: 1, UT(1000) },
83 { ut_type: RUN_LVL, ut_pid: 1, UT(2000) },
84 { ut_type: INIT_PROCESS, ut_pid: 5, ut_id: "si", UT(3000) },
85 { ut_type: LOGIN_PROCESS, ut_pid: 23, ut_line: "tty1", ut_id: "1",
86 ut_user: "LOGIN", UT(4000) },
87 { ut_type: USER_PROCESS, ut_pid: 24, ut_line: "tty2", ut_id: "2",
88 ut_user: "albert", UT(8000) },
89 { ut_type: USER_PROCESS, ut_pid: 196, ut_line: "ttyp0", ut_id: "p0",
90 ut_user: "niels", UT(10000) },
91 { ut_type: DEAD_PROCESS, ut_line: "ttyp1", ut_id: "p1", UT(16000) },
92 { ut_type: EMPTY },
93 { ut_type: EMPTY }
94 };
95 int num_entries = sizeof entry / sizeof (struct utmp);
96
97 time_t entry_time = 20000;
98 pid_t entry_pid = 234;
99
100 static int
101 do_init (void)
102 {
103 int n;
104
105 setutent ();
106
107 for (n = 0; n < num_entries; n++)
108 {
109 if (pututline (&entry[n]) == NULL)
110 {
111 error (0, errno, "cannot write UTMP entry");
112 return 1;
113 }
114 }
115
116 endutent ();
117
118 return 0;
119 }
120
121
122 static int
123 do_check (void)
124 {
125 struct utmp *ut;
126 int n;
127
128 setutent ();
129
130 n = 0;
131 while ((ut = getutent ()))
132 {
133 if (n < num_entries &&
134 memcmp (ut, &entry[n], sizeof (struct utmp)))
135 {
136 error (0, 0, "UTMP entry does not match");
137 return 1;
138 }
139
140 n++;
141 }
142
143 if (n != num_entries)
144 {
145 error (0, 0, "number of UTMP entries is incorrect");
146 return 1;
147 }
148
149 endutent ();
150
151 return 0;
152 }
153
154 static int
155 simulate_login (const char *line, const char *user)
156 {
157 int n;
158
159 for (n = 0; n < num_entries; n++)
160 {
161 if (strcmp (line, entry[n].ut_line) == 0 ||
162 entry[n].ut_type == DEAD_PROCESS)
163 {
164 if (entry[n].ut_pid == DEAD_PROCESS)
165 entry[n].ut_pid = (entry_pid += 27);
166 entry[n].ut_type = USER_PROCESS;
167 strcpy (entry[n].ut_user, user);
168 #if _HAVE_UT_TV - 0 || defined UTMPX
169 entry[n].ut_tv.tv_sec = (entry_time += 1000);
170 #else
171 entry[n].ut_time = (entry_time += 1000);
172 #endif
173 setutent ();
174
175 if (pututline (&entry[n]) == NULL)
176 {
177 error (0, errno, "cannot write UTMP entry");
178 return 1;
179 }
180
181 endutent ();
182
183 return 0;
184 }
185 }
186
187 error (0, 0, "no entries available");
188 return 1;
189 }
190
191 static int
192 simulate_logout (const char *line)
193 {
194 int n;
195
196 for (n = 0; n < num_entries; n++)
197 {
198 if (strcmp (line, entry[n].ut_line) == 0)
199 {
200 entry[n].ut_type = DEAD_PROCESS;
201 entry[n].ut_user[0] = '\0';
202 #if _HAVE_UT_TV - 0 || defined UTMPX
203 entry[n].ut_tv.tv_sec = (entry_time += 1000);
204 #else
205 entry[n].ut_time = (entry_time += 1000);
206 #endif
207 setutent ();
208
209 if (pututline (&entry[n]) == NULL)
210 {
211 error (0, errno, "cannot write UTMP entry");
212 return 1;
213 }
214
215 endutent ();
216
217 return 0;
218 }
219 }
220
221 error (0, 0, "no entry found for `%s'", line);
222 return 1;
223 }
224
225 static int
226 check_login (const char *line)
227 {
228 struct utmp *up;
229 struct utmp ut;
230 int n;
231
232 setutent ();
233
234 strcpy (ut.ut_line, line);
235 up = getutline (&ut);
236 if (up == NULL)
237 {
238 error (0, errno, "cannot get entry for line `%s'", line);
239 return 1;
240 }
241
242 endutent ();
243
244 for (n = 0; n < num_entries; n++)
245 {
246 if (strcmp (line, entry[n].ut_line) == 0)
247 {
248 if (memcmp (up, &entry[n], sizeof (struct utmp)))
249 {
250 error (0, 0, "UTMP entry does not match");
251 return 1;
252 }
253
254 return 0;
255 }
256 }
257
258 error (0, 0, "bogus entry for line `%s'", line);
259 return 1;
260 }
261
262 static int
263 check_logout (const char *line)
264 {
265 struct utmp ut;
266
267 setutent ();
268
269 strcpy (ut.ut_line, line);
270 if (getutline (&ut) != NULL)
271 {
272 error (0, 0, "bogus login entry for `%s'", line);
273 return 1;
274 }
275
276 endutent ();
277
278 return 0;
279 }
280
281 static int
282 check_id (const char *id)
283 {
284 struct utmp *up;
285 struct utmp ut;
286 int n;
287
288 setutent ();
289
290 ut.ut_type = USER_PROCESS;
291 strcpy (ut.ut_id, id);
292 up = getutid (&ut);
293 if (up == NULL)
294 {
295 error (0, errno, "cannot get entry for ID `%s'", id);
296 return 1;
297 }
298
299 endutent ();
300
301 for (n = 0; n < num_entries; n++)
302 {
303 if (strcmp (id, entry[n].ut_id) == 0)
304 {
305 if (memcmp (up, &entry[n], sizeof (struct utmp)))
306 {
307 error (0, 0, "UTMP entry does not match");
308 return 1;
309 }
310
311 return 0;
312 }
313 }
314
315 error (0, 0, "bogus entry for ID `%s'", id);
316 return 1;
317 }
318
319 static int
320 check_type (int type)
321 {
322 struct utmp *up;
323 struct utmp ut;
324 int n;
325
326 setutent ();
327
328 ut.ut_type = type;
329 up = getutid (&ut);
330 if (up == NULL)
331 {
332 error (0, errno, "cannot get entry for type `%d'", type);
333 return 1;
334 }
335
336 endutent ();
337
338 for (n = 0; n < num_entries; n++)
339 {
340 if (type == entry[n].ut_type)
341 {
342 if (memcmp (up, &entry[n], sizeof (struct utmp)))
343 {
344 error (0, 0, "UTMP entry does not match");
345 return 1;
346 }
347
348 return 0;
349 }
350 }
351
352 error (0, 0, "bogus entry for type `%d'", type);
353 return 1;
354 }
355
356 static int
357 do_test (int argc, char *argv[])
358 {
359 int result = 0;
360
361 utmpname (name);
362
363 result |= do_init ();
364 result |= do_check ();
365
366 result |= simulate_login ("tty1", "erwin");
367 result |= do_check ();
368
369 result |= simulate_login ("ttyp1", "paul");
370 result |= do_check ();
371
372 result |= simulate_logout ("tty2");
373 result |= do_check ();
374
375 result |= simulate_logout ("ttyp0");
376 result |= do_check ();
377
378 result |= simulate_login ("ttyp2", "richard");
379 result |= do_check ();
380
381 result |= check_login ("tty1");
382 result |= check_logout ("ttyp0");
383 result |= check_id ("p1");
384 result |= check_id ("2");
385 result |= check_id ("si");
386 result |= check_type (BOOT_TIME);
387 result |= check_type (RUN_LVL);
388
389 return result;
390 }
This page took 0.057669 seconds and 6 git commands to generate.