From 655224ac2a65ea3b21301474069e768015e24665 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 8 Aug 1997 00:27:32 +0000 Subject: [PATCH] Definition of updwtmp function. --- login/updwtmp.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 login/updwtmp.c 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 , 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 +#include +#include +#include +#include +#include + +#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); +}