logwtmp.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* wtmp support rubbish (i.e. complete crap)
  2. Written by Erik Andersen <andersee@debian.org>
  3. The GNU C Library is free software
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <utmp.h>
  21. #include <fcntl.h>
  22. #include <sys/file.h>
  23. void logwtmp (const char *line, const char *name, const char *host)
  24. {
  25. struct utmp lutmp;
  26. memset (&(lutmp), 0, sizeof (struct utmp));
  27. lutmp.ut_type = (name && *name)? USER_PROCESS : DEAD_PROCESS;
  28. lutmp.ut_pid = getpid();
  29. strncpy(lutmp.ut_line, line, sizeof(lutmp.ut_line)-1);
  30. strncpy(lutmp.ut_name, name, sizeof(lutmp.ut_name)-1);
  31. strncpy(lutmp.ut_host, host, sizeof(lutmp.ut_host)-1);
  32. #if __WORDSIZE_COMPAT32 == 0
  33. gettimeofday(&(lutmp.ut_tv), NULL);
  34. #else
  35. {
  36. struct timeval tv;
  37. gettimeofday (&tv, NULL);
  38. lutmp.ut_tv.tv_sec = tv.tv_sec;
  39. lutmp.ut_tv.tv_usec = tv.tv_usec;
  40. }
  41. #endif
  42. updwtmp(_PATH_WTMP, &(lutmp));
  43. }
  44. #if 0
  45. /* This is enabled in uClibc/libc/misc/utmp/wtent.c */
  46. extern void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
  47. {
  48. int fd;
  49. fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
  50. if (fd >= 0) {
  51. if (lockf(fd, F_LOCK, 0)==0) {
  52. write(fd, (const char *) lutmp, sizeof(struct utmp));
  53. lockf(fd, F_ULOCK, 0);
  54. close(fd);
  55. }
  56. }
  57. }
  58. #endif