* Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
@ 2008-06-17 14:06 palani saravanan
2008-06-17 14:30 ` David Newall
0 siblings, 1 reply; 5+ messages in thread
From: palani saravanan @ 2008-06-17 14:06 UTC (permalink / raw)
To: linux-kernel
Hi,
[Forgive me if this is not correct alias for this question.
(Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.)]
In linux, I see that it just goes beyond the file size and returns the resulting offset.
For example, 'rc = lseek(fd, 4L, SEEK_END);' on a file which has 5 byte contents,
it returns rc as 9.
I expect that it would return size of the file, i.e.) 5.
I am not finding any posix documentation for this either.
May I know the rational behind this behavior?
Does the file pointer internally really points to the new location.?
Thanks,
Saravanan
Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
2008-06-17 14:06 Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size palani saravanan
@ 2008-06-17 14:30 ` David Newall
0 siblings, 0 replies; 5+ messages in thread
From: David Newall @ 2008-06-17 14:30 UTC (permalink / raw)
To: palani saravanan; +Cc: linux-kernel
palani saravanan wrote:
> In linux, I see that it just goes beyond the file size and returns the resulting offset.
> For example, 'rc = lseek(fd, 4L, SEEK_END);' on a file which has 5 byte contents,
> it returns rc as 9.
>
Sounds right. It's documented that way, too:
"The lseek() function allows the file offset to be set beyond the
end of the file (but this
does not change the size of the file)."
-- man 2 lseek
> I expect that it would return size of the file, i.e.) 5.
>
Only if you pass (0,SEEK_END).
> Does the file pointer internally really points to the new location.?
>
So the documentation promises.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
@ 2008-06-21 15:48 palani saravanan
2008-06-22 2:06 ` David Newall
0 siblings, 1 reply; 5+ messages in thread
From: palani saravanan @ 2008-06-21 15:48 UTC (permalink / raw)
To: David Newall; +Cc: linux-kernel
Thanks David !
I have one more question, Does lseek() permits -ve value as offset, with SEEK_SET mode?
When I execute a simple c program to check that it doesn't return any error.
errno = 0;
rd_rc = lseek(rd_fd, -10, SEEK_SET);
returns rd_rc = -10 and errno remains 0.
>From the linux code, I am expecting the return value of -EINVAL,
when SEEK_SET is passed with negative offset (generic_file_llseek).
http://lxr.linux.no/linux/fs/read_write.c#L34
What is the expected behavior as per posix and linux?
Thanks,
Saravanan
----- Original Message ----
From: David Newall <davidn@davidnewall.com>
To: palani saravanan <busybeesaravanan0072003@yahoo.co.in>
Cc: linux-kernel@vger.kernel.org
Sent: Tuesday, 17 June, 2008 8:00:09 PM
Subject: Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
palani saravanan wrote:
> In linux, I see that it just goes beyond the file size and returns the resulting offset.
> For example, 'rc = lseek(fd, 4L, SEEK_END);' on a file which has 5 byte contents,
> it returns rc as 9.
>
Sounds right. It's documented that way, too:
"The lseek() function allows the file offset to be set beyond the
end of the file (but this
does not change the size of the file)."
-- man 2 lseek
> I expect that it would return size of the file, i.e.) 5.
>
Only if you pass (0,SEEK_END).
> Does the file pointer internally really points to the new location.?
>
So the documentation promises.
Download prohibited? No problem. CHAT from any browser, without download. Go to http://in.messenger.yahoo.com/webmessengerpromo.php/
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
2008-06-21 15:48 palani saravanan
@ 2008-06-22 2:06 ` David Newall
0 siblings, 0 replies; 5+ messages in thread
From: David Newall @ 2008-06-22 2:06 UTC (permalink / raw)
To: palani saravanan; +Cc: linux-kernel
palani saravanan wrote:
> Does lseek() permits -ve value as offset, with SEEK_SET mode? ...
> errno = 0;
> rd_rc = lseek(rd_fd, -10, SEEK_SET);
> returns rd_rc = -10 and errno remains 0.
>
No, lseek does not permit negative file offsets, and should return
EINVAL in your example. You may have discovered a bug. For reference,
on my system lseek returns -1 and sets errno to EINVAL, as it should.
If you're running a recent kernel it might be worth looking closer into
this; for older kernels it's probably not worth the effort.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
@ 2008-06-23 8:38 palani saravanan
0 siblings, 0 replies; 5+ messages in thread
From: palani saravanan @ 2008-06-23 8:38 UTC (permalink / raw)
To: David Newall; +Cc: linux-kernel
Thanks David for clarification !
I ran this program in the kernel version, 'Linux 2.6.9-42.7.ELsmp'.
However, In a recent kernel version, 'Linux 2.6.24-19 ', the error EINVAL is returned as you mentioned.
Thanks,
Saravanan
----- Original Message ----
From: David Newall <davidn@davidnewall.com>
To: palani saravanan <busybeesaravanan0072003@yahoo.co.in>
Cc: linux-kernel@vger.kernel.org
Sent: Sunday, 22 June, 2008 7:36:12 AM
Subject: Re: Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.
palani saravanan wrote:
> Does lseek() permits -ve value as offset, with SEEK_SET mode? ...
> errno = 0;
> rd_rc = lseek(rd_fd, -10, SEEK_SET);
> returns rd_rc = -10 and errno remains 0.
>
No, lseek does not permit negative file offsets, and should return
EINVAL in your example. You may have discovered a bug. For reference,
on my system lseek returns -1 and sets errno to EINVAL, as it should.
If you're running a recent kernel it might be worth looking closer into
this; for older kernels it's probably not worth the effort.
Save all your chat conversations. Find them online at http://in.messenger.yahoo.com/webmessengerpromo.php
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-06-23 8:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-17 14:06 Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size palani saravanan
2008-06-17 14:30 ` David Newall
-- strict thread matches above, loose matches on Subject: below --
2008-06-21 15:48 palani saravanan
2008-06-22 2:06 ` David Newall
2008-06-23 8:38 palani saravanan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox