utent.c 5.8 KB

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