(atomic_add): Don't allow address register for operand 0.
[glibc.git] / login / tst-utmp.c
blob4b922e6386bada6228ed6cfba8165b02625f8865
1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <error.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <time.h>
28 #ifdef UTMPX
29 # include <utmpx.h>
30 # define utmp utmpx
31 # define utmpname utmpxname
32 # define setutent setutxent
33 # define getutent getutxent
34 # define endutent endutxent
35 # define getutline getutxline
36 # define getutid getutxid
37 # define pututline pututxline
38 #else
39 # include <utmp.h>
40 #endif
43 /* Prototype for our test function. */
44 static int do_test (int argc, char *argv[]);
46 /* We have a preparation function. */
47 static void do_prepare (int argc, char *argv[]);
48 #define PREPARE do_prepare
50 /* This defines the `main' function and some more. */
51 #include <test-skeleton.c>
54 /* These are for the temporary file we generate. */
55 char *name;
56 int fd;
58 static void
59 do_prepare (int argc, char *argv[])
61 size_t name_len;
63 name_len = strlen (test_dir);
64 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
65 mempcpy (mempcpy (name, test_dir, name_len),
66 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
67 add_temp_file (name);
69 /* Open our test file. */
70 fd = mkstemp (name);
71 if (fd == -1)
72 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
75 struct utmp entry[] =
77 #if _HAVE_UT_TV || defined UTMPX
78 #define UT(a) ut_tv:{tv_sec:(a)}
79 #else
80 #define UT(a) ut_time:(a)
81 #endif
83 { ut_type: BOOT_TIME, ut_pid: 1, UT(1000) },
84 { ut_type: RUN_LVL, ut_pid: 1, UT(2000) },
85 { ut_type: INIT_PROCESS, ut_pid: 5, ut_id: "si", UT(3000) },
86 { ut_type: LOGIN_PROCESS, ut_pid: 23, ut_line: "tty1", ut_id: "1",
87 ut_user: "LOGIN", UT(4000) },
88 { ut_type: USER_PROCESS, ut_pid: 24, ut_line: "tty2", ut_id: "2",
89 ut_user: "albert", UT(8000) },
90 { ut_type: USER_PROCESS, ut_pid: 196, ut_line: "ttyp0", ut_id: "p0",
91 ut_user: "niels", UT(10000) },
92 { ut_type: DEAD_PROCESS, ut_line: "ttyp1", ut_id: "p1", UT(16000) },
93 { ut_type: EMPTY },
94 { ut_type: EMPTY }
96 int num_entries = sizeof entry / sizeof (struct utmp);
98 time_t entry_time = 20000;
99 pid_t entry_pid = 234;
101 static int
102 do_init (void)
104 int n;
106 setutent ();
108 for (n = 0; n < num_entries; n++)
110 if (pututline (&entry[n]) == NULL)
112 error (0, errno, "cannot write UTMP entry");
113 return 1;
117 endutent ();
119 return 0;
123 static int
124 do_check (void)
126 struct utmp *ut;
127 int n;
129 setutent ();
131 n = 0;
132 while ((ut = getutent ()))
134 if (n < num_entries &&
135 memcmp (ut, &entry[n], sizeof (struct utmp)))
137 error (0, 0, "UTMP entry does not match");
138 return 1;
141 n++;
144 if (n != num_entries)
146 error (0, 0, "number of UTMP entries is incorrect");
147 return 1;
150 endutent ();
152 return 0;
155 static int
156 simulate_login (const char *line, const char *user)
158 int n;
160 for (n = 0; n < num_entries; n++)
162 if (strcmp (line, entry[n].ut_line) == 0 ||
163 entry[n].ut_type == DEAD_PROCESS)
165 if (entry[n].ut_pid == DEAD_PROCESS)
166 entry[n].ut_pid = (entry_pid += 27);
167 entry[n].ut_type = USER_PROCESS;
168 strcpy (entry[n].ut_user, user);
169 #if _HAVE_UT_TV - 0 || defined UTMPX
170 entry[n].ut_tv.tv_sec = (entry_time += 1000);
171 #else
172 entry[n].ut_time = (entry_time += 1000);
173 #endif
174 setutent ();
176 if (pututline (&entry[n]) == NULL)
178 error (0, errno, "cannot write UTMP entry");
179 return 1;
182 endutent ();
184 return 0;
188 error (0, 0, "no entries available");
189 return 1;
192 static int
193 simulate_logout (const char *line)
195 int n;
197 for (n = 0; n < num_entries; n++)
199 if (strcmp (line, entry[n].ut_line) == 0)
201 entry[n].ut_type = DEAD_PROCESS;
202 entry[n].ut_user[0] = '\0';
203 #if _HAVE_UT_TV - 0 || defined UTMPX
204 entry[n].ut_tv.tv_sec = (entry_time += 1000);
205 #else
206 entry[n].ut_time = (entry_time += 1000);
207 #endif
208 setutent ();
210 if (pututline (&entry[n]) == NULL)
212 error (0, errno, "cannot write UTMP entry");
213 return 1;
216 endutent ();
218 return 0;
222 error (0, 0, "no entry found for `%s'", line);
223 return 1;
226 static int
227 check_login (const char *line)
229 struct utmp *up;
230 struct utmp ut;
231 int n;
233 setutent ();
235 strcpy (ut.ut_line, line);
236 up = getutline (&ut);
237 if (up == NULL)
239 error (0, errno, "cannot get entry for line `%s'", line);
240 return 1;
243 endutent ();
245 for (n = 0; n < num_entries; n++)
247 if (strcmp (line, entry[n].ut_line) == 0)
249 if (memcmp (up, &entry[n], sizeof (struct utmp)))
251 error (0, 0, "UTMP entry does not match");
252 return 1;
255 return 0;
259 error (0, 0, "bogus entry for line `%s'", line);
260 return 1;
263 static int
264 check_logout (const char *line)
266 struct utmp ut;
268 setutent ();
270 strcpy (ut.ut_line, line);
271 if (getutline (&ut) != NULL)
273 error (0, 0, "bogus login entry for `%s'", line);
274 return 1;
277 endutent ();
279 return 0;
282 static int
283 check_id (const char *id)
285 struct utmp *up;
286 struct utmp ut;
287 int n;
289 setutent ();
291 ut.ut_type = USER_PROCESS;
292 strcpy (ut.ut_id, id);
293 up = getutid (&ut);
294 if (up == NULL)
296 error (0, errno, "cannot get entry for ID `%s'", id);
297 return 1;
300 endutent ();
302 for (n = 0; n < num_entries; n++)
304 if (strcmp (id, entry[n].ut_id) == 0)
306 if (memcmp (up, &entry[n], sizeof (struct utmp)))
308 error (0, 0, "UTMP entry does not match");
309 return 1;
312 return 0;
316 error (0, 0, "bogus entry for ID `%s'", id);
317 return 1;
320 static int
321 check_type (int type)
323 struct utmp *up;
324 struct utmp ut;
325 int n;
327 setutent ();
329 ut.ut_type = type;
330 up = getutid (&ut);
331 if (up == NULL)
333 error (0, errno, "cannot get entry for type `%d'", type);
334 return 1;
337 endutent ();
339 for (n = 0; n < num_entries; n++)
341 if (type == entry[n].ut_type)
343 if (memcmp (up, &entry[n], sizeof (struct utmp)))
345 error (0, 0, "UTMP entry does not match");
346 return 1;
349 return 0;
353 error (0, 0, "bogus entry for type `%d'", type);
354 return 1;
357 static int
358 do_test (int argc, char *argv[])
360 int result = 0;
362 utmpname (name);
364 result |= do_init ();
365 result |= do_check ();
367 result |= simulate_login ("tty1", "erwin");
368 result |= do_check ();
370 result |= simulate_login ("ttyp1", "paul");
371 result |= do_check ();
373 result |= simulate_logout ("tty2");
374 result |= do_check ();
376 result |= simulate_logout ("ttyp0");
377 result |= do_check ();
379 result |= simulate_login ("ttyp2", "richard");
380 result |= do_check ();
382 result |= check_login ("tty1");
383 result |= check_logout ("ttyp0");
384 result |= check_id ("p1");
385 result |= check_id ("2");
386 result |= check_id ("si");
387 result |= check_type (BOOT_TIME);
388 result |= check_type (RUN_LVL);
390 return result;