2004-11-03 13:51:07 +00:00
|
|
|
.\" Hey Emacs! This file is -*- nroff -*- source.
|
|
|
|
.\"
|
|
|
|
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
|
|
|
|
.\" 1993 Michael Haardt, Ian Jackson.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
|
|
|
.\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
|
|
|
|
.\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
|
|
|
|
.\" <michael@cantor.informatik.rwth-aachen.de>
|
|
|
|
.\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
|
|
|
|
.\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
|
|
|
|
.\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
|
|
|
|
.\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
|
|
|
|
.\" <michael@cantor.informatik.rwth-aachen.de>
|
|
|
|
.\"
|
2009-02-23 04:57:43 +00:00
|
|
|
.TH READ 2 2009-02-23 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
read \- read from a file descriptor
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <unistd.h>
|
|
|
|
.sp
|
|
|
|
.BI "ssize_t read(int " fd ", void *" buf ", size_t " count );
|
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR read ()
|
2004-11-03 13:51:07 +00:00
|
|
|
attempts to read up to
|
|
|
|
.I count
|
|
|
|
bytes from file descriptor
|
|
|
|
.I fd
|
|
|
|
into the buffer starting at
|
|
|
|
.IR buf .
|
|
|
|
.PP
|
|
|
|
If
|
|
|
|
.I count
|
2007-05-12 09:06:04 +00:00
|
|
|
is zero,
|
|
|
|
.BR read ()
|
|
|
|
returns zero and has no other results.
|
2004-11-03 13:51:07 +00:00
|
|
|
If
|
|
|
|
.I count
|
2007-06-22 17:16:20 +00:00
|
|
|
is greater than
|
|
|
|
.BR SSIZE_MAX ,
|
|
|
|
the result is unspecified.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
|
|
|
On success, the number of bytes read is returned (zero indicates end of
|
|
|
|
file), and the file position is advanced by this number.
|
|
|
|
It is not an error if this number is smaller than the number of bytes
|
|
|
|
requested; this may happen for example because fewer bytes are actually
|
|
|
|
available right now (maybe because we were close to end-of-file, or
|
|
|
|
because we are reading from a pipe, or from a terminal), or because
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR read ()
|
|
|
|
was interrupted by a signal.
|
2004-11-03 13:51:07 +00:00
|
|
|
On error, \-1 is returned, and
|
|
|
|
.I errno
|
2007-04-12 22:42:49 +00:00
|
|
|
is set appropriately.
|
|
|
|
In this case it is left unspecified whether
|
2004-11-03 13:51:07 +00:00
|
|
|
the file position (if any) changes.
|
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B EAGAIN
|
2009-02-23 04:57:43 +00:00
|
|
|
The file descriptor
|
|
|
|
.I fd
|
accept.2, connect.2, eventfd.2, flock.2, open.2, posix_fadvise.2, read.2, recv.2, sched_setscheduler.2, select_tut.2, send.2, signalfd.2, splice.2, timerfd_create.2, write.2, flockfile.3, mkfifo.3, mq_notify.3, mq_open.3, pthread_tryjoin_np.3, scanf.3, random.4, ddp.7, epoll.7, fifo.7, ip.7, pipe.7, socket.7, spufs.7: Global fix: s/non-blocking/nonblocking/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:43:10 +00:00
|
|
|
refers to a file other than a socket and has been marked nonblocking
|
2009-02-23 04:57:43 +00:00
|
|
|
.RB ( O_NONBLOCK ),
|
|
|
|
and the read would block.
|
|
|
|
.TP
|
|
|
|
.BR EAGAIN " or " EWOULDBLOCK
|
|
|
|
.\" Actually EAGAIN on Linux
|
|
|
|
The file descriptor
|
|
|
|
.I fd
|
accept.2, connect.2, eventfd.2, flock.2, open.2, posix_fadvise.2, read.2, recv.2, sched_setscheduler.2, select_tut.2, send.2, signalfd.2, splice.2, timerfd_create.2, write.2, flockfile.3, mkfifo.3, mq_notify.3, mq_open.3, pthread_tryjoin_np.3, scanf.3, random.4, ddp.7, epoll.7, fifo.7, ip.7, pipe.7, socket.7, spufs.7: Global fix: s/non-blocking/nonblocking/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:43:10 +00:00
|
|
|
refers to a socket and has been marked nonblocking
|
2009-02-23 04:57:43 +00:00
|
|
|
.RB ( O_NONBLOCK ),
|
|
|
|
and the read would block.
|
|
|
|
POSIX.1-2001 allows either error to be returned for this case,
|
|
|
|
and does not require these constants to have the same value,
|
|
|
|
so a portable application should check for both possibilities.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EBADF
|
|
|
|
.I fd
|
|
|
|
is not a valid file descriptor or is not open for reading.
|
|
|
|
.TP
|
|
|
|
.B EFAULT
|
|
|
|
.I buf
|
|
|
|
is outside your accessible address space.
|
|
|
|
.TP
|
|
|
|
.B EINTR
|
2008-07-04 15:50:36 +00:00
|
|
|
The call was interrupted by a signal before any data was read; see
|
|
|
|
.BR signal (7).
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
.I fd
|
2005-10-10 13:07:32 +00:00
|
|
|
is attached to an object which is unsuitable for reading;
|
2007-04-12 22:42:49 +00:00
|
|
|
or the file was opened with the
|
2005-10-10 13:07:32 +00:00
|
|
|
.B O_DIRECT
|
|
|
|
flag, and either the address specified in
|
|
|
|
.IR buf ,
|
|
|
|
the value specified in
|
|
|
|
.IR count ,
|
|
|
|
or the current file offset is not suitably aligned.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
2007-09-27 17:59:11 +00:00
|
|
|
.B EINVAL
|
|
|
|
.I fd
|
|
|
|
was created via a call to
|
2007-10-13 20:29:01 +00:00
|
|
|
.BR timerfd_create (2)
|
2007-09-27 17:59:11 +00:00
|
|
|
and the wrong size buffer was given to
|
|
|
|
.BR read ();
|
|
|
|
see
|
2007-10-13 20:29:01 +00:00
|
|
|
.BR timerfd_create (2)
|
2007-09-27 17:59:11 +00:00
|
|
|
for further information.
|
|
|
|
.TP
|
2004-11-03 13:51:07 +00:00
|
|
|
.B EIO
|
2007-04-12 22:42:49 +00:00
|
|
|
I/O error.
|
|
|
|
This will happen for example when the process is in a
|
2004-11-03 13:51:07 +00:00
|
|
|
background process group, tries to read from its controlling tty,
|
2007-06-21 05:38:48 +00:00
|
|
|
and either it is ignoring or blocking
|
|
|
|
.B SIGTTIN
|
|
|
|
or its process group
|
2007-04-12 22:42:49 +00:00
|
|
|
is orphaned.
|
|
|
|
It may also occur when there is a low-level I/O error
|
2004-11-03 13:51:07 +00:00
|
|
|
while reading from a disk or tape.
|
|
|
|
.TP
|
|
|
|
.B EISDIR
|
|
|
|
.I fd
|
|
|
|
refers to a directory.
|
|
|
|
.PP
|
|
|
|
Other errors may occur, depending on the object connected to
|
|
|
|
.IR fd .
|
|
|
|
POSIX allows a
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR read ()
|
2004-11-03 13:51:07 +00:00
|
|
|
that is interrupted after reading some data
|
|
|
|
to return \-1 (with
|
|
|
|
.I errno
|
2007-06-22 17:16:20 +00:00
|
|
|
set to
|
|
|
|
.BR EINTR )
|
|
|
|
or to return the number of bytes already read.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2006-08-03 13:57:17 +00:00
|
|
|
SVr4, 4.3BSD, POSIX.1-2001.
|
2007-05-16 05:33:35 +00:00
|
|
|
.SH NOTES
|
2004-11-03 13:51:07 +00:00
|
|
|
On NFS file systems, reading small amounts of data will only update the
|
2008-03-18 09:52:36 +00:00
|
|
|
timestamp the first time, subsequent calls may not do so.
|
2007-04-12 22:42:49 +00:00
|
|
|
This is caused
|
2004-11-03 13:51:07 +00:00
|
|
|
by client side attribute caching, because most if not all NFS clients
|
2007-04-12 22:42:49 +00:00
|
|
|
leave st_atime (last file access time)
|
2005-07-19 07:15:17 +00:00
|
|
|
updates to the server and client side reads satisfied from the
|
|
|
|
client's cache will not cause st_atime updates on the server as there are no
|
2007-04-12 22:42:49 +00:00
|
|
|
server side reads.
|
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/
The man pages were rather inconsistent in the use of "Unix"
versus "UNIX". Let's go with the trademark usage.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
|
|
|
UNIX semantics can be obtained by disabling client
|
2004-11-03 13:51:07 +00:00
|
|
|
side attribute caching, but in most situations this will substantially
|
|
|
|
increase server load and decrease performance.
|
|
|
|
.PP
|
2008-03-19 07:26:08 +00:00
|
|
|
Many file systems and disks were considered to be fast enough that the
|
2007-04-12 22:42:49 +00:00
|
|
|
implementation of
|
2004-11-03 13:51:07 +00:00
|
|
|
.B O_NONBLOCK
|
2007-04-12 22:42:49 +00:00
|
|
|
was deemed unnecessary.
|
2007-06-22 17:16:20 +00:00
|
|
|
So,
|
|
|
|
.B O_NONBLOCK
|
|
|
|
may not be available on files
|
2004-11-03 13:51:07 +00:00
|
|
|
and/or disks.
|
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR close (2),
|
|
|
|
.BR fcntl (2),
|
|
|
|
.BR ioctl (2),
|
|
|
|
.BR lseek (2),
|
2005-10-10 13:07:32 +00:00
|
|
|
.BR open (2),
|
2006-02-08 04:18:25 +00:00
|
|
|
.BR pread (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR readdir (2),
|
|
|
|
.BR readlink (2),
|
2006-05-29 04:48:26 +00:00
|
|
|
.BR readv (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR select (2),
|
|
|
|
.BR write (2),
|
2006-05-29 04:48:26 +00:00
|
|
|
.BR fread (3)
|