From a394f4ad640f97f272bd607c2311cf4d26dc9870 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Mon, 21 Aug 2006 20:12:18 +0000
Subject: [PATCH] use a customized configure macro to tell the script that
 uCLinux does not have fork() that works drop all startup options and code
 related to forking if it is not available

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@40790 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 acinclude.m4    | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 configure       |  4 ++--
 configure.ac    |  2 +-
 main/asterisk.c | 25 ++++++++++++++++-----
 4 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/acinclude.m4 b/acinclude.m4
index 1bd0937030..b621923ef3 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -115,3 +115,63 @@ if test  "x$GNU_MAKE" = "xNot Found"  ; then
 fi
 AC_SUBST([GNU_MAKE])
 ])
+
+# AST_FUNC_FORK
+# -------------
+AN_FUNCTION([fork],  [AST_FUNC_FORK])
+AN_FUNCTION([vfork], [AST_FUNC_FORK])
+AC_DEFUN([AST_FUNC_FORK],
+[AC_REQUIRE([AC_TYPE_PID_T])dnl
+AC_CHECK_HEADERS(vfork.h)
+AC_CHECK_FUNCS(fork vfork)
+if test "x$ac_cv_func_fork" = xyes; then
+  _AST_FUNC_FORK
+else
+  ac_cv_func_fork_works=$ac_cv_func_fork
+fi
+if test "x$ac_cv_func_fork_works" = xcross; then
+  case $host in
+    *-*-amigaos* | *-*-msdosdjgpp* | *-*-uclinux* )
+      # Override, as these systems have only a dummy fork() stub
+      ac_cv_func_fork_works=no
+      ;;
+    *)
+      ac_cv_func_fork_works=yes
+      ;;
+  esac
+  AC_MSG_WARN([result $ac_cv_func_fork_works guessed because of cross compilation])
+fi
+ac_cv_func_vfork_works=$ac_cv_func_vfork
+if test "x$ac_cv_func_vfork" = xyes; then
+  _AC_FUNC_VFORK
+fi;
+if test "x$ac_cv_func_fork_works" = xcross; then
+  ac_cv_func_vfork_works=$ac_cv_func_vfork
+  AC_MSG_WARN([result $ac_cv_func_vfork_works guessed because of cross compilation])
+fi
+
+if test "x$ac_cv_func_vfork_works" = xyes; then
+  AC_DEFINE(HAVE_WORKING_VFORK, 1, [Define to 1 if `vfork' works.])
+else
+  AC_DEFINE(vfork, fork, [Define as `fork' if `vfork' does not work.])
+fi
+if test "x$ac_cv_func_fork_works" = xyes; then
+  AC_DEFINE(HAVE_WORKING_FORK, 1, [Define to 1 if `fork' works.])
+fi
+])# AST_FUNC_FORK
+
+
+# _AST_FUNC_FORK
+# -------------
+AC_DEFUN([_AST_FUNC_FORK],
+  [AC_CACHE_CHECK(for working fork, ac_cv_func_fork_works,
+    [AC_RUN_IFELSE(
+      [AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
+	[
+	  /* By Ruediger Kuhlmann. */
+	  return fork () < 0;
+	])],
+      [ac_cv_func_fork_works=yes],
+      [ac_cv_func_fork_works=no],
+      [ac_cv_func_fork_works=cross])])]
+)# _AST_FUNC_FORK
diff --git a/configure b/configure
index f6af7a1c3f..8b6e7c547d 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 40597 .
+# From configure.ac Revision: 40722 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.60.
 #
@@ -10219,7 +10219,7 @@ else
 fi
 if test "x$ac_cv_func_fork_works" = xcross; then
   case $host in
-    *-*-amigaos* | *-*-msdosdjgpp*)
+    *-*-amigaos* | *-*-msdosdjgpp* | *-*-uclinux* )
       # Override, as these systems have only a dummy fork() stub
       ac_cv_func_fork_works=no
       ;;
diff --git a/configure.ac b/configure.ac
index 0a68a6ed81..8106de3fd1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -198,7 +198,7 @@ AC_CHECK_TYPES([ptrdiff_t])
 AC_FUNC_CHOWN
 AC_FUNC_CLOSEDIR_VOID
 AC_FUNC_ERROR_AT_LINE
-AC_FUNC_FORK
+AST_FUNC_FORK
 AC_FUNC_FSEEKO
 AC_PROG_GCC_TRADITIONAL
 # XXX: these are commented out until we determine whether it matters if our malloc()
diff --git a/main/asterisk.c b/main/asterisk.c
index 84df3b0243..a514627529 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -647,7 +647,8 @@ int ast_safe_system(const char *s)
 	struct rusage rusage;
 	int status;
 
-	ast_replace_sigchld();
+#if HAVE_WORKING_FORK
+    	ast_replace_sigchld();
 
 	pid = fork();
 
@@ -674,6 +675,9 @@ int ast_safe_system(const char *s)
 	}
 
 	ast_unreplace_sigchld();
+#else
+	res = -1;
+#endif
 
 	return res;
 }
@@ -2101,7 +2105,10 @@ static int show_cli_help(void) {
 	printf("   -U <user>       Run as a user other than the caller\n");
 	printf("   -c              Provide console CLI\n");
 	printf("   -d              Enable extra debugging\n");
+#if HAVE_WORKING_FORK
 	printf("   -f              Do not fork\n");
+	printf("   -F              Always fork\n");
+#endif
 	printf("   -g              Dump core in case of a crash\n");
 	printf("   -h              This help screen\n");
 	printf("   -i              Initialize crypto keys at startup\n");
@@ -2209,12 +2216,14 @@ static void ast_readconfig(void)
 			if (sscanf(v->value, "%d", &option_debug) != 1) {
 				option_debug = ast_true(v->value);
 			}
+#if HAVE_WORKING_FORK
 		/* Disable forking (-f at startup) */
 		} else if (!strcasecmp(v->name, "nofork")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_NO_FORK);
 		/* Always fork, even if verbose or debug are enabled (-F at startup) */
 		} else if (!strcasecmp(v->name, "alwaysfork")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_ALWAYS_FORK);
+#endif
 		/* Run quietly (-q at startup ) */
 		} else if (!strcasecmp(v->name, "quiet")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_QUIET);
@@ -2326,9 +2335,14 @@ int main(int argc, char *argv[])
 	/* Check for options */
 	while ((c = getopt(argc, argv, "mtThfdvVqprRgciInx:U:G:C:L:M:")) != -1) {
 		switch (c) {
+#if HAVE_WORKING_FORK
 		case 'F':
 			ast_set_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK);
 			break;
+		case 'f':
+			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
+			break;
+#endif
 		case 'd':
 			option_debug++;
 			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
@@ -2336,9 +2350,6 @@ int main(int argc, char *argv[])
 		case 'c':
 			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE);
 			break;
-		case 'f':
-			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
-			break;
 		case 'n':
 			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_COLOR);
 			break;
@@ -2560,6 +2571,7 @@ int main(int argc, char *argv[])
 	} else
 		ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
 
+#if HAVE_WORKING_FORK
 	if (ast_opt_always_fork || !ast_opt_no_fork) {
 		daemon(0, 0);
 		ast_mainpid = getpid();
@@ -2572,6 +2584,7 @@ int main(int argc, char *argv[])
 		} else
 			ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
 	}
+#endif
 
 	/* Test recursive mutex locking. */
 	if (test_for_thread_safety())
@@ -2719,9 +2732,11 @@ int main(int argc, char *argv[])
 
 	}
 	/* Do nothing */
-	for(;;)  {	/* apparently needed for the MACos */
+	for (;;) {	/* apparently needed for Mac OS X */
 		struct pollfd p = { -1 /* no descriptor */, 0, 0 };
+
 		poll(&p, 0, -1);
 	}
+
 	return 0;
 }
-- 
GitLab