From nobody@FreeBSD.org  Thu May  3 05:47:55 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 91E0316A400
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  3 May 2007 05:47:55 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 83E4013C447
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  3 May 2007 05:47:55 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l435ltYZ025360
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 3 May 2007 05:47:55 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l435grGW024560;
	Thu, 3 May 2007 05:42:53 GMT
	(envelope-from nobody)
Message-Id: <200705030542.l435grGW024560@www.freebsd.org>
Date: Thu, 3 May 2007 05:42:53 GMT
From: Alexander Melkov<melkov@comptek.ru>
To: freebsd-gnats-submit@FreeBSD.org
Subject: lockf(1): on closing stdin, stdout, stderr
X-Send-Pr-Version: www-3.0

>Number:         112379
>Category:       bin
>Synopsis:       [patch] [request] lockf(1): on closing stdin, stdout, stderr
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 03 05:50:04 GMT 2007
>Closed-Date:    
>Last-Modified:  Sun Jan 27 09:19:18 UTC 2008
>Originator:     Alexander Melkov
>Release:        6.2
>Organization:
>Environment:
>Description:
Because lockf command forks but doesn't close input and output file handles 
in the main process, closing stdin, stdout or stderr inside child processes 
doesn't cause respective ends of pipes seen as closed by programs that read 
write to them.

For example:
(script1.sh | lockf foo script2.sh 2>&1 &) | mail -s something somewhere

Suppose that script2.sh closes its standard input and continues some long-time 
processing before script1.sh completes writing to its stdndard output.
script1.sh will hang for a long time (it would not without lockf).

As a more practical example, script2.sh redirects its output at some point
and continues its business. Because of lockf, mail will needlessly wait until
script2.sh completes.

I think that stdin should always be closed, and -s switch may be used as a flag
that closing stdout and stderr is desirable.

>How-To-Repeat:

>Fix:
This is a patch against src/usr.bin/lockf/lockf.c,v 1.11.8.1

--- lockf.c.orig        Thu Jan 11 17:13:33 2007
+++ lockf.c     Thu May  3 09:09:42 2007
@@ -120,6 +120,11 @@
        signal(SIGINT, SIG_IGN);
        signal(SIGQUIT, SIG_IGN);
        signal(SIGTERM, killed);
+       fclose(stdin);
+       if (silent) {
+               fclose(stdout);
+               fclose(stderr);
+       }
        if (waitpid(child, &status, 0) == -1)
                err(EX_OSERR, "waitpid failed");
        return (WIFEXITED(status) ? WEXITSTATUS(status) : 1);

>Release-Note:
>Audit-Trail:
>Unformatted:
