logwtmp.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wtmp support rubbish (i.e. complete crap)
  4. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <utmp.h>
  12. #include <fcntl.h>
  13. #include <sys/file.h>
  14. void logwtmp(const char *line, const char *name, const char *host)
  15. {
  16. struct utmp lutmp;
  17. memset(&lutmp, 0, sizeof(lutmp));
  18. lutmp.ut_type = (name && *name) ? USER_PROCESS : DEAD_PROCESS;
  19. lutmp.ut_pid = getpid();
  20. strncpy(lutmp.ut_line, line, sizeof(lutmp.ut_line)-1);
  21. strncpy(lutmp.ut_name, name, sizeof(lutmp.ut_name)-1);
  22. strncpy(lutmp.ut_host, host, sizeof(lutmp.ut_host)-1);
  23. #if !defined __WORDSIZE_TIME64_COMPAT32
  24. gettimeofday(&lutmp.ut_tv, NULL);
  25. #else
  26. {
  27. struct timeval tv;
  28. gettimeofday(&tv, NULL);
  29. lutmp.ut_tv.tv_sec = tv.tv_sec;
  30. lutmp.ut_tv.tv_usec = tv.tv_usec;
  31. }
  32. #endif
  33. updwtmp(_PATH_WTMP, &lutmp);
  34. }
  35. #if 0
  36. /* This is enabled in uClibc/libc/misc/utmp/wtent.c */
  37. void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
  38. {
  39. int fd;
  40. fd = open(wtmp_file, O_APPEND | O_WRONLY);
  41. if (fd >= 0) {
  42. if (lockf(fd, F_LOCK, 0) == 0) {
  43. write(fd, lutmp, sizeof(*lutmp));
  44. lockf(fd, F_ULOCK, 0);
  45. close(fd);
  46. }
  47. }
  48. }
  49. #endif