public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
* readlink() example sometimes fails
@ 2016-08-18  5:30 Ursache Vladimir
       [not found] ` <CAJbVpmwRAi1dEJw=zUtsJLE2RRMGtsjqBJk4x+_KsJGWnoRsMA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 8+ messages in thread
From: Ursache Vladimir @ 2016-08-18  5:30 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

 Hi,
the readlink() example from:

http://man7.org/linux/man-pages/man2/readlink.2.html

relies on lstat()'s st_size, but that doesn't work for files contents
created dynamically by the Linux kernel which often report a zero
size, for example the link at:
/sys/block/sda

the example code will fail because stat.st_size reports zero and you
try to read (stat.st_size + 1) which will succeed, which will generate
the error : "symlink increased in size between lstat() and
readlink()".

Somewhat related, the same issue is true for reading regular text
files, e.g: "/proc/filesystems" which will report stat.st_size = 0.

My quick workaround:

if (stat.st_size != 0)
    // work as usual
else if (file_is_a_link)
    // malloc 4K of ram and try to readlink() into it
else if (is_regular_file)
    // read() into a byte array that grows accordingly

In case it matters, I'm using Ubuntu 16.04 amd64.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-08-29 19:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-18  5:30 readlink() example sometimes fails Ursache Vladimir
     [not found] ` <CAJbVpmwRAi1dEJw=zUtsJLE2RRMGtsjqBJk4x+_KsJGWnoRsMA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-08-20  3:32   ` Michael Kerrisk (man-pages)
     [not found]     ` <28d2728c-e43c-dd90-3beb-fac899646ded-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-08-20  7:57       ` Ursache Vladimir
     [not found]         ` <CAJbVpmxJWuuO9-z=pavdnQh4ou0piJ72LK-T5fzt2QkJ+WZaAg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-08-26 17:47           ` Ursache Vladimir
     [not found]             ` <CAJbVpmwb1EU5J3jh0wKosQF9pxsS3dhwTaR5g=bzf9OL-Dr=SQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-08-28 20:48               ` Michael Kerrisk (man-pages)
     [not found]                 ` <57aec2e3-be44-2389-46c8-dc62f7d2eefe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-08-29  8:00                   ` Ursache Vladimir
2016-08-29 15:17                   ` Mats Wichmann
     [not found]                     ` <daf306a0-3e0c-8edb-016d-8e68bac02c79-mBRmNHn34rVzbRFIqnYvSA@public.gmane.org>
2016-08-29 19:04                       ` Michael Kerrisk (man-pages)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox