From f6c6ea30d2c8b66dd9ba534622bd87763c2c5679 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Fri, 8 Dec 2023 17:21:54 +0100 Subject: [PATCH] Improve FREEd PID1 --- etc/freed/init/jvm.args | 10 --- etc/freed/{init => pid1}/config.ini | 6 +- etc/freed/pid1/jvm.args | 16 +++++ etc/freed/{init => pid1}/system.properties | 0 local.mk | 26 ++++++++ {usr/jbin => sjbin}/.classpath | 0 {usr/jbin => sjbin}/.gitignore | 2 +- {usr/jbin => sjbin}/.project | 2 +- {usr/jbin => sjbin}/META-INF/MANIFEST.MF | 0 {usr/jbin => sjbin}/Makefile | 8 ++- {usr/jbin => sjbin}/build.properties | 0 .../src/freed-pid1.java | 62 ++++++++++++++++++- 12 files changed, 111 insertions(+), 21 deletions(-) delete mode 100644 etc/freed/init/jvm.args rename etc/freed/{init => pid1}/config.ini (88%) create mode 100644 etc/freed/pid1/jvm.args rename etc/freed/{init => pid1}/system.properties (100%) create mode 100644 local.mk rename {usr/jbin => sjbin}/.classpath (100%) rename {usr/jbin => sjbin}/.gitignore (68%) rename {usr/jbin => sjbin}/.project (95%) rename {usr/jbin => sjbin}/META-INF/MANIFEST.MF (100%) rename {usr/jbin => sjbin}/Makefile (55%) rename {usr/jbin => sjbin}/build.properties (100%) rename usr/jbin/src/freed-init.java => sjbin/src/freed-pid1.java (51%) diff --git a/etc/freed/init/jvm.args b/etc/freed/init/jvm.args deleted file mode 100644 index 6e7d6a6..0000000 --- a/etc/freed/init/jvm.args +++ /dev/null @@ -1,10 +0,0 @@ --Dosgi.configuration.cascaded=true --Dosgi.sharedConfiguration.area=/etc/freed/init --Dosgi.sharedConfiguration.area.readOnly=true --Dosgi.configuration.area=/var/lib/freed/init/state/ --Dosgi.instance.area=/var/lib/freed/init/data/ - --Dorg.osgi.framework.system.packages.extra=sun.security.internal.spec,sun.security.provider,com.sun.net.httpserver,com.sun.jndi.ldap,com.sun.jndi.ldap.sasl,com.sun.jndi.dns,com.sun.security.jgss,com.sun.nio.file,com.sun.nio.sctp --Dorg.eclipse.equinox.http.jetty.autostart=false - --cp /usr/local/share/a2/osgi/equinox/org.argeo.tp.osgi/org.eclipse.osgi.3.18.jar:/usr/local/share/a2/org.argeo.cms/org.argeo.init.2.3.jar \ No newline at end of file diff --git a/etc/freed/init/config.ini b/etc/freed/pid1/config.ini similarity index 88% rename from etc/freed/init/config.ini rename to etc/freed/pid1/config.ini index bdb2a9a..ecc9502 100644 --- a/etc/freed/init/config.ini +++ b/etc/freed/pid1/config.ini @@ -1,5 +1,5 @@ osgi.clean=true -osgi.console=true +#osgi.console=true argeo.osgi.start.2=\ org.eclipse.equinox.http.servlet,\ @@ -18,8 +18,8 @@ org.argeo.cms.lib.equinox,\ org.argeo.cms.lib.jetty,\ org.argeo.cms.jshell,\ -argeo.http.port=80 -argeo.sshd.port=22 +argeo.http.port=8080 +#argeo.sshd.port=22 argeo.osgi.sources=\ a2:///?\ diff --git a/etc/freed/pid1/jvm.args b/etc/freed/pid1/jvm.args new file mode 100644 index 0000000..2a6402a --- /dev/null +++ b/etc/freed/pid1/jvm.args @@ -0,0 +1,16 @@ +-Dosgi.configuration.cascaded=true +-Dosgi.sharedConfiguration.area=/usr/local/etc/freed/pid1 +-Dosgi.sharedConfiguration.area.readOnly=true +-Dosgi.configuration.area=/var/lib/freed/pid1/state/ +-Dosgi.instance.area=/var/lib/freed/pid1/data/ + +-Dorg.osgi.framework.system.packages.extra=sun.security.utils,sun.security.internal.spec,sun.security.provider,com.sun.net.httpserver,com.sun.jndi.ldap,com.sun.jndi.ldap.sasl,com.sun.jndi.dns,com.sun.security.jgss,com.sun.nio.file,com.sun.nio.sctp +-Dorg.eclipse.equinox.http.jetty.autostart=false + +-Dfile.encoding=UTF-8 +-Dnative.encoding=UTF-8 +-Dsun.stdout.encoding=UTF-8 +-Dsun.stderr.encoding=UTF-8 +-Dsun.jnu.encoding=UTF-8 + +-cp /usr/local/share/a2/osgi/equinox/org.argeo.tp.osgi/org.eclipse.osgi.3.18.jar:/usr/local/share/a2/org.argeo.cms/org.argeo.init.2.3.jar \ No newline at end of file diff --git a/etc/freed/init/system.properties b/etc/freed/pid1/system.properties similarity index 100% rename from etc/freed/init/system.properties rename to etc/freed/pid1/system.properties diff --git a/local.mk b/local.mk new file mode 100644 index 0000000..531f8ab --- /dev/null +++ b/local.mk @@ -0,0 +1,26 @@ +# GNU coding standards +prefix ?= /usr/local +exec_prefix ?= $(prefix) +bindir ?= $(exec_prefix)/bin +sbindir ?= $(exec_prefix)/sbin +libdir ?= $(exec_prefix)/lib +libexecdir ?= $(exec_prefix)/libexec +sysconfdir = $(prefix)/etc +srcdir ?= src +datarootdir ?= $(prefix)/share + +COPY=cp --reflink=auto + +all: + $(MAKE) -C sjbin all + +clean: + $(MAKE) -C sjbin clean + +install: + $(COPY) -r etc/* $(DESTDIR)$(sysconfdir) + $(COPY) -r usr/bin/* $(DESTDIR)$(bindir) + $(COPY) -r usr/share/* $(DESTDIR)$(datarootdir) + $(COPY) -r usr/lib/* $(DESTDIR)$(libdir) + $(COPY) -r usr/libexec/* $(DESTDIR)$(libexec) + $(MAKE) -C sjbin install diff --git a/usr/jbin/.classpath b/sjbin/.classpath similarity index 100% rename from usr/jbin/.classpath rename to sjbin/.classpath diff --git a/usr/jbin/.gitignore b/sjbin/.gitignore similarity index 68% rename from usr/jbin/.gitignore rename to sjbin/.gitignore index 8c068ec..01c3b51 100644 --- a/usr/jbin/.gitignore +++ b/sjbin/.gitignore @@ -1,4 +1,4 @@ /bin/ /src/* !/src/*.java -/freed-init +/freed-pid1 diff --git a/usr/jbin/.project b/sjbin/.project similarity index 95% rename from usr/jbin/.project rename to sjbin/.project index dfb0678..295adf7 100644 --- a/usr/jbin/.project +++ b/sjbin/.project @@ -1,6 +1,6 @@ - freed-jbin + freed-sjbin diff --git a/usr/jbin/META-INF/MANIFEST.MF b/sjbin/META-INF/MANIFEST.MF similarity index 100% rename from usr/jbin/META-INF/MANIFEST.MF rename to sjbin/META-INF/MANIFEST.MF diff --git a/usr/jbin/Makefile b/sjbin/Makefile similarity index 55% rename from usr/jbin/Makefile rename to sjbin/Makefile index 94f73a1..959f751 100644 --- a/usr/jbin/Makefile +++ b/sjbin/Makefile @@ -6,7 +6,6 @@ sysconfdir = $(prefix)/etc srcdir ?= src EXECUTABLES=$(notdir $(patsubst %.java,%,$(wildcard $(srcdir)/*.java))) -#EXECUTABLES=$(FILE:src/%.java=%) all: $(EXECUTABLES) @@ -19,5 +18,8 @@ clean: $(RM) $(EXECUTABLES) install: - cp -v --preserve=mode freed-init $(DESTDIR)$(sbindir) - cp -v ../../etc/freed/init/* $(DESTDIR)$(sysconfdir)/freed/init +# TODO generalise + cp -v --preserve=mode freed-pid1 $(DESTDIR)$(sbindir) + sed -i 's|/usr/local|$(prefix)|' $(DESTDIR)$(sbindir)/freed-pid1 + sed -i 's|/usr/local|$(prefix)|' $(DESTDIR)$(sysconfdir)/freed/pid1/jvm.args +# cp -v ../../etc/freed/init/* $(DESTDIR)$(sysconfdir)/freed/init diff --git a/usr/jbin/build.properties b/sjbin/build.properties similarity index 100% rename from usr/jbin/build.properties rename to sjbin/build.properties diff --git a/usr/jbin/src/freed-init.java b/sjbin/src/freed-pid1.java similarity index 51% rename from usr/jbin/src/freed-init.java rename to sjbin/src/freed-pid1.java index f9e9cb8..0239cb7 100644 --- a/usr/jbin/src/freed-init.java +++ b/sjbin/src/freed-pid1.java @@ -1,6 +1,10 @@ -//#! /usr/bin/java --source 17 @/etc/freed/init/jvm.args +//#! /usr/bin/java --source 17 @/usr/local/etc/freed/pid1/jvm.args +import java.io.IOException; import java.lang.management.ManagementFactory; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.TreeMap; import org.argeo.init.Service; @@ -8,14 +12,14 @@ import org.argeo.init.Service; import jdk.jshell.tool.JavaShellToolBuilder; import sun.misc.Signal; -class FreedInit { +class FreedPid1 { public static void main(String... args) { 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())); @@ -54,7 +58,59 @@ class FreedInit { } } else { + // init Linux services + initSysctl(); + startInitDService("networking"); + startInitDService("nslcd"); + + waitForNetwork(); + + // init Argeo CMS Service.main(args); } } + + static void initSysctl() { + try { + Path sysctlD = Paths.get("/etc/sysctl.d/"); + for (Path conf : Files.newDirectoryStream(sysctlD, "*.conf")) { + try { + new ProcessBuilder("/usr/sbin/sysctl", "-p", conf.toString()).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + static void startInitDService(String serviceName) { + Path serviceInit = Paths.get("/etc/init.d/", serviceName); + if (Files.exists(serviceInit)) + try { + new ProcessBuilder(serviceInit.toString(), "start").start().waitFor(); + System.out.println("Service " + serviceName + " started"); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + new ProcessBuilder(serviceInit.toString(), "stop").start().waitFor(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + }, "Stop service " + serviceName)); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + else + 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(); + } + } } -- 2.30.2