utent.c 6.4 KB

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