~ chicken-core (chicken-5) 62662bb20557b9b43505c7ad1710a727aa3104e0


commit 62662bb20557b9b43505c7ad1710a727aa3104e0
Author:     Kooda <kooda@upyum.com>
AuthorDate: Sat Mar 19 13:21:43 2016 +0100
Commit:     Peter Bex <peter@more-magic.net>
CommitDate: Sun Apr 3 14:02:47 2016 +0200

    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 <evhan@foldling.org>
    Signed-off-by: Peter Bex <peter@more-magic.net>

diff --git a/chicken.h b/chicken.h
index be5f308d..e64d0bba 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 39dee2a8..e9cdd14f 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 ede68772..a21d0b03 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 f11789ae..8f1e011f 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));
 }
 
 
Trap