@PostConstruct void init() { SecurityManager security = System.getSecurityManager(); if (security == null) { canKill = true; } else { try { security.checkExit(0); canKill = true; } catch (Exception e) { canKill = false; } } }
/** * {@inheritDoc} * * <p>This method inspects the stack trace and checks who is calling {@link System#exit(int)} and * similar methods * * @throws SecurityException if the caller of this method is not the test runner itself. */ @Override public void checkExit(final int status) { AccessController.doPrivileged( new PrivilegedAction<Void>() { @Override public Void run() { final String systemClassName = System.class.getName(), runtimeClassName = Runtime.class.getName(); String exitMethodHit = null; for (final StackTraceElement se : Thread.currentThread().getStackTrace()) { final String className = se.getClassName(), methodName = se.getMethodName(); if (("exit".equals(methodName) || "halt".equals(methodName)) && (systemClassName.equals(className) || runtimeClassName.equals(className))) { exitMethodHit = className + '#' + methodName + '(' + status + ')'; continue; } if (exitMethodHit != null) { if (className.startsWith(TEST_RUNNER_PACKAGE)) { // this exit point is allowed, we return normally from closure: return /*void*/ null; } else { // anything else in stack trace is not allowed, break and throw SecurityException // below: break; } } } if (exitMethodHit == null) { // should never happen, only if JVM hides stack trace - replace by generic: exitMethodHit = "JVM exit method"; } throw new SecurityException( exitMethodHit + " calls are not allowed because they terminate the test runner's JVM."); } }); // we passed the stack check, delegate to super, so default policy can still deny permission: super.checkExit(status); }
private void build(Applet applet, String[] args, int width, int height) { ++instances; this.applet = applet; this.args = args; applet.setStub(this); name = applet.getClass().getName(); setTitle(name); // Set up properties. Properties props = System.getProperties(); props.put("browser", "Acme.MainFrame"); props.put("browser.version", "11jul96"); props.put("browser.vendor", "Acme Laboratories"); props.put("browser.vendor.url", "http://www.acme.com/"); // Turn args into parameters by way of the properties list. if (args != null) parseArgs(args, props); // If width and height are specified in the parameters, override // the compiled-in values. String widthStr = getParameter("width"); if (widthStr != null) width = Integer.parseInt(widthStr); String heightStr = getParameter("height"); if (heightStr != null) height = Integer.parseInt(heightStr); // Were width and height specified somewhere? if (width == -1 || height == -1) { System.err.println("Width and height must be specified."); return; } // Lay out components. Container contentPane = getContentPane(); contentPane.add(applet, BorderLayout.CENTER); // Set up size. pack(); validate(); appletSize = applet.getSize(); applet.setSize(width, height); setVisible(true); /* Added WindowListener inner class to detect close events. */ SecurityManager sm = System.getSecurityManager(); boolean doExit = true; if (sm != null) { try { sm.checkExit(0); } catch (SecurityException e) { doExit = false; } } final boolean _doExit = doExit; // WindowListener inner class to detect close events. addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent winEvent) { if (JMainFrame.this.applet != null) { JMainFrame.this.applet.destroy(); } hide(); try { dispose(); } catch (IllegalStateException e) { } if (_doExit) { System.exit(0); } } }); // Start a separate thread to call the applet's init() and start() // methods, in case they take a long time. (new Thread(this)).start(); }
/** * Tell the VM to run the finalize() method on every single Object before it exits. Note that the * JVM may still exit abnormally and not perform this, so you still don't have a guarantee. And * besides that, this is inherently unsafe in multi-threaded code, as it may result in deadlock as * multiple threads compete to manipulate objects. This value defaults to <code>false</code>. * There is a security check, <code>checkExit(0)</code>. * * @param finalizeOnExit whether to finalize all Objects on exit * @throws SecurityException if permission is denied * @see #exit(int) * @see #gc() * @since 1.1 * @deprecated never rely on finalizers to do a clean, thread-safe, mop-up from your code */ public static void runFinalizersOnExit(boolean finalizeOnExit) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkExit(0); current.finalizeOnExit = finalizeOnExit; }
/** * Forcibly terminate the virtual machine. This call never returns. It is much more severe than * <code>exit</code>, as it bypasses all shutdown hooks and initializers. Use caution in calling * this! Of course, there is a security check, <code>checkExit(status)</code>. * * @param status the status to exit with * @throws SecurityException if permission is denied * @since 1.3 * @see #exit(int) * @see #addShutdownHook(Thread) */ public void halt(int status) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkExit(status); exitInternal(status); }
/** * Exit the Java runtime. This method will either throw a SecurityException or it will never * return. The status code is returned to the system; often a non-zero status code indicates an * abnormal exit. Of course, there is a security check, <code>checkExit(status)</code>. * * <p>First, all shutdown hooks are run, in unspecified order, and concurrently. Next, if * finalization on exit has been enabled, all pending finalizers are run. Finally, the system * calls <code>halt</code>. * * <p>If this is run a second time after shutdown has already started, there are two actions. If * shutdown hooks are still executing, it blocks indefinitely. Otherwise, if the status is nonzero * it halts immediately; if it is zero, it blocks indefinitely. This is typically called by <code> * System.exit</code>. * * @param status the status to exit with * @throws SecurityException if permission is denied * @see #addShutdownHook(Thread) * @see #runFinalizersOnExit(boolean) * @see #runFinalization() * @see #halt(int) */ public void exit(int status) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkExit(status); exitNoChecks(status); }
@Override public void checkExit(int status) { super.checkExit(status); throw new RuntimeException("System.exit() was called. Raising exception."); }
@Override public void checkExit(final int pStatus) { if (finalSecurityManager != null) finalSecurityManager.checkExit(pStatus); }
@Override public void checkExit(final int status) { super.checkExit(status); if (Thread.currentThread().getName().startsWith("interpreter")) throw new SecurityException(); if (securityManager != null) securityManager.checkExit(status); }