From: Christos Zoulas Date: Mon, 5 Feb 2007 16:46:40 +0000 (+0000) Subject: make socket/pipe reading more robust. X-Git-Tag: FILE4_20~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7304bff15d6fccfc617baff169e9b44e1f4ce126;p=file make socket/pipe reading more robust. 1. give a hint to sread that we are reading from a child or not 2. loop 5 times on select. 3. fix select error processing. --- diff --git a/ChangeLog b/ChangeLog index bddbfe99..6a6d3471 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ +2007-02-05 11:35 Christos Zoulas + + * make socket/pipe reading more robust + 2007-01-25 16:01 Christos Zoulas * Centralize all the tests in file_buffer. diff --git a/magic/Magdir/tex b/magic/Magdir/tex index a6a11ec2..52adb870 100644 --- a/magic/Magdir/tex +++ b/magic/Magdir/tex @@ -29,25 +29,25 @@ 0 string This\ is\ Info\ file GNU Info text # TeX documents, from Daniel Quinlan (quinlan@yggdrasil.com) -0 string \\input TeX document text -0 string \\section LaTeX document text -0 string \\setlength LaTeX document text -0 string \\documentstyle LaTeX document text -0 string \\chapter LaTeX document text -0 string \\documentclass LaTeX 2e document text -0 string \\relax LaTeX auxiliary file -0 string \\contentsline LaTeX table of contents -0 string %\ -*-latex-*- LaTeX document text +0 search/400 \\input TeX document text +0 search/400 \\section LaTeX document text +0 search/400 \\setlength LaTeX document text +0 search/400 \\documentstyle LaTeX document text +0 search/400 \\chapter LaTeX document text +0 search/400 \\documentclass LaTeX 2e document text +0 search/400 \\relax LaTeX auxiliary file +0 search/400 \\contentsline LaTeX table of contents +0 search/400 %\ -*-latex-*- LaTeX document text # Tex document, from Hendrik Scholz 0 string \\ifx TeX document text # Index and glossary files -0 string \\indexentry LaTeX raw index file -0 string \\begin{theindex} LaTeX sorted index -0 string \\glossaryentry LaTeX raw glossary -0 string \\begin{theglossary} LaTeX sorted glossary -0 string This\ is\ makeindex Makeindex log file +0 search/400 \\indexentry LaTeX raw index file +0 search/400 \\begin{theindex} LaTeX sorted index +0 search/400 \\glossaryentry LaTeX raw glossary +0 search/400 \\begin{theglossary} LaTeX sorted glossary +0 search/400 This\ is\ makeindex Makeindex log file # End of TeX diff --git a/magic/magic.mime b/magic/magic.mime index 6b909d0b..cb9bd7c3 100644 --- a/magic/magic.mime +++ b/magic/magic.mime @@ -592,6 +592,14 @@ # RTF - Rich Text Format 0 string {\\rtf text/rtf +# TeX documents, from Daniel Quinlan (quinlan@yggdrasil.com) +0 search/400 \\input text/x-tex +0 search/400 \\section text/x-tex +0 search/400 \\setlength text/x-tex +0 search/400 \\documentstyle text/x-tex +0 search/400 \\chapter text/x-tex +0 search/400 \\documentclass text/x-tex + #------------------------------------------------------------------------------ # animation: file(1) magic for animation/movie formats # diff --git a/src/compress.c b/src/compress.c index d80a4b12..0b17adaa 100644 --- a/src/compress.c +++ b/src/compress.c @@ -51,7 +51,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$File: compress.c,v 1.47 2007/01/16 14:56:45 ljt Exp $") +FILE_RCSID("@(#)$File: compress.c,v 1.48 2007/01/25 21:05:46 christos Exp $") #endif private struct { @@ -154,9 +154,9 @@ swrite(int fd, const void *buf, size_t n) * `safe' read for sockets and pipes. */ protected ssize_t -sread(int fd, void *buf, size_t n) +sread(int fd, void *buf, size_t n, int canbepipe) { - int rv; + int rv, cnt; #ifdef FIONREAD int t = 0; #endif @@ -166,11 +166,12 @@ sread(int fd, void *buf, size_t n) goto nocheck; #ifdef FIONREAD - if ((ioctl(fd, FIONREAD, &t) < 0) || (t == 0)) { + if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1) || (t == 0)) { #ifdef FD_ZERO - for (;;) { + for (cnt = 0;; cnt++) { fd_set check; struct timeval tout = {0, 100 * 1000}; + int rv; FD_ZERO(&check); FD_SET(fd, &check); @@ -179,12 +180,14 @@ sread(int fd, void *buf, size_t n) * Avoid soft deadlock: do not read if there * is nothing to read from sockets and pipes. */ - if (select(fd + 1, &check, NULL, NULL, &tout) <= 0) { + rv = select(fd + 1, &check, NULL, NULL, &tout); + if (rv == -1) { if (errno == EINTR || errno == EAGAIN) continue; + } else if (rv == 0 && cnt >= 5) { return 0; - } - break; + } else + break; } #endif (void)ioctl(fd, FIONREAD, &t); @@ -245,7 +248,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes) r = 1; else { - while ((r = sread(fd, buf, sizeof(buf))) > 0) + while ((r = sread(fd, buf, sizeof(buf), 1)) > 0) if (swrite(tfd, buf, (size_t)r) != r) break; } @@ -446,7 +449,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, n = 0; goto err; } - if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) { + if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) { #ifdef DEBUG (void)fprintf(stderr, "Read failed (%s)\n", strerror(errno)); diff --git a/src/file.h b/src/file.h index 98eb0c27..9324e189 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.86 2007/01/18 05:29:33 ljt Exp $ + * @(#)$File: file.h,v 1.87 2007/01/25 21:05:47 christos Exp $ */ #ifndef __file_h__ @@ -331,7 +331,7 @@ protected void file_mdump(struct magic *); protected void file_showstr(FILE *, const char *, size_t); protected size_t file_mbswidth(const char *); protected const char *file_getbuffer(struct magic_set *); -protected ssize_t sread(int, void *, size_t); +protected ssize_t sread(int, void *, size_t, int); #ifdef ENABLE_CONDITIONALS protected int file_check_mem(struct magic_set *, unsigned int); #endif diff --git a/src/funcs.c b/src/funcs.c index 3246a086..6b7c6bc3 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -38,7 +38,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.25 2007/01/16 14:58:48 ljt Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.26 2007/01/25 21:05:47 christos Exp $") #endif /* lint */ #ifndef HAVE_VSNPRINTF @@ -166,16 +166,16 @@ file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, #endif /* try compression stuff */ - if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0 || + if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 || (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) { /* Check if we have a tar file */ - if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0 || + if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 || (m = file_is_tar(ms, buf, nb)) == 0) { /* try tests in /etc/magic (or surrogate magic file) */ - if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0 || + if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 || (m = file_softmagic(ms, buf, nb)) == 0) { /* try known keywords, check whether it is ASCII */ - if ((ms->flags & MAGIC_NO_CHECK_ASCII) == 0 || + if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 || (m = file_ascmagic(ms, buf, nb)) == 0) { /* abandon hope, all ye who remain here */ if (file_printf(ms, ms->flags & MAGIC_MIME ? diff --git a/src/magic.c b/src/magic.c index e9c86625..e12be838 100644 --- a/src/magic.c +++ b/src/magic.c @@ -63,7 +63,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.37 2007/01/16 14:58:48 ljt Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.38 2007/01/25 21:05:47 christos Exp $") #endif /* lint */ #ifdef __EMX__ @@ -305,7 +305,7 @@ magic_file(struct magic_set *ms, const char *inname) ssize_t r = 0; while ((r = sread(fd, (void *)&buf[nbytes], - (size_t)(HOWMANY - nbytes))) > 0) { + (size_t)(HOWMANY - nbytes), 1)) > 0) { nbytes += r; if (r < PIPE_BUF) break; }