linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stepan Kasal <kasal@math.cas.cz>
To: linux-hotplug@vger.kernel.org
Subject: Re: [Cooker] strange problem with "hotplug" package
Date: Tue, 06 May 2003 15:54:35 +0000	[thread overview]
Message-ID: <marc-linux-hotplug-105223678025411@msgid-missing> (raw)
In-Reply-To: <marc-linux-hotplug-105222892315303@msgid-missing>

Hello to all people reading this,

On Tue, May 06, 2003 at 05:45:40PM +0400, Andrey Borzenkov wrote:
> bor@cooker% grep -q bor /etc/passwd <&- >&-
> grep: write error: Bad file descriptor
> bor@cooker% echo $?
> 1

the exit code is always correct.  The only problem is the bogus
error message.
So, we should not try to close(stdout) if -q was given.

Curiously enough, the problem doesn't appear with

	grep -q bor /etc/passwd >&-

because the file /etc/passwd is opened with file descriptor 1,
and it is closed on exit instead of stdout.
(With <&- >&-, /etc/passwd is fd 0 and the bug bites.)
Cause: if a match was found, we exit(0) immediately, without closing
the open file.  This optimization can be moved after the input file
is closed, without any loss.

The patch attached to the end of this mail should fix both problems.
(Apply to the 2.5.1 source tree.)  It will appear in the next release.

Regards,
	Stepan Kasal

Tue May  6 17:49:29 CEST 2003  Stepan Kasal  <kasal@math.cas.cz>

	* src/grep.c(main): Don't register atexit(close_stdout) if -q
	  was given---no output will be written; there is also no need
	  to use close_stdout_set_status().
	* src/grep.c(grepbuf): move exit(0) ...
	  (grepfile): ... here, when the bufdesc is closed; this doesn't
	  present any performance loss, done_on_match is 1 and ensures
	  that we get out quickly.

--- grep-2.5.1.orig/src/grep.c	Tue Mar 26 16:54:12 2002
+++ grep-2.5.1/src/grep.c	Tue May  6 17:47:38 2003
@@ -722,8 +722,6 @@ grepbuf (char const *beg, char const *li
           outleft--;
 	  if (!outleft || done_on_match)
 	    {
-	      if (exit_on_match)
-		exit (0);
 	      after_last_match = bufoffset - (buflim - endp);
 	      return nlines;
 	    }
@@ -978,6 +976,9 @@ grepfile (char const *file, struct stats
 	    }
     }
 
+  if (!status && exit_on_match)
+	exit (0);
+
   return status;
 }
 
@@ -1348,8 +1349,6 @@ main (int argc, char **argv)
   textdomain (PACKAGE);
 #endif
 
-  atexit (close_stdout);
-
   prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
 
   while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
@@ -1524,7 +1523,6 @@ main (int argc, char **argv)
 
       case 'q':
 	exit_on_match = 1;
-	close_stdout_set_status(0);
 	break;
 
       case 'R':
@@ -1630,6 +1628,10 @@ main (int argc, char **argv)
 	break;
 
       }
+
+  /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */
+  if (!exit_on_match)
+    atexit(close_stdout);
 
   /* POSIX.2 says that -q overrides -l, which in turn overrides the
      other output options.  */


-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com

_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

  reply	other threads:[~2003-05-06 15:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-06 13:45 [Cooker] strange problem with "hotplug" package Andrey Borzenkov
2003-05-06 15:54 ` Stepan Kasal [this message]
2003-05-06 20:12 ` Jim Meyering
2003-05-08 12:24 ` Stepan Kasal
2003-05-08 16:17 ` Jim Meyering

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=marc-linux-hotplug-105223678025411@msgid-missing \
    --to=kasal@math.cas.cz \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).