utent.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* Locking is done in __getutent */
  91. struct utmp *getutid (const struct utmp *utmp_entry)
  92. {
  93. struct utmp *lutmp;
  94. while ((lutmp = __getutent(static_fd)) != NULL) {
  95. if ( (utmp_entry->ut_type == RUN_LVL ||
  96. utmp_entry->ut_type == BOOT_TIME ||
  97. utmp_entry->ut_type == NEW_TIME ||
  98. utmp_entry->ut_type == OLD_TIME) &&
  99. lutmp->ut_type == utmp_entry->ut_type)
  100. {
  101. return lutmp;
  102. }
  103. if ( (utmp_entry->ut_type == INIT_PROCESS ||
  104. utmp_entry->ut_type == DEAD_PROCESS ||
  105. utmp_entry->ut_type == LOGIN_PROCESS ||
  106. utmp_entry->ut_type == USER_PROCESS) &&
  107. !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
  108. {
  109. return lutmp;
  110. }
  111. }
  112. return NULL;
  113. }
  114. /* Locking is done in __getutent */
  115. struct utmp *getutline(const struct utmp *utmp_entry)
  116. {
  117. struct utmp *lutmp;
  118. while ((lutmp = __getutent(static_fd)) != NULL) {
  119. if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
  120. !strcmp(lutmp->ut_line, utmp_entry->ut_line))
  121. {
  122. return lutmp;
  123. }
  124. }
  125. return NULL;
  126. }
  127. struct utmp *pututline (const struct utmp *utmp_entry)
  128. {
  129. LOCK;
  130. /* Ignore the return value. That way, if they've already positioned
  131. the file pointer where they want it, everything will work out. */
  132. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  133. if (getutid(utmp_entry) != NULL) {
  134. lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  135. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  136. return NULL;
  137. } else {
  138. lseek(static_fd, (off_t) 0, SEEK_END);
  139. if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  140. return NULL;
  141. }
  142. UNLOCK;
  143. return (struct utmp *)utmp_entry;
  144. }
  145. int utmpname (const char *new_ut_name)
  146. {
  147. LOCK;
  148. if (new_ut_name != NULL) {
  149. if (static_ut_name != default_file_name)
  150. free((char *)static_ut_name);
  151. static_ut_name = strdup(new_ut_name);
  152. if (static_ut_name == NULL) {
  153. /* We should probably whine about out-of-memory
  154. * errors here... Instead just reset to the default */
  155. static_ut_name = default_file_name;
  156. }
  157. }
  158. if (static_fd != -1)
  159. close(static_fd);
  160. UNLOCK;
  161. return 0;
  162. }