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_GETFD, 0);
  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. }
  91. static_fd = -1;
  92. UNLOCK;
  93. }
  94. /* Locking is done in __getutent */
  95. struct utmp *getutent(void)
  96. {
  97. return __getutent(static_fd);
  98. }
  99. /* Locking is done in __getutent */
  100. libc_hidden_proto(getutid)
  101. 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_def(getutid)
  125. /* Locking is done in __getutent */
  126. struct utmp *getutline(const struct utmp *utmp_entry)
  127. {
  128. struct utmp *lutmp;
  129. while ((lutmp = __getutent(static_fd)) != NULL) {
  130. if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
  131. !strcmp(lutmp->ut_line, utmp_entry->ut_line))
  132. {
  133. return lutmp;
  134. }
  135. }
  136. return NULL;
  137. }
  138. struct utmp *pututline (const struct utmp *utmp_entry)
  139. {
  140. LOCK;
  141. /* Ignore the return value. That way, if they've already positioned
  142. the file pointer where they want it, everything will work out. */
  143. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  144. if (getutid(utmp_entry) != NULL)
  145. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  146. else
  147. lseek(static_fd, (off_t) 0, SEEK_END);
  148. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  149. utmp_entry = NULL;
  150. UNLOCK;
  151. return (struct utmp *)utmp_entry;
  152. }
  153. int utmpname (const char *new_ut_name)
  154. {
  155. LOCK;
  156. if (new_ut_name != NULL) {
  157. if (static_ut_name != default_file_name)
  158. free((char *)static_ut_name);
  159. static_ut_name = strdup(new_ut_name);
  160. if (static_ut_name == NULL) {
  161. /* We should probably whine about out-of-memory
  162. * errors here... Instead just reset to the default */
  163. static_ut_name = default_file_name;
  164. }
  165. }
  166. if (static_fd != -1)
  167. close(static_fd);
  168. UNLOCK;
  169. return 0;
  170. }