@Override
    public void checkExit(int status) {
        if (userSystemExitMonitored()) {
            switch (userSystemExitMode) {
                case DISABLED:
                    break;
                case LOG:
                    // Add exception trace log to help users to debug where exit came from.
                    LOG.warn(
                            "Exiting JVM with status {} is monitored: The system will exit due to this call.",
                            status,
                            new UserSystemExitException());
                    break;
                case THROW:
                    throw new UserSystemExitException();
                default:
                    // Must not happen if exhaustively handling all modes above. Logging as being
                    // already at exit path.
                    LOG.warn("No valid check exit mode configured: {}", userSystemExitMode);
            }
        }
        // As this security manager is current at outer most of the chain and it has exit guard
        // option, invoke inner security manager here after passing guard checking above, if any.
        if (originalSecurityManager != null) {
            originalSecurityManager.checkExit(status);
        }
        // At this point, exit is determined. Halt if defined, otherwise check ended, JVM will call
        // System.exit
        if (haltOnSystemExit) {
            Runtime.getRuntime().halt(status);
        }
    }
@Test
    void testExistingSecurityManagerRespected() {
        // Don't set the following security manager directly to system, which makes test hang.
        SecurityManager originalSecurityManager =
                new SecurityManager() {
                    @Override
                    public void checkPermission(Permission perm) {
                        throw new SecurityException("not allowed");
                    }
                };
        FlinkSecurityManager flinkSecurityManager =
                new FlinkSecurityManager(
                        ClusterOptions.UserSystemExitMode.DISABLED, false, originalSecurityManager);

        assertThatThrownBy(() -> flinkSecurityManager.checkExit(TEST_EXIT_CODE))
                .isInstanceOf(SecurityException.class)
                .hasMessage("not allowed");
    }