On Wed, Jul 25, 2012 at 11:37:32PM +0200, Frank Brodbeck wrote:
> On Wed, Jul 25, 2012 at 10:51:56PM +0200, Otto Moerbeek wrote:
> > Userland prorams do not share memory or symbols with the kernel at all
> > that is a fundamental thing in Unix. Your code just references a bunch
> > of uninitialized vars. 
> > 
> > Chekc opendev(3) (source in src/lib/libutil/opendev.c) which is a
> > userland function to do the translation you want. Note it interfaces
> > with the kernel via ioctl(2), the actual work is done by the diskmap
> > device driver that calls disk_map(), all in kernel mode.
> 
> Funny, I first looked at opendev(3) and the DIOCMAP ioctl before I went
> on to disk_map(). Obviously I missed something. Will have a look at
> opendev(3) again. 
> 
> Thanks a lot for the pointer.
> 
> Frank.

Maybe this helps:

#include <sys/types.h>
#include <sys/dkio.h>
#include <sys/disk.h>
#include <sys/ioctl.h>

#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
        struct dk_diskmap dm;
        const char *dmpath = "/dev/diskmap";
        char dev[PATH_MAX];
        char *d;

        if (argc != 2) {
                fprintf(stderr, "usage: duid2dev <duid|device>\n");
                exit(1);
        }

        bzero(&dm, sizeof(dm));
        dm.flags = DM_OPENPART;

        if ((dm.fd = open(dmpath, O_RDONLY)) < 0)
                err(1, "open: %s", dmpath);

        strlcpy(dev, argv[1], PATH_MAX);
        dm.device = dev;

        if (ioctl(dm.fd, DIOCMAP, &dm) == -1)
                err(1, "ioctl");

        d = strrchr(dm.device, '/');
        if (d[1] == 'r')
                dm.device = d + 2;
        else
                dm.device = d + 1;
        dm.device[strlen(dm.device) - 1] = '\0';

        printf("%s\n", dm.device);
        close(dm.fd);
        exit(0);
}

> 
> -- 
> Frank Brodbeck <f...@guug.de>

Reply via email to