4

Language: C OS: Ubuntu

I'm simply trying to create a FIFO named pipe using the command:

state = mknod("pipe.txt", S_IFIFO | 0666, 0);

the problem is i always get the state's value to be -1 (meaning it has failed) instead of 0.

perror returns 'pipe.txt: File exists'

i have no idea how should i debug such issue or what could be the reason, hope anyone code guide me what's wrong.

(note: the file pipe.txt exist on same path as source file.)

S..
  • 5,511
  • 2
  • 36
  • 43
Popokoko
  • 6,413
  • 16
  • 47
  • 58
  • "mknod() returns zero on success, or -1 if an error occurred (in which case, errno is set appropriately)." What is `errno`? If you use `perror`, this is very easy to see from the beginning. – Jonathon Reinhart May 15 '13 at 07:46
  • Use `perror("mknod: pipe.txt")` after detecting `mknod()` failure. – jxh May 15 '13 at 07:46
  • When a system call such as `mkfifo` returns `-1` it means there is an error. To get the error you need to look at `errno`. To print a usable string version of the error code in `errno` use either `strerror` to get a printable string, or use `perror` to print it directly. However, you should only check `errno` **iff** a function failed (i.e. returned `-1` or `NULL` or what it specifies as a failed return value). – Some programmer dude May 15 '13 at 07:47
  • Your'e right about perror, the error im getting: 'pipe.txt: File exists'. it might be i don't understand how things work but i thought the file is suppose to be exist, isn't that the whole idea? – Popokoko May 15 '13 at 07:49
  • Popokoko, no, if you follow the link in my answer (http://linux.die.net/man/2/mknod), you'll see that it's _not_ supposed to exist. `mknod` _creates_ an entry in the filesystem space. – paxdiablo May 15 '13 at 07:52

2 Answers2

5

Read: int mknod(const char *path, mode_t mode, rdev_t dev_identifier);
General Description:
Creates a new character special file or FIFO special file (named pipe), with the path name specified in the path argument.

If file already exists then it will fails with error: File exists

To avoid this error, remove(unlink()) the file, As I am doing in my below code(read comment):

int main() {
  char* file="pipe.txt";
  unlink(file);  // Add before mknod()
  int state = mknod(file, S_IFIFO | 0666, 0);
  if(state < 0){
    perror("mknod() error");
  }
  return 0;
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Just make sure it's _your_ file before you unlink it :-) – paxdiablo May 15 '13 at 08:05
  • 1
    thank you @Grijesh Chauhan, your code example clarify it. i had some misunderstanding of how things work. Also, thank you @paxdiablo! – Popokoko May 15 '13 at 08:14
  • small update: i think i messed up things, after using the unlink command im unable to run my project again, something is stuck in the background: while building it says: Blocked: The user operating is waiting for background work :( – Popokoko May 15 '13 at 08:18
  • @Popokoko consider Paxdiablo's comment also. – Grijesh Chauhan May 15 '13 at 08:28
  • Do u have any idea how can i stop that blocked issue in the background? – Popokoko May 15 '13 at 08:43
  • @Popokoko Sorry Popokoko :( No idea!!...it may depends on your project as well, If its only due to after adding `unlink()` then first read unlink's man page, I linked. – Grijesh Chauhan May 15 '13 at 08:48
  • Thank you for the great answer. Could we possibly turn this into a mimimum working example by adding all necessary `#include` statements? – puk Nov 11 '13 at 19:57
0

You should examine errno to see what the error is but it's probably EEXIST since I believe that's what happens if the file already exists.

From the Linux documentation for mknod:

If pathname already exists, or is a symbolic link, this call fails with an EEXIST error.

However, if the file already exists and is the pipe you created in an earlier run, you can safely reopen it. All mknod (and its often preferred cousin, mkfifo) does is actually create the FIFO, you still have to open it at both ends to get the data transfer happening.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Erm, I think i now get it, i was pretty sure it should get an EXISTING file, though i understand it should get a filedescriptor, or something like that – Popokoko May 15 '13 at 07:54
  • @Popokoko, you don't get a file descriptor from `mknod`, you only get a 0 or -1. You have to _open_ the created fifo in order to use it. – paxdiablo May 15 '13 at 07:58