1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
Author: Alexandre Rossi <alexandre.rossi@gmail.com>
Date: Mon Dec 13 09:45:35 2021 +0100
Subject: Linux: make davmail notify readyness to systemd
Forwarded: https://github.com/mguessan/davmail/pull/188
--- davmail.git.orig/build.xml 2025-10-26 09:41:09.604359716 +0100
+++ davmail.git/build.xml 2025-10-26 09:42:27.009831260 +0100
@@ -36,6 +36,10 @@
<available classname="javafx.embed.swing.JFXPanel" classpathref="classpath"/>
</condition>
+ <condition property="is.jna">
+ <available classname="com.sun.jna.Native" classpathref="classpath"/>
+ </condition>
+
<condition property="is.javafx.message" value="available" else="missing">
<available classname="javafx.embed.swing.JFXPanel" property="is.javafx" classpathref="classpath"/>
</condition>
@@ -111,6 +115,7 @@
<echo level="info" message="Compile on debian with openjfx"/>
<javac srcdir="src/java" destdir="target/classes" encoding="UTF-8"
includeantruntime="false">
+ <exclude name="davmail/util/SystemdNotify.java" unless="is.jna"/>
<compilerarg value="--add-exports"/>
<compilerarg value="java.naming/com.sun.jndi.ldap=ALL-UNNAMED"/>
<compilerarg value="--add-exports"/>
@@ -130,6 +135,7 @@
<javac srcdir="src/java" destdir="target/classes" encoding="UTF-8"
includeantruntime="false">
<exclude name="davmail/exchange/auth/*Interactive*" unless="is.javafx"/>
+ <exclude name="davmail/util/SystemdNotify.java" unless="is.jna"/>
<compilerarg value="--add-exports"/>
<compilerarg value="java.naming/com.sun.jndi.ldap=ALL-UNNAMED"/>
<compilerarg value="--add-exports"/>
@@ -146,6 +152,7 @@
<javac srcdir="src/java" destdir="target/classes" source="1.8" target="1.8" debug="on" encoding="UTF-8"
includeantruntime="false">
<exclude name="davmail/exchange/auth/*Interactive*" unless="is.javafx"/>
+ <exclude name="davmail/util/SystemdNotify.java" unless="is.jna"/>
<classpath>
<path refid="classpath"/>
</classpath>
--- davmail.git.orig/src/init/davmail.service 2025-10-26 09:41:09.604359716 +0100
+++ davmail.git/src/init/davmail.service 2025-10-26 09:41:09.600359640 +0100
@@ -6,7 +6,7 @@
After=network.target
[Service]
-Type=simple
+Type=notify
User=davmail
PermissionsStartOnly=true
AmbientCapabilities=CAP_NET_BIND_SERVICE
--- davmail.git.orig/src/java/davmail/DavGateway.java 2025-10-26 09:41:09.604359716 +0100
+++ davmail.git/src/java/davmail/DavGateway.java 2025-10-26 09:41:09.600359640 +0100
@@ -187,7 +187,16 @@
if (showStartupBanner) {
DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING", currentVersion, messages));
}
- if (!errorMessages.isEmpty()) {
+ if (errorMessages.isEmpty()) {
+ if (System.getenv("INVOCATION_ID") != null) {
+ // we are run by systemd
+ try {
+ Class.forName("davmail.util.SystemdNotify").getMethod("ready").invoke(null);
+ } catch (Exception e) {
+ LOGGER.warn("Running on a host with systemd but could not notify readiness.");
+ }
+ }
+ } else {
DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
}
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ davmail.git/src/java/davmail/util/SystemdNotify.java 2025-10-26 09:41:09.600359640 +0100
@@ -0,0 +1,95 @@
+// Based on this answer https://stackoverflow.com/a/36945256/784804
+
+/*
+ * Copyright (c) 2018 Ian Kirk
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package davmail.util;
+
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+
+public final class SystemdNotify {
+
+ interface SystemD extends Library {
+ final SystemD INSTANCE = Native.load(SYSTEMD_SO, SystemD.class);
+
+ int sd_notify(int unset_environment, String state);
+ }
+
+ private static final String SYSTEMD_SO = "systemd";
+ private static final String READY = "READY=1";
+ private static final String RELOADING = "RELOADING=1";
+ private static final String STOPPING = "STOPPING=1";
+ private static final String STATUS = "STATUS=%s";
+ private static final String WATCHDOG = "WATCHDOG=1";
+ private static final String MAINPID = "MAINPID=%d";
+ private static final String ERRNO = "ERRNO=%d";
+ private static final String BUSERROR = "BUSERROR=%s";
+
+ public static void busError(final String error) {
+ notify(String.format(BUSERROR, error));
+ }
+
+ public static void errno(final int errno) {
+ notify(String.format(ERRNO, errno));
+ }
+
+ public static void mainPid(final long pid) {
+ notify(String.format(MAINPID, pid));
+ }
+
+ public static void notify(final String message) {
+ try {
+ final int returnCode = SystemD.INSTANCE.sd_notify(0, message);
+ if (returnCode < 0)
+ throw new RuntimeException(
+ String.format("sd_notify returned %d", returnCode));
+ } catch (UnsatisfiedLinkError e) {
+ // libsystemd could not be loaded, ignore and do nothing
+ } catch (final Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void ready() {
+ notify(READY);
+ }
+
+ public static void reloading() {
+ notify(RELOADING);
+ }
+
+ public static void status(final String text) {
+ notify(String.format(STATUS, text));
+ }
+
+ public static void stopping() {
+ notify(STOPPING);
+ }
+
+ public static void watchdog() {
+ notify(WATCHDOG);
+ }
+
+ private SystemdNotify() {
+ throw new RuntimeException("This class should not be instantiated");
+ }
+}
|