Patchwork Suggested patch: reset errno after isatty()

login
register
mail settings
Submitter Ketil Froyn
Date Nov. 2, 2010, 2:37 p.m.
Message ID <AANLkTimwumfui-_T1ScDYwMLKbNh1ia-SDMUg56wTfN5@mail.gmail.com>
Download mbox | patch
Permalink /patch/69889/
State New
Headers show

Comments

Ketil Froyn - Nov. 2, 2010, 2:37 p.m.
isatty() uses an ioctl and the resulting error code to determine if an
fd is a tty or not. If it isn't, errno is set to ENOTTY. Later in the
code, pread() fails, or rather returns 0 immediately. When this
happens, the following perror("pread") tells me:

pread: Not a typewriter

which is the wrong error. Here's strace showing the issue:

open("/sdcard/mtd5.dump", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0644) = 4
ioctl(4, TCGETS or SNDCTL_TMR_TIMEBASE, 0xbee43ab0) = -1 ENOTTY (Not a
typewriter)
write(2, "Block size 131072, page size 204"..., 47) = 47
write(2, "Dumping data starting at 0x00000"..., 64) = 64
ioctl(3, 0x40084d0b, 0xbee43c30)        = 0
pread(3, "", 2048, 0)                   = 0
write(2, "pread", 5)                    = 5
write(2, ": ", 2)                       = 2
write(2, "Not a typewriter", 16)        = 16
write(2, "\n", 1)                       = 1

And the included patch (below, against v1.4.1, but simple enough)
should take care of it.

Though if someone knows what could be causing the pread() to keep
failing, I'd be interested in hearing it! And the mtd device is
reported as having a page size of 2048 and oob size 56, so to get even
this far I added that as a valid page size. Isn't it?
Matthieu CASTET - Nov. 3, 2010, 8:23 a.m.
Ketil Froyn a écrit :
> isatty() uses an ioctl and the resulting error code to determine if an
> fd is a tty or not. If it isn't, errno is set to ENOTTY. Later in the
> code, pread() fails, or rather returns 0 immediately. When this
> happens, the following perror("pread") tells me:
> 
> pread: Not a typewriter
> 
> which is the wrong error. Here's strace showing the issue:
> 
> open("/sdcard/mtd5.dump", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0644) = 4
> ioctl(4, TCGETS or SNDCTL_TMR_TIMEBASE, 0xbee43ab0) = -1 ENOTTY (Not a
> typewriter)
> write(2, "Block size 131072, page size 204"..., 47) = 47
> write(2, "Dumping data starting at 0x00000"..., 64) = 64
> ioctl(3, 0x40084d0b, 0xbee43c30)        = 0
> pread(3, "", 2048, 0)                   = 0
> write(2, "pread", 5)                    = 5
> write(2, ": ", 2)                       = 2
> write(2, "Not a typewriter", 16)        = 16
> write(2, "\n", 1)                       = 1
> 
> And the included patch (below, against v1.4.1, but simple enough)
> should take care of it.
> 
> Though if someone knows what could be causing the pread() to keep
> failing, I'd be interested in hearing it! And the mtd device is
> reported as having a page size of 2048 and oob size 56, so to get even
> this far I added that as a valid page size. Isn't it?
Why errno is checked when pread return 0 ?
Errno should be checked only when return is -1.

man pread :
RETURN VALUE
        On success, the number of bytes read or written is returned 
(zero indi-
        cates  that  nothing  was  written,  in the case of pwrite(), or 
end of
        file, in the case of pread()), or -1 on error, in which case 
errno  is
        set to indicate the error.

Patch

diff --git a/nanddump.c b/nanddump.c
index 709b2db..ba370ad 100644
--- a/nanddump.c
+++ b/nanddump.c
@@ -374,6 +374,7 @@  int main(int argc, char * const argv[])
                close(fd);
                exit(EXIT_FAILURE);
        }
+       errno = 0;

        /* Initialize start/end addresses and block size */
        if (length)