1

I am trying to create a simple tool that can create, write and read a named pipe. Here is the code.

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        //create pipe
        system("rm /tmp/myPipe");
        system("mkfifo /tmp/myPipe");
        system("chmod 666 /tmp/myPipe");

        // write to pipe
        NSString *text = [NSString stringWithFormat:@"this is a test"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:@"/tmp/myPipe" isDirectory:NO])
        {
            NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];
            [fileHandle seekToEndOfFile];
            [fileHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
            [fileHandle closeFile];

        }
    }
    return 0;
}

The file is created correctly with perms of prw-rw-rw- 1 xxx wheel 0 May 17 09:48 myPipe|

When I run the tool it hangs at the line:

NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];

What am I missing here?

jscs
  • 63,694
  • 13
  • 151
  • 195
jbrown94305
  • 185
  • 2
  • 11
  • Not that it is the cause of your issue, but `-fileExistsAtPath:isDirectory:` takes a `BOOL *` argument as the second argument. Your code is only not crashing on that line because you passed `NO` which is equivalent to passing `NULL`. – bbum May 17 '17 at 17:49
  • You can't mix a fifo and a pipe, apparently. Much more info here: https://forums.developer.apple.com/thread/74001 – bbum May 17 '17 at 17:51

1 Answers1

0

Nothing is wrong here other than my complete lack of understanding of what the code does. There was no "hang" just the code waiting for an external msg in the pipe.

I just needed to launch terminal and enter

echo "foo" > /tmp/myPipe

Thanks to everyone who assisted ...

jbrown94305
  • 185
  • 2
  • 11
  • yes, more precisely: "Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa." – Kasper Aug 27 '18 at 21:16