utent.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #endif
  88. libc_hidden_def(getutent)
  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. 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()) != NULL) {
  135. if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) {
  136. if (strncmp(lutmp->ut_line, utmp_entry->ut_line, sizeof(lutmp->ut_line)) == 0) {
  137. break;
  138. }
  139. }
  140. }
  141. __UCLIBC_MUTEX_UNLOCK(utmplock);
  142. return lutmp;
  143. }
  144. libc_hidden_def(getutline)
  145. struct utmp *pututline(const struct utmp *utmp_entry)
  146. {
  147. __UCLIBC_MUTEX_LOCK(utmplock);
  148. /* Ignore the return value. That way, if they've already positioned
  149. the file pointer where they want it, everything will work out. */
  150. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  151. if (__getutid(utmp_entry) != NULL)
  152. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  153. else
  154. lseek(static_fd, (off_t) 0, SEEK_END);
  155. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  156. utmp_entry = NULL;
  157. __UCLIBC_MUTEX_UNLOCK(utmplock);
  158. return (struct utmp *)utmp_entry;
  159. }
  160. libc_hidden_def(pututline)
  161. int utmpname(const char *new_ut_name)
  162. {
  163. __UCLIBC_MUTEX_LOCK(utmplock);
  164. if (new_ut_name != NULL) {
  165. if (static_ut_name != default_file_name)
  166. free((char *)static_ut_name);
  167. static_ut_name = strdup(new_ut_name);
  168. if (static_ut_name == NULL) {
  169. /* We should probably whine about out-of-memory
  170. * errors here... Instead just reset to the default */
  171. static_ut_name = default_file_name;
  172. }
  173. }
  174. if (static_fd >= 0) {
  175. close_not_cancel_no_status(static_fd);
  176. static_fd = -1;
  177. }
  178. __UCLIBC_MUTEX_UNLOCK(utmplock);
  179. return 0; /* or maybe return -(static_ut_name != new_ut_name)? */
  180. }
  181. libc_hidden_def(utmpname)