utent.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "internal/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 UT static_utmp;
  26. static const char default_file[] = __DEFAULT_PATH_UTMP;
  27. static const char *current_file = default_file;
  28. /* This function must be called with the LOCK held */
  29. static void __set_unlocked(void)
  30. {
  31. if (static_fd < 0) {
  32. static_fd = open_not_cancel_2(current_file, O_RDWR | O_CLOEXEC);
  33. if (static_fd < 0) {
  34. static_fd = open_not_cancel_2(current_file, 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 set(void)
  49. {
  50. __UCLIBC_MUTEX_LOCK(utmplock);
  51. __set_unlocked();
  52. __UCLIBC_MUTEX_UNLOCK(utmplock);
  53. }
  54. #else
  55. strong_alias(__set_unlocked,set)
  56. #endif
  57. /* not used in libc_hidden_def(set) */
  58. other(setutxent,setutent)
  59. /* This function must be called with the LOCK held */
  60. static struct UT *__get_unlocked(void)
  61. {
  62. if (static_fd < 0) {
  63. __set_unlocked();
  64. if (static_fd < 0)
  65. return NULL;
  66. }
  67. if (read_not_cancel(static_fd, &static_utmp,
  68. sizeof(static_utmp)) == sizeof(static_utmp)) {
  69. return &static_utmp;
  70. }
  71. return NULL;
  72. }
  73. #if defined __UCLIBC_HAS_THREADS__
  74. struct UT *get(void)
  75. {
  76. struct UT *ret;
  77. __UCLIBC_MUTEX_LOCK(utmplock);
  78. ret = __get_unlocked();
  79. __UCLIBC_MUTEX_UNLOCK(utmplock);
  80. return ret;
  81. }
  82. #else
  83. strong_alias(__get_unlocked,get)
  84. #endif
  85. /* not used in libc_hidden_def(get) */
  86. other(getutxent,getutent)
  87. void end(void)
  88. {
  89. __UCLIBC_MUTEX_LOCK(utmplock);
  90. if (static_fd >= 0)
  91. close_not_cancel_no_status(static_fd);
  92. static_fd = -1;
  93. __UCLIBC_MUTEX_UNLOCK(utmplock);
  94. }
  95. /* not used in libc_hidden_def(end) */
  96. other(endutxent,endutent)
  97. /* This function must be called with the LOCK held */
  98. static struct UT *__getid_unlocked(const struct UT *utmp_entry)
  99. {
  100. struct UT *lutmp;
  101. unsigned type;
  102. /* We use the fact that constants we are interested in are: */
  103. /* RUN_LVL=1, ... OLD_TIME=4; INIT_PROCESS=5, ... USER_PROCESS=8 */
  104. type = utmp_entry->ut_type - 1;
  105. type /= 4;
  106. while ((lutmp = __get_unlocked()) != NULL) {
  107. if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) {
  108. /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */
  109. return lutmp;
  110. }
  111. if (type == 1
  112. && strncmp(lutmp->ut_id, utmp_entry->ut_id,
  113. sizeof(lutmp->ut_id)) == 0) {
  114. /* INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, DEAD_PROCESS */
  115. return lutmp;
  116. }
  117. }
  118. return NULL;
  119. }
  120. #if defined __UCLIBC_HAS_THREADS__
  121. struct UT *getid(const struct UT *utmp_entry)
  122. {
  123. struct UT *ret;
  124. __UCLIBC_MUTEX_LOCK(utmplock);
  125. ret = __getid_unlocked(utmp_entry);
  126. __UCLIBC_MUTEX_UNLOCK(utmplock);
  127. return ret;
  128. }
  129. #else
  130. strong_alias(__getid_unlocked,getid)
  131. #endif
  132. /* not used in libc_hidden_def(getid) */
  133. other(getutxid,getutid)
  134. struct UT *getline(const struct UT *utmp_entry)
  135. {
  136. struct UT *lutmp;
  137. __UCLIBC_MUTEX_LOCK(utmplock);
  138. while ((lutmp = __get_unlocked()) != NULL) {
  139. if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) {
  140. if (strncmp(lutmp->ut_line, utmp_entry->ut_line,
  141. sizeof(lutmp->ut_line)) == 0) {
  142. break;
  143. }
  144. }
  145. }
  146. __UCLIBC_MUTEX_UNLOCK(utmplock);
  147. return lutmp;
  148. }
  149. /* libc_hidden_def(getline) */
  150. other(getutxline,getutline)
  151. struct UT *putline(const struct UT *utmp_entry)
  152. {
  153. __UCLIBC_MUTEX_LOCK(utmplock);
  154. /* Ignore the return value. That way, if they've already positioned
  155. the file pointer where they want it, everything will work out. */
  156. lseek(static_fd, (off_t) - sizeof(struct UT), SEEK_CUR);
  157. if (__getid_unlocked(utmp_entry) != NULL)
  158. lseek(static_fd, (off_t) - sizeof(struct UT), SEEK_CUR);
  159. else
  160. lseek(static_fd, (off_t) 0, SEEK_END);
  161. if (write(static_fd, utmp_entry, sizeof(struct UT))
  162. != sizeof(struct UT))
  163. utmp_entry = NULL;
  164. __UCLIBC_MUTEX_UNLOCK(utmplock);
  165. return (struct UT *)utmp_entry;
  166. }
  167. /* not used in libc_hidden_def(putline) */
  168. other(pututxline,pututline)
  169. int name(const char *new_file)
  170. {
  171. __UCLIBC_MUTEX_LOCK(utmplock);
  172. if (new_file != NULL) {
  173. if (current_file != default_file)
  174. free((char *)current_file);
  175. current_file = strdup(new_file);
  176. if (current_file == NULL) {
  177. /* We should probably whine about out-of-memory
  178. * errors here... Instead just reset to the default */
  179. current_file = default_file;
  180. }
  181. }
  182. if (static_fd >= 0) {
  183. close_not_cancel_no_status(static_fd);
  184. static_fd = -1;
  185. }
  186. __UCLIBC_MUTEX_UNLOCK(utmplock);
  187. return 0; /* or maybe return -(current_file != new_file)? */
  188. }
  189. /* not used in libc_hidden_def(name) */
  190. other(utmpxname,utmpname)
  191. void updw(const char *wtmp_file, const struct UT *lutmp)
  192. {
  193. int fd;
  194. fd = open_not_cancel_2(wtmp_file, O_APPEND | O_WRONLY);
  195. if (fd >= 0) {
  196. if (lockf(fd, F_LOCK, 0) == 0) {
  197. write_not_cancel(fd, lutmp, sizeof(struct UT));
  198. lockf(fd, F_ULOCK, 0);
  199. close_not_cancel_no_status(fd);
  200. }
  201. }
  202. }
  203. /* not used in libc_hidden_def(updw) */
  204. other(updwtmpx,updwtmp)