utent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <paths.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <utmp.h>
  15. static const char *ut_name = _PATH_UTMP;
  16. static int ut_fd = -1;
  17. struct utmp *__getutent(int utmp_fd)
  18. {
  19. static struct utmp utmp;
  20. if (read(utmp_fd, (char *) &utmp, sizeof(struct utmp)) !=
  21. sizeof(struct utmp)) return NULL;
  22. return &utmp;
  23. }
  24. void setutent(void)
  25. {
  26. if (ut_fd != -1)
  27. close(ut_fd);
  28. if ((ut_fd = open(ut_name, O_RDWR)) < 0) {
  29. ut_fd = -1;
  30. }
  31. }
  32. void endutent(void)
  33. {
  34. if (ut_fd != -1)
  35. close(ut_fd);
  36. ut_fd = -1;
  37. }
  38. struct utmp *getutent(void)
  39. {
  40. if (ut_fd == -1)
  41. setutent();
  42. if (ut_fd == -1)
  43. return NULL;
  44. return __getutent(ut_fd);
  45. }
  46. struct utmp *getutid (const struct utmp *utmp_entry)
  47. {
  48. struct utmp *utmp;
  49. if (ut_fd == -1)
  50. setutent();
  51. if (ut_fd == -1)
  52. return NULL;
  53. while ((utmp = __getutent(ut_fd)) != NULL) {
  54. if ((utmp_entry->ut_type == RUN_LVL ||
  55. utmp_entry->ut_type == BOOT_TIME ||
  56. utmp_entry->ut_type == NEW_TIME ||
  57. utmp_entry->ut_type == OLD_TIME) &&
  58. utmp->ut_type == utmp_entry->ut_type) return utmp;
  59. if ((utmp_entry->ut_type == INIT_PROCESS ||
  60. utmp_entry->ut_type == DEAD_PROCESS ||
  61. utmp_entry->ut_type == LOGIN_PROCESS ||
  62. utmp_entry->ut_type == USER_PROCESS) &&
  63. !strcmp(utmp->ut_id, utmp_entry->ut_id)) return utmp;
  64. }
  65. return NULL;
  66. }
  67. struct utmp *getutline(const struct utmp *utmp_entry)
  68. {
  69. struct utmp *utmp;
  70. if (ut_fd == -1)
  71. setutent();
  72. if (ut_fd == -1)
  73. return NULL;
  74. #if 0
  75. /* This is driving me nuts. It's not an implementation problem -
  76. it's a matter of how things _SHOULD_ behave. Groan. */
  77. lseek(ut_fd, SEEK_CUR, -sizeof(struct utmp));
  78. #endif
  79. while ((utmp = __getutent(ut_fd)) != NULL) {
  80. if ((utmp->ut_type == USER_PROCESS ||
  81. utmp->ut_type == LOGIN_PROCESS) &&
  82. !strcmp(utmp->ut_line, utmp_entry->ut_line)) return utmp;
  83. }
  84. return NULL;
  85. }
  86. struct utmp *pututline (const struct utmp *utmp_entry)
  87. {
  88. struct utmp *ut;
  89. /* Ignore the return value. That way, if they've already positioned
  90. the file pointer where they want it, everything will work out. */
  91. (void) lseek(ut_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  92. if ((ut = getutid(utmp_entry)) != NULL) {
  93. lseek(ut_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
  94. if (write(ut_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  95. return NULL;
  96. } else {
  97. lseek(ut_fd, (off_t) 0, SEEK_END);
  98. if (write(ut_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
  99. return NULL;
  100. }
  101. return (struct utmp *)utmp_entry;
  102. }
  103. int utmpname (const char *new_ut_name)
  104. {
  105. if (new_ut_name != NULL)
  106. ut_name = new_ut_name;
  107. if (ut_fd != -1)
  108. close(ut_fd);
  109. return 0;
  110. }
  111. extern void updwtmp(const char *wtmp_file, const struct utmp *ut)
  112. {
  113. int fd;
  114. fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
  115. if (fd >= 0) {
  116. write(fd, (const char *) ut, sizeof(*ut));
  117. close(fd);
  118. }
  119. }