Date: Tue, 12 Jan 2010 22:06:54 +0000 (UTC) From: Ed Schouten <ed@FreeBSD.org> To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r202177 - user/ed/utmpx/lib/libc/gen Message-ID: <201001122206.o0CM6s7Z014046@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: ed Date: Tue Jan 12 22:06:53 2010 New Revision: 202177 URL: http://svn.freebsd.org/changeset/base/202177 Log: Several improvements to the handling of the utmpx files. - Just use O_EXLOCK instead of calling lockf() by hand. This will already provide exclusive file access. - Let the functions use stdio instead of operating on the file descriptors directly. When utmp and lastlogin files get a bit bigger, it will read chunks of 4K instead of 197 bytes at a time. Modified: user/ed/utmpx/lib/libc/gen/pututxline.c Modified: user/ed/utmpx/lib/libc/gen/pututxline.c ============================================================================== --- user/ed/utmpx/lib/libc/gen/pututxline.c Tue Jan 12 21:45:03 2010 (r202176) +++ user/ed/utmpx/lib/libc/gen/pututxline.c Tue Jan 12 22:06:53 2010 (r202177) @@ -31,35 +31,43 @@ __FBSDID("$FreeBSD$"); #include <sys/endian.h> #include <sys/stat.h> #include <fcntl.h> +#include <stdio.h> #include <string.h> #include <unistd.h> #include <utmpx.h> #include "utxdb.h" #include "un-namespace.h" -static int +static FILE * futx_open(const char *file) { int fd; + FILE *fp; struct stat sb; - fd = _open(file, O_CREAT|O_RDWR, 0644); + fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK, 0644); if (fd < 0) - return (-1); + return (NULL); /* Safety check: never use broken files. */ if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { _close(fd); - return (-1); + return (NULL); + } + + fp = fdopen(fd, "r+"); + if (fp == NULL) { + _close(fd); + return (NULL); } - return (fd); + return (fp); } static void utx_active_add(const struct futx *fu) { - int fd; + FILE *fp; struct futx fe; off_t partial = -1; @@ -67,14 +75,10 @@ utx_active_add(const struct futx *fu) * Register user login sessions. Overwrite entries of sessions * that have already been terminated. */ - fd = futx_open(_PATH_UTX_ACTIVE); - if (fd < 0) - return; - if (lockf(fd, F_LOCK, 0) == -1) { - _close(fd); + fp = futx_open(_PATH_UTX_ACTIVE); + if (fp == NULL) return; - } - while (_read(fd, &fe, sizeof fe) == sizeof fe) { + while (fread(&fe, sizeof fe, 1, fp) == 1) { switch (fe.fu_type) { case USER_PROCESS: case INIT_PROCESS: @@ -82,7 +86,7 @@ utx_active_add(const struct futx *fu) case DEAD_PROCESS: /* Overwrite when ut_id matches. */ if (memcmp(fu->fu_id, fe.fu_id, sizeof fe.fu_id) == 0) { - lseek(fd, -sizeof fe, SEEK_CUR); + fseeko(fp, -sizeof fe, SEEK_CUR); goto exact; } if (fe.fu_type != DEAD_PROCESS) @@ -91,7 +95,7 @@ utx_active_add(const struct futx *fu) default: /* Allow us to overwrite unused records. */ if (partial == -1) - partial = lseek(fd, 0, SEEK_CUR) - sizeof fe; + partial = fseeko(fp, 0, SEEK_CUR) - sizeof fe; break; } } @@ -101,29 +105,25 @@ utx_active_add(const struct futx *fu) * match was found, just append a new record. */ if (partial != -1) - lseek(fd, partial, SEEK_SET); + fseeko(fp, partial, SEEK_SET); exact: - _write(fd, fu, sizeof *fu); - _close(fd); + fwrite(fu, sizeof *fu, 1, fp); + fclose(fp); } static int utx_active_remove(struct futx *fu) { - int fd; + FILE *fp; struct futx fe; /* * Remove user login sessions, having the same ut_id. */ - fd = futx_open(_PATH_UTX_ACTIVE); - if (fd < 0) - return (0); - if (lockf(fd, F_LOCK, 0) == -1) { - _close(fd); + fp = futx_open(_PATH_UTX_ACTIVE); + if (fp == NULL) return (0); - } - while (_read(fd, &fe, sizeof fe) == sizeof fe) { + while (fread(&fe, sizeof fe, 1, fp) == 1) { switch (fe.fu_type) { case USER_PROCESS: case INIT_PROCESS: @@ -139,14 +139,14 @@ utx_active_remove(struct futx *fu) fu->fu_tv = fe.fu_tv; /* Terminate session. */ - lseek(fd, -sizeof fe, SEEK_CUR); - _write(fd, fu, sizeof *fu); - _close(fd); + fseeko(fp, -sizeof fe, SEEK_CUR); + fwrite(fu, sizeof *fu, 1, fp); + fclose(fp); return (0); } } - _close(fd); + fclose(fp); return (1); } @@ -160,7 +160,7 @@ utx_active_purge(void) static void utx_lastlogin_add(const struct futx *fu) { - int fd; + FILE *fp; struct futx fe; /* @@ -168,14 +168,10 @@ utx_lastlogin_add(const struct futx *fu) * current user already has an entry. If not, append a new * entry. */ - fd = futx_open(_PATH_UTX_LASTLOGIN); - if (fd < 0) - return; - if (lockf(fd, F_LOCK, 0) == -1) { - _close(fd); + fp = futx_open(_PATH_UTX_LASTLOGIN); + if (fp == NULL) return; - } - while (_read(fd, &fe, sizeof fe) == sizeof fe) { + while (fread(&fe, sizeof fe, 1, fp) == 1) { if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0) continue; @@ -184,12 +180,12 @@ utx_lastlogin_add(const struct futx *fu) goto done; /* Found a previous lastlogin entry for this user. */ - lseek(fd, -sizeof fe, SEEK_CUR); + fseeko(fp, -sizeof fe, SEEK_CUR); break; } - _write(fd, fu, sizeof *fu); + fwrite(fu, sizeof *fu, 1, fp); done: - _close(fd); + fclose(fp); } static void
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001122206.o0CM6s7Z014046>