utent.c 4.5 KB

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