From 49b986270035c7f86c3dc85383fd1da2e413fef9 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Thu, 18 Jul 1996 08:43:26 +0000 Subject: Thu Jul 18 04:33:21 1996 Roland McGrath * sysdeps/mach/hurd/errnos.awk: Move EWOULDBLOCK check before errno==0 check. mib's clueless hacking broke it. Thu Jul 18 04:27:21 1996 Roland McGrath * Makerules ($(libdir)/libc.so): Add missing / after $(cryptobjdir). Wed Jul 17 10:54:20 1996 David Mosberger-Tang * sysdeps/unix/sysv/linux/alpha/start.S: Move to ... * sysdeps/alpha/elf/start.S: here. * misc/sys/ustat.h: New file. * misc/ustat.h: Ditto. * sysdeps/generic/ustatbits.h: Ditto. * libc-orig/sysdeps/stub/ustat.c: Ditto. * sysdeps/unix/sysv/linux/ustatbits.h: Ditto. * misc/Makefile (headers): Mention ustat.h, sys/ustat.h, and ustatbits.h. (routines): Mention ustat. * sysdeps/unix/sysv/linux/syscalls.list: Mention klogctl in alphabetically correct place. Mention ustat. * sysdeps/unix/alpha/sysdep.S: Make errno a bss symbol, not a common symbol (makes errno accessible from gdb). * login/pututline_r.c (pututline_r): Test ut_id in repositioning test, not ut_type. * login/logwtmp.c (logwtmp): Reimplemented. Must ensure that a record always gets appended at end of file (wtmp is a log file, after all). Set ut_type to DEAD_PROCESS if name is empty. * login/getutid_r.c (getutid_r): Handle search for UT_TYPE in separate loop from search for UT_ID. Fix code so it is in sync with Unix man page. * login/getutline_r.c (getutline_r): Stop searching only if ut_line matches and type is either USER_PROCESS or LOGIN_PROCESS. Tue Jul 16 15:06:18 1996 David Mosberger-Tang * sysdeps/unix/sysv/linux/netinet/in.h: Base on to avoid duplicate declarations. Sun Jul 14 19:08:55 1996 David Mosberger-Tang * manual/maint.texi (Supported Configurations): Add alpha-gnu-linux and alpha-gnu-linuxecoff targets. Fri Jul 12 16:58:44 1996 David Mosberger-Tang * sysdeps/unix/sysv/linux/netinet/in.h: Declare bindresvport(). Sat Jun 22 23:30:07 1996 David Mosberger-Tang * resolv/res_send.c (res_send): Declare FROMLEN as size_t instead of int. * resolv/res_comp.c (res_hnok): Use empty statement in place of NULL (which generates a "useless expression" warning in gcc). --- login/logwtmp.c | 62 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 21 deletions(-) (limited to 'login/logwtmp.c') diff --git a/login/logwtmp.c b/login/logwtmp.c index 371f34e..2ae6a68 100644 --- a/login/logwtmp.c +++ b/login/logwtmp.c @@ -17,33 +17,31 @@ 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 void logwtmp (const char *line, const char *name, const char *host) { - struct utmp_data data; struct utmp ut; + struct stat st; + size_t written; + int fd; - /* Tell that we want to use the UTMP file. */ - if (utmpname (_PATH_WTMP) == 0) - return; - - /* Open UTMP file. */ - setutent_r (&data); - - /* Position at end of file. */ - data.loc_utmp = lseek (data.ut_fd, 0, SEEK_END); - if (data.loc_utmp == -1) - return; + /* Open WTMP file. */ + fd = __open (_PATH_WTMP, O_WRONLY | O_APPEND); + if (fd < 0) + return; /* Set information in new entry. */ - bzero (&ut, sizeof (ut)); + memset (&ut, 0, sizeof (ut)); + ut.ut_pid = getpid (); #if _HAVE_UT_TYPE - 0 - ut.ut_type = USER_PROCESS; + ut.ut_type = name[0] ? USER_PROCESS : DEAD_PROCESS; #endif strncpy (ut.ut_line, line, sizeof ut.ut_line); strncpy (ut.ut_name, name, sizeof ut.ut_name); @@ -52,14 +50,36 @@ logwtmp (const char *line, const char *name, const char *host) #endif #if _HAVE_UT_TV - 0 - gettimeofday (&ut.ut_tv, NULL); + __gettimeofday (&ut.ut_tv, NULL); #else - time (&ut.ut_time); + __time (&ut.ut_time); #endif - /* Write the entry. */ - pututline_r (&ut, &data); + /* 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); + } + + /* Remeber 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, &ut, sizeof (ut)); + if (written > 0 && written != sizeof (ut)) + ftruncate (fd, st.st_size); + +done: + /* And unlock the file. */ + __flock (fd, LOCK_UN); - /* Close UTMP file. */ - endutent_r (&data); + /* Close WTMP file. */ + __close (fd); } -- cgit v1.1