Skip to content

Commit 80b8b50

Browse files
committed
Linux: make davmail notify readyness to systemd
1 parent 4c1e126 commit 80b8b50

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import davmail.pop.PopServer;
3030
import davmail.smtp.SmtpServer;
3131
import davmail.ui.tray.DavGatewayTray;
32+
import davmail.util.SystemdNotify;
3233
import org.apache.log4j.Logger;
3334

3435
import java.awt.*;
@@ -183,7 +184,9 @@ 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+
SystemdNotify.ready();
189+
} else {
187190
DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
188191
}
189192

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
33+
final SystemD INSTANCE = Native.load(SYSTEMD_SO, SystemD.class);
34+
35+
int sd_notify(int unset_environment, String state);
36+
}
37+
38+
private static final String SYSTEMD_SO = "systemd";
39+
private static final String READY = "READY=1";
40+
private static final String RELOADING = "RELOADING=1";
41+
private static final String STOPPING = "STOPPING=1";
42+
private static final String STATUS = "STATUS=%s";
43+
private static final String WATCHDOG = "WATCHDOG=1";
44+
private static final String MAINPID = "MAINPID=%d";
45+
private static final String ERRNO = "ERRNO=%d";
46+
private static final String BUSERROR = "BUSERROR=%s";
47+
48+
public static void busError(final String error) {
49+
notify(String.format(BUSERROR, error));
50+
}
51+
52+
public static void errno(final int errno) {
53+
notify(String.format(ERRNO, errno));
54+
}
55+
56+
public static void mainPid(final long pid) {
57+
notify(String.format(MAINPID, pid));
58+
}
59+
60+
public static void notify(final String message) {
61+
try {
62+
final int returnCode = SystemD.INSTANCE.sd_notify(0, message);
63+
if (returnCode < 0)
64+
throw new RuntimeException(
65+
String.format("sd_notify returned %d", returnCode));
66+
} catch (UnsatisfiedLinkError e) {
67+
// libsystemd could not be loaded, ignore and do nothing
68+
} catch (final Exception e) {
69+
throw new RuntimeException(e);
70+
}
71+
}
72+
73+
public static void ready() {
74+
notify(READY);
75+
}
76+
77+
public static void reloading() {
78+
notify(RELOADING);
79+
}
80+
81+
public static void status(final String text) {
82+
notify(String.format(STATUS, text));
83+
}
84+
85+
public static void stopping() {
86+
notify(STOPPING);
87+
}
88+
89+
public static void watchdog() {
90+
notify(WATCHDOG);
91+
}
92+
93+
private SystemdNotify() {
94+
throw new RuntimeException("This class should not be instantiated");
95+
}
96+
}

0 commit comments

Comments
 (0)