From nobody@FreeBSD.org  Sun Jan 19 03:00:34 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 1E5AA3B2
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 19 Jan 2014 03:00:34 +0000 (UTC)
Received: from oldred.freebsd.org (oldred.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id E41EC16F8
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 19 Jan 2014 03:00:33 +0000 (UTC)
Received: from oldred.freebsd.org ([127.0.1.6])
	by oldred.freebsd.org (8.14.5/8.14.7) with ESMTP id s0J30Xgk097649
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 19 Jan 2014 03:00:33 GMT
	(envelope-from nobody@oldred.freebsd.org)
Received: (from nobody@localhost)
	by oldred.freebsd.org (8.14.5/8.14.5/Submit) id s0J30X85097613;
	Sun, 19 Jan 2014 03:00:33 GMT
	(envelope-from nobody)
Message-Id: <201401190300.s0J30X85097613@oldred.freebsd.org>
Date: Sun, 19 Jan 2014 03:00:33 GMT
From: Henry Hu <henry.hu.sh@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: waitpid() in linux threads fails with ECHILD
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         185873
>Category:       kern
>Synopsis:       [linux] waitpid() in linux threads fails with ECHILD
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    dchagin
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 19 03:10:00 UTC 2014
>Closed-Date:    
>Last-Modified:  Sun Apr 20 05:18:53 UTC 2014
>Originator:     Henry Hu
>Release:        FreeBSD 11-CURRENT
>Organization:
Columbia University
>Environment:
FreeBSD pepsi 11.0-CURRENT FreeBSD 11.0-CURRENT #1 r260031M: Sun Jan  5 18:25:51 EST 2014     root@pepsi:/usr/obj/usr/src/sys/MYKERNEL  amd64

>Description:
If a Linux program
1. use fork() to fork a child
2. create a thread
3. use waitpid() in the new thread to wait for the child

Then waitpid() returns -1, with errno ECHILD.

This affects some applications:
1. intellij (experimental port available) with Oracle JDK
2. android studio (based on intellij)
and possible other programs using Oracle JDK.

If you use java.lang.ProcessBuilder to create a child and get its output,
Oracle SDK works in this way:

1. use fork() to create a child
2. create a thread to wait for the child
3. the thread calls waitpid() to wait for child exit
4. the thread reads the child's output

Because waitpid() incorrectly returns -1 here before the child exits,
the child may have not produced the output, which results in empty output
received by the caller. The caller may incorrectly assume that the child
is not working.

In intellij, it calls "git --version" to obtain the git version. Because
the output is empty, it assumes that git is not working, and disables
some features.
>How-To-Repeat:
A simple test program:

#include <stdio.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>

int child;

void* worker(void* arg) {
	int status;
	printf("worker waiting\n");
	int ret = waitpid(child, &status, 0);
	printf("waitpid ret: %d status: %d\n", ret, status);
	return NULL;
}

int main() {
	child = fork();
	if (child == 0) {
		printf("child running\n");
		sleep(3);
		printf("child exit\n");
	} else {
		printf("forked: %d\n", child);
		pthread_t thr;
		pthread_create(&thr, NULL, worker, NULL);
		sleep(5);
	}
}

If run it natively on a FreeBSD/Linux machine, it outputs

forked: 98484
child running
worker waiting
child exit
waitpid ret: 98484 status: 0

However, if run a Linux version on a FreeBSD machine, it outputs

forked: 95940
child running
worker waiting
waitpid ret: -1 status: 0
child exit
>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-emulation 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Sun Apr 20 02:58:16 UTC 2014 
Responsible-Changed-Why:  

Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=185873 
Responsible-Changed-From-To: freebsd-emulation->dchagin 
Responsible-Changed-By: dchagin 
Responsible-Changed-When: Sun Apr 20 05:17:47 UTC 2014 
Responsible-Changed-Why:  
Grab. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=185873 
>Unformatted:
