~ chicken-core (chicken-5) 691fee5db54e1dfa2733296bbafb4317a5dee73d


commit 691fee5db54e1dfa2733296bbafb4317a5dee73d
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 15:04:10 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 2b120a1f..1c01298e 100644
--- a/chicken.h
+++ b/chicken.h
@@ -952,6 +952,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
@@ -1874,7 +1875,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 d4016992..73d42eb9 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 56bb246d..ca148f2d 100644
--- a/posixunix.scm
+++ b/posixunix.scm
@@ -1587,6 +1587,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"))
@@ -1596,7 +1598,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 a50ed774..fe22310e 100644
--- a/runtime.c
+++ b/runtime.c
@@ -1402,7 +1402,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) {
@@ -1619,7 +1619,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));
 }
 
 
@@ -1648,7 +1648,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));
 }
 
 
@@ -1665,7 +1665,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));
 }
 
 
@@ -4587,7 +4587,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;
 }
 
@@ -4709,8 +4709,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