Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -117,6 +121,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>
Expand Down
2 changes: 1 addition & 1 deletion src/init/davmail.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Documentation=http://davmail.sourceforge.net/sslsetup.html
After=network.target

[Service]
Type=simple
Type=notify
User=davmail
PermissionsStartOnly=true
AmbientCapabilities=CAP_NET_BIND_SERVICE
Expand Down
11 changes: 10 additions & 1 deletion src/java/davmail/DavGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ public static void start() {
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));
}

Expand Down
95 changes: 95 additions & 0 deletions src/java/davmail/util/SystemdNotify.java
Original file line number Diff line number Diff line change
@@ -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");
}
}