From 6fd02510af08d799ba936c904165f40606d14875 Mon Sep 17 00:00:00 2001 From: "wtc%netscape.com" Date: Wed, 23 Jan 2002 03:18:57 +0000 Subject: [PATCH] Bugzilla bug 119340: an inelegant but more reliable way to kill the multithreaded 'selfserv' process on Linux. Modified files: cmd/selfserv/selfserv.c tests/common/init.sh tests/ssl/ssl.sh git-svn-id: svn://10.0.0.236/trunk@112572 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/security/nss/cmd/selfserv/selfserv.c | 31 +++++++++++++++++++- mozilla/security/nss/tests/common/init.sh | 7 +---- mozilla/security/nss/tests/ssl/ssl.sh | 20 +++++++++++-- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/mozilla/security/nss/cmd/selfserv/selfserv.c b/mozilla/security/nss/cmd/selfserv/selfserv.c index 185aa92a56f..818b60f4ea6 100644 --- a/mozilla/security/nss/cmd/selfserv/selfserv.c +++ b/mozilla/security/nss/cmd/selfserv/selfserv.c @@ -141,6 +141,15 @@ static int requestCert; static int verbose; static SECItem bigBuf; +static const char *pidFile; +/* + * On Linux, the first thread we create writes its pid to a file. + * (See bug 119340 for why we do this.) + */ +#ifdef LINUX +static PRInt32 threadIndex; +#endif + static PRThread * acceptorThread; static PRLogModuleInfo *lm; @@ -420,6 +429,27 @@ thread_wrapper(void * arg) { perThread * slot = (perThread *)arg; +#ifdef LINUX /* bug 119403 */ + if (pidFile && PR_AtomicIncrement(&threadIndex) == 1) { + char *pidFile2; + FILE *tmpfile; + + pidFile2 = PORT_Alloc(PORT_Strlen(pidFile) + 3); + if (pidFile2 == NULL) { + return; /* failed */ + } + strcpy(pidFile2, pidFile); + strcat(pidFile2, ".2"); + tmpfile=fopen(pidFile2,"w+"); + + if (tmpfile) { + fprintf(tmpfile,"%d",getpid()); + fclose(tmpfile); + } + PORT_Free(pidFile2); + } +#endif /* LINUX */ + slot->rv = (* slot->startFunc)(slot->a, slot->b, slot->c); /* notify the thread exit handler. */ @@ -1384,7 +1414,6 @@ main(int argc, char **argv) char * cipherString= NULL; const char * dir = "."; char * passwd = NULL; - const char * pidFile = NULL; char * tmp; char * envString; PRFileDesc * listen_sock; diff --git a/mozilla/security/nss/tests/common/init.sh b/mozilla/security/nss/tests/common/init.sh index 6a9be351112..5f7331e1b89 100644 --- a/mozilla/security/nss/tests/common/init.sh +++ b/mozilla/security/nss/tests/common/init.sh @@ -300,11 +300,6 @@ if [ -z "${INIT_SOURCED}" -o "${INIT_SOURCED}" != "TRUE" ]; then tee ${LOGFILE} KILL="kill" - if [ "${OS_ARCH}" = "Linux" ]; then -#on linux the selfserv needs up to 30 seconds to fully die and free -#the socket - SLEEP="sleep 5" # taking the chance and trying on the tinderboxes - fi if [ `uname -s` = "SunOS" ]; then PS="/usr/5bin/ps" else @@ -389,7 +384,7 @@ if [ -z "${INIT_SOURCED}" -o "${INIT_SOURCED}" != "TRUE" ]; then export PATH LD_LIBRARY_PATH SHLIB_PATH LIBPATH export DOMSUF HOSTADDR - export KILL SLEEP PS + export KILL PS export MOZILLA_ROOT SECURITY_ROOT DIST TESTDIR OBJDIR HOSTDIR QADIR export LOGFILE SCRIPTNAME diff --git a/mozilla/security/nss/tests/ssl/ssl.sh b/mozilla/security/nss/tests/ssl/ssl.sh index 06b672fdfc7..ed8e34e1444 100755 --- a/mozilla/security/nss/tests/ssl/ssl.sh +++ b/mozilla/security/nss/tests/ssl/ssl.sh @@ -141,13 +141,29 @@ wait_for_selfserv() ######################################################################## kill_selfserv() { - ${KILL} `cat ${SERVERPID}` + # Bug 119340: This is an inelegant but more reliable way to ensure + # that a multithreaded process on Linux has been completely killed. + # Recall that each thread is a process on Linux. Instead of killing + # the primary thread, we kill the first thread created by selfserv and + # let the thread manager take care of terminating the other threads in + # selfserv. The assumption we rely on is that the primary thread is + # the last one to go because it's the parent of the thread manager. + # + # On Linux, the first thread created by selfserv writes its pid to the + # {SERVERPID}.2 file. + if [ "${OS_ARCH}" = "Linux" ]; then + ${KILL} `cat ${SERVERPID}.2` + else + ${KILL} `cat ${SERVERPID}` + fi wait `cat ${SERVERPID}` if [ ${fileout} -eq 1 ]; then cat ${SERVEROUTFILE} fi - ${SLEEP} #FIXME linux waits 30 seconds - find a shorter way (sockets free) rm ${SERVERPID} + if [ "${OS_ARCH}" = "Linux" ]; then + rm ${SERVERPID}.2 + fi } ########################### start_selfserv #############################