Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when does open(2) fail with errno == EMLINK?

i came across a bit of code which says

int fd = open(fn, flags, 0);
if (fd < 0 && errno != EMLINK)
  ...

flags is either O_RDONLY or O_RDONLY|O_NOFOLLOW

IEEE Std 1003.1, 2013 (SUSv4) has just

  • [EMLINK] Too many links. An attempt was made to have the link count of a single file exceed {LINK_MAX}.

  • {LINK_MAX} Maximum number of links to a single file.

how does opening a file increase its link count?

like image 308
just somebody Avatar asked Jun 06 '14 15:06

just somebody


2 Answers

Good question. When O_NOFOLLOW was added, they chose to reuse an existing error code rather than make up a new one. EMLINK in this case signifies that the file is a symlink and is returned on FreeBSD. Linux and Darwin return ELOOP rather than EMLINK, while NetBSD uses EFTYPE.

My manpages say O_NOFOLLOW is a FreeBSD extension subsequently added to Linux (ie you won't find its behaviour explained in older versions of the SUS, but it is included in the POSIX 2008 with the Linux return code).

like image 181
Nicholas Wilson Avatar answered Nov 20 '22 08:11

Nicholas Wilson


This is not specified by SUS v4. See http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html

like image 2
Jean-Baptiste Yunès Avatar answered Nov 20 '22 10:11

Jean-Baptiste Yunès