1

I would like to use mknod in my code to create a file, but man says, that

The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified.

Does that mean, that mknod is not really portable and I should use some other way to create a function? How about calling open and the instantly close? Which way is safer?

gruszczy
  • 40,948
  • 31
  • 128
  • 181

2 Answers2

7

mknod is not for creating files. It's for creating device nodes. A portable application will never need to create device nodes because whether they exist, what they are, and how they're implemented/numbered is an implementation detail.

mknod historically allowed you to create fifos too (and perhaps ordinary files?), but there are instead standard interfaces for this: mkfifo for fifos, and creat (or open with O_CREAT) for ordinary files.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Still, I need to open and then close the returned descriptor, which requires two operations (and four damn lines, since I am always checking the return codes). So there is no single operation for creating a file? – gruszczy Dec 28 '10 at 23:52
  • I generally consider checking the return value of `close` useless. If you pass it a valid file descriptor, the only way `close` can fail is if you've installed an interrupting signal handler (which I consider harmful, aside from very isolated and intentional usage) or on I/O errors (e.g. with NFS). And since there's really no good way of recovering from the latter, I follow the principle of "never test for an error you don't know how to handle". :-) – R.. GitHub STOP HELPING ICE Dec 29 '10 at 03:01
  • Oh, but to answer your question, no, there is no single library call to create a file. Are you really sure you'd want to create a file without having it open? Usually you want to use `O_EXCL` when creating new files to ensure that you really were the one to create it, and if you close and later reopen the file, the whole point of `O_EXCL` is defeated. – R.. GitHub STOP HELPING ICE Dec 29 '10 at 03:03
1

If you read the rest of that paragraph, you will see:

However, nowadays one should never use mknod() for this purpose;
one should use mkfifo(3), a function especially defined for this purpose.

so what POSIX.1-2001says here is already outdated. I would just ignore it. mknodis still being used by the init scripts in the Linux systems.

ismail
  • 46,010
  • 9
  • 86
  • 95
  • 2
    In a linux init script, mknod is being used for an appropriate implementation-specific purpose, ie making a device node. – Chris Stratton Dec 28 '10 at 17:35
  • I have assumed the author was asking for creating a device node :) – ismail Dec 28 '10 at 17:47
  • OP seems to be using it to create ordinary files. In all my years of unix coding, I had no idea there were implementations of `mknod` that could even do this, and it sounds like a really horrible abuse of the function... – R.. GitHub STOP HELPING ICE Dec 29 '10 at 03:04