utent.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <bits/uClibc_mutex.h>
  20. __UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
  21. /* Do not create extra unlocked functions if no locking is needed */
  22. #if defined __UCLIBC_HAS_THREADS__
  23. # define static_if_threaded static
  24. #else
  25. # define static_if_threaded /* nothing */
  26. # define __setutent setutent
  27. # define __getutent getutent
  28. # define __getutid getutid
  29. #endif
  30. /* Some global crap */
  31. static int static_fd = -1;
  32. static struct utmp static_utmp;
  33. static const char default_file_name[] = _PATH_UTMP;
  34. static const char *static_ut_name = default_file_name;
  35. /* This function must be called with the LOCK held */
  36. static_if_threaded void __setutent(void)
  37. {
  38. if (static_fd < 0) {
  39. static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC);
  40. if (static_fd < 0) {
  41. static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC);
  42. if (static_fd < 0) {
  43. return; /* static_fd remains < 0 */
  44. }
  45. }
  46. #ifndef __ASSUME_O_CLOEXEC
  47. /* Make sure the file will be closed on exec() */
  48. fcntl(static_fd, F_SETFD, FD_CLOEXEC);
  49. #endif
  50. return;
  51. }
  52. lseek(static_fd, 0, SEEK_SET);
  53. }
  54. #if defined __UCLIBC_HAS_THREADS__
  55. void setutent(void)
  56. {
  57. __UCLIBC_MUTEX_LOCK(utmplock);
  58. __setutent();
  59. __UCLIBC_MUTEX_UNLOCK(utmplock);
  60. }
  61. #endif
  62. libc_hidden_def(setutent)
  63. /* This function must be called with the LOCK held */
  64. static_if_threaded struct utmp *__getutent(void)
  65. {
  66. if (static_fd < 0) {
  67. __setutent();
  68. if (static_fd < 0) {
  69. return NULL;
  70. }
  71. }
  72. if (read(static_fd, &static_utmp, sizeof(static_utmp)) == sizeof(static_utmp)) {
  73. return &static_utmp;
  74. }
  75. return NULL;
  76. }
  77. #if defined __UCLIBC_HAS_THREADS__
  78. struct utmp *getutent(void)
  79. {
  80. struct utmp *ret;
  81. __UCLIBC_MUTEX_LOCK(utmplock);
  82. ret = __getutent();
  83. __UCLIBC_MUTEX_UNLOCK(utmplock);
  84. return ret;
  85. }
  86. #endif
  87. void endutent(void)
  88. {
  89. __UCLIBC_MUTEX_LOCK(utmplock);
  90. if (static_fd >= 0)
  91. close(static_fd);
  92. static_fd = -1;
  93. __UCLIBC_MUTEX_UNLOCK(utmplock);
  94. }
  95. /* This function must be called with the LOCK held */
  96. static_if_threaded struct utmp *__getutid(const struct utmp *utmp_entry)
  97. {
  98. struct utmp *lutmp;
  99. unsigned type;
  100. /* We use the fact that constants we are interested in are: */
  101. /* RUN_LVL=1, ... OLD_TIME=4; INIT_PROCESS=5, ... USER_PROCESS=8 */
  102. type = utmp_entry->ut_type - 1;
  103. type /= 4;
  104. while ((lutmp = __getutent()) != NULL) {
  105. if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) {
  106. /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */
  107. return lutmp;
  108. }
  109. if (type == 1 && strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)) == 0) {
  110. /* INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, DEAD_PROCESS */
  111. return lutmp;
  112. }
  113. }
  114. return NULL;
  115. }
  116. #if defined __UCLIBC_HAS_THREADS__
  117. struct utmp *getutid(const struct utmp *utmp_entry)
  118. {
  119. struct utmp *ret;
  120. __UCLIBC_MUTEX_LOCK(utmplock);
  121. ret = __getutid(utmp_entry);
  122. __UCLIBC_MUTEX_UNLOCK(utmplock);
  123. return ret;
  124. }
  125. #endif
  126. struct utmp *getutline(const struct utmp *utmp_entry)
  127. {
  128. struct utmp *lutmp;
  129. __UCLIBC_MUTEX_LOCK(utmplock);
  130. while ((lutmp = __getutent()) != NULL) {
  131. if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) {
  132. if (strncmp(lutmp->ut_line, utmp_entry->ut_line, sizeof(lutmp->ut_line)) == 0) {
  133. break;
  134. }
  135. }
  136. }
  137. __UCLIBC_MUTEX_UNLOCK(utmplock);
  138. return lutmp;
  139. }
  140. struct utmp *pututline(const struct utmp *utmp_entry)
  141. {
  142. __UCLIBC_MUTEX_LOCK(utmplock);
  143. /* Ignore the return value. That way, if they've already positioned
  144. the file pointer where they want it, everything will work out. */
  145. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  146. if (__getutid(utmp_entry) != NULL)
  147. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  148. else
  149. lseek(static_fd, (off_t) 0, SEEK_END);
  150. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  151. utmp_entry = NULL;
  152. __UCLIBC_MUTEX_UNLOCK(utmplock);
  153. return (struct utmp *)utmp_entry;
  154. }
  155. int utmpname(const char *new_ut_name)
  156. {
  157. __UCLIBC_MUTEX_LOCK(utmplock);
  158. if (new_ut_name != NULL) {
  159. if (static_ut_name != default_file_name)
  160. free((char *)static_ut_name);
  161. static_ut_name = strdup(new_ut_name);
  162. if (static_ut_name == NULL) {
  163. /* We should probably whine about out-of-memory
  164. * errors here... Instead just reset to the default */
  165. static_ut_name = default_file_name;
  166. }
  167. }
  168. if (static_fd >= 0) {
  169. close(static_fd);
  170. static_fd = -1;
  171. }
  172. __UCLIBC_MUTEX_UNLOCK(utmplock);
  173. return 0; /* or maybe return -(static_ut_name != new_ut_name)? */
  174. }