From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stepan Kasal Date: Tue, 06 May 2003 15:54:35 +0000 Subject: Re: [Cooker] strange problem with "hotplug" package Message-Id: List-Id: References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: linux-hotplug@vger.kernel.org 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 * 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