From: Rich Felker <dalias@libc.org>
To: 黄建忠 <jianzhong.huang@i-soft.com.cn>
Cc: musl@lists.openwall.com
Subject: Re: fdopen/fflush problem
Date: Fri, 26 Sep 2014 02:18:10 -0400 [thread overview]
Message-ID: <20140926061810.GQ23797@brightrain.aerifal.cx> (raw)
In-Reply-To: <54250245.2070108@i-soft.com.cn>
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:
http://man7.org/linux/man-pages/man3/mkstemp.3.html
Rich
next prev parent reply other threads:[~2014-09-26 6:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-26 6:05 黄建忠
2014-09-26 6:18 ` Rich Felker [this message]
2014-09-26 6:39 ` Szabolcs Nagy
2014-09-26 7:05 ` Rich Felker
2014-09-26 7:23 ` u-wsnj
2014-09-27 13:29 ` Anthony G. Basile
2014-09-27 13:38 ` Rich Felker
2014-09-27 16:56 ` Anthony G. Basile
2014-09-26 6:37 ` Szabolcs Nagy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140926061810.GQ23797@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=jianzhong.huang@i-soft.com.cn \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).