I would add a umask before launching process with java, but the java process api (i use java 9) does not provide a method to set an umask.
I tried to run a process by executing umask with a value that allows me to create but not read (0644). it did not work ...
Optional<String> umask = ProcessUtil.getUmask();
if (!umask.isPresent()) {
this.restartOnFail();
return;
}
ProcessUtil.setUmask(parent.config.umask);
this.timeAtLaunch = System.currentTimeMillis();
this.status = ProcessStatus.LAUNCHING;
this.process = pb.start();
this.onExitProcess();
ProcessUtil.setUmask(umask.get());
The setUmask code is :
public static void setUmask(String mask) {
try {
String[] commands = {"umask", mask};
Process exec = Runtime.getRuntime().exec(commands);
exec.waitFor();
} catch (Exception e) {
Server.global.logErr("Impossible to set umask for %s.", mask);
}
}
I have tried to do that and it seems to not work (I launched a process that creates a file and reads it, and it should just create it and make a error when reading it.) because the process created a file that did not have the permissions set by umask. He was able to read the file, which should not have happened ...
I have created a ready runnable class :
import java.io.*;
public class StackOverFlowTest {
public static void setUmask(String mask) {
try {
String[] commands = {"umask", mask};
Process exec = Runtime.getRuntime().exec(commands);
exec.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void runCommand(String [] command) throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println("stdout: " + s);
}
while ((s = stdError.readLine()) != null) {
System.out.println("stderr: " + s);
}
proc.waitFor();
}
public static void main(String[] args) {
setUmask("0644"); // set this permission -----w--w-
String[] create = {"touch", "file.stackoverflow"};
String[] read = {"cat", "file.stackoverflow"};
try {
runCommand(create);
runCommand(read);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This code actually don't show any error when cat is launch and it's a problem !
(Sorry for my english)
So there is someone that have a tip to launch process with some permissions with java ?