Commit Diff


commit - 07bda1263eb4e319e9d8b278e81d75f67a417b66
commit + 1d2533d0101fd1721ab26837485c0b094205c3bd
blob - 0fdb54b97426d717873ee89310a66c7a8caa03d7
blob + df5bdb9647ac7624e1ebf549620392cec69df9ca
--- src/libthread/FreeBSD.c
+++ src/libthread/FreeBSD.c
@@ -346,6 +346,13 @@ _pthreadinit(void)
 	signal(SIGUSR2, sigusr2handler);
 }
 
+void
+_threadpexit(void)
+{
+	_exit(0);
+}
+
+
 /*
  * FreeBSD 4 and earlier needs the context functions.
  */
blob - 103cb4281c7d516535266fb0681b9beb4dacd3f3
blob + 3d31a4a9667d7bfe5dd956a5ea98df88e37f590f
--- src/libthread/Linux.c
+++ src/libthread/Linux.c
@@ -348,3 +348,8 @@ _pthreadinit(void)
 	signal(SIGUSR2, sigusr2handler);
 }
 
+void
+_threadpexit(void)
+{
+	_exit(0);
+}
blob - dab6e42b825f3cd5925366aa907cf2c628b9de58
blob + 1b6ae6f67193ece5035ae806a78574bcc7979a1a
--- src/libthread/daemonize.c
+++ src/libthread/daemonize.c
@@ -9,10 +9,13 @@ static int threadpassfd;
 static void
 child(void)
 {
-	int status;
-	if(wait(&status) == sigpid)
-		if(WIFEXITED(status))
-			 _exit(WEXITSTATUS(status));
+	int status, pid;
+	pid = wait(&status);
+	if(pid < 0)
+		fprint(2, "wait: %r\n");
+	else if(WIFEXITED(status))
+		 _exit(WEXITSTATUS(status));
+print("pid %d if %d %d\n", pid, WIFEXITED(status), WEXITSTATUS(status));
 	_exit(97);
 }
 
@@ -51,6 +54,7 @@ _threadsetupdaemonize(void)
 	if(fcntl(p[0], F_SETFD, 1) < 0 || fcntl(p[1], F_SETFD, 1) < 0)
 		sysfatal("passer pipe pipe fcntl: %r");
 
+	signal(SIGCHLD, sigpass);
 	switch(pid = fork()){
 	case -1:
 		sysfatal("passer fork: %r");
@@ -58,6 +62,7 @@ _threadsetupdaemonize(void)
 		close(p[1]);
 		break;
 	case 0:
+		signal(SIGCHLD, SIG_DFL);
 		rfork(RFNOTEG);
 		close(p[0]);
 		threadpassfd = p[1];
@@ -89,7 +94,9 @@ _threadsetupdaemonize(void)
 void
 threaddaemonize(void)
 {
-	write(threadpassfd, "0", 1);
-	close(threadpassfd);
-	threadpassfd = -1;
+	if(threadpassfd >= 0){
+		write(threadpassfd, "0", 1);
+		close(threadpassfd);
+		threadpassfd = -1;
+	}
 }
blob - 8d3c7f9accd2f650d620f723bd2dc144e66ca9a8
blob + fcc309b7b422a26d81d9231f2918ac8ffb74ed5b
--- src/libthread/pthread.c
+++ src/libthread/pthread.c
@@ -132,3 +132,8 @@ threadexitsall(char *msg)
 	exits(msg);
 }
 
+void
+_threadpexit(void)
+{
+	pthread_exit(0);
+}
blob - 92e9394099b9401aed44562f15eefa99245fb302
blob + 84e21717f6a40d99c268546f327cfd1e6bf4ed4c
--- src/libthread/thread.c
+++ src/libthread/thread.c
@@ -475,6 +475,8 @@ int
 main(int argc, char **argv)
 {
 	Proc *p;
+
+	argv0 = argv[0];
 
 	_threadsetupdaemonize();
 
@@ -503,7 +505,8 @@ main(int argc, char **argv)
 		mainstacksize = 65536;
 	_threadcreate(p, threadmainstart, nil, mainstacksize);
 	scheduler(p);
-	return 0;	/* not reached */
+	threaddaemonize();
+	_threadpexit();
 }
 
 /*
blob - 32afa5fecc9446046bd199ea3ad3f50b5b1604b0
blob + 5cdba60cd69b5e75ebe0d2fd91cacf4c62417591
--- src/libthread/threadimpl.h
+++ src/libthread/threadimpl.h
@@ -122,3 +122,4 @@ extern int _threadspawn(int*, char*, char**);
 extern int _runthreadspawn(int*, char*, char**);
 extern void _threadsetupdaemonize(void);
 extern void _threaddodaemonize(char*);
+extern void _threadpexit(void);