Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/dev_mkdb FIxed a problem with string length (a lead...



details:   https://anonhg.NetBSD.org/src/rev/a06760c02bb3
branches:  trunk
changeset: 512651:a06760c02bb3
user:      manu <manu%NetBSD.org@localhost>
date:      Thu Jul 12 20:46:39 2001 +0000

description:
FIxed a problem with string length (a leading / was appearing in w, who and others)
We were using rename() to create the target file from the temp file. Now the temp file is created in the same directory of the targer file so that rename() will always work (it needs to have the 
files in the same filesystem)
Using the -o file, the output file may be on a world writable directory, we hence open the temporary file using O_EXCL, and we loop trying different names until it works.

diffstat:

 usr.sbin/dev_mkdb/dev_mkdb.c |  42 ++++++++++++++++++++++++++++++------------
 1 files changed, 30 insertions(+), 12 deletions(-)

diffs (81 lines):

diff -r 070c67d634f7 -r a06760c02bb3 usr.sbin/dev_mkdb/dev_mkdb.c
--- a/usr.sbin/dev_mkdb/dev_mkdb.c      Thu Jul 12 19:24:40 2001 +0000
+++ b/usr.sbin/dev_mkdb/dev_mkdb.c      Thu Jul 12 20:46:39 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dev_mkdb.c,v 1.13 2001/07/08 20:01:43 manu Exp $       */
+/*     $NetBSD: dev_mkdb.c,v 1.14 2001/07/12 20:46:39 manu Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "from: @(#)dev_mkdb.c   8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: dev_mkdb.c,v 1.13 2001/07/08 20:01:43 manu Exp $");
+__RCSID("$NetBSD: dev_mkdb.c,v 1.14 2001/07/12 20:46:39 manu Exp $");
 #endif
 #endif /* not lint */
 
@@ -88,6 +88,8 @@
        char *pathv[2];
        char path_dev[MAXPATHLEN + 1] = _PATH_DEV;
        char cur_dir[MAXPATHLEN + 1];
+       struct timeval tv;
+       char *q;
 
        while ((ch = getopt(argc, argv, "o:")) != -1)
                switch (ch) {
@@ -120,18 +122,34 @@
        if (ftsp == NULL)
                err(1, "fts_open: %s", path_dev);
 
-       /* 
-        * We use rename() to produce the dev.db file, and rename() 
-        * is not able to move files across filesystems. Hence we use
-        * /var/run for dev.tmp, since dev.db will be in /var/run.
-        */
-       (void)snprintf(dbtmp, MAXPATHLEN, "%sdev.tmp", _PATH_VARRUN);
        if (dbname_arg)
                strncpy(dbname, dbname_arg, MAXPATHLEN);
        else
                (void)snprintf(dbname, MAXPATHLEN, "%sdev.db", _PATH_VARRUN);
-       db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
-           S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
+       /* 
+        * We use rename() to produce the dev.db file from a temporary file,
+        * and rename() is not able to move files across filesystems. Hence we 
+        * need the temporary file to be in the same directory as dev.db.
+        *
+        * Additionally, we might be working in a world writable directory, 
+        * we must ensure that we are not opening an existing file, therefore
+        * the loop on dbopen. 
+        */
+       (void)strncpy(dbtmp, dbname, MAXPATHLEN);
+       q = rindex(dbtmp, '/');
+       errno = 0;
+       do {
+               (void)gettimeofday(&tv, NULL);
+               if (q) {
+                       (void)snprintf(q, MAXPATHLEN - (long)(q - dbtmp), 
+                           "%s.%ld.tmp", q, tv.tv_usec);       
+               } else {
+                       (void)snprintf(dbtmp, MAXPATHLEN, "./%s.%ld.tmp",
+                           dbname, tv.tv_usec);
+               }
+               db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
+                   S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
+       } while (errno == EEXIST);
        if (db == NULL)
                err(1, "%s", dbtmp);
 
@@ -167,8 +185,8 @@
                 * Create the data; nul terminate the name so caller doesn't
                 * have to.  Skip path_dev and slash.
                 */
-               strlcpy(buf, p->fts_path + strlen(path_dev), sizeof(buf));
-               data.size = p->fts_pathlen - strlen(path_dev) + 1;
+               strlcpy(buf, p->fts_path + (strlen(path_dev) + 1), sizeof(buf));
+               data.size = p->fts_pathlen - (strlen(path_dev) + 1) + 1;
                if ((*db->put)(db, &key, &data, 0)) {
                        err(1, "dbput %s", dbtmp);
                }



Home | Main Index | Thread Index | Old Index