logwtmp.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <utmp.h>
  22. void logwtmp (const char *line, const char *name, const char *host)
  23. {
  24. struct utmp ut;
  25. struct flock lock;
  26. int ut_fd;
  27. /* Set information in new entry. */
  28. memset (&ut, 0, sizeof (ut));
  29. #if _HAVE_UT_PID - 0
  30. ut.ut_pid = getpid ();
  31. #endif
  32. #if _HAVE_UT_TYPE - 0
  33. ut.ut_type = name[0] ? USER_PROCESS : DEAD_PROCESS;
  34. #endif
  35. strncpy (ut.ut_line, line, sizeof ut.ut_line);
  36. strncpy (ut.ut_name, name, sizeof ut.ut_name);
  37. #if _HAVE_UT_HOST - 0
  38. strncpy (ut.ut_host, host, sizeof ut.ut_host);
  39. #endif
  40. #if _HAVE_UT_TV - 0
  41. gettimeofday (&ut.ut_tv, NULL);
  42. #else
  43. time (&ut.ut_time);
  44. #endif
  45. /* updwtmp (_PATH_WTMP, &ut); */
  46. /* from tinylogin */
  47. if ((ut_fd = open(_PATH_WTMP, O_APPEND | O_WRONLY)) >= 0) {
  48. /* Lock the utmp file before updating */
  49. lock.l_type = F_WRLCK;
  50. lock.l_whence = SEEK_SET;
  51. lock.l_start = 0;
  52. lock.l_len = 0;
  53. if (fcntl(ut_fd, F_SETLK, &lock) >= 0) {
  54. write(ut_fd, (void *) &ut, sizeof(ut));
  55. /* Now unlock the utmp file */
  56. lock.l_type = F_UNLCK;
  57. }
  58. close(ut_fd);
  59. }
  60. }