utmp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. utmp.h
  9. Abstract:
  10. This header contains older definitions for the user accounting database.
  11. Author:
  12. Evan Green 4-Mar-2015
  13. --*/
  14. #ifndef _UTMP_H
  15. #define _UTMP_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <paths.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //
  29. // Define file paths.
  30. //
  31. #define UTMP_FILE _PATH_UTMP
  32. #define UTMP_FILENAME _PATH_UTMP
  33. #define WTMP_FILE _PATH_WTMP
  34. #define WTMP_FILENAME _PATH_WTMP
  35. //
  36. // Define the sizes of various arrays.
  37. //
  38. #define UT_LINESIZE 32
  39. #define UT_NAMESIZE 32
  40. #define UT_HOSTSIZE 256
  41. //
  42. // Define values for the type field of a utmp structure. Note that these are
  43. // the same values as are in utmpx.h.
  44. //
  45. //
  46. // Empty: No valid user accounting information.
  47. //
  48. #define EMPTY 0
  49. //
  50. // Identifies a change in system run-level
  51. //
  52. #define RUN_LVL 1
  53. //
  54. // Identifies the time of system boot
  55. //
  56. #define BOOT_TIME 2
  57. //
  58. // Identifies time after the system clock changed
  59. //
  60. #define NEW_TIME 3
  61. //
  62. // Identifies time when the system clock changed
  63. //
  64. #define OLD_TIME 4
  65. //
  66. // Identifies a process spawned by the init process
  67. //
  68. #define INIT_PROCESS 5
  69. //
  70. // Identifies the session leader of a logged in user
  71. //
  72. #define LOGIN_PROCESS 6
  73. //
  74. // Identifies a normal user process
  75. //
  76. #define USER_PROCESS 7
  77. //
  78. // Identifies a session leader who has exited.
  79. //
  80. #define DEAD_PROCESS 8
  81. //
  82. // Define some compatibility member names of the utmp structure.
  83. //
  84. #define ut_name ut_user
  85. #define ut_time ut_tv.tv_sec
  86. #define ut_xtime ut_tv.tv_sec
  87. #define ut_addr ut_addr_v6[0]
  88. //
  89. // ------------------------------------------------------ Data Type Definitions
  90. //
  91. /*++
  92. Structure Description:
  93. This structure defines the format of the database that stores entries of
  94. previous logins.
  95. Members:
  96. ll_time - Stores the time of last login.
  97. ll_line - Stores the terminal the login occurred under.
  98. ll_host - Stores the host name that last logged in.
  99. --*/
  100. struct lastlog {
  101. time_t ll_time;
  102. char ll_line[UT_LINESIZE];
  103. char ll_host[UT_HOSTSIZE];
  104. };
  105. /*++
  106. Structure Description:
  107. This structure defines the exit status code in a utmp structure.
  108. Members:
  109. e_termination - Stores the process termination status.
  110. e_exit - Stores the process exit status.
  111. --*/
  112. struct exit_status {
  113. short int e_termination;
  114. short int e_exit;
  115. };
  116. /*++
  117. Structure Description:
  118. This structure defines the format of the user accounting database entries.
  119. Note that this is exactly the same as the utmpx structure.
  120. Members:
  121. ut_type - Stores the type of entry.
  122. ut_pid - Stores the process ID of the entry.
  123. ut_line - Stores the device name.
  124. ut_id - Stores the inittab ID.
  125. ut_user - Stores the user name.
  126. ut_host - Stores the host name.
  127. ut_exit - Stores the process exit status.
  128. ut_session - Stores the session ID.
  129. ut_tv - Stores the timestamp of the entry.
  130. ut_addr_v6 - Stores the Internet address of the remote host.
  131. --*/
  132. struct utmp {
  133. short int ut_type;
  134. pid_t ut_pid;
  135. char ut_line[UT_LINESIZE];
  136. char ut_id[4];
  137. char ut_user[UT_NAMESIZE];
  138. char ut_host[UT_HOSTSIZE];
  139. struct exit_status ut_exit;
  140. long int ut_session;
  141. struct timeval ut_tv;
  142. int32_t ut_addr_v6[4];
  143. char __ut_reserved[32];
  144. };
  145. //
  146. // -------------------------------------------------------------------- Globals
  147. //
  148. //
  149. // -------------------------------------------------------- Function Prototypes
  150. //
  151. LIBC_API
  152. void
  153. setutent (
  154. void
  155. );
  156. /*++
  157. Routine Description:
  158. This routine resets the current pointer into the user database back to the
  159. beginning. This function is neither thread-safe nor reentrant. This
  160. function is equivalent to setutxent, and new applications should use that
  161. function.
  162. Arguments:
  163. None.
  164. Return Value:
  165. None.
  166. --*/
  167. LIBC_API
  168. void
  169. endutent (
  170. void
  171. );
  172. /*++
  173. Routine Description:
  174. This routine closes the user accounting database. This function is neither
  175. thread-safe nor reentrant. This function is equivalent to endutxent, and
  176. new applications should use that function.
  177. Arguments:
  178. None.
  179. Return Value:
  180. None.
  181. --*/
  182. LIBC_API
  183. struct utmp *
  184. getutent (
  185. void
  186. );
  187. /*++
  188. Routine Description:
  189. This routine returns the next entry in the user accounting database. If
  190. the database is not already open, it will open it. If it reaches the end
  191. of the database, it fails. This function is neither thread-safe nor
  192. reentrant. Since utmp and utmpx structures are the same, this function is
  193. equivalent to getutxent, and new applications should use that function.
  194. Arguments:
  195. None.
  196. Return Value:
  197. Returns a pointer to a copy of the user accounting information on success.
  198. NULL on failure, and errno may be set on error.
  199. --*/
  200. LIBC_API
  201. struct utmp *
  202. getutid (
  203. const struct utmp *Id
  204. );
  205. /*++
  206. Routine Description:
  207. This routine searches forward from the current point in the user accounting
  208. database. If the ut_type value of the supplied utmp structure is
  209. BOOT_TIME, OLD_TIME, or NEW_TIME, then it stops when it finds an entry with
  210. a matching ut_type value. If the ut_type is INIT_PROCESS, USER_PROCESS, or
  211. DEAD_PROCESS, it stops when it finds an entry whose type is one of these
  212. four and whose ut_id matches the one in the given structure. If the end of
  213. the database is reached without a match, the routine shall fail. This
  214. function is neither thread-safe nor reentrant. Since utmp and utmpx
  215. structures are the same, this function is equivalent to getutxent, and new
  216. applications should use that function.
  217. Arguments:
  218. Id - Supplies a pointer to a structure containing the type and possibly
  219. user ID to match on.
  220. Return Value:
  221. Returns a pointer to a copy of the user accounting information on success.
  222. NULL on failure, and errno may be set on error.
  223. --*/
  224. LIBC_API
  225. struct utmp *
  226. getutline (
  227. const struct utmp *Line
  228. );
  229. /*++
  230. Routine Description:
  231. This routine searches forward from the current point in the user accounting
  232. database, looking for an entry of type LOGIN_PROCESS or USER_PROCESS which
  233. also matches the ut_line value in the given structure. If the end of the
  234. database is reached without a match, the routine shall fail. This function
  235. is neither thread-safe nor reentrant.
  236. This function may cache data, so to search for multiple occurrences it is
  237. important to zero out the static data (the return value from the previous
  238. result). Otherwise, the same result may be returned infinitely.
  239. Since utmp and utmpx structures are the same, this function is equivalent
  240. to getutxline, and new applications should use that function.
  241. Arguments:
  242. Line - Supplies a pointer to a structure containing the line to match.
  243. Return Value:
  244. Returns a pointer to a copy of the user accounting information on success.
  245. NULL on failure, and errno may be set on error.
  246. --*/
  247. LIBC_API
  248. struct utmp *
  249. pututline (
  250. const struct utmp *Value
  251. );
  252. /*++
  253. Routine Description:
  254. This routine writes out the structure to the user accounting database. It
  255. shall use getutxid to search for a record that satisfies the request. If
  256. the search succeeds, then the entry will be replaced. Otherwise, a new
  257. entry is made at the end of the user accounting database. The caller must
  258. have sufficient privileges. The implicit read done by this function if it
  259. finds it is not already at the correct place shall not modify the static
  260. structure passed as a return of the other utx functions (so the application
  261. may use that space to write back a modified value). This function is
  262. neither thread-safe nor reentrant. Since utmp and utmpx structures are the
  263. same, this function is equivalent to getutxline, and new applications
  264. should use that function.
  265. Arguments:
  266. Value - Supplies a pointer to a structure containing the new data.
  267. Return Value:
  268. Returns a pointer to a copy of the written user accounting information on
  269. success.
  270. NULL on failure, and errno may be set on error.
  271. --*/
  272. LIBC_API
  273. int
  274. utmpname (
  275. const char *FilePath
  276. );
  277. /*++
  278. Routine Description:
  279. This routine updates the file path that utmp* functions open and access.
  280. This must be called before those routines open the file. This routine does
  281. not check to ensure the file exists. This routine is neither thread-safe
  282. nor reentrant. This routine is equivalent to utmpxname, and new
  283. applications should call that function.
  284. Arguments:
  285. FilePath - Supplies a pointer to the new file path. A copy of this string
  286. will be made.
  287. Return Value:
  288. 0 on success.
  289. -1 on failure, and errno will be set to contain more information.
  290. --*/
  291. LIBC_API
  292. int
  293. updwtmp (
  294. const char *FileName,
  295. const struct utmp *Record
  296. );
  297. /*++
  298. Routine Description:
  299. This routine adds an entry into the wtmp user database.
  300. Arguments:
  301. FileName - Supplies a pointer to path of the wtmp file to open. Set this to
  302. WTMP_FILE by default.
  303. Record - Supplies a pointer to the record to append.
  304. Return Value:
  305. 0 on success.
  306. -1 on failure, and errno will be set to contain more information.
  307. --*/
  308. LIBC_API
  309. int
  310. login_tty (
  311. int TerminalDescriptor
  312. );
  313. /*++
  314. Routine Description:
  315. This routine prepares for a login on the given terminal. It creates a new
  316. session, makes the given terminal descriptor the controlling terminal for
  317. the session, sets the terminal as standard input, output, and error, and
  318. closes the given descriptor.
  319. Arguments:
  320. TerminalDescriptor - Supplies the file descriptor of the terminal to start
  321. a login on.
  322. Return Value:
  323. 0 on success.
  324. -1 on failure, and errno will be set to contain more information.
  325. --*/
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif