Home > Archive > Unix Programming > September 2006 > open FIFO for write error
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
open FIFO for write error
|
|
| freegnu 2006-09-25, 10:01 pm |
| #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define StreamFifo "test.mpg"
int main()
{
int h_r, h_w, pid;
unlink(StreamFifo);
mkfifo(StreamFifo,0666);
pid = fork();
if(pid < 0){
return -1;
}else if(0 == pid){
h_r = open(StreamFifo,O_RDONLY | O_CREAT | O_TRUNC | O_NDELAY);
if(-1 == h_r)printf("read ");
printf("hr = %d\n", h_r);
}else{
h_w = open(StreamFifo,O_WRONLY | O_CREAT | O_TRUNC | O_NDELAY);
if(-1 == h_w)printf("write error\n ");
if(ENXIO == errno)printf(" ENXIO\n");//this will be printed
}
return 0;
}
| |
| Alan Curry 2006-09-25, 10:01 pm |
| In article <efa581$20o$1@news.yaako.com>, freegnu <freegnu@163.com> wrote:
[snip headers and other boring stuff]
> mkfifo(StreamFifo,0666);
> pid = fork();
> if(pid < 0){
> return -1;
>
> }else if(0 == pid){
>
> h_r = open(StreamFifo,O_RDONLY | O_CREAT | O_TRUNC | O_NDELAY);
> if(-1 == h_r)printf("read ");
> printf("hr = %d\n", h_r);
> }else{
> h_w = open(StreamFifo,O_WRONLY | O_CREAT | O_TRUNC | O_NDELAY);
> if(-1 == h_w)printf("write error\n ");
> if(ENXIO == errno)printf(" ENXIO\n");//this will be printed
> }
You have a race condition. The parent tries to open the FIFO for writing and
the child tries to open it for reading, but you can't know which one will
happen first. If the reader opens first, everything is fine. If the writer
opens first, it must block until the reader shows up. Since you disabled
blocking with O_NDELAY, you get the ENXIO.
The fix: don't use O_NDELAY in the open(). If you want to use the pipe in
non-blocking mode, you can set O_NONBLOCK with fcntl() after the open
succeeds.
--
The attacker\x92s overall goal would very probably be to convince other users
to run an unsafe program, by using the digital signature to convince them
that it is actually bona fide Microsoft software and therefore safe to run.
-- security bulletin MS01-017 ushers in a new definition of "safe"
| |
| Barry Margolin 2006-09-26, 4:01 am |
| In article <efa581$20o$1@news.yaako.com>, "freegnu" <freegnu@163.com>
wrote:
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
> #include <errno.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
>
> #define StreamFifo "test.mpg"
>
> int main()
> {
> int h_r, h_w, pid;
> unlink(StreamFifo);
> mkfifo(StreamFifo,0666);
> pid = fork();
> if(pid < 0){
> return -1;
>
> }else if(0 == pid){
>
> h_r = open(StreamFifo,O_RDONLY | O_CREAT | O_TRUNC | O_NDELAY);
> if(-1 == h_r)printf("read ");
> printf("hr = %d\n", h_r);
> }else{
> h_w = open(StreamFifo,O_WRONLY | O_CREAT | O_TRUNC | O_NDELAY);
> if(-1 == h_w)printf("write error\n ");
> if(ENXIO == errno)printf(" ENXIO\n");//this will be printed
> }
> return 0;
>
> }
Isn't this basically the same problem you wrote about in your other
thread, titled "open FIFO error"? Please don't start two different
threads for the same issue, it gets very confusing.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| freegnu 2006-09-26, 4:01 am |
| if find , when
h_w = open(StreamFifo,O_WRONLY | O_CREAT | O_TRUNC );
fcntl(h_w, O_NDELAY);//i will return -1
the fcntl() will return -1;
"Alan Curry" <pacman@TheWorld.com> 写入消息新闻:efa6dl$q1l$1@pcls6.std.com...
> In article <efa581$20o$1@news.yaako.com>, freegnu <freegnu@163.com> wrote:
> [snip headers and other boring stuff]
>
> You have a race condition. The parent tries to open the FIFO for writing
> and
> the child tries to open it for reading, but you can't know which one will
> happen first. If the reader opens first, everything is fine. If the writer
> opens first, it must block until the reader shows up. Since you disabled
> blocking with O_NDELAY, you get the ENXIO.
>
> The fix: don't use O_NDELAY in the open(). If you want to use the pipe in
> non-blocking mode, you can set O_NONBLOCK with fcntl() after the open
> succeeds.
>
> --
> The attacker\x92s overall goal would very probably be to convince other
> users
> to run an unsafe program, by using the digital signature to convince them
> that it is actually bona fide Microsoft software and therefore safe to
> run.
> -- security bulletin MS01-017 ushers in a new definition of
> "safe"
| |
| Nils O. Sel錽dal 2006-09-26, 4:01 am |
| freegnu wrote:
> if find , when
> h_w = open(StreamFifo,O_WRONLY | O_CREAT | O_TRUNC );
> fcntl(h_w, O_NDELAY);//i will return -1
>
> the fcntl() will return -1;
That's because you're using fcntl wrongly. See the documentaton,
and read it atleast twice.
You'd want e.g. fcntl(h_w,F_SETFL, O_NONBLOCK);
You're also using open wrongly, if you specify the O_CREAT flag,
the 3 argument - mode - must also be specified.
|
|
|
|
|