Subject: mkstemp and magiclinks (@uid)
To: None <tech-userlevel@netbsd.org>
From: Juan RP <juan@xtrarom.org>
List: tech-userlevel
Date: 02/15/2007 10:10:30
Hi, I've been playing with perusertmp and when it's enabled man(1)
does not work because couldn't use /tmp.

I verified with the man code and it uses mkstemp(3) to tranform the
manpage (or whatever it does).

The following shows a simple test program to demonstrate that
mkstemp(3) thinks that the link is not a link or directory.

[juan@nocturno][~/mkstemp]> sudo sysctl -w vfs.generic.magiclinks=1
vfs.generic.magiclinks: 0 -> 1
[juan@nocturno][~/mkstemp]>

[juan@nocturno][~/mkstemp]> mkdir blah
[juan@nocturno][~/mkstemp]> ./test blah
blah/test.01975a created
[juan@nocturno][~/mkstemp]> ln -s blah/@uid foo
[juan@nocturno][~/mkstemp]> ls -l
total 48
-rw-r--r--  1 juan  wheel    48 Feb 14 21:49 Makefile
drwxr-xr-x  2 juan  wheel   512 Feb 15 10:02 blah
lrwxr-xr-x  1 juan  wheel     9 Feb 15 10:02 foo -> blah/@uid
-rwxr-xr-x  1 juan  wheel  6424 Feb 14 21:49 test
-rw-r--r--  1 juan  wheel   707 Feb 14 21:23 test.c
-rw-r--r--  1 juan  wheel  1520 Feb 14 21:49 test.o
[juan@nocturno][~/mkstemp]> ./test foo
test: not a directory or symlink to directory
[juan@nocturno][~/mkstemp]>

Does anyone know what's the problem?

This is the code I'm using to verify that mkstemp works:

#include <sys/param.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TMPL    "test.XXXXXX"

int main(int argc, char **argv)
{
        struct stat st;
        char buf[MAXPATHLEN - 1];

        setprogname(argv[0]);

        if (argc != 2) {
                (void)printf("usage: %s directory\n", getprogname());
                return 1;
        }

        stat(argv[1], &st);
        (void)snprintf(buf, sizeof(buf), "%s/%s", argv[1], TMPL);

        switch (st.st_mode & S_IFMT) {
        case S_IFDIR:
        case S_IFLNK:
                if (mkstemp(buf) == -1)
                        errx(errno, "mkstemp %s", strerror(errno));

                (void)printf("%s created\n", buf);
                break;
        default:
                errx(1, "not a directory or symlink to directory");
        }

        return 0;
}

-- 

http://plog.xtrarom.org/
Juan RP's blog - NetBSD/pkgsrc news in Spanish