Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write system call, what's the number of bytes limit?

Tags:

c

types

linux

The write system call prototype is:

ssize_t write(int fd, const void *buf, size_t count);

The count parameter is unsigned, and the return value is signed.

The help page says:

On success, the number of bytes written is returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately.

However, it doesn't say what is the limit to the count parameter. It still doesn't say the behavior when count is greater than SSIZE_MAX.

Considering write is a system call that can be used to generic devices/files/whatever, if the device supports write operations bigger than SSIZE_MAX, the returning type can't handle the real number of bytes writen.

Doesn't make sense to me to be able to pass an unsigned number of bytes and get back an signed number of bytes as a result. Why not just pass an signed number?

It feels like the prototype of the write function in sort of error prone, or at least it leaves a possible hole in the path.

Does anyone knows the details about it or where can I find this information?

like image 437
Marcus Avatar asked Sep 21 '12 13:09

Marcus


People also ask

What is write () system call?

The write is one of the most basic routines provided by a Unix-like operating system kernel. It writes data from a buffer declared by the user to a given device, such as a file. This is the primary way to output data from a program by directly using a system call. The destination is identified by a numeric code.

What does write () do in C?

The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. Before any action described below is taken, and if nbyte is zero and the file is a regular file, the write() function may detect and return errors as described below.

What does the write () system call return on success number of bytes actually written it returns nothing data that it writes 1?

On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and the file descriptor refers to a regular file, 0 may be returned, or an error could be detected. For a special file, the results are not portable.

Does write system call block?

No, it only blocks the process until the content of the buffer is copied to kernel space.


1 Answers

I don't think there is a hard limit, it depends on what fd points to. If it's a file on the filesystem for instance, then the file system driver will choke if you exceed the "max file size limit", returning EFBIG error:

EFBIG An attempt was made to write a file that exceeds the implementation- defined maximum file size or the process file size limit.

like image 169
Martin Wickman Avatar answered Sep 28 '22 04:09

Martin Wickman