tags:

views:

36

answers:

1

For some reason I can't get 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?

+2  A: 

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

wich
That got it. Thanks.
Scott