1 module sdnotify;
2 
3 import ssll;
4 
5 import std.string : toStringz, format;
6 
7 private enum libNames = [
8     "libsystemd.so",
9     "libsystemd.so.0"
10 ];
11 
12 private __gshared void* lib;
13 
14 alias pid_t = size_t;
15 
16 ///
17 void initSystemDLib()
18 {
19     if (lib !is null) return;
20 
21     foreach (name; libNames)
22     {
23         lib = loadLibrary(name);
24         if (lib !is null) break;
25     }
26 
27     if (lib is null)
28         assert(0, "can't load systemd lib");
29 
30     loadApiSymbols();
31 }
32 
33 ///
34 void cleanupSystemDLib() { unloadLibrary(lib); }
35 
36 mixin SSLL_INIT;
37 
38 public import core.sys.posix.sys.uio : iovec;
39 
40 @api("lib") @nogc
41 {
42     int sd_notify(int unset_environment, const char* state) { mixin(SSLL_CALL); }
43     int sd_pid_notify(pid_t pid, int unset_environment, const char *state) { mixin(SSLL_CALL); }
44 
45     extern (C) // because variablic arguments
46     {
47         pragma(mangle, "sdutil_dlib_sd_journal_print") // because extern(C)
48         int sd_journal_print(int priority, const char* fmt, ...) { mixin(SSLL_CALL); }
49 
50         pragma(mangle, "sdutil_dlib_sd_journal_send")
51         int sd_journal_send(const char* fmt, ...) { mixin(SSLL_CALL); }
52 
53         pragma(mangle, "sdutil_dlib_sd_journal_sendv")
54         int sd_journal_sendv(const iovec* buf, int count) { mixin(SSLL_CALL); }
55     }
56 
57     int sd_journal_perror(const char *message) { mixin(SSLL_CALL); }
58 }
59 
60 ///
61 int sdNotify(int unset_environment, string state)
62 { return sd_notify(unset_environment, state.toStringz); }
63 
64 /// shortcut 
65 int sdNotify_ready(int unset_environment=0) @nogc
66 { return sd_notify(unset_environment, "READY=1"); }
67 
68 /// ditto
69 int sdNotify_reloading(int unset_environment=0) @nogc
70 { return sd_notify(unset_environment, "RELOADING=1"); }
71 
72 /// ditto
73 int sdNotify_stopping(int unset_environment=0) @nogc
74 { return sd_notify(unset_environment, "STOPPING=1"); }
75 
76 /// ditto
77 int sdNotify_watchdog(int unset_environment=0) @nogc
78 { return sd_notify(unset_environment, "WATCHDOG=1"); }
79 
80 ///
81 int sdPidNotify(pid_t pid, int unset_environment, string state)
82 { return sd_pid_notify(pid, unset_environment, state.toStringz); }
83 
84 ///
85 int sdNotifyf(Args...)(int unset_environment, string fmt, Args args)
86 { return sd_notify(unset_environment, format(fmt, args)); }
87 
88 ///
89 int sdPidNotifyf(Args...)(pid_t pid, int unset_environment, string fmt, Args args)
90 { return sd_pid_notify(pid, unset_environment, format(fmt, args)); }