diff options
author | Ulrich Drepper <drepper@redhat.com> | 1997-08-08 00:27:32 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 1997-08-08 00:27:32 +0000 |
commit | 655224ac2a65ea3b21301474069e768015e24665 (patch) | |
tree | dd7e574014af73b4103df9fd8cc73587444abf81 /login/updwtmp.c | |
parent | 4a5bd8f420993ca98e4db0d8c11572bc27d5d7dd (diff) |
Definition of updwtmp function.
Diffstat (limited to 'login/updwtmp.c')
-rw-r--r-- | login/updwtmp.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/login/updwtmp.c b/login/updwtmp.c new file mode 100644 index 0000000000..fb0c74c9cf --- /dev/null +++ b/login/updwtmp.c @@ -0,0 +1,69 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <utmp.h> +#include <sys/file.h> +#include <sys/stat.h> + +#include "utmp-private.h" + + +void +updwtmp (const char *wtmp_file, const struct utmp *utmp) +{ + struct stat st; + size_t written; + int fd; + + /* Open WTMP file. */ + fd = __open (wtmp_file, O_WRONLY | O_APPEND); + if (fd < 0) + return; + + /* Try to lock the file. */ + if (__flock (fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS) + { + /* Oh, oh. The file is already locked. Wait a bit and try again. */ + sleep (1); + + /* This time we ignore the error. */ + __flock (fd, LOCK_EX | LOCK_NB); + } + + /* Remember original size of log file: */ + if (__fstat (fd, &st) < 0) + goto done; + + /* Write the entry. If we can't write all the bytes, reset the file + size back to the original size. That way, no partial entries + will remain. */ + written = __write (fd, utmp, sizeof (struct utmp)); + if (written > 0 && written != sizeof (struct utmp)) + ftruncate (fd, st.st_size); + +done: + /* And unlock the file. */ + __flock (fd, LOCK_UN); + + /* Close WTMP file. */ + __close (fd); +} |