tst-utmp.c 7.4 KB

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