utent.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* vi: set sw=4 ts=4: */
  2. /* utent.c <ndf@linux.mit.edu> */
  3. /* Let it be known that this is very possibly the worst standard ever. HP-UX
  4. does one thing, someone else does another, linux another... If anyone
  5. actually has the standard, please send it to me.
  6. Note that because of the way this stupid stupid standard works, you
  7. have to call endutent() to close the file even if you've not called
  8. setutent -- getutid and family use the same file descriptor.
  9. Modified by Erik Andersen for uClibc...
  10. */
  11. #include <features.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <paths.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <utmp.h>
  20. #include <not-cancel.h>
  21. #include <bits/uClibc_mutex.h>
  22. __UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
  23. /* Some global crap */
  24. static int static_fd = -1;
  25. static struct utmp static_utmp;
  26. static const char default_file_name[] = _PATH_UTMP;
  27. static const char *static_ut_name = default_file_name;
  28. /* This function must be called with the LOCK held */
  29. static void __setutent_unlocked(void)
  30. {
  31. if (static_fd < 0) {
  32. static_fd = open_not_cancel_2(static_ut_name, O_RDWR | O_CLOEXEC);
  33. if (static_fd < 0) {
  34. static_fd = open_not_cancel_2(static_ut_name, O_RDONLY | O_CLOEXEC);
  35. if (static_fd < 0) {
  36. return; /* static_fd remains < 0 */
  37. }
  38. }
  39. #ifndef __ASSUME_O_CLOEXEC
  40. /* Make sure the file will be closed on exec() */
  41. fcntl_not_cancel(static_fd, F_SETFD, FD_CLOEXEC);
  42. #endif
  43. return;
  44. }
  45. lseek(static_fd, 0, SEEK_SET);
  46. }
  47. #if defined __UCLIBC_HAS_THREADS__
  48. void setutent(void)
  49. {
  50. __UCLIBC_MUTEX_LOCK(utmplock);
  51. __setutent_unlocked();
  52. __UCLIBC_MUTEX_UNLOCK(utmplock);
  53. }
  54. #else
  55. strong_alias(__setutent_unlocked,setutent)
  56. #endif
  57. libc_hidden_def(setutent)
  58. /* This function must be called with the LOCK held */
  59. static struct utmp *__getutent_unlocked(void)
  60. {
  61. if (static_fd < 0) {
  62. __setutent_unlocked();
  63. if (static_fd < 0)
  64. return NULL;
  65. }
  66. if (read_not_cancel(static_fd, &static_utmp,
  67. sizeof(static_utmp)) == sizeof(static_utmp)) {
  68. return &static_utmp;
  69. }
  70. return NULL;
  71. }
  72. #if defined __UCLIBC_HAS_THREADS__
  73. struct utmp *getutent(void)
  74. {
  75. struct utmp *ret;
  76. __UCLIBC_MUTEX_LOCK(utmplock);
  77. ret = __getutent_unlocked();
  78. __UCLIBC_MUTEX_UNLOCK(utmplock);
  79. return ret;
  80. }
  81. #else
  82. strong_alias(__getutent_unlocked,getutent)
  83. #endif
  84. libc_hidden_def(getutent)
  85. void endutent(void)
  86. {
  87. __UCLIBC_MUTEX_LOCK(utmplock);
  88. if (static_fd >= 0)
  89. close_not_cancel_no_status(static_fd);
  90. static_fd = -1;
  91. __UCLIBC_MUTEX_UNLOCK(utmplock);
  92. }
  93. libc_hidden_def(endutent)
  94. /* This function must be called with the LOCK held */
  95. static struct utmp *__getutid_unlocked(const struct utmp *utmp_entry)
  96. {
  97. struct utmp *lutmp;
  98. unsigned type;
  99. /* We use the fact that constants we are interested in are: */
  100. /* RUN_LVL=1, ... OLD_TIME=4; INIT_PROCESS=5, ... USER_PROCESS=8 */
  101. type = utmp_entry->ut_type - 1;
  102. type /= 4;
  103. while ((lutmp = __getutent_unlocked()) != NULL) {
  104. if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) {
  105. /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */
  106. return lutmp;
  107. }
  108. if (type == 1
  109. && strncmp(lutmp->ut_id, utmp_entry->ut_id,
  110. sizeof(lutmp->ut_id)) == 0) {
  111. /* INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, DEAD_PROCESS */
  112. return lutmp;
  113. }
  114. }
  115. return NULL;
  116. }
  117. #if defined __UCLIBC_HAS_THREADS__
  118. struct utmp *getutid(const struct utmp *utmp_entry)
  119. {
  120. struct utmp *ret;
  121. __UCLIBC_MUTEX_LOCK(utmplock);
  122. ret = __getutid_unlocked(utmp_entry);
  123. __UCLIBC_MUTEX_UNLOCK(utmplock);
  124. return ret;
  125. }
  126. #else
  127. strong_alias(__getutid_unlocked,getutid)
  128. #endif
  129. libc_hidden_def(getutid)
  130. struct utmp *getutline(const struct utmp *utmp_entry)
  131. {
  132. struct utmp *lutmp;
  133. __UCLIBC_MUTEX_LOCK(utmplock);
  134. while ((lutmp = __getutent_unlocked()) != NULL) {
  135. if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) {
  136. if (strncmp(lutmp->ut_line, utmp_entry->ut_line,
  137. sizeof(lutmp->ut_line)) == 0) {
  138. break;
  139. }
  140. }
  141. }
  142. __UCLIBC_MUTEX_UNLOCK(utmplock);
  143. return lutmp;
  144. }
  145. libc_hidden_def(getutline)
  146. struct utmp *pututline(const struct utmp *utmp_entry)
  147. {
  148. __UCLIBC_MUTEX_LOCK(utmplock);
  149. /* Ignore the return value. That way, if they've already positioned
  150. the file pointer where they want it, everything will work out. */
  151. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  152. if (__getutid_unlocked(utmp_entry) != NULL)
  153. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  154. else
  155. lseek(static_fd, (off_t) 0, SEEK_END);
  156. if (write(static_fd, utmp_entry, sizeof(struct utmp))
  157. != sizeof(struct utmp))
  158. utmp_entry = NULL;
  159. __UCLIBC_MUTEX_UNLOCK(utmplock);
  160. return (struct utmp *)utmp_entry;
  161. }
  162. libc_hidden_def(pututline)
  163. int utmpname(const char *new_ut_name)
  164. {
  165. __UCLIBC_MUTEX_LOCK(utmplock);
  166. if (new_ut_name != NULL) {
  167. if (static_ut_name != default_file_name)
  168. free((char *)static_ut_name);
  169. static_ut_name = strdup(new_ut_name);
  170. if (static_ut_name == NULL) {
  171. /* We should probably whine about out-of-memory
  172. * errors here... Instead just reset to the default */
  173. static_ut_name = default_file_name;
  174. }
  175. }
  176. if (static_fd >= 0) {
  177. close_not_cancel_no_status(static_fd);
  178. static_fd = -1;
  179. }
  180. __UCLIBC_MUTEX_UNLOCK(utmplock);
  181. return 0; /* or maybe return -(static_ut_name != new_ut_name)? */
  182. }
  183. libc_hidden_def(utmpname)