Skip to content

Instantly share code, notes, and snippets.

@yrro
Last active August 19, 2024 12:20
Show Gist options
  • Save yrro/18dc22513f1001d0ec8d to your computer and use it in GitHub Desktop.
Save yrro/18dc22513f1001d0ec8d to your computer and use it in GitHub Desktop.
The poor Java programmer's alternative to calling sd_notify
/*
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package systemd;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.function.Supplier;
@lombok.extern.slf4j.Slf4j
public class SdDaemon {
public static void status(String status) {
notify("--status=" + status);
}
public static void ready() {
notify("--ready");
}
public static boolean booted() {
return Files.isDirectory(Paths.get("/run/systemd/system"), LinkOption.NOFOLLOW_LINKS);
}
private static void notify(String arg) {
if (!booted() || System.getenv("NOTIFY_SOCKET") == null)
return;
try {
Process p = new ProcessBuilder("systemd-notify", arg)
.redirectErrorStream(true)
.start();
if (ignoreInterruptedException(p::waitFor) == 0)
return;
log.error("Failed to notify systemd of/that {}; systemd-notify exited with status {}", arg, p.exitValue());
try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
r.lines().forEach(l -> log.error("systemd-notify: {}", l));
}
} catch (Exception e) {
log.warn("Failed to notify systemd of service readiness", e);
}
}
private interface ThrowingSupplier<T, E extends Throwable> {
T get() throws E;
}
private static <T> T ignoreInterruptedException(ThrowingSupplier<T, InterruptedException> r) {
Supplier<Object> s;
for (;;) {
try {
return r.get();
} catch (InterruptedException e) {
continue;
}
}
}
}
@nicobrevin
Copy link

Hiya @yrro - are you happy that this code is public domain and so I can copy pasta like a crazy guy?

@yrro
Copy link
Author

yrro commented Feb 5, 2024

I've just put a license statement at the top - enjoy!

@septatrix
Copy link

This looks great, thanks for the inspiration. I also got an implementation working which does not need to spawn any subprocess. However, as Java (to my knowledge) does not support AF_UNIX Datagram sockets I had to add junixsocket as a dependency: https://gist.github.com/septatrix/dcb1d7763c941be399da5dc97397e6c3

@yrro
Copy link
Author

yrro commented Aug 17, 2024

Nice. FYI, Unix domain sockets have been supported since Java 16.
https://inside.java/2021/02/03/jep380-unix-domain-sockets-channels/

@septatrix
Copy link

Yes I was inspired to write my gist after discovering that, however, as written above, sd_notify uses datagram socket which Java does not yet support AFAIK. Additionally Java also does not support abstract paths or file descriptor passing but those are not as commonly used. My gist should support the former but I haven't looked into the latter

@yrro
Copy link
Author

yrro commented Aug 19, 2024

Oh yeah, no support for datagram sockets. Can't have Java making people's lives too easy can we! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment