Skip to content

Commit ce55edd

Browse files
authored
Merge pull request #14 from lemenkov/sd_pid_notify_with_fds
sd_pid_notify_with_fds
2 parents a1b6e24 + 39416b8 commit ce55edd

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

c_src/sd_notify.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,33 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
#include "erl_nif.h"
2525
#include <systemd/sd-daemon.h>
2626

27-
static ERL_NIF_TERM sd_notify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
27+
static ERL_NIF_TERM sd_pid_notify_with_fds_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
2828
{
29-
int unset_environment = 0;
30-
enif_get_int(env, argv[0], &unset_environment);
31-
29+
ERL_NIF_TERM head, tail;
3230
unsigned int length = 0;
33-
enif_get_list_length(env, argv[1], &length);
3431

35-
char* state = (char*)enif_alloc(++length);
36-
enif_get_string(env, argv[1], state, length, ERL_NIF_LATIN1);
37-
int result = sd_notify(unset_environment, state);
38-
enif_free(state);
39-
40-
return enif_make_int(env, result);
41-
}
42-
43-
44-
static ERL_NIF_TERM sd_pid_notify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
45-
{
46-
int pid = 0;
32+
pid_t pid = 0;
4733
enif_get_int(env, argv[0], &pid);
4834

4935
int unset_environment = 0;
5036
enif_get_int(env, argv[1], &unset_environment);
5137

52-
unsigned int length = 0;
5338
enif_get_list_length(env, argv[2], &length);
54-
55-
5639
char* state = (char*)enif_alloc(++length);
57-
enif_get_string(env, argv[1], state, length, ERL_NIF_LATIN1);
58-
int result = sd_pid_notify(pid, unset_environment, state);
40+
enif_get_string(env, argv[2], state, length, ERL_NIF_LATIN1);
41+
42+
enif_get_list_length(env, argv[3], &length);
43+
int* fds = (int*)enif_alloc(++length * sizeof(int));
44+
ERL_NIF_TERM list = argv[3];
45+
int i = 0;
46+
while(enif_get_list_cell(env, list, &head, &tail)) {
47+
enif_get_int(env, head, &fds[i]);
48+
i++;
49+
list = tail;
50+
}
51+
int result = sd_pid_notify_with_fds(pid, unset_environment, state, fds, length);
52+
53+
enif_free(fds);
5954
enif_free(state);
6055

6156
return enif_make_int(env, result);
@@ -64,9 +59,8 @@ static ERL_NIF_TERM sd_pid_notify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TE
6459

6560
static ErlNifFunc nif_funcs[] =
6661
{
67-
{"sd_notify", 2, sd_notify_nif},
68-
{"sd_pid_notify", 3, sd_pid_notify_nif},
62+
{"sd_pid_notify_with_fds", 4, sd_pid_notify_with_fds_nif},
6963

7064
};
7165

72-
ERL_NIF_INIT(sd_notify, nif_funcs, NULL, NULL, NULL, NULL);
66+
ERL_NIF_INIT(sd_notify, nif_funcs, NULL, NULL, NULL, NULL);

src/sd_notify.erl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
-module(sd_notify).
2929

30-
-export([sd_notify/2, sd_notifyf/3, sd_pid_notify/3, sd_pid_notifyf/4]).
30+
-export([sd_notify/2, sd_notifyf/3, sd_pid_notify/3, sd_pid_notifyf/4, sd_pid_notify_with_fds/4]).
3131

3232
-on_load(init/0).
3333

@@ -51,18 +51,20 @@ init() ->
5151
end,
5252
erlang:load_nif(filename:join(PrivDir, ?MODULE) ++ "_drv", 0).
5353

54-
sd_notify(_, _) ->
55-
?nif_stub.
54+
sd_notify(UnsetEnv, Data) ->
55+
sd_pid_notify_with_fds(0, UnsetEnv, Data, []).
5656

57-
sd_pid_notify(_, _, _) ->
58-
?nif_stub.
57+
sd_pid_notify(Pid, UnsetEnv, Data) ->
58+
sd_pid_notify_with_fds(Pid, UnsetEnv, Data, []).
5959

6060
sd_notifyf(UnsetEnv, Format, Data) ->
61-
sd_notify(UnsetEnv, lists:flatten(io_lib:format(Format, Data))).
62-
61+
sd_pid_notify_with_fds(0, UnsetEnv, lists:flatten(io_lib:format(Format, Data)), []).
6362

6463
sd_pid_notifyf(Pid, UnsetEnv, Format, Data) ->
65-
sd_pid_notify(Pid, UnsetEnv, lists:flatten(io_lib:format(Format, Data))).
64+
sd_pid_notify_with_fds(Pid, UnsetEnv, lists:flatten(io_lib:format(Format, Data)), []).
65+
66+
sd_pid_notify_with_fds(_, _, _, _) ->
67+
?nif_stub.
6668

6769
%% ===================================================================
6870
%% EUnit tests

0 commit comments

Comments
 (0)