Author: ed
Date: Sat Feb 11 11:11:13 2012
New Revision: 231514
URL: http://svn.freebsd.org/changeset/base/231514

Log:
  Set read buffer size to multiple of sizeof(struct futx).
  
  If the utmpx database gets updated while an application is reading it,
  there is a chance the reading application processes partially
  overwritten entries. To solve this, make sure we always read a multiple
  of sizeof(struct futx) at a time.
  
  MFC after:    2 weeks

Modified:
  head/lib/libc/gen/getutxent.c

Modified: head/lib/libc/gen/getutxent.c
==============================================================================
--- head/lib/libc/gen/getutxent.c       Sat Feb 11 08:58:12 2012        
(r231513)
+++ head/lib/libc/gen/getutxent.c       Sat Feb 11 11:11:13 2012        
(r231514)
@@ -70,13 +70,18 @@ setutxdb(int db, const char *file)
        if (uf == NULL)
                return (-1);
 
-       /* Safety check: never use broken files. */
-       if (db != UTXDB_LOG && _fstat(fileno(uf), &sb) != -1 &&
-           sb.st_size % sizeof(struct futx) != 0) {
-               fclose(uf);
-               uf = NULL;
-               errno = EFTYPE;
-               return (-1);
+       if (db != UTXDB_LOG) {
+               /* Safety check: never use broken files. */
+               if (_fstat(fileno(uf), &sb) != -1 &&
+                   sb.st_size % sizeof(struct futx) != 0) {
+                       fclose(uf);
+                       uf = NULL;
+                       errno = EFTYPE;
+                       return (-1);
+               }
+               /* Prevent reading of partial records. */
+               (void)setvbuf(uf, NULL, _IOFBF,
+                   rounddown(BUFSIZ, sizeof(struct futx)));
        }
 
        udb = db;
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to