summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Lemenkov <lemenkov@gmail.com>2014-06-02 15:01:26 +0400
committerPeter Lemenkov <lemenkov@gmail.com>2014-06-02 16:54:45 +0400
commit142417a957d23e1a4e5d29012e350940089429cb (patch)
tree91e22fecb5670dd01ddf513783b51ccd00b9a560
downloaderlang-sd_notify-142417a957d23e1a4e5d29012e350940089429cb.tar.gz
Initial commit
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
-rw-r--r--.gitignore9
-rw-r--r--Makefile17
-rw-r--r--c_src/sd_notify.c25
-rw-r--r--src/sd_notify.app.src12
-rw-r--r--src/sd_notify.erl44
5 files changed, 107 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..19f4bd6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,9 @@
+*.diff
+*.orig
+*.patch
+*.rej
+*~
+*.o
+*.so
+.eunit/
+ebin/
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..dfeaa69
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+REBAR ?= $(shell which rebar 2>/dev/null || which ./rebar)
+
+REBAR_FLAGS ?=
+
+all: compile
+
+compile:
+ $(REBAR) compile $(REBAR_FLAGS)
+
+test: compile
+ $(REBAR) eunit $(REBAR_FLAGS)
+
+clean:
+ $(REBAR) clean $(REBAR_FLAGS)
+
+dialyzer:
+ $(REBAR) dialyze $(REBAR_FLAGS)
diff --git a/c_src/sd_notify.c b/c_src/sd_notify.c
new file mode 100644
index 0000000..dd667ca
--- /dev/null
+++ b/c_src/sd_notify.c
@@ -0,0 +1,25 @@
+#include "erl_nif.h"
+#include <systemd/sd-daemon.h>
+
+static ERL_NIF_TERM sd_notify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int unset_environment = 0;
+ enif_get_int(env, argv[0], &unset_environment);
+
+ unsigned int length = 0;
+ enif_get_list_length(env, argv[1], &length);
+
+ char* state = (char*)enif_alloc(++length);
+ enif_get_string(env, argv[1], state, length, ERL_NIF_LATIN1);
+ sd_notify(unset_environment, state);
+ enif_free(state);
+
+ return enif_make_atom(env, "ok");
+}
+
+static ErlNifFunc nif_funcs[] =
+{
+ {"sd_notify", 2, sd_notify_nif}
+};
+
+ERL_NIF_INIT(sd_notify, nif_funcs, NULL, NULL, NULL, NULL);
diff --git a/src/sd_notify.app.src b/src/sd_notify.app.src
new file mode 100644
index 0000000..6350ab4
--- /dev/null
+++ b/src/sd_notify.app.src
@@ -0,0 +1,12 @@
+{application, sd_notify,
+ [
+ {description, "Erlang sd_notify NIF"},
+ {vsn, "1"},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib
+ ]},
+ {mod, { sd_notify, []}},
+ {env, []}
+ ]}.
diff --git a/src/sd_notify.erl b/src/sd_notify.erl
new file mode 100644
index 0000000..95754e3
--- /dev/null
+++ b/src/sd_notify.erl
@@ -0,0 +1,44 @@
+-module(sd_notify).
+
+-export([sd_notify/2, sd_notifyf/3]).
+
+-on_load(init/0).
+
+-define(nif_stub, nif_stub_error(?LINE)).
+
+nif_stub_error(Line) ->
+ erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
+init() ->
+ PrivDir = case code:priv_dir(?MODULE) of
+ {error, bad_name} ->
+ EbinDir = filename:dirname(code:which(?MODULE)),
+ AppPath = filename:dirname(EbinDir),
+ filename:join(AppPath, "priv");
+ Path ->
+ Path
+ end,
+ erlang:load_nif(filename:join(PrivDir, ?MODULE) ++ "_drv", 0).
+
+sd_notify(_, _) ->
+ ?nif_stub.
+
+sd_notifyf(UnsetEnv, Format, Data) ->
+ sd_notify(UnsetEnv, lists:flatten(io_lib:format(Format, Data))).
+
+%% ===================================================================
+%% EUnit tests
+%% ===================================================================
+-ifdef(TEST).
+
+sd_notify_test() ->
+ ?assertEqual(ok, ok).
+
+sd_notifyf_test() ->
+ ?assertEqual(ok, ok).
+
+-endif.