From owner-svn-src-user@FreeBSD.ORG Tue Jan 12 09:55:25 2010 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 784B310656A8; Tue, 12 Jan 2010 09:55:25 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 4F0478FC0C; Tue, 12 Jan 2010 09:55:25 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0C9tPnt047683; Tue, 12 Jan 2010 09:55:25 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0C9tPZf047681; Tue, 12 Jan 2010 09:55:25 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201001120955.o0C9tPZf047681@svn.freebsd.org> From: Ed Schouten Date: Tue, 12 Jan 2010 09:55:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202147 - user/ed/utmpx/lib/libc/gen X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2010 09:55:25 -0000 Author: ed Date: Tue Jan 12 09:55:25 2010 New Revision: 202147 URL: http://svn.freebsd.org/changeset/base/202147 Log: Fix allocation policy of utx.active. It is a bad idea to always overwrite the first DEAD_PROCESS/invalid entry we see, because there may be another DEAD_PROCESS entry further on whose ut_id value matches the record we want to add, making getutxid() return multiple records. Be sure to scan the file for an exact match and only overwrite DEAD_PROCESS/invalid entries when no exact match was found. This still keeps the file size to a minimum, because the maximum size of the file will be limited to the highest amount of logins at a certain moment in 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 09:07:55 2010 (r202146) +++ user/ed/utmpx/lib/libc/gen/pututxline.c Tue Jan 12 09:55:25 2010 (r202147) @@ -61,6 +61,7 @@ utx_active_add(const struct futx *fu) { int fd; struct futx fe; + off_t partial = -1; /* * Register user login sessions. Overwrite entries of sessions @@ -78,15 +79,30 @@ utx_active_add(const struct futx *fu) case USER_PROCESS: case INIT_PROCESS: case LOGIN_PROCESS: - if (memcmp(fu->fu_id, fe.fu_id, sizeof fe.fu_id) != 0) - continue; + 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); + goto exact; + } + if (fe.fu_type != DEAD_PROCESS) + break; /* FALLTHROUGH */ default: - lseek(fd, -sizeof fe, SEEK_CUR); - goto found; + /* Allow us to overwrite unused records. */ + if (partial == -1) + partial = lseek(fd, 0, SEEK_CUR); + break; } } -found: + + /* + * No exact match found. Use the partial match. If no partial + * match was found, just append a new record. + */ + if (partial != -1) + lseek(fd, partial, SEEK_SET); +exact: _write(fd, fu, sizeof *fu); lockf(fd, F_ULOCK, 0); _close(fd);