summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape
diff options
context:
space:
mode:
Diffstat (limited to 'pki/base/common/src/com/netscape')
-rw-r--r--pki/base/common/src/com/netscape/certsrv/apps/CMS.java4
-rw-r--r--pki/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java2
-rw-r--r--pki/base/common/src/com/netscape/cms/logging/LogFile.java9
-rw-r--r--pki/base/common/src/com/netscape/cmscore/apps/CMSEngine.java56
-rw-r--r--pki/base/common/src/com/netscape/cmscore/apps/Upgrade.java4
-rw-r--r--pki/base/common/src/com/netscape/cmscore/util/OsSubsystem.java440
6 files changed, 55 insertions, 460 deletions
diff --git a/pki/base/common/src/com/netscape/certsrv/apps/CMS.java b/pki/base/common/src/com/netscape/certsrv/apps/CMS.java
index 5b8aa2c78..3a36c71bc 100644
--- a/pki/base/common/src/com/netscape/certsrv/apps/CMS.java
+++ b/pki/base/common/src/com/netscape/certsrv/apps/CMS.java
@@ -347,8 +347,8 @@ public final class CMS {
*
* @return process id of the server
*/
- public static int getpid() {
- return _engine.getpid();
+ public static int getPID() {
+ return _engine.getPID();
}
/**
diff --git a/pki/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java b/pki/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
index d299b5814..ba9731867 100644
--- a/pki/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
+++ b/pki/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
@@ -111,7 +111,7 @@ public interface ICMSEngine extends ISubsystem {
*
* @return process id of the server
*/
- public int getpid();
+ public int getPID();
public void reinit(String id) throws EBaseException;
diff --git a/pki/base/common/src/com/netscape/cms/logging/LogFile.java b/pki/base/common/src/com/netscape/cms/logging/LogFile.java
index 15ffafec5..0a5f18579 100644
--- a/pki/base/common/src/com/netscape/cms/logging/LogFile.java
+++ b/pki/base/common/src/com/netscape/cms/logging/LogFile.java
@@ -184,11 +184,6 @@ public class LogFile implements ILogEventListener, IExtendedPluginInfo {
private Thread mFlushThread = null;
/**
- * The current pid for the log entries
- */
- protected int mPid = CMS.getpid();
-
- /**
* The selected log event types
*/
protected String mSelectedEventsList = null;
@@ -1105,12 +1100,12 @@ public class LogFile implements ILogEventListener, IExtendedPluginInfo {
// This should follow the Common Log Format which still needs
// some work.
if (ev.getMultiline() == ILogger.L_MULTILINE) {
- entry = mPid + "." + Thread.currentThread().getName() + " - ["
+ entry = CMS.getPID() + "." + Thread.currentThread().getName() + " - ["
+ mLogDateFormat.format(mDate) + "] [" +
Integer.toString(ev.getSource()) + "] [" + Integer.toString(ev.getLevel())
+ "] " + prepareMultiline(ev.toString());
} else {
- entry = mPid + "." + Thread.currentThread().getName() + " - ["
+ entry = CMS.getPID() + "." + Thread.currentThread().getName() + " - ["
+ mLogDateFormat.format(mDate) + "] [" +
Integer.toString(ev.getSource()) + "] [" + Integer.toString(ev.getLevel())
+ "] " + ev.toString();
diff --git a/pki/base/common/src/com/netscape/cmscore/apps/CMSEngine.java b/pki/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
index ba8462727..5fdcaece0 100644
--- a/pki/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
+++ b/pki/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
@@ -17,8 +17,10 @@
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.apps;
+import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
@@ -167,7 +169,6 @@ import com.netscape.cmscore.security.PWsdrCache;
import com.netscape.cmscore.time.SimpleTimeSource;
import com.netscape.cmscore.usrgrp.UGSubsystem;
import com.netscape.cmscore.util.Debug;
-import com.netscape.cmscore.util.OsSubsystem;
import com.netscape.cmsutil.net.ISocketFactory;
import com.netscape.cmsutil.password.IPasswordStore;
import com.netscape.cmsutil.util.Utils;
@@ -182,7 +183,9 @@ public class CMSEngine implements ICMSEngine {
public static final SubsystemRegistry mSSReg = SubsystemRegistry.getInstance();
- public static String instanceDir; /* path to instance <server-root>/cert-<instance-name> */
+ public String instanceDir; /* path to instance <server-root>/cert-<instance-name> */
+ private String instanceId;
+ private int pid;
private IConfigStore mConfig = null;
private ISubsystem mOwner = null;
@@ -204,8 +207,6 @@ public class CMSEngine implements ICMSEngine {
new SubsystemInfo(LogSubsystem.ID,
LogSubsystem.getInstance()),
new SubsystemInfo(
- OsSubsystem.ID, OsSubsystem.getInstance()),
- new SubsystemInfo(
JssSubsystem.ID, JssSubsystem.getInstance()),
new SubsystemInfo(
DBSubsystem.ID, DBSubsystem.getInstance()),
@@ -256,6 +257,22 @@ public class CMSEngine implements ICMSEngine {
* private constructor.
*/
public CMSEngine() {
+
+ // Shutdown on SIGINT, SIGTERM, or SIGHUP.
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ public void run() {
+ /*LogDoc
+ *
+ * @phase watchdog check
+ */
+ getLogger().log(ILogger.EV_SYSTEM,
+ ILogger.S_OTHER,
+ ILogger.LL_INFO,
+ "OS: Received shutdown signal");
+
+ shutdown();
+ };
+ });
}
/**
@@ -359,6 +376,7 @@ public class CMSEngine implements ICMSEngine {
}
instanceDir = config.getString("instanceRoot");
+ instanceId = config.getString("instanceId");
loadDynSubsystems();
@@ -958,7 +976,6 @@ public class CMSEngine implements ICMSEngine {
* @exception EBaseException if any subsystem fails to startup.
*/
public void startup() throws EBaseException {
- //OsSubsystem.nativeExit(0);
startupSubsystems(mStaticSubsystems);
if (mDynSubsystems != null)
startupSubsystems(mDynSubsystems);
@@ -1539,7 +1556,7 @@ public class CMSEngine implements ICMSEngine {
return (File.separator.equals("\\"));
}
- private static void shutdownHttpServer() {
+ private void shutdownHttpServer() {
try {
String cmds[] = null;
@@ -1691,8 +1708,31 @@ public class CMSEngine implements ICMSEngine {
return new PWCBsdr();
}
- public int getpid() {
- return OsSubsystem.getpid();
+ public int getPID() {
+ if (pid != 0) return pid;
+
+ BufferedReader bf = null;
+ try {
+ // PID file is be created by wrapper script (e.g. /usr/sbin/tomcat6)
+ String dir = mConfig.getString("pidDir");
+ String name = dir+File.separator+instanceId+".pid";
+
+ if (dir == null) return pid;
+ File file = new File(name);
+ if (!file.exists()) return pid;
+
+ bf = new BufferedReader(new FileReader(file));
+ String value = bf.readLine();
+ pid = Integer.parseInt(value);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ } finally {
+ if (bf != null) try { bf.close(); } catch (Exception e) { e.printStackTrace(); }
+ }
+
+ return pid;
}
public Date getCurrentDate() {
diff --git a/pki/base/common/src/com/netscape/cmscore/apps/Upgrade.java b/pki/base/common/src/com/netscape/cmscore/apps/Upgrade.java
index 326e9b5e6..cd5b2991f 100644
--- a/pki/base/common/src/com/netscape/cmscore/apps/Upgrade.java
+++ b/pki/base/common/src/com/netscape/cmscore/apps/Upgrade.java
@@ -21,7 +21,7 @@ import java.io.File;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.cmscore.util.OsSubsystem;
+import com.netscape.cmsutil.util.Utils;
public final class Upgrade {
public static void perform422to45(IConfigStore c)
@@ -124,7 +124,7 @@ public final class Upgrade {
c.putString("log.instance.System.type",
"system");
- if (!OsSubsystem.isUnix()) {
+ if (Utils.isNT()) {
c.putString("log.impl.NTEventLog.class",
"com.netscape.certsrv.logging.NTEventLog");
diff --git a/pki/base/common/src/com/netscape/cmscore/util/OsSubsystem.java b/pki/base/common/src/com/netscape/cmscore/util/OsSubsystem.java
deleted file mode 100644
index 6e5162d9a..000000000
--- a/pki/base/common/src/com/netscape/cmscore/util/OsSubsystem.java
+++ /dev/null
@@ -1,440 +0,0 @@
-// --- BEGIN COPYRIGHT BLOCK ---
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; version 2 of the License.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License along
-// with this program; if not, write to the Free Software Foundation, Inc.,
-// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-//
-// (C) 2007 Red Hat, Inc.
-// All rights reserved.
-// --- END COPYRIGHT BLOCK ---
-package com.netscape.cmscore.util;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.IOException;
-
-import com.netscape.certsrv.apps.CMS;
-import com.netscape.certsrv.base.EBaseException;
-import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.certsrv.base.ISubsystem;
-import com.netscape.certsrv.logging.ILogger;
-import com.netscape.cmscore.base.SubsystemRegistry;
-import com.netscape.osutil.LibC;
-import com.netscape.osutil.OSUtil;
-import com.netscape.osutil.Signal;
-import com.netscape.osutil.SignalListener;
-import com.netscape.osutil.UserID;
-
-/**
- * This object contains the OS independent interfaces. It's currently
- * used for Unix signal and user handling, but could eventually be extended
- * for NT interfaces.
- * <P>
- *
- * @author mikep
- * @version $Revision$, $Date$
- */
-public final class OsSubsystem implements ISubsystem {
-
- public static final String ID = "os";
- protected IConfigStore mConfig;
- protected String mInstanceDir;
- protected ISubsystem mOwner;
- protected ILogger mLogger = null;
- protected static SignalThread mSignalThread = null;
-
- private static final String PROP_OS = "os";
- private static final String PROP_USERID = "userid";
-
- // singleton enforcement
-
- private static OsSubsystem mInstance = new OsSubsystem();
-
- public static OsSubsystem getInstance() {
- return mInstance;
- }
-
- // end singleton enforcement.
-
- /**
- * Constructor for an OS subsystem
- */
- private OsSubsystem() {
- }
-
- /**
- * Retrieves subsystem name.
- */
- public String getId() {
- return ID;
- }
-
- public void setId(String id) throws EBaseException {
- throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_OPERATION"));
- }
-
- /**
- * Initializes this subsystem with the given configuration
- * store.
- * <P>
- *
- * @param owner owner of this subsystem
- * @param config configuration store
- * @exception EBaseException failed to initialize
- */
- public void init(ISubsystem owner, IConfigStore config)
- throws EBaseException {
-
- mOwner = owner;
- mConfig = config;
- mLogger = CMS.getLogger();
-
- mInstanceDir = CMS.getConfigStore().getString("instanceRoot");
-
- // We currently only deal with Unix and NT
- if (isUnix()) {
- //initUnix();
- } else {
- initNT();
- }
- try {
- //System.out.println(" The dir I'm seeing is " + mInstanceDir);
- String pidName = mInstanceDir + File.separator + "config" + File.separator + "cert-pid";
- BufferedWriter pidOut = new BufferedWriter(new FileWriter(pidName));
- int pid = OsSubsystem.getpid();
-
- pidOut.write(Integer.toString(pid));
- pidOut.close();
- OSUtil.getFileWriteLock(pidName);
- } catch (Exception e) {
- //XX to stderr XXXXXX
- //e.printStackTrace();
- }
- }
-
- /**
- * Starts up OS
- */
- public void startup() throws EBaseException {
- if (isUnix()) {
- String pf = mConfig.getString("pidFile", null);
-
- if (pf == null) {
- return; // development environment does not rely on this
- }
- // dont ever call detach in Java environment,
- // it does a fork()
- // LibC.detach();
-
- // need to do pid here, pid will be changed after detach
- int pid = LibC.getpid();
- String pidStr = Integer.toString(pid);
-
- try {
- FileOutputStream fos = new FileOutputStream(pf);
-
- fos.write(pidStr.getBytes());
- fos.close();
- } catch (IOException e) {
-
- /*LogDoc
- *
- * @phase start OS subsystem
- * @message OS: <exception thrown>
- */
- mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
- ILogger.LL_FAILURE, "OS: " + e.toString());
- }
- }
- }
-
- /**
- * Returns the process ID of the Certificate Server process. Works
- * on Unix and NT.
- */
- public static int getpid() {
- if (isUnix()) {
- return LibC.getpid();
- } else {
- return OSUtil.getNTpid();
- }
- }
-
- /**
- * Used to change the process user id usually called after the appropriate
- * network ports have been opened.
- */
- public void setUserId() throws EBaseException {
- if (!isUnix())
- return;
-
- String userid;
-
- userid = mConfig.getString(PROP_USERID, null);
- String id = String.valueOf(UserID.get());
-
- // Change the userid to the prefered Unix user
- if (userid == null) {
-
- /*LogDoc
- *
- * @phase set user id
- * @arg0 default user id
- */
- mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
- ILogger.LL_FAILURE,
- "OS: No user id in config file. Running as {0}", id);
- } else {
- Object[] params = { userid, id };
-
- try {
- UserID.set(userid);
- } catch (IllegalArgumentException e) {
-
- /*LogDoc
- *
- * @phase set user id
- * @arg0 supplied user id in config
- * @arg1 default user id
- */
- mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
- ILogger.LL_FAILURE,
- "OS: No such user as {0}. Running as {1}", params);
- } catch (SecurityException e) {
-
- /*LogDoc
- *
- * @phase set user id
- * @arg0 supplied user id in config
- * @arg1 default user id
- */
- mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
- ILogger.LL_FAILURE,
- "OS: Can't change process uid to {0}. Running as {1}",
- params);
- }
- }
- }
-
- private void initNT() {
- }
-
- /**
- * Stops the watchdog. You need to call this if you want the
- * server to really shutdown, otherwise the watchdog will just
- * restart us.
- * <P>
- */
- public static void stop() {
- if (isUnix()) {
- shutdownUnix();
- Signal.send(LibC.getppid(), Signal.SIGTERM);
- } else {
-
- /*LogDoc
- *
- * @phase stop watchdog
- */
- CMS.getLogger().log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
- ILogger.LL_INFO,
- "OS: stop the NT watchdog!");
- }
- }
-
- /**
- * Stops this system.
- * <P>
- */
- public void shutdown() {
- if (isUnix()) {
- shutdownUnix();
- } else {
- shutdownNT();
- }
- }
-
- /**
- * Shutdown the unix system handlers
- * <P>
- */
- private static void shutdownUnix() {
-
- // Don't accidentally stop this thread
- //if (Thread.currentThread() != mSignalThread && mSignalThread != null) {
- // mSignalThread.stop();
- // mSignalThread = null;
- //}
-
- /* Don't release this signals to protect the process
- Signal.release(Signal.SIGHUP);
- Signal.release(Signal.SIGTERM);
- Signal.release(Signal.SIGINT);
- */
- }
-
- /**
- * Shutdown the NT system handlers
- * <P>
- */
- private void shutdownNT() {
- }
-
- /**
- * Restart the server
- * <P>
- */
- public void restart() {
-
- /**
- * if (isUnix()) {
- * restartUnix();
- * } else {
- * restartNT();
- * }
- **/
- }
-
- /**
- * Returns the root configuration storage of this system.
- * <P>
- *
- * @return configuration store of this subsystem
- */
- public IConfigStore getConfigStore() {
- return mConfig;
- }
-
- /**
- * A universal routine to decide if we are Unix or something else.
- * This is mostly used for signal handling and uids.
- *
- * <P>
- *
- * @return true if these OS the JavaVM is running on is some Unix varient
- */
- public static boolean isUnix() {
- // XXX What about MacOS?
- return (File.separatorChar == '/');
- }
-
- /**
- * Unix signal thread. Sleep for a second and then check on the
- * signals we're interested in. If one is set, do the right stuff
- */
- final class SignalThread extends Thread {
-
- /**
- * Signal thread constructor
- */
- public SignalThread() {
- super();
- super.setName("OsSignal-" + (Thread.activeCount() + 1));
- }
-
- /**
- * Check for signal changes every second
- */
- public void run() {
- while (true) {
- // Sleep for the interval and then check for caught signals
- // synchronized (Thread.this) {
- synchronized (this) {
- try {
- // Thread.this.wait(1000);
- this.wait(1000);
- } catch (InterruptedException e) {
- // Not very interesting...
- }
- }
-
- // wants us to exit?
- if (Signal.caught(Signal.SIGINT) > 0 ||
- Signal.caught(Signal.SIGTERM) > 0) {
-
- /*LogDoc
- *
- * @phase watchdog check
- */
- mLogger.log(ILogger.EV_SYSTEM,
- ILogger.S_OTHER,
- ILogger.LL_INFO,
- "OS: Received shutdown signal");
- SubsystemRegistry.getInstance().get("MAIN").shutdown();
- return;
- }
-
- // Tell to restart us
- if (Signal.caught(Signal.SIGHUP) > 0) {
-
- /*LogDoc
- *
- * @phase watchdog check
- */
- mLogger.log(ILogger.EV_SYSTEM,
- ILogger.S_OTHER,
- ILogger.LL_INFO,
- "OS: Received restart signal");
- restart();
- return;
- }
-
- }
- }
-
- }
-}
-
-class SIGTERMListener extends SignalListener {
- private OsSubsystem mOS;
-
- public SIGTERMListener(OsSubsystem os) {
- mOS = os;
- }
-
- public void process() {
- System.out.println("SIGTERMListener process");
- // XXX - temp, should call shutdown
- System.exit(0);
- //PKIServer.getPKIServer().shutdown();
- }
-}
-
-class SIGINTListener extends SignalListener {
- private OsSubsystem mOS;
-
- public SIGINTListener(OsSubsystem os) {
- mOS = os;
- }
-
- public void process() {
- System.out.println("SIGINTListener process");
- // XXX - temp, should call shutdown
- System.exit(0);
- //PKIServer.getPKIServer().shutdown();
- }
-}
-
-class SIGHUPListener extends SignalListener {
- private OsSubsystem mOS;
-
- public SIGHUPListener(OsSubsystem os) {
- mOS = os;
- }
-
- public void process() {
- System.out.println("SIGHUPListener process");
- // XXX - temp, should call shutdown
- // System.exit(0);
- //PKIServer.getPKIServer().shutdown();
- }
-}