utent.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <unistd.h>
  9. #include <fcntl.h>
  10. #include <paths.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <utmp.h>
  14. static const char *ut_name = _PATH_UTMP;
  15. static int ut_fd = -1;
  16. struct utmp *__getutent(int utmp_fd)
  17. {
  18. static struct utmp utmp;
  19. if (read(utmp_fd, (char *) &utmp, sizeof(struct utmp)) !=
  20. sizeof(struct utmp)) return NULL;
  21. return &utmp;
  22. }
  23. void setutent(void)
  24. {
  25. if (ut_fd != -1)
  26. close(ut_fd);
  27. if ((ut_fd = open(ut_name, O_RDONLY)) < 0) {
  28. perror("setutent: Can't open utmp file");
  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(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(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(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(utmp_entry), SEEK_CUR);
  92. if ((ut = getutid(utmp_entry)) != NULL) {
  93. lseek(ut_fd, (off_t) - sizeof(utmp_entry), SEEK_CUR);
  94. if (write(ut_fd, (char *) utmp_entry, sizeof(utmp_entry))
  95. != sizeof(utmp_entry))
  96. return NULL;
  97. } else {
  98. lseek(ut_fd, (off_t) 0, SEEK_END);
  99. if (write(ut_fd, (char *) utmp_entry, sizeof(utmp_entry))
  100. != sizeof(utmp_entry))
  101. return NULL;
  102. }
  103. return utmp_entry;
  104. }
  105. void utmpname(const char *new_ut_name)
  106. {
  107. if (new_ut_name != NULL)
  108. ut_name = new_ut_name;
  109. if (ut_fd != -1)
  110. close(ut_fd);
  111. }