tst-utmp.c 7.8 KB

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