utent.c 5.4 KB

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