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