From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6200 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: fdopen/fflush problem Date: Fri, 26 Sep 2014 02:18:10 -0400 Message-ID: <20140926061810.GQ23797@brightrain.aerifal.cx> References: <54250245.2070108@i-soft.com.cn> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1411712321 6931 80.91.229.3 (26 Sep 2014 06:18:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 26 Sep 2014 06:18:41 +0000 (UTC) Cc: musl@lists.openwall.com To: =?utf-8?B?6buE5bu65b+g?= Original-X-From: musl-return-6213-gllmg-musl=m.gmane.org@lists.openwall.com Fri Sep 26 08:18:35 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1XXOrR-0005mv-Ic for gllmg-musl@plane.gmane.org; Fri, 26 Sep 2014 08:18:33 +0200 Original-Received: (qmail 28364 invoked by uid 550); 26 Sep 2014 06:18:33 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 28350 invoked from network); 26 Sep 2014 06:18:32 -0000 Content-Disposition: inline In-Reply-To: <54250245.2070108@i-soft.com.cn> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:6200 Archived-At: 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 > #include > #include > > 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