From owner-freebsd-java@FreeBSD.ORG Thu Oct 23 02:19:30 2003 Return-Path: Delivered-To: freebsd-java@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A5BEB16A4B3; Thu, 23 Oct 2003 02:19:30 -0700 (PDT) Received: from phantom.cris.net (phantom.cris.net [212.110.130.74]) by mx1.FreeBSD.org (Postfix) with ESMTP id A7F3543FDD; Thu, 23 Oct 2003 02:19:27 -0700 (PDT) (envelope-from phantom@FreeBSD.org.ua) Received: (from phantom@localhost) by phantom.cris.net (8.12.6/8.12.6) id h9N9Sdmx075628; Thu, 23 Oct 2003 12:28:39 +0300 (EEST) (envelope-from phantom) Date: Thu, 23 Oct 2003 12:28:39 +0300 From: Alexey Zelkin To: java@freebsd.org Message-ID: <20031023122839.A75570@phantom.cris.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i X-Operating-System: FreeBSD 4.7-STABLE i386 Subject: jdk14 fork() problem fix X-BeenThere: freebsd-java@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Porting Java to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Oct 2003 09:19:30 -0000 hi, This is an intermediate version of fix of jdk fork problem (i.e. Runtime.getRuntime().exec() and friends). It does affect only people who use libc_r (kse and thr should not be affected). Please try this one and report me if it fixes problems for you. PS: If you are rebuilding already built jdk (i.e. object files are already compiled) remove 'control/build/bsd-i586/tmp/java/java.lang' directory before restarting of build. Index: UNIXProcess_md.c.bsd =================================================================== RCS file: /home/jdk14-cvs/jdk142-src/j2se/src/solaris/native/java/lang/UNIXProcess_md.c.bsd,v retrieving revision 1.2 diff -u -r1.2 UNIXProcess_md.c.bsd --- UNIXProcess_md.c.bsd 15 Oct 2003 15:49:39 -0000 1.2 +++ UNIXProcess_md.c.bsd 23 Oct 2003 10:20:48 -0000 @@ -22,6 +22,12 @@ #include #include +#if defined(__FreeBSD__) +#include +#include +#include +#endif + /* path in the environment */ static char **PATH = 0; /* effective uid */ @@ -228,6 +234,61 @@ } } +#if defined(__FreeBSD__) + +extern pid_t __sys_fork(void); + +static pid_t +jdk_fork_wrapper() +{ + pid_t resultPid; +#if (__FreeBSD_version < 5) + static int is_libc_r = -1; + void *funcref; + + if (is_libc_r == -1) { + is_libc_r = 1; + + /* + * BSDNOTE: Check for loaded symbols. + * + * If "_thr_critical_enter" is found assume we are using 'libthr'. + * If _kse_critical_enter is found assume we are using 'libkse'. + * Otherwise we are using libc_r. + * + * If libc_r is loaded, use fork system call drectly to avoid + * problems with using protected pages. + * + * --phantom + */ + funcref = dlsym(RTLD_DEFAULT, "_kse_critical_enter"); + if (funcref != NULL) + is_libc_r = 0; + else { + funcref = dlsym(RTLD_DEFAULT, "_thr_critical_enter"); + if (funcref != NULL) + is_libc_r = 0; + } + } + + if (is_libc_r == 0) { + /* Not a libc_r */ + resultPid = fork(); + } else { +#endif /* __FreeBSD_version < 5 */ + pthread_suspend_all_np(); + resultPid = __sys_fork(); + if (resultPid != 0) + /* leave child in single threading mode */ + pthread_resume_all_np(); +#if (__FreeBSD_version < 5) + } +#endif /* __FreeBSD_version < 5 */ + + return resultPid; +} +#endif /* __FreeBSD__ */ + JNIEXPORT jint JNICALL Java_java_lang_UNIXProcess_forkAndExec(JNIEnv *env, jobject process, @@ -335,8 +396,12 @@ if (path != NULL) { cwd = (char *)JNU_GetStringPlatformChars(env, path, NULL); } - + +#if defined(__FreeBSD__) + resultPid = jdk_fork_wrapper(); +#else resultPid = fork(); +#endif if (resultPid < 0) { char errmsg[128];