On Mon, Apr 08, 2019 at 12:35:41AM +0100, Rafael Neves wrote:
> Hi tech@,
> 
> When I had to change a HD between machines I figured out that -P option
> of mount_mfs(8) does not work with DUIDs: 
> 
> # mount_mfs -P ca7552589896b01e.d swap /mnt
> mount_mfs: cannot stat ca7552589896b01e.d: No such file or directory
> 
> mount_mfs(8) already allows a DUID as the `special` argument, it is not
> documented though. The patch bellow allow using -P with a DUID as block
> device.
> 
> Important:
> I used the same approach used in the code (pointer swap) to resolve DUID
> to device using opendev(3). But after the patch there will be two
> opendev(3) calls with non-NULL realpath, and as manpage states the
> stored path is overwritten by subsequent calls.
> 
> It took some time of code reading and rereading until I convinced myself
> that there is no problem in this *specific* situation.
> 
> However, I think that should be safier to use malloc(size = PATH_MAX) +
> strlcpy() on `pop` and `special`. That way, code refactoring doesn't to
> care about the relative position of the opendev(3) calls.
> 
> What do you think? Comments?
> 
[snip]

Here is a revised patch that uses malloc(3)+strlcpy(3) idiom, and 
document that newfs(8) accepts DUID as `special` and as an argument
of -P option.

Comments?

Regards,
Rafael

Patch:

Index: sbin/newfs/newfs.8
===================================================================
RCS file: /cvs/src/sbin/newfs/newfs.8,v
retrieving revision 1.74
diff -u -p -u -p -r1.74 newfs.8
--- sbin/newfs/newfs.8  17 Mar 2016 07:18:34 -0000      1.74
+++ sbin/newfs/newfs.8  14 Apr 2019 19:30:07 -0000
@@ -88,8 +88,10 @@ The
 .Ar special
 file should be a raw device,
 for example
-.Pa /dev/rsd0a ;
-if a relative path like
+.Pa /dev/rsd0a ,
+or a
+.Xr disklabel 8
+UID (DUID); if a relative path like
 .Pa sd0a
 is specified,
 the corresponding raw device is used.
@@ -293,7 +295,7 @@ is a directory, populate the created mfs
 contents of the directory.
 If
 .Ar file
-is a block device, populate the created mfs file system with the
+is a block device or a DUID, populate the created mfs file system with the
 contents of the FFS file system contained on the device.
 .El
 .Pp
Index: sbin/newfs/newfs.c
===================================================================
RCS file: /cvs/src/sbin/newfs/newfs.c,v
retrieving revision 1.111
diff -u -p -u -p -r1.111 newfs.c
--- sbin/newfs/newfs.c  25 Nov 2018 17:12:10 -0000      1.111
+++ sbin/newfs/newfs.c  14 Apr 2019 19:30:07 -0000
@@ -174,7 +174,7 @@ main(int argc, char *argv[])
        struct stat st;
        struct statfs *mp;
        struct rlimit rl;
-       int fsi = -1, oflagset = 0, fso, len, n, maxpartitions;
+       int fsi = -1, oflagset = 0, fso, fp, fd, len, n, maxpartitions;
        char *cp = NULL, *s1, *s2, *special, *opstring, *realdev;
 #ifdef MFS
        char mountfromname[BUFSIZ];
@@ -382,7 +382,10 @@ main(int argc, char *argv[])
                fso = opendev(special, O_WRONLY, 0, &realdev);
                if (fso < 0)
                        fatal("%s: %s", special, strerror(errno));
-               special = realdev;
+
+               if ((special = malloc(PATH_MAX)) == NULL)
+                       fatal("cannot allocate memory");
+               strlcpy(special, realdev, PATH_MAX);
 
                /* Bail if target special is mounted */
                n = getmntinfo(&mp, MNT_NOWAIT);
@@ -526,6 +529,17 @@ havelabel:
                        args.export_info.ex_flags = MNT_EXRDONLY;
                if (mntflags & MNT_NOPERM)
                        mntflags |= MNT_NODEV | MNT_NOEXEC;
+
+               if (pop != NULL && isduid(pop, 0)) { 
+                       fd = opendev(pop, O_RDONLY, OPENDEV_BLCK, &realdev);
+                       if (fd < 0)
+                               err(1, "could not open %s", pop);
+                       close(fd);
+
+                       if ((pop = malloc(PATH_MAX)) == NULL)
+                               fatal("cannot allocate memory");
+                       strlcpy(pop, realdev, PATH_MAX);
+               }
 
                switch (pid = fork()) {
                case -1:

Reply via email to