tst-utmp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Tests for UTMP functions.
  2. Copyright (C) 1998, 2001-2003 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. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <time.h>
  21. #ifdef UTMPX
  22. # include <utmpx.h>
  23. # define utmp utmpx
  24. # define utmpname utmpxname
  25. # define setutent setutxent
  26. # define getutent getutxent
  27. # define endutent endutxent
  28. # define getutline getutxline
  29. # define getutid getutxid
  30. # define pututline pututxline
  31. #else
  32. # include <utmp.h>
  33. #endif
  34. #if _HAVE_UT_TYPE || defined UTMPX
  35. /* Prototype for our test function. */
  36. static int do_test (int argc, char *argv[]);
  37. /* We have a preparation function. */
  38. static void do_prepare (int argc, char *argv[]);
  39. #define PREPARE do_prepare
  40. /* This defines the `main' function and some more. */
  41. #include "../test-skeleton.c"
  42. /* These are for the temporary file we generate. */
  43. char *name;
  44. int fd;
  45. static void
  46. do_prepare (int argc, char *argv[])
  47. {
  48. size_t name_len;
  49. name_len = strlen (test_dir);
  50. name = malloc (name_len + sizeof ("/utmpXXXXXX"));
  51. mempcpy (mempcpy (name, test_dir, name_len),
  52. "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
  53. add_temp_file (name);
  54. /* Open our test file. */
  55. fd = mkstemp (name);
  56. if (fd == -1) {
  57. fprintf (stderr, "cannot open test file `%s': ", name);
  58. perror (NULL);
  59. exit (EXIT_FAILURE);
  60. }
  61. }
  62. struct utmp entry[] =
  63. {
  64. #if _HAVE_UT_TV || defined UTMPX
  65. #define UT(a) .ut_tv = { .tv_sec = (a)}
  66. #else
  67. #define UT(a) .ut_time = (a)
  68. #endif
  69. { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) },
  70. { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) },
  71. { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) },
  72. { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1",
  73. .ut_user = "LOGIN", UT(4000) },
  74. { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2",
  75. .ut_user = "albert", UT(8000) },
  76. { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0",
  77. .ut_user = "niels", UT(10000) },
  78. { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) },
  79. { .ut_type = EMPTY },
  80. { .ut_type = EMPTY }
  81. };
  82. int num_entries = sizeof entry / sizeof (struct utmp);
  83. time_t entry_time = 20000;
  84. pid_t entry_pid = 234;
  85. static int
  86. do_init (void)
  87. {
  88. int n;
  89. setutent ();
  90. for (n = 0; n < num_entries; n++)
  91. {
  92. if (pututline (&entry[n]) == NULL)
  93. {
  94. perror ("cannot write UTMP entry");
  95. return 1;
  96. }
  97. }
  98. endutent ();
  99. return 0;
  100. }
  101. static int
  102. do_check (void)
  103. {
  104. struct utmp *ut;
  105. int n;
  106. setutent ();
  107. n = 0;
  108. while ((ut = getutent ()))
  109. {
  110. if (n < num_entries &&
  111. memcmp (ut, &entry[n], sizeof (struct utmp)))
  112. {
  113. fprintf (stderr, "UTMP entry does not match\n");
  114. return 1;
  115. }
  116. n++;
  117. }
  118. if (n != num_entries)
  119. {
  120. fprintf (stderr, "number of UTMP entries is incorrect\n");
  121. return 1;
  122. }
  123. endutent ();
  124. return 0;
  125. }
  126. static int
  127. simulate_login (const char *line, const char *user)
  128. {
  129. int n;
  130. for (n = 0; n < num_entries; n++)
  131. {
  132. if (strcmp (line, entry[n].ut_line) == 0 ||
  133. entry[n].ut_type == DEAD_PROCESS)
  134. {
  135. if (entry[n].ut_pid == DEAD_PROCESS)
  136. entry[n].ut_pid = (entry_pid += 27);
  137. entry[n].ut_type = USER_PROCESS;
  138. strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user));
  139. #if _HAVE_UT_TV - 0 || defined UTMPX
  140. entry[n].ut_tv.tv_sec = (entry_time += 1000);
  141. #else
  142. entry[n].ut_time = (entry_time += 1000);
  143. #endif
  144. setutent ();
  145. if (pututline (&entry[n]) == NULL)
  146. {
  147. perror ("cannot write UTMP entry");
  148. return 1;
  149. }
  150. endutent ();
  151. return 0;
  152. }
  153. }
  154. fprintf (stderr, "no entries available\n");
  155. return 1;
  156. }
  157. static int
  158. simulate_logout (const char *line)
  159. {
  160. int n;
  161. for (n = 0; n < num_entries; n++)
  162. {
  163. if (strcmp (line, entry[n].ut_line) == 0)
  164. {
  165. entry[n].ut_type = DEAD_PROCESS;
  166. strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user));
  167. #if _HAVE_UT_TV - 0 || defined UTMPX
  168. entry[n].ut_tv.tv_sec = (entry_time += 1000);
  169. #else
  170. entry[n].ut_time = (entry_time += 1000);
  171. #endif
  172. setutent ();
  173. if (pututline (&entry[n]) == NULL)
  174. {
  175. perror ("cannot write UTMP entry");
  176. return 1;
  177. }
  178. endutent ();
  179. return 0;
  180. }
  181. }
  182. fprintf (stderr, "no entry found for `%s'\n", line);
  183. return 1;
  184. }
  185. static int
  186. check_login (const char *line)
  187. {
  188. struct utmp *up;
  189. struct utmp ut;
  190. int n;
  191. setutent ();
  192. strcpy (ut.ut_line, line);
  193. up = getutline (&ut);
  194. if (up == NULL)
  195. {
  196. fprintf (stderr, "cannot get entry for line `%s': ", line);
  197. perror(NULL);
  198. return 1;
  199. }
  200. endutent ();
  201. for (n = 0; n < num_entries; n++)
  202. {
  203. if (strcmp (line, entry[n].ut_line) == 0)
  204. {
  205. if (memcmp (up, &entry[n], sizeof (struct utmp)))
  206. {
  207. fprintf (stderr, "UTMP entry does not match\n");
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. }
  213. fprintf (stderr, "bogus entry for line `%s'\n", line);
  214. return 1;
  215. }
  216. static int
  217. check_logout (const char *line)
  218. {
  219. struct utmp ut;
  220. setutent ();
  221. strcpy (ut.ut_line, line);
  222. if (getutline (&ut) != NULL)
  223. {
  224. fprintf (stderr, "bogus login entry for `%s'\n", line);
  225. return 1;
  226. }
  227. endutent ();
  228. return 0;
  229. }
  230. static int
  231. check_id (const char *id)
  232. {
  233. struct utmp *up;
  234. struct utmp ut;
  235. int n;
  236. setutent ();
  237. ut.ut_type = USER_PROCESS;
  238. strcpy (ut.ut_id, id);
  239. up = getutid (&ut);
  240. if (up == NULL)
  241. {
  242. fprintf (stderr, "cannot get entry for ID `%s': ", id);
  243. perror (NULL);
  244. return 1;
  245. }
  246. endutent ();
  247. for (n = 0; n < num_entries; n++)
  248. {
  249. if (strcmp (id, entry[n].ut_id) == 0)
  250. {
  251. if (memcmp (up, &entry[n], sizeof (struct utmp)))
  252. {
  253. fprintf (stderr, "UTMP entry does not match\n");
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. }
  259. fprintf (stderr, "bogus entry for ID `%s'\n", id);
  260. return 1;
  261. }
  262. static int
  263. check_type (int type)
  264. {
  265. struct utmp *up;
  266. struct utmp ut;
  267. int n;
  268. setutent ();
  269. ut.ut_type = type;
  270. up = getutid (&ut);
  271. if (up == NULL)
  272. {
  273. fprintf (stderr, "cannot get entry for type `%d': ", type);
  274. perror (NULL);
  275. return 1;
  276. }
  277. endutent ();
  278. for (n = 0; n < num_entries; n++)
  279. {
  280. if (type == entry[n].ut_type)
  281. {
  282. if (memcmp (up, &entry[n], sizeof (struct utmp)))
  283. {
  284. fprintf (stderr, "UTMP entry does not match\n");
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. }
  290. fprintf (stderr, "bogus entry for type `%d'\n", type);
  291. return 1;
  292. }
  293. static int
  294. do_test (int argc, char *argv[])
  295. {
  296. int result = 0;
  297. utmpname (name);
  298. result |= do_init ();
  299. result |= do_check ();
  300. result |= simulate_login ("tty1", "erwin");
  301. result |= do_check ();
  302. result |= simulate_login ("ttyp1", "paul");
  303. result |= do_check ();
  304. result |= simulate_logout ("tty2");
  305. result |= do_check ();
  306. result |= simulate_logout ("ttyp0");
  307. result |= do_check ();
  308. result |= simulate_login ("ttyp2", "richard");
  309. result |= do_check ();
  310. result |= check_login ("tty1");
  311. result |= check_logout ("ttyp0");
  312. result |= check_id ("p1");
  313. result |= check_id ("2");
  314. result |= check_id ("si");
  315. result |= check_type (BOOT_TIME);
  316. result |= check_type (RUN_LVL);
  317. return result;
  318. }
  319. #else
  320. /* No field 'ut_type' in struct utmp. */
  321. int
  322. main ()
  323. {
  324. return 0;
  325. }
  326. #endif