]> granicus.if.org Git - file/commitdiff
make socket/pipe reading more robust.
authorChristos Zoulas <christos@zoulas.com>
Mon, 5 Feb 2007 16:46:40 +0000 (16:46 +0000)
committerChristos Zoulas <christos@zoulas.com>
Mon, 5 Feb 2007 16:46:40 +0000 (16:46 +0000)
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.

ChangeLog
magic/Magdir/tex
magic/magic.mime
src/compress.c
src/file.h
src/funcs.c
src/magic.c

index bddbfe99567772ca60d37d100c4b65854d9c79a6..6a6d34715741390aa59e8cad6dc857f6e72d241b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
 
+2007-02-05 11:35 Christos Zoulas <christos@zoulas.com>
+
+       * make socket/pipe reading more robust
+
 2007-01-25 16:01 Christos Zoulas <christos@zoulas.com>
 
        * Centralize all the tests in file_buffer.
index a6a11ec23d627f462c85ccedc749ef415575f595..52adb8704a2f7eac6d973e63e9dd61c5dd03e09e 100644 (file)
 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 <hendrik@scholz.net>
 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
 
index 6b909d0b41b697c06e3ed52fadabeb284662375a..cb9bd7c367b83fe82648693caf477e66df21bc67 100644 (file)
 # 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
 #
index d80a4b1274b290a80dd2046ce9c3ce7e78d326ed..0b17adaa2ff26c92c9b2cdd48a18620df87c08fc 100644 (file)
@@ -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));
index 98eb0c27ac4dfffb28b93a6a407fc6706536ef59..9324e1890ae85a2bc56eab119edc8a0671032c84 100644 (file)
@@ -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
index 3246a086143989da5aec7d101f24c6c1437f7c1e..6b7c6bc3c28e08124d8cf0a26af7d00f782e71cf 100644 (file)
@@ -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 ?
index e9c8662524b856f41160fac0448025ec11493be4..e12be83834d13913bdc058d3216042bc3037173c 100644 (file)
@@ -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;
                }