utent.c 5.7 KB

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