Skip to content

Commit 0dc4a7f

Browse files
committed
Linux: make davmail notify readyness to systemd
1 parent 45937af commit 0dc4a7f

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

build.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<available classname="javafx.embed.swing.JFXPanel" classpathref="classpath"/>
3737
</condition>
3838

39+
<condition property="is.jna">
40+
<available classname="com.sun.jna.Native" classpathref="classpath"/>
41+
</condition>
42+
3943
<condition property="is.javafx.message" value="available" else="missing">
4044
<available classname="javafx.embed.swing.JFXPanel" property="is.javafx" classpathref="classpath"/>
4145
</condition>
@@ -117,6 +121,7 @@
117121
<javac srcdir="src/java" destdir="target/classes" source="1.8" target="1.8" debug="on" encoding="UTF-8"
118122
includeantruntime="false">
119123
<exclude name="davmail/exchange/auth/*Interactive*" unless="is.javafx"/>
124+
<exclude name="davmail/util/SystemdNotify.java" unless="is.jna"/>
120125
<classpath>
121126
<path refid="classpath"/>
122127
</classpath>

src/init/davmail.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Documentation=http://davmail.sourceforge.net/sslsetup.html
66
After=network.target
77

88
[Service]
9-
Type=simple
9+
Type=notify
1010
User=davmail
1111
PermissionsStartOnly=true
1212
AmbientCapabilities=CAP_NET_BIND_SERVICE

src/java/davmail/DavGateway.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.log4j.Logger;
3333

3434
import java.awt.*;
35+
import java.io.File;
3536
import java.io.IOException;
3637
import java.lang.reflect.InvocationTargetException;
3738
import java.util.ArrayList;
@@ -183,7 +184,16 @@ public static void start() {
183184
if (showStartupBanner) {
184185
DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING", currentVersion, messages));
185186
}
186-
if (!errorMessages.isEmpty()) {
187+
if (errorMessages.isEmpty()) {
188+
File f = new File("/run/systemd/system");
189+
if (f.isDirectory()) {
190+
try {
191+
Class.forName("davmail.util.SystemdNotify").getMethod("ready").invoke(null);
192+
} catch (Exception e) {
193+
LOGGER.warn("Running on a host with systemd but could not notify readiness.");
194+
}
195+
}
196+
} else {
187197
DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
188198
}
189199

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Based on this answer https://stackoverflow.com/a/36945256/784804
2+
3+
/*
4+
* Copyright (c) 2018 Ian Kirk
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package davmail.util;
25+
26+
import com.sun.jna.Library;
27+
import com.sun.jna.Native;
28+
29+
public final class SystemdNotify {
30+
31+
interface SystemD extends Library {
32+
final SystemD INSTANCE = Native.load(SYSTEMD_SO, SystemD.class);
33+
34+
int sd_notify(int unset_environment, String state);
35+
}
36+
37+
private static final String SYSTEMD_SO = "systemd";
38+
private static final String READY = "READY=1";
39+
private static final String RELOADING = "RELOADING=1";
40+
private static final String STOPPING = "STOPPING=1";
41+
private static final String STATUS = "STATUS=%s";
42+
private static final String WATCHDOG = "WATCHDOG=1";
43+
private static final String MAINPID = "MAINPID=%d";
44+
private static final String ERRNO = "ERRNO=%d";
45+
private static final String BUSERROR = "BUSERROR=%s";
46+
47+
public static void busError(final String error) {
48+
notify(String.format(BUSERROR, error));
49+
}
50+
51+
public static void errno(final int errno) {
52+
notify(String.format(ERRNO, errno));
53+
}
54+
55+
public static void mainPid(final long pid) {
56+
notify(String.format(MAINPID, pid));
57+
}
58+
59+
public static void notify(final String message) {
60+
try {
61+
final int returnCode = SystemD.INSTANCE.sd_notify(0, message);
62+
if (returnCode < 0)
63+
throw new RuntimeException(
64+
String.format("sd_notify returned %d", returnCode));
65+
} catch (UnsatisfiedLinkError e) {
66+
// libsystemd could not be loaded, ignore and do nothing
67+
} catch (final Exception e) {
68+
throw new RuntimeException(e);
69+
}
70+
}
71+
72+
public static void ready() {
73+
notify(READY);
74+
}
75+
76+
public static void reloading() {
77+
notify(RELOADING);
78+
}
79+
80+
public static void status(final String text) {
81+
notify(String.format(STATUS, text));
82+
}
83+
84+
public static void stopping() {
85+
notify(STOPPING);
86+
}
87+
88+
public static void watchdog() {
89+
notify(WATCHDOG);
90+
}
91+
92+
private SystemdNotify() {
93+
throw new RuntimeException("This class should not be instantiated");
94+
}
95+
}

0 commit comments

Comments
 (0)