utent.c 5.2 KB

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