|
| 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