import java.io.IOException;
import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
+import java.net.InterfaceAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.Enumeration;
import java.util.TreeMap;
import org.argeo.init.Service;
class FreedPid1 {
public static void main(String... args) {
- final long pid = ProcessHandle.current().pid();
- System.out.println("FREEd Init daemon starting with pid " + pid + "...");
+ try {
+ final long pid = ProcessHandle.current().pid();
+ System.out.println("FREEd Init daemon starting with pid " + pid + "...");
// System.out.println(System.getProperty("user.dir"));
// System.out.println(System.getProperty("user.name"));
// System.out.println(System.getProperty("user.home"));
- // System.setProperty("user.dir", "/tmp");
- for (Object key : new TreeMap<>(System.getProperties()).keySet()) {
- System.out.println(key + "=" + System.getProperty(key.toString()));
- }
+ // System.setProperty("user.dir", "/tmp");
+ for (Object key : new TreeMap<>(System.getProperties()).keySet()) {
+ System.out.println(key + "=" + System.getProperty(key.toString()));
+ }
- System.out.flush();
+ flushStd();
- Signal.handle(new Signal("TERM"), (signal) -> {
- System.out.println("SIGTERM caught");
- System.exit(0);
- });
- Signal.handle(new Signal("INT"), (signal) -> {
- System.out.println("SIGINT caught");
- System.exit(0);
- });
- Signal.handle(new Signal("HUP"), (signal) -> {
- System.out.println("SIGHUP caught");
- System.exit(0);
- });
-
- if (args.length > 0 && ("1".equals(args[0]) //
- || "single".equals(args[0]) //
- || "emergency".equals(args[0]))) {
- // TODO check if we can remove dependency to management
- String classpath = ManagementFactory.getRuntimeMXBean().getClassPath();
- String feedbackMode = "concise";
- // TODO --startup script
- JavaShellToolBuilder builder = JavaShellToolBuilder.builder();
- try {
- builder.start("--execution", "direct", "--class-path", classpath, "--feedback", feedbackMode);
- } catch (Exception e) {
- e.printStackTrace();
- System.err.flush();
- System.exit(1);
- return;
- }
+ Signal.handle(new Signal("TERM"), (signal) -> {
+ System.out.println("SIGTERM caught");
+ System.exit(0);
+ });
+ Signal.handle(new Signal("INT"), (signal) -> {
+ System.out.println("SIGINT caught");
+ System.exit(0);
+ });
+ Signal.handle(new Signal("HUP"), (signal) -> {
+ System.out.println("SIGHUP caught");
+ System.exit(0);
+ });
- } else {
- // init Linux services
- initSysctl();
- startInitDService("networking");
- startInitDService("nslcd");
+ if (args.length > 0 && ("1".equals(args[0]) //
+ || "single".equals(args[0]) //
+ || "emergency".equals(args[0]))) {
+ // TODO check if we can remove dependency to management
+ String classpath = ManagementFactory.getRuntimeMXBean().getClassPath();
+ String feedbackMode = "concise";
+ // TODO --startup script
+ JavaShellToolBuilder builder = JavaShellToolBuilder.builder();
+ try {
+ builder.start("--execution", "direct", "--class-path", classpath, "--feedback", feedbackMode);
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.err.flush();
+ System.exit(1);
+ return;
+ }
- waitForNetwork();
+ } else {
+ // init Linux services
+ initSysctl();
+ startInitDService("networking");
+ flushStd();
+
+ if (!waitForNetwork(30 * 1000)) {
+ System.err.println("No network available");
+ }
+ flushStd();
- // init Argeo CMS
- Service.main(args);
+ startInitDService("nslcd");
+
+ // init Argeo CMS
+ Service.main(args);
+ }
+ } catch (Throwable e) {
+ System.err.println("Unexpected exception in free-pid1 init, shutting down... " + e.getMessage());
+ e.printStackTrace();
+ flushStd();
+ System.exit(1);
+// if(e instanceof Error)
+// throw e;
}
}
System.out.println("Service " + serviceName + " not found and therefore not started");
}
- static void waitForNetwork() {
- // TODO Do it properly
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
+ static boolean waitForNetwork(long timeout) {
+ long begin = System.currentTimeMillis();
+ long duration = 0;
+ boolean networkAvailable = false;
+ networkAvailable: while (!networkAvailable) {
+ duration = System.currentTimeMillis() - begin;
+ if (duration > timeout)
+ break networkAvailable;
+ Enumeration<NetworkInterface> netInterfaces = null;
+ try {
+ netInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (SocketException e) {
+ throw new IllegalStateException("Cannot list network interfaces", e);
+ }
+ if (netInterfaces != null) {
+ while (netInterfaces.hasMoreElements()) {
+ NetworkInterface netInterface = netInterfaces.nextElement();
+ for (InterfaceAddress addr : netInterface.getInterfaceAddresses()) {
+ InetAddress inetAddr = addr.getAddress();
+ if (!inetAddr.isLoopbackAddress() && !inetAddr.isLinkLocalAddress()) {
+ try {
+ if (inetAddr.isReachable((int) timeout)) {
+ networkAvailable = true;
+ System.out.println("Network available after " + duration + " ms. IP: " + inetAddr);
+ break networkAvailable;
+ }
+ } catch (IOException e) {
+ System.err.println(
+ "Cannot check whether " + inetAddr + " is reachable: " + e.getMessage());
+ }
+ }
+ }
+ }
+ } else {
+ throw new IllegalStateException("No network interface has been found");
+ }
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
}
+ return networkAvailable;
}
+
+ static void flushStd() {
+ System.err.flush();
+ System.out.flush();
+ }
+
}