开发者

open() fails to open files

For some reason I can't g开发者_StackOverflow中文版et open() to open a file. Here's my code.

static int context_ctor(X86Context *ctx)
{
    char file[512];
    memset(ctx, 0, sizeof(X86Context));

    sprintf(file, "%s.%d", "test", getpid());

    ctx->fp = open(file, O_RDWR);

    if(ctx->fp < 0) {
        printf("errno %d %s\n", errno, file);
        return VISUAL_ERROR_GENERAL;
    }

    ctx->buf = mmap(0, MAXFILESIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, ctx->fp, 0);

    printf("context_ctor: %p\n", ctx->buf);

    close(ctx->fp);
    exit(0);
}

And here's the output:

errno 2 test.12356

Looking up the error code reveals:

[EACCES]
    Permission denied.

I know I have permission to read/write/execute files in this directory. I even tried /tmp/test.pid. Any idea?


If you're trying to create a new file you need to use O_CREAT, so:

ctx->fp = open(file, O_CREATE | O_RDWR);

By the way, you may want to use strerror(errno) to show your errors

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜