Java tutorial
/** * Copyright (c) 2012 Yahoo! Inc. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. See accompanying LICENSE file. */ package ape; import java.io.IOException; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.ParseException; /** * This command remounts the root filesystem as read only. This is done with the sysrq trigger. * You can check to see if sysrq is enabled by typing the following * into a bash shell on the host in question: * * cat /proc/sys/kernel/sysrq * * If 1 is returned then the sysrq module is enabled. If it returns 0 then you * can enable it by typing the following: * * echo 1 > /proc/sys/kernel/sysrq * * The KernelPanicCommand works by echoing a character to the sysrq trigger which * will execute a particular command (see http://en.wikipedia.org/wiki/Magic_SysRq_key) * for a full list of what the sysrq trigger can do. * * */ public class RemountFileSysReadOnlyCommand extends ApeCommand { private Option option; /** * The constructor for this command simply creates its Option object (used by * the CLI parser) */ public RemountFileSysReadOnlyCommand() { option = OptionBuilder.withValueSeparator().withDescription("Remounts all filesystems as read-only") .withLongOpt("remount").create("r"); } public boolean runImpl(String[] args) throws ParseException, IOException { String cmd = "echo u > /proc/sysrq-trigger"; ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(true); Process p = null; try { p = pb.start(); } catch (IOException e) { System.err.println("Executing remount catches IOException"); e.printStackTrace(); return false; } // If the process returns a non-zero value, there is some error executing the command try { if (p.waitFor() != 0) { System.err.println("Non-zero return code (" + p.exitValue() + ") when executing: '" + cmd + "'"); return false; } } catch (InterruptedException e) { System.err.println("Executing '" + cmd + "' was interrupted"); e.printStackTrace(); return false; } return true; } /** * Get the option of RemountFileSysReadOnlyCommand */ public Option getOption() { return option; } /** * Get the long flag for RemountFileSysReadOnlyCommand */ public String getName() { return option.getLongOpt(); } }