utent.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. void attribute_hidden __setutent(void)
  31. {
  32. int ret;
  33. LOCK;
  34. if (static_fd == -1) {
  35. if ((static_fd = __open(static_ut_name, O_RDWR)) < 0) {
  36. if ((static_fd = __open(static_ut_name, O_RDONLY)) < 0) {
  37. goto bummer;
  38. }
  39. }
  40. /* Make sure the file will be closed on exec() */
  41. ret = fcntl(static_fd, F_GETFD, 0);
  42. if (ret >= 0) {
  43. ret = fcntl(static_fd, F_GETFD, 0);
  44. }
  45. if (ret < 0) {
  46. bummer:
  47. UNLOCK;
  48. static_fd = -1;
  49. __close(static_fd);
  50. return;
  51. }
  52. }
  53. lseek(static_fd, 0, SEEK_SET);
  54. UNLOCK;
  55. return;
  56. }
  57. strong_alias(__setutent,setutent)
  58. static struct utmp *__getutent(int utmp_fd)
  59. {
  60. if (utmp_fd == -1) {
  61. __setutent();
  62. }
  63. if (utmp_fd == -1) {
  64. return NULL;
  65. }
  66. LOCK;
  67. if (__read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) != sizeof(struct utmp))
  68. {
  69. return NULL;
  70. }
  71. UNLOCK;
  72. return &static_utmp;
  73. }
  74. void endutent(void)
  75. {
  76. LOCK;
  77. if (static_fd != -1) {
  78. __close(static_fd);
  79. }
  80. static_fd = -1;
  81. UNLOCK;
  82. }
  83. /* Locking is done in __getutent */
  84. struct utmp *getutent(void)
  85. {
  86. return __getutent(static_fd);
  87. }
  88. /* Locking is done in __getutent */
  89. struct utmp attribute_hidden *__getutid (const struct utmp *utmp_entry)
  90. {
  91. struct utmp *lutmp;
  92. while ((lutmp = __getutent(static_fd)) != NULL) {
  93. if ( (utmp_entry->ut_type == RUN_LVL ||
  94. utmp_entry->ut_type == BOOT_TIME ||
  95. utmp_entry->ut_type == NEW_TIME ||
  96. utmp_entry->ut_type == OLD_TIME) &&
  97. lutmp->ut_type == utmp_entry->ut_type)
  98. {
  99. return lutmp;
  100. }
  101. if ( (utmp_entry->ut_type == INIT_PROCESS ||
  102. utmp_entry->ut_type == DEAD_PROCESS ||
  103. utmp_entry->ut_type == LOGIN_PROCESS ||
  104. utmp_entry->ut_type == USER_PROCESS) &&
  105. !__strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
  106. {
  107. return lutmp;
  108. }
  109. }
  110. return NULL;
  111. }
  112. strong_alias(__getutid,getutid)
  113. /* Locking is done in __getutent */
  114. struct utmp *getutline(const struct utmp *utmp_entry)
  115. {
  116. struct utmp *lutmp;
  117. while ((lutmp = __getutent(static_fd)) != NULL) {
  118. if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
  119. !__strcmp(lutmp->ut_line, utmp_entry->ut_line))
  120. {
  121. return lutmp;
  122. }
  123. }
  124. return NULL;
  125. }
  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. }