tcataldo commented on code in PR #2405:
URL: https://github.com/apache/cassandra/pull/2405#discussion_r1297439016


##########
src/java/org/apache/cassandra/service/SystemD.java:
##########
@@ -0,0 +1,102 @@
+package org.apache.cassandra.service;
+
+import java.io.File;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.sun.jna.Library; // NOSONAR
+import com.sun.jna.Native; // NOSONAR
+
+public class SystemD {
+
+       private static final Logger logger = 
LoggerFactory.getLogger(SystemD.class);
+       private static final Api INSTANCE = init();
+
+       private static final CLibrary LIBC = (CLibrary) Native.loadLibrary("c", 
CLibrary.class);
+
+       private interface CLibrary extends Library {
+
+               int getpid();
+       }
+
+       public static enum SystemDLocation {
+               Centos("/usr/lib64/libsystemd.so.0"),
+
+               // 16.04, 18.04, stretch
+               OldUbuntuDebian("/lib/x86_64-linux-gnu/libsystemd.so.0"),
+
+               // /lib link to /usr/lib
+               UbuntuDebian("/usr/lib/x86_64-linux-gnu/libsystemd.so.0");
+
+               public final String systemdLibLocation;
+
+               private SystemDLocation(String lib) {
+                       this.systemdLibLocation = lib;
+               }
+       }
+
+       public static class Api {
+
+               private final RawApi impl;
+
+               private Api(RawApi rawApi) {
+                       this.impl = rawApi;
+               }
+
+               public void notifyReady() {
+                       int pid = LIBC.getpid();
+                       logger.info("Notify ready through systemd for PID 
{}...", pid);
+                       int errorCode = impl.sd_pid_notify(pid, 0, "READY=1");
+                       if (errorCode <= 0) {
+                               logger.error("Notify failed: {}", errorCode);
+                       } else {
+                               logger.info("Notified for {}, errorCode: {}", 
pid, errorCode);
+                       }
+               }
+
+       }
+
+       private interface RawApi extends Library {
+
+               int sd_pid_notify(int pid, int unset, String state); // NOSONAR
+
+       }
+
+       private static SystemDLocation figureOutLocation() {
+               for (SystemDLocation loc : SystemDLocation.values()) {
+                       if (new File(loc.systemdLibLocation).exists()) {
+                               logger.info("Selected location {}", loc);
+                               return loc;
+                       }
+               }
+               return null;
+       }
+
+       @VisibleForTesting
+       public static boolean isAvailable() {
+               if (INSTANCE == null) {
+                       return false;
+               }
+               return true;
+       }
+
+       @VisibleForTesting
+       public static Api get() {
+               if (!isAvailable()) {
+                       throw new RuntimeException("SystemD is not available");
+               }
+               return INSTANCE;
+       }
+
+       private static Api init() {
+               SystemDLocation loc = figureOutLocation();
+               if (loc != null) {
+                       RawApi rawApi = (RawApi) 
Native.loadLibrary(loc.systemdLibLocation, RawApi.class);

Review Comment:
   Maybe Native.loadLibrary("systemd") would work & would avoid trying multiple 
locations



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to