3
0

utmp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * utmp/wtmp support routines.
  4. *
  5. * Copyright (C) 2010 Denys Vlasenko
  6. *
  7. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include <utmp.h>
  11. static void touch(const char *filename)
  12. {
  13. if (access(filename, R_OK | W_OK) == -1)
  14. close(open(filename, O_WRONLY | O_CREAT, 0664));
  15. }
  16. void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname)
  17. {
  18. struct utmp utent;
  19. char *id;
  20. unsigned width;
  21. memset(&utent, 0, sizeof(utent));
  22. utent.ut_pid = pid;
  23. utent.ut_type = new_type;
  24. tty_name = skip_dev_pfx(tty_name);
  25. safe_strncpy(utent.ut_line, tty_name, sizeof(utent.ut_line));
  26. if (username)
  27. safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
  28. if (hostname)
  29. safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host));
  30. utent.ut_tv.tv_sec = time(NULL);
  31. /* Invent our own ut_id. ut_id is only 4 chars wide.
  32. * Try to fit something remotely meaningful... */
  33. id = utent.ut_id;
  34. width = sizeof(utent.ut_id);
  35. if (tty_name[0] == 'p') {
  36. /* if "ptyXXX", map to "pXXX" */
  37. /* if "pts/XX", map to "p/XX" */
  38. *id++ = 'p';
  39. width--;
  40. } /* else: usually it's "ttyXXXX", map to "XXXX" */
  41. if (strlen(tty_name) > 3)
  42. tty_name += 3;
  43. strncpy(id, tty_name, width);
  44. touch(_PATH_UTMP);
  45. //utmpname(_PATH_UTMP);
  46. setutent();
  47. /* Append new one (hopefully, unless we collide on ut_id) */
  48. pututline(&utent);
  49. endutent();
  50. #if ENABLE_FEATURE_WTMP
  51. /* "man utmp" says wtmp file should *not* be created automagically */
  52. /*touch(bb_path_wtmp_file);*/
  53. updwtmp(bb_path_wtmp_file, &utent);
  54. #endif
  55. }
  56. /*
  57. * Read "man utmp" to make sense out of it.
  58. */
  59. void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname)
  60. {
  61. struct utmp utent;
  62. struct utmp *utp;
  63. touch(_PATH_UTMP);
  64. //utmpname(_PATH_UTMP);
  65. setutent();
  66. /* Did init/getty/telnetd/sshd/... create an entry for us?
  67. * It should be (new_type-1), but we'd also reuse
  68. * any other potentially stale xxx_PROCESS entry */
  69. while ((utp = getutent()) != NULL) {
  70. if (utp->ut_pid == pid
  71. // && ut->ut_line[0]
  72. && utp->ut_id[0] /* must have nonzero id */
  73. && ( utp->ut_type == INIT_PROCESS
  74. || utp->ut_type == LOGIN_PROCESS
  75. || utp->ut_type == USER_PROCESS
  76. || utp->ut_type == DEAD_PROCESS
  77. )
  78. ) {
  79. if (utp->ut_type >= new_type) {
  80. /* Stale record. Nuke hostname */
  81. memset(utp->ut_host, 0, sizeof(utp->ut_host));
  82. }
  83. /* NB: pututline (see later) searches for matching utent
  84. * using getutid(utent) - we must not change ut_id
  85. * if we want *exactly this* record to be overwritten!
  86. */
  87. break;
  88. }
  89. }
  90. //endutent(); - no need, pututline can deal with (and actually likes)
  91. //the situation when utmp file is positioned on found record
  92. if (!utp) {
  93. if (new_type != DEAD_PROCESS)
  94. write_new_utmp(pid, new_type, tty_name, username, hostname);
  95. else
  96. endutent();
  97. return;
  98. }
  99. /* Make a copy. We can't use *utp, pututline's internal getutid
  100. * will overwrite it before it is used! */
  101. utent = *utp;
  102. utent.ut_type = new_type;
  103. if (tty_name)
  104. safe_strncpy(utent.ut_line, skip_dev_pfx(tty_name), sizeof(utent.ut_line));
  105. if (username)
  106. safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
  107. if (hostname)
  108. safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host));
  109. utent.ut_tv.tv_sec = time(NULL);
  110. /* Update, or append new one */
  111. //setutent();
  112. pututline(&utent);
  113. endutent();
  114. #if ENABLE_FEATURE_WTMP
  115. /* "man utmp" says wtmp file should *not* be created automagically */
  116. /*touch(bb_path_wtmp_file);*/
  117. updwtmp(bb_path_wtmp_file, &utent);
  118. #endif
  119. }