utent.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_THREADS__
  20. #include <pthread.h>
  21. static pthread_mutex_t utmplock = PTHREAD_MUTEX_INITIALIZER;
  22. # define LOCK pthread_mutex_lock(&utmplock)
  23. # define UNLOCK pthread_mutex_unlock(&utmplock);
  24. #else
  25. # define LOCK
  26. # define UNLOCK
  27. #endif
  28. /* Some global crap */
  29. static int static_fd = -1;
  30. static struct utmp static_utmp;
  31. static const char default_file_name[] = _PATH_UTMP;
  32. static const char *static_ut_name = (const char *) default_file_name;
  33. static struct utmp *__getutent(int utmp_fd)
  34. {
  35. if (utmp_fd == -1) {
  36. setutent();
  37. }
  38. if (utmp_fd == -1) {
  39. return NULL;
  40. }
  41. LOCK;
  42. if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) != sizeof(struct utmp))
  43. {
  44. return NULL;
  45. }
  46. UNLOCK;
  47. return &static_utmp;
  48. }
  49. void setutent(void)
  50. {
  51. int ret;
  52. LOCK;
  53. if (static_fd == -1) {
  54. if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
  55. if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
  56. goto bummer;
  57. }
  58. }
  59. /* Make sure the file will be closed on exec() */
  60. ret = fcntl(static_fd, F_GETFD, 0);
  61. if (ret >= 0) {
  62. ret = fcntl(static_fd, F_GETFD, 0);
  63. }
  64. if (ret < 0) {
  65. bummer:
  66. UNLOCK;
  67. static_fd = -1;
  68. close(static_fd);
  69. return;
  70. }
  71. }
  72. lseek(static_fd, 0, SEEK_SET);
  73. UNLOCK;
  74. return;
  75. }
  76. void endutent(void)
  77. {
  78. LOCK;
  79. if (static_fd != -1) {
  80. close(static_fd);
  81. }
  82. static_fd = -1;
  83. UNLOCK;
  84. }
  85. /* Locking is done in __getutent */
  86. struct utmp *getutent(void)
  87. {
  88. return __getutent(static_fd);
  89. }
  90. #if 0
  91. struct utmp * getutent(void)
  92. {
  93. struct utmp *result;
  94. static struct utmp buffer;
  95. if (getutent_r(&buffer, &result) < 0)
  96. return NULL;
  97. return result;
  98. }
  99. #endif
  100. /* Locking is done in __getutent */
  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. !strcmp(lutmp->ut_id, utmp_entry->ut_id))
  118. {
  119. return lutmp;
  120. }
  121. }
  122. return NULL;
  123. }
  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. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  146. return NULL;
  147. } else {
  148. lseek(static_fd, (off_t) 0, SEEK_END);
  149. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  150. return NULL;
  151. }
  152. UNLOCK;
  153. return (struct utmp *)utmp_entry;
  154. }
  155. int utmpname (const char *new_ut_name)
  156. {
  157. LOCK;
  158. if (new_ut_name != NULL) {
  159. if (static_ut_name != default_file_name)
  160. free((char *)static_ut_name);
  161. static_ut_name = strdup(new_ut_name);
  162. if (static_ut_name == NULL) {
  163. /* We should probably whine about out-of-memory
  164. * errors here... Instead just reset to the default */
  165. static_ut_name = default_file_name;
  166. }
  167. }
  168. if (static_fd != -1)
  169. close(static_fd);
  170. UNLOCK;
  171. return 0;
  172. }