mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: ?????? <jianzhong.huang@i-soft.com.cn>
Cc: musl@lists.openwall.com
Subject: Re: fdopen/fflush problem
Date: Fri, 26 Sep 2014 08:37:58 +0200	[thread overview]
Message-ID: <20140926063757.GE21835@port70.net> (raw)
In-Reply-To: <54250245.2070108@i-soft.com.cn>

* ?????? <jianzhong.huang@i-soft.com.cn> [2014-09-26 14:05:57 +0800]:
> Hi, there,
> I encounter a problem about fdopen and fflush with musl, here is the code:

the strace is

open("/tmp/abcdeLidlpm", O_ACCMODE|O_CREAT|O_EXCL|O_LARGEFILE|O_CLOEXEC, 0600) = 3
fcntl64(3, F_SETFD, FD_CLOEXEC)         = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
writev(1, [{"filename: /tmp/abcdeLidlpm", 26}, {"\n", 1}], 2filename: /tmp/abcdeLidlpm
) = 27
fcntl64(3, F_SETFD, FD_CLOEXEC)         = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfbdb314) = -1 ENOTTY (Inappropriate ioctl for device)
writev(3, [{"test string\n", 12}, {NULL, 0}], 2) = -1 EBADF (Bad file descriptor)

> int main()
> {
> char filename[17] = "/tmp/abcdeXXXXXX";
> int fd = mkostemp(filename, O_WRONLY|O_CLOEXEC);

it seems O_WRONLY gets lost

this is what musl does:

	if ((fd = open(template, flags | O_RDWR | O_CREAT | O_EXCL, 0600))>=0)

and O_WRONLY is 1, O_RDWR is 2 and O_ACCMODE is 3

so it is a musl bug: due to historical accidents
O_RDONLY and O_WRONLY are not proper bitflags
and thus O_WRONLY|O_RDWR has special meaning

(for details see "File access mode" section in
http://man7.org/linux/man-pages/man2/open.2.html
)

> 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?
> 
> By the way, I did not subscribe to this mail list with this mail box,
> please CC to jianzhong.huang AT i-soft.com.cn
> 
> -- 
> Huang JianZhong
> 
> 


      parent reply	other threads:[~2014-09-26  6:37 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
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 [this message]

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=20140926063757.GE21835@port70.net \
    --to=nsz@port70.net \
    --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).