utent.c 5.3 KB

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