Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ext/standard/tests/streams/gh19570.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-19570 (unable to fseek in /dev/zero and /dev/null)
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Linux") die("skip only for Linux");
?>
--FILE--
<?php
var_dump(fseek(fopen("/dev/zero", "rb"), 1*1024*1024, SEEK_SET));
var_dump(fseek(fopen("/dev/null", "rb"), 1*1024*1024, SEEK_SET));
?>
--EXPECT--
int(0)
int(0)
15 changes: 15 additions & 0 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
# include <limits.h>
#endif

#ifdef __linux__
# include <sys/sysmacros.h>
#endif

#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC)
#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC)
Expand Down Expand Up @@ -255,7 +259,18 @@ PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
static void detect_is_seekable(php_stdio_stream_data *self) {
#if defined(S_ISFIFO) && defined(S_ISCHR)
if (self->fd >= 0 && do_fstat(self, 0) == 0) {
#ifdef __linux__
if (S_ISCHR(self->sb.st_mode)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that not just linux, allows some character devices being seekable, e.g. freebsd and very likely macos. The equivalent for freebsd at least, can t rely on device versions at all but on a "poorer" solution such as if fseek returns -1 and errno is ESPIPE then it is not seekable.

/* /dev/null & /dev/zero are exceptions, check their major/minor ID
* https://www.kernel.org/doc/Documentation/admin-guide/devices.txt */
self->is_seekable = major(self->sb.st_rdev) == 1
&& (minor(self->sb.st_rdev) == 3 || minor(self->sb.st_rdev) == 5);
} else {
self->is_seekable = !S_ISFIFO(self->sb.st_mode);
}
#else
self->is_seekable = !(S_ISFIFO(self->sb.st_mode) || S_ISCHR(self->sb.st_mode));
#endif
self->is_pipe = S_ISFIFO(self->sb.st_mode);
}
#elif defined(PHP_WIN32)
Expand Down
Loading