tst-utmp.c 8.1 KB

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