public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: "Michael Kerrisk (man-pages)" <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Ursache Vladimir <f35f22fan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org,
	linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: readlink() example sometimes fails
Date: Mon, 29 Aug 2016 08:48:12 +1200	[thread overview]
Message-ID: <57aec2e3-be44-2389-46c8-dc62f7d2eefe@gmail.com> (raw)
In-Reply-To: <CAJbVpmwb1EU5J3jh0wKosQF9pxsS3dhwTaR5g=bzf9OL-Dr=SQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

On 08/27/2016 05:47 AM, Ursache Vladimir wrote:
> Here's another issue, this time related to stat(), st_size and regular files.
> 
> I think stat(2) should be updated in relation to exceptions with
> st_size and regular files.
> Currently the man pages describe this exception (corner case) as:
> 
> "For most files under the /proc directory, stat() does not return the
> file size in the st_size
> field; instead the field is returned with the value 0."
> 
> However, I found at /sys/devices/cpu/ and in other (Linux kernel)
> directories (all) regular files
> to report 4096 bytes yet their real size is only a few bytes (for
> example the regular
>  file "/sys/devices/cpu/type" st_size=4096 bytes yet one can only read 2 bytes).
> 
> Hence in some cases (st_size=0) it reports a smaller size, and in the
> latter case - a bigger size
> (st_size=4096).
> 
> I don't know if these cases are posix compliant and since you have a
> lot more experience I hope you'll
> write the proper explanation in place of the one mentioned at the top.
> I would write something
> like this:
> 
> "Many regular files generated by the (Linux) kernel at /proc or /sys
> return an st_size that has nothing
> to do with the real file size so one should try to read as much as one
> can and append
> a '\0' at the end if it's a text file";
> 
> But then the regular file at /proc/kcore reports st_size=many terabytes.

So, it's not going to be possible to explain every corner case here, but
text such as you suggest is good to alert the programmer. Starting from 
your suggestion, I reworked this to:

       For many pseudofiles that are autogenerated by the kernel,  stat()
       does not return an accurate value in the st_size field.  For exam‐
       ple, the value 0 is returned for many files under the /proc direc‐
       tory,  while various files under /sys report a size of 4096 bytes,
       even though the file content is  smaller.   For  such  files,  one
       should  simply  try  to read as many bytes as possible (and append
       '\0' to the returned buffer if  it  is  to  be  interpreted  as  a
       string).

Okay?

Cheers,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
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

  parent reply	other threads:[~2016-08-28 20:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]
     [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)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=57aec2e3-be44-2389-46c8-dc62f7d2eefe@gmail.com \
    --to=mtk.manpages-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=f35f22fan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox