dinit-utmp.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Wrappers for utmp/wtmp & equivalent database access.
  2. #ifndef DINIT_UTMP_H_INCLUDED
  3. #define DINIT_UTMP_H_INCLUDED
  4. #include <mconfig.h> // pull in any explicit configuration
  5. // Configuration:
  6. // USE_UTMPX - whether to update the utmp[x] database. If 0, no-op stubs are defined.
  7. // USE_UPDWTMPX - whether to use the updwtmpx function to log boot (contingent on USE_UTMPX).
  8. // CLEAR_UTMP_ON_BOOT - whether to explicitly clear the utmp database file before writing the
  9. // boot entry.
  10. #ifndef USE_UTMPX
  11. #if __linux__ || __FreeBSD__ || __DragonFly__
  12. #define USE_UTMPX 1
  13. #if __linux__
  14. // Should be safe to #include <utmpx.h>, but it may be stub implementation (Musl). Need to check
  15. // that after include:
  16. #define CHECK_UTMP_PATH 1
  17. #endif
  18. #else
  19. #define USE_UTMPX 0
  20. #endif
  21. #endif
  22. #ifndef USE_UPDWTMPX
  23. #ifdef __linux__
  24. #define USE_UPDWTMPX 1
  25. #else
  26. #define USE_UPDWTMPX 0
  27. #endif
  28. #endif
  29. #ifndef CLEAR_UTMP_ON_BOOT
  30. #if __linux__
  31. #define CLEAR_UTMP_ON_BOOT 1
  32. #else
  33. #define CLEAR_UTMP_ON_BOOT 0
  34. #endif
  35. #endif
  36. #if USE_UTMPX
  37. #include <utmpx.h>
  38. // Musl has a utmpx.h header but only stub implementations of the functions, and does not define _PATH_UTMPX
  39. // nor _PATH_WTMPX.
  40. #if CHECK_UTMP_PATH
  41. #undef CHECK_UTMP_PATH
  42. #if !defined(_PATH_UTMPX) || !defined(_PATH_WTMPX)
  43. #undef USE_UTMPX
  44. #define USE_UTMPX 0
  45. #endif
  46. #endif
  47. #endif
  48. #if USE_UTMPX
  49. #include <cstring>
  50. #include <sys/time.h>
  51. // Set the time for a utmpx record to the current time.
  52. inline void set_current_time(struct utmpx *record)
  53. {
  54. #ifdef __linux__
  55. // On Linux, ut_tv is not necessarily actually a struct timeval - on x86_64 the tv_sec and tv_usec
  56. // fields are actually int32_t (by default) to preserve structural compatibility with 32-bit
  57. // utmp format.
  58. timeval curtime;
  59. gettimeofday(&curtime, nullptr);
  60. record->ut_tv.tv_sec = curtime.tv_sec;
  61. record->ut_tv.tv_usec = curtime.tv_usec;
  62. #else
  63. gettimeofday(&record->ut_tv, nullptr);
  64. #endif
  65. }
  66. // Log the boot time to the wtmp database (or equivalent).
  67. inline bool log_boot()
  68. {
  69. struct utmpx record;
  70. memset(&record, 0, sizeof(record));
  71. record.ut_type = BOOT_TIME;
  72. set_current_time(&record);
  73. // On FreeBSD, putxline will update all appropriate databases. On Linux, it only updates
  74. // the utmp database: we need to update the wtmp database explicitly:
  75. #if USE_UPDWTMPX
  76. updwtmpx(_PATH_WTMPX, &record);
  77. #endif
  78. #if CLEAR_UTMP_ON_BOOT
  79. truncate(_PATH_UTMPX, 0);
  80. #endif
  81. setutxent();
  82. bool success = (pututxline(&record) != NULL);
  83. endutxent();
  84. return success;
  85. }
  86. // Create a utmp entry for the specified process, with the given id and tty line.
  87. inline bool create_utmp_entry(const char *utmp_id, const char *utmp_line, pid_t pid)
  88. {
  89. struct utmpx record;
  90. memset(&record, 0, sizeof(record));
  91. record.ut_type = INIT_PROCESS;
  92. record.ut_pid = pid;
  93. set_current_time(&record);
  94. strncpy(record.ut_id, utmp_id, sizeof(record.ut_id));
  95. strncpy(record.ut_line, utmp_line, sizeof(record.ut_line));
  96. setutxent();
  97. bool success = (pututxline(&record) != NULL);
  98. endutxent();
  99. return success;
  100. }
  101. // Clear the utmp entry for the given id/line/process.
  102. inline void clear_utmp_entry(const char *utmp_id, const char *utmp_line)
  103. {
  104. struct utmpx record;
  105. memset(&record, 0, sizeof(record));
  106. record.ut_type = DEAD_PROCESS;
  107. set_current_time(&record);
  108. strncpy(record.ut_id, utmp_id, sizeof(record.ut_id));
  109. strncpy(record.ut_line, utmp_line, sizeof(record.ut_line));
  110. struct utmpx *result;
  111. setutxent();
  112. // Try to find an existing entry by id/line and copy the process ID:
  113. if (*utmp_id) {
  114. result = getutxid(&record);
  115. }
  116. else {
  117. result = getutxline(&record);
  118. }
  119. if (result) {
  120. record.ut_pid = result->ut_pid;
  121. }
  122. pututxline(&record);
  123. endutxent();
  124. }
  125. #else // Don't update databases:
  126. inline bool log_boot()
  127. {
  128. return true;
  129. }
  130. inline bool create_utmp_entry(const char *utmp_id, const char *utmp_line)
  131. {
  132. return true;
  133. }
  134. inline void clear_utmp_entry(const char *utmp_id, const char *utmp_line)
  135. {
  136. return;
  137. }
  138. #endif
  139. #endif