Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 26 Sep 2014 02:18:10 -0400
From: Rich Felker <>
To: 黄建忠 <>
Subject: Re: fdopen/fflush problem

On Fri, Sep 26, 2014 at 02:05:57PM +0800, 黄建忠 wrote:
> Hi, there,
> I encounter a problem about fdopen and fflush with musl, here is the code:
> #include <stdio.h>
> #include <stdlib.h>
> #include <fcntl.h>
> int main()
> {
> char filename[17] = "/tmp/abcdeXXXXXX";
> int fd = mkostemp(filename, O_WRONLY|O_CLOEXEC);
> printf("filename: %s\n", filename);
> if(fd < 0)
> printf("fd error\n");
> FILE *f = fdopen(fd, "we");
> fputs("test string\n", f);
> fflush(f);
> if(ferror(f))
> printf("file error\n");
> fclose(f);
> }
> As expected, the final file should contains one line and no ferror reported.
> Anybody can help to check it?

See the following text from the man page:

    The mkostemp() function is like mkstemp(), with the difference
    that the following bits—with the same meaning as for open(2)—may
    be specified in flags: O_APPEND, O_CLOEXEC, and O_SYNC. Note that
    when creating the file, mkostemp() includes the values O_RDWR,
    O_CREAT, and O_EXCL in the flags argument given to open(2);
    including these values in the flags argument given to mkostemp()
    is unnecessary, and produces errors on some systems.

mkostemp implicitly uses the access mode O_RDWR. The flags argument
should only contain additional flags you want, not the access mode.
When you pass O_WRONLY, it gets OR'd onto O_RDWR, producing a result
of "3" which is "no access".

I could add code to make mkostemp strip the access mode bits before
OR'ing on O_RDWR, but since it's documented in the man page that this
can "produce errors", I think it's best for you just not to pass
O_WRONLY. Changing this line:

int fd = mkostemp(filename, O_WRONLY|O_CLOEXEC);

to this:

int fd = mkostemp(filename, O_CLOEXEC);

should fix the problem.

Here's a link to the man page:


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.