On Sat, 21 Dec 2019, Steffen Nurpmeso wrote:
> Philip Guenther wrote in <[email protected]>:
> |On Sat, 21 Dec 2019, Steffen Nurpmeso wrote:
...
> |> /* There are problems with dup()ing of file-descriptors for child \
> |> processes.
> |> * We have to somehow accomplish that the FILE* fp makes itself \
> |> comfortable
> |> * with the *real* offset of the underlaying file descriptor.
> |> * POSIX Issue 7 overloaded fflush(3): if used on a readable stream, \
> |> then
> |> *
> |> * if the file is not already at EOF, and the file is one capable of
> |> * seeking, the file offset of the underlying open file description \
> |> shall
> |> * be set to the file position of the stream */
> |
> |Wheee: do _any_ BSDs implement that? A quick eyeball of Net, Free, and
> |Dragonfly find they all do nothing and return either EBADF (Net, Open) or
> |0 (Free, Dragonfly), the latter with a citation to SUSv3.
>
> The test runs on FreeBSD 11.2,3-RC2/12 and also Dragonfly 5.6.1,
> as well as SunOS 5.9..11, GNU and musl Linux. That is all i have
> at the moment.
Can you provide a more specific test case which passes on glibc and either
FreeBSD or Dragonfly but not on OpenBSD?
The quick test program I whipped up below gives consistent results on
OpenBSD-current and FreeBSD 11.3-RELEASE-p3, and different results on
Centos 7.7.
Philip Guenther
OpenBSD:
123 123 16384 *
123 16384 *
246 16384 *
0 16384 *
0 16384 *
5123 5123 16384 *
5123 16384 *
10246 16384 *
0 16384 *
0 16384 *
21507 21507 32768 *
21507 32768 *
43014 49152 *
0 0
0 0
FreeBSD 11.3:
123 123 16384 *
123 16384 *
246 16384 *
0 16384 *
0 16384 *
5123 5123 16384 *
5123 16384 *
10246 16384 *
0 16384 *
0 16384 *
21507 21507 32768 *
21507 32768 *
43014 49152 *
0 0
0 0
CentOS 7:
123 123 4096 *
123 123
246 4219 *
0 0
0 0
5123 5123 8192 *
5123 5123
10246 13315 *
0 0
0 0
21507 21507 24576 *
21507 21507
43014 46083 *
0 0
0 0
-------
#include <stdio.h>
#include <unistd.h>
/* some nice large readable file */
#ifdef __linux__
# define FNAME "/etc/mime.types"
#else
# define FNAME "/etc/termcap"
#endif
char buf[8192 * 4];
static void
show(FILE *f, int fd)
{
long t = ftell(f);
off_t o = lseek(fd, 0, SEEK_CUR);
printf("\t%lu\t%llu%s\n", t, (long long)o, t == o ? "" : "\t*");
}
static void
try(FILE *f, int fd, int off)
{
rewind(f);
fread(buf, off, 1, f);
printf("%d", off);
show(f, fd);
fflush(f);
show(f, fd);
fread(buf, off, 1, f);
show(f, fd);
rewind(f);
show(f, fd);
fflush(f);
show(f, fd);
}
int
main(void)
{
FILE *f = fopen(FNAME, "r");
int fd = fileno(f);
try(f, fd, 123);
try(f, fd, 5123);
try(f, fd, 2 * 8192 + 5123);
return 0;
}