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 @api("lib") @nogc
39 {
40     int sd_notify(int unset_environment, const char* state) { mixin(SSLL_CALL); }
41     int sd_pid_notify(pid_t pid, int unset_environment, const char *state) { mixin(SSLL_CALL); }
42 
43     extern (C) // because variablic arguments
44     {
45         pragma(mangle, "sdutil_dlib_sd_journal_print") // because extern(C)
46         int sd_journal_print(int priority, const char* fmt, ...) { mixin(SSLL_CALL); }
47 
48         pragma(mangle, "sdutil_dlib_sd_journal_send")
49         int sd_journal_send(const char* fmt, ...) { mixin(SSLL_CALL); }
50     }
51 
52     int sd_journal_perror(const char *message) { mixin(SSLL_CALL); }
53 }
54 
55 ///
56 int sdNotify(int unset_environment, string state)
57 { return sd_notify(unset_environment, state.toStringz); }
58 
59 /// shortcut 
60 int sdNotify_ready(int unset_environment=0) @nogc
61 { return sd_notify(unset_environment, "READY=1"); }
62 
63 /// ditto
64 int sdNotify_reloading(int unset_environment=0) @nogc
65 { return sd_notify(unset_environment, "RELOADING=1"); }
66 
67 /// ditto
68 int sdNotify_stopping(int unset_environment=0) @nogc
69 { return sd_notify(unset_environment, "STOPPING=1"); }
70 
71 /// ditto
72 int sdNotify_watchdog(int unset_environment=0) @nogc
73 { return sd_notify(unset_environment, "WATCHDOG=1"); }
74 
75 ///
76 int sdPidNotify(pid_t pid, int unset_environment, string state)
77 { return sd_pid_notify(pid, unset_environment, state.toStringz); }
78 
79 ///
80 int sdNotifyf(Args...)(int unset_environment, string fmt, Args args)
81 { return sd_notify(unset_environment, format(fmt, args)); }
82 
83 ///
84 int sdPidNotifyf(Args...)(pid_t pid, int unset_environment, string fmt, Args args)
85 { return sd_pid_notify(pid, unset_environment, format(fmt, args)); }