Hi all,

Signoff attached.

As Kooda says, this applies and tests fine on the chicken-5 branch as
well.

Thanks very much for pursuing this, and thanks also to lemonboy for the
initial report and subsequent testing and investigation.

Cheers,

Evan
>From 7d7fd72b9738a359d619d277d43b80dc07a8aefa Mon Sep 17 00:00:00 2001
From: Kooda <ko...@upyum.com>
Date: Sat, 19 Mar 2016 13:21:43 +0100
Subject: [PATCH] Clean up process exit and flush output streams

Flush standard output streams before both forking and exiting, and use
C_exit_runtime() uniformly to exit the process.

This fixes bug #1269.

See this mail thread for details:

  http://lists.nongnu.org/archive/html/chicken-hackers/2016-03/msg00022.html

Signed-off-by: Evan Hanson <ev...@foldling.org>
---
 chicken.h     |  3 ++-
 dbg-stub.c    |  2 +-
 posixunix.scm |  4 +++-
 runtime.c     | 14 +++++++-------
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/chicken.h b/chicken.h
index be5f308..e64d0bb 100644
--- a/chicken.h
+++ b/chicken.h
@@ -907,6 +907,7 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) C_noret;
 # define C_fflush                   fflush
 # define C_getchar                  getchar
 # define C_exit                     exit
+# define C__exit                    _exit
 # define C_dlopen                   dlopen
 # define C_dlclose                  dlclose
 # define C_dlsym                    dlsym
@@ -1751,7 +1752,7 @@ C_fctexport C_word C_message(C_word msg);
 C_fctexport C_word C_fcall C_equalp(C_word x, C_word y) C_regparm;
 C_fctexport C_word C_fcall C_set_gc_report(C_word flag) C_regparm;
 C_fctexport C_word C_fcall C_start_timer(void) C_regparm;
-C_fctexport C_word C_exit_runtime(C_word code);
+C_fctexport C_word C_exit_runtime(C_word code) C_noret;
 C_fctexport C_word C_fcall C_set_print_precision(C_word n) C_regparm;
 C_fctexport C_word C_fcall C_get_print_precision(void) C_regparm;
 C_fctexport C_word C_fcall C_read_char(C_word port) C_regparm;
diff --git a/dbg-stub.c b/dbg-stub.c
index 39dee2a..e9cdd14 100644
--- a/dbg-stub.c
+++ b/dbg-stub.c
@@ -201,7 +201,7 @@ terminate(char *msg)
 {
   fprintf(stderr, "%s\n", msg);
   socket_close();
-  _exit(1);
+  C_exit_runtime(C_fix(1));
 }
 
 
diff --git a/posixunix.scm b/posixunix.scm
index ede6877..a21d0b0 100644
--- a/posixunix.scm
+++ b/posixunix.scm
@@ -1577,6 +1577,8 @@ EOF
 (define process-fork
   (let ((fork (foreign-lambda int "C_fork")))
     (lambda (#!optional thunk killothers)
+      ;; flush all stdio streams before fork
+      ((foreign-lambda int "C_fflush" c-pointer) #f)
       (let ((pid (fork)))
 	(when (fx= -1 pid) 
 	  (posix-error #:process-error 'process-fork "cannot create child process"))
@@ -1586,7 +1588,7 @@ EOF
 		 (lambda (thunk) (thunk)))
 	     (lambda ()
 	       (thunk)
-	       ((foreign-lambda void "_exit" int) 0) ))
+	       (exit 0)))
 	    pid)))))
 
 (define process-execute
diff --git a/runtime.c b/runtime.c
index f11789a..8f1e011 100644
--- a/runtime.c
+++ b/runtime.c
@@ -1311,7 +1311,7 @@ void CHICKEN_parse_command_line(int argc, char *argv[], C_word *heap, C_word *st
 		 " -:S              do not handle segfaults or other serious conditions\n"
 		 "\n  SIZE may have a `k' (`K'), `m' (`M') or `g' (`G') suffix, meaning size\n"
 		 "  times 1024, 1048576, and 1073741824, respectively.\n\n");
-	  exit(0);
+	  C_exit_runtime(C_fix(0));
 
 	case 'h':
 	  switch(*ptr) {
@@ -1527,7 +1527,7 @@ void C_ccall termination_continuation(C_word c, C_word *av)
     C_dbg(C_text("debug"), C_text("application terminated normally\n"));
   }
 
-  exit(0);
+  C_exit_runtime(C_fix(0));
 }
 
 
@@ -1556,7 +1556,7 @@ void usual_panic(C_char *msg)
   } /* fall through if not WIN32 GUI app */
 
   C_dbg("panic", C_text("%s - execution terminated\n\n%s"), msg, dmp);
-  C_exit(1);
+  C_exit_runtime(C_fix(1));
 }
 
 
@@ -1573,7 +1573,7 @@ void horror(C_char *msg)
   } /* fall through */
 
   C_dbg("horror", C_text("\n%s - execution terminated"), msg);  
-  C_exit(1);
+  C_exit_runtime(C_fix(1));
 }
 
 
@@ -4152,7 +4152,7 @@ C_word C_halt(C_word msg)
   if(dmp != NULL) 
     C_dbg("", C_text("\n%s"), dmp);
   
-  C_exit(EX_SOFTWARE);
+  C_exit_runtime(C_fix(EX_SOFTWARE));
   return 0;
 }
 
@@ -4274,8 +4274,8 @@ void C_ccall C_stop_timer(C_word c, C_word *av)
 
 C_word C_exit_runtime(C_word code)
 {
-  exit(C_unfix(code));
-  return 0;			/* to please the compiler... */
+  C_fflush(NULL);
+  C__exit(C_unfix(code));
 }
 
 
-- 
2.7.0

_______________________________________________
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to