Skip to content

Commit 39416b8

Browse files
committed
Implement sd_pid_notify_with_fds
Signed-off-by: Peter Lemenkov <lemenkov@redhat.com>
1 parent 9551319 commit 39416b8

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

c_src/sd_notify.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +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_pid_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 pid = 0;
29+
ERL_NIF_TERM head, tail;
30+
unsigned int length = 0;
31+
32+
pid_t pid = 0;
3033
enif_get_int(env, argv[0], &pid);
3134

3235
int unset_environment = 0;
3336
enif_get_int(env, argv[1], &unset_environment);
3437

35-
unsigned int length = 0;
3638
enif_get_list_length(env, argv[2], &length);
37-
3839
char* state = (char*)enif_alloc(++length);
3940
enif_get_string(env, argv[2], state, length, ERL_NIF_LATIN1);
40-
int result = sd_pid_notify(pid, unset_environment, state);
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);
4154
enif_free(state);
4255

4356
return enif_make_int(env, result);
@@ -46,7 +59,7 @@ static ERL_NIF_TERM sd_pid_notify_nif(ErlNifEnv* env, int argc, const ERL_NIF_TE
4659

4760
static ErlNifFunc nif_funcs[] =
4861
{
49-
{"sd_pid_notify", 3, sd_pid_notify_nif},
62+
{"sd_pid_notify_with_fds", 4, sd_pid_notify_with_fds_nif},
5063

5164
};
5265

src/sd_notify.erl

Lines changed: 9 additions & 7 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

@@ -52,17 +52,19 @@ init() ->
5252
erlang:load_nif(filename:join(PrivDir, ?MODULE) ++ "_drv", 0).
5353

5454
sd_notify(UnsetEnv, Data) ->
55-
sd_pid_notify(0, 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)