Skip to content

Commit 3eca5f4

Browse files
committed
Linux: make davmail notify readyness to systemd
1 parent ff76ebb commit 3eca5f4

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-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
@@ -28,6 +28,7 @@
2828
import davmail.pop.PopServer;
2929
import davmail.smtp.SmtpServer;
3030
import davmail.ui.tray.DavGatewayTray;
31+
import davmail.util.SystemdNotify;
3132
import org.apache.log4j.Logger;
3233

3334
import java.awt.*;
@@ -158,7 +159,9 @@ public static void start() {
158159
if (showStartupBanner) {
159160
DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING", currentVersion, messages));
160161
}
161-
if (!errorMessages.isEmpty()) {
162+
if (errorMessages.isEmpty()) {
163+
SystemdNotify.ready();
164+
} else {
162165
DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
163166
}
164167

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
private static String osType;
48+
49+
public static void busError(final String error) {
50+
notify(String.format(BUSERROR, error));
51+
}
52+
53+
public static void errno(final int errno) {
54+
notify(String.format(ERRNO, errno));
55+
}
56+
57+
public static void mainPid(final long pid) {
58+
notify(String.format(MAINPID, pid));
59+
}
60+
61+
public static void notify(final String message) {
62+
63+
if (!osType().startsWith("linux"))
64+
return;
65+
66+
try {
67+
final int returnCode = SystemD.INSTANCE.sd_notify(0, message);
68+
if (returnCode < 0)
69+
throw new RuntimeException(
70+
String.format("sd_notify returned %d", returnCode));
71+
} catch (final Exception e) {
72+
throw new RuntimeException(e);
73+
}
74+
}
75+
76+
public static void ready() {
77+
notify(READY);
78+
}
79+
80+
public static void reloading() {
81+
notify(RELOADING);
82+
}
83+
84+
public static void status(final String text) {
85+
notify(String.format(STATUS, text));
86+
}
87+
88+
public static void stopping() {
89+
notify(STOPPING);
90+
}
91+
92+
public static void watchdog() {
93+
notify(WATCHDOG);
94+
}
95+
96+
synchronized static private String osType() {
97+
if (osType == null) {
98+
osType = System.getProperty("os.name").toLowerCase();
99+
}
100+
return osType;
101+
}
102+
103+
private SystemdNotify() {
104+
throw new RuntimeException("This class should not be instantiated");
105+
}
106+
}

0 commit comments

Comments
 (0)