├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── docker ├── centos_22 │ └── Dockerfile ├── centos_23 │ └── Dockerfile └── ubuntu_23 │ └── Dockerfile ├── packaging ├── centos │ ├── Makefile │ └── erlang-sd_notify.spec └── deb │ ├── Makefile │ └── debian │ ├── changelog │ ├── compat │ ├── control │ ├── erlang-sd-notify.dirs │ ├── rules │ └── source │ └── format ├── rebar ├── rebar.config ├── rebar.lock ├── rebar3 ├── src ├── sd_notify.app.src └── sd_notify.erl └── test └── sd_notify_test.erl /.gitignore: -------------------------------------------------------------------------------- 1 | *.d 2 | *.diff 3 | *.o 4 | *.orig 5 | *.patch 6 | *.rej 7 | *.so 8 | *~ 9 | .DS_Store 10 | .eunit/ 11 | .rebar/ 12 | compile_commands.json 13 | deb-build 14 | ebin/ 15 | packaging/centos/BUILD/ 16 | packaging/centos/RPMS/ 17 | packaging/centos/SOURCES/ 18 | packaging/centos/SPECS/ 19 | packaging/centos/tmp/ 20 | _build/* 21 | 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | services: 4 | - docker 5 | 6 | before_install: 7 | - docker build -t build_centos_23 docker/centos_23/ 8 | - docker build -t build_centos_22 docker/centos_22/ 9 | 10 | script: 11 | - docker run -v $TRAVIS_BUILD_DIR:/home/sd/ build_centos_23 /bin/sh -c "cd /home/sd/; make clean; make all; make test" 12 | - docker run -v $TRAVIS_BUILD_DIR:/home/sd/ build_centos_22 /bin/sh -c "cd /home/sd/; make clean; make all; make test" 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is the MIT license. 2 | 3 | Copyright (c) 2014 Peter Lemenkov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 6 | software and associated documentation files (the "Software"), to deal in the Software 7 | without restriction, including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies 13 | or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 17 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 18 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 19 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 20 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | REBAR3 ?= $(shell which rebar3 2>/dev/null || which ./rebar3) 2 | 3 | REBAR_FLAGS ?= 4 | 5 | all: compile 6 | 7 | compile: 8 | $(REBAR3) compile $(REBAR_FLAGS) 9 | 10 | check: test 11 | test: clean compile 12 | rm -rf _build 13 | $(REBAR3) eunit $(REBAR_FLAGS) 14 | 15 | clean: 16 | $(REBAR3) clean $(REBAR_FLAGS) 17 | 18 | dialyzer: 19 | $(REBAR3) dialyze $(REBAR_FLAGS) 20 | 21 | deb: 22 | cd packaging/deb && $(MAKE) TOPDIR=$(PWD) deb 23 | 24 | rpm: 25 | cd packaging/centos && $(MAKE) TOPDIR=$(PWD) rpm -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Erlang systemd-notify 3 | =============== 4 | Erlang module for native access to the `systemd-notify` facilities. 5 | 6 | Build status: [![Build Status](https://travis-ci.org/systemd/erlang-sd_notify.svg?branch=master)](https://travis-ci.org/systemd/erlang-sd_notify) 7 | 8 | 9 | Installation 10 | ============ 11 | 12 | On Fedora/CentOS 13 | 14 | ```bash 15 | rpm -i erlang-sd_notify-{version}-1.el7.centos.x86_64.rpm 16 | ``` 17 | 18 | On Debian/Ubuntu 19 | 20 | ```bash 21 | dpkg -i erlang-sd-notify_{version}-1_amd64.deb 22 | ``` 23 | 24 | On openSUSE/SLES distributions 25 | 26 | You can install the package using the openSUSE [repositories](https://build.opensuse.org/repositories/network:messaging:amqp/erlang-sd_notify) 27 | 28 | For example for Leap15: 29 | ```bash 30 | sudo zypper addrepo -f https://download.opensuse.org/repositories/network:/messaging:/amqp/openSUSE_Leap_15/network:messaging:amqp.repo 31 | sudo zypper --gpg-auto-import-keys refresh 32 | sudo zypper install erlang-sd_notify 33 | ``` 34 | 35 | 36 | Build from source using Docker 37 | === 38 | 39 | Ubuntu 40 | 41 | ```bash 42 | docker build -t build_ubuntu_{version} docker/ubuntu_{version}/ 43 | docker run -v {sd_notify_dir}:/home/sd/ build_ubuntu_{version} /bin/sh -c "cd /home/sd/; make deb" 44 | ``` 45 | 46 | Centos 47 | 48 | ```bash 49 | docker build -t build_centos_{version} docker/centos_{version}/ 50 | docker run -v {sd_notify_dir}:/home/sd/ build_centos_{version} /bin/sh -c "cd /home/sd/; make rpm" 51 | ``` 52 | 53 | Example 54 | 55 | ```bash 56 | docker build -t build_ubuntu_19 docker/ubuntu_19/ 57 | docker run -v /home/gabriele/erlang-sd_notify:/home/sd/ build_ubuntu_19 /bin/sh -c "cd /home/sd/; make all" 58 | ``` 59 | 60 | Download Binaries 61 | === 62 | [Github Repository](https://github.com/systemd/erlang-sd_notify/releases) 63 | 64 | Usage 65 | ===== 66 | 67 | Quick example: 68 | 69 | ```bash 70 | [root@a499ee66251a]# erl 71 | ... 72 | 1> sd_notify:sd_notify(0,"READY=1"). 73 | 0 74 | ``` 75 | -------------------------------------------------------------------------------- /docker/centos_22/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM centos 2 | 3 | RUN yum -y update 4 | 5 | RUN yum install -y \ 6 | make \ 7 | git \ 8 | wget \ 9 | which \ 10 | epel-release \ 11 | nano 12 | 13 | RUN yum install -y https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/22/el/7/x86_64/erlang-22.3.4.2-1.el7.x86_64.rpm 14 | 15 | RUN wget https://s3.amazonaws.com/rebar3/rebar3 && chmod +x rebar3 && mv rebar3 /usr/local/bin/ 16 | -------------------------------------------------------------------------------- /docker/centos_23/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM centos 2 | 3 | RUN yum -y update 4 | 5 | RUN yum install -y \ 6 | make \ 7 | git \ 8 | wget \ 9 | which \ 10 | epel-release \ 11 | nano \ 12 | rpm-build 13 | 14 | RUN yum install -y https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/23/el/7/x86_64/erlang-23.0.2-1.el7.x86_64.rpm 15 | 16 | RUN wget https://s3.amazonaws.com/rebar3/rebar3 && chmod +x rebar3 && mv rebar3 /usr/local/bin/ 17 | -------------------------------------------------------------------------------- /docker/ubuntu_23/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | 3 | RUN apt-get -y update 4 | 5 | RUN apt-get install -y \ 6 | make \ 7 | git \ 8 | wget \ 9 | build-essential \ 10 | devscripts \ 11 | fakeroot \ 12 | debhelper \ 13 | apt-transport-https 14 | 15 | RUN wget -O - 'https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc' | apt-key add - 16 | 17 | RUN echo "deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-23.x" > /etc/apt/sources.list.d/bintray.rabbitmq.list 18 | 19 | RUN apt-get update -y 20 | RUN apt-get install erlang -y 21 | 22 | RUN wget https://s3.amazonaws.com/rebar3/rebar3 && chmod +x rebar3 && mv rebar3 /usr/local/bin/ -------------------------------------------------------------------------------- /packaging/centos/Makefile: -------------------------------------------------------------------------------- 1 | FINAL_OUTPUT_DIR=FINAL_RPMS 2 | 3 | 4 | 5 | VERSION=1.1 6 | 7 | # Directory names 8 | RPM_BUILD_DIR=rpm-build 9 | PKG_NAME=erlang-sd_notify 10 | RPM_SOURCE_DIR=$(PKG_NAME)-$(VERSION) 11 | 12 | TOP_DIR=$(shell pwd) 13 | 14 | TARBALL_DIR=$(TOPDIR) 15 | SOURCE_TGZ=$(PKG_NAME)-$(VERSION).tar.gz 16 | TARGET_DIR=$(TARGET_TOP)/usr/lib/erlang/lib/sd_notify-$(VERSION) 17 | 18 | 19 | DEFINES=--define '_topdir $(TOP_DIR)' --define '_tmppath $(TOP_DIR)/tmp' --define '_sysconfdir /etc' --define '_localstatedir /var' 20 | 21 | rpm: clean erlang-sd_notify 22 | 23 | prepare: 24 | mkdir -p BUILD SOURCES SPECS SRPMS RPMS tmp dist $(TARBALL_DIR) 25 | tar czf $(TARBALL_DIR)/$(SOURCE_TGZ) -C $(TOPDIR) src/ rebar.config LICENSE 26 | cp $(TOPDIR)/$(SOURCE_TGZ) SOURCES 27 | rm $(TOPDIR)/$(SOURCE_TGZ) 28 | cp erlang-sd_notify.spec SPECS 29 | 30 | erlang-sd_notify: prepare 31 | mkdir -p $(FINAL_OUTPUT_DIR) 32 | rpmbuild -vvv -bb --nodeps SPECS/erlang-sd_notify.spec $(DEFINES) 33 | find RPMS -name "*.rpm" -exec sh -c 'mv {} `echo {} | sed 's#^RPMS\/noarch#$(FINAL_OUTPUT_DIR)#'`' ';' 34 | 35 | clean: 36 | rm -rf BUILDROOT BUILD SOURCES SPECS SRPMS RPMS tmp $(FINAL_OUTPUT_DIR) dist 37 | -------------------------------------------------------------------------------- /packaging/centos/erlang-sd_notify.spec: -------------------------------------------------------------------------------- 1 | %global realname sd_notify 2 | %global upstream systemd 3 | %global upstream_version 1.1 4 | %global debug_package %{nil} 5 | %global output_dir _build/default/lib/sd_notify/ebin 6 | 7 | 8 | 9 | Name: erlang-%{realname} 10 | Version: %{upstream_version} 11 | Release: 1%{?dist} 12 | Summary: Erlang interface to systemd notify subsystem 13 | License: MIT 14 | URL: https://github.com/%{upstream}/%{realname} 15 | VCS: scm:git:https://github.com/%{upstream}/%{realname}.git 16 | Source0: https://github.com/%{upstream}/%{realname}/archive/%{version}/erlang-%{realname}-%{version}.tar.gz 17 | Source1: erlang-sd_notify-rebar.config 18 | BuildRequires: erlang-rebar 19 | 20 | 21 | %description 22 | %{summary}. 23 | 24 | %prep 25 | %setup -c . 26 | 27 | %build 28 | rebar3 compile 29 | 30 | 31 | %install 32 | mkdir -p $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin 33 | install -m 644 -p %{output_dir}/%{realname}.app $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin 34 | install -m 644 -p %{output_dir}/%{realname}.beam $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin 35 | 36 | 37 | %files 38 | %doc LICENSE 39 | %dir %{_libdir}/erlang/lib/%{realname}-%{version}/ 40 | %dir %{_libdir}/erlang/lib/%{realname}-%{version}/ebin/ 41 | %{_libdir}/erlang/lib/%{realname}-%{version}/ebin/%{realname}.app 42 | %{_libdir}/erlang/lib/%{realname}-%{version}/ebin/%{realname}.beam 43 | 44 | 45 | %changelog 46 | * Tue Jul 7 2020 Gabriele Santomaggio - 1.1 47 | - build for 1.1 - Update build for Erlang 23.0 48 | 49 | * Thu Apr 13 2017 Gabriele Santomaggio - 1.0 50 | - build for 1.0 51 | 52 | * Wed Dec 14 2016 Gabriele Santomaggio - 0.14 53 | - build for 0.14 54 | 55 | * Sat Nov 5 2016 Gabriele Santomaggio - 0.13 56 | - build for 0.13 57 | 58 | * Wed Jun 17 2015 Fedora Release Engineering - 0.1-6 59 | - Rebuilt for https://fedoraproject.org/wiki/Fedora_23_Mass_Rebuild 60 | 61 | * Tue Nov 04 2014 Peter Lemenkov - 0.1-5 62 | - Rebuild with Erlang 17.3.3 63 | 64 | * Thu Oct 2 2014 John Eckersberg - 0.1-4 65 | - Explicitly link shared library with libsystemd (#1148604) 66 | 67 | * Thu Aug 28 2014 Peter Lemenkov - 0.1-3 68 | - Rebuild with Erlang 17.2.1 69 | 70 | * Sat Aug 16 2014 Fedora Release Engineering 71 | - Rebuilt for https://fedoraproject.org/wiki/Fedora_21_22_Mass_Rebuild 72 | 73 | * Tue Jun 03 2014 Peter Lemenkov - 0.1-1 74 | - initial build 75 | 76 | 77 | -------------------------------------------------------------------------------- /packaging/deb/Makefile: -------------------------------------------------------------------------------- 1 | VERSION=1.1 2 | 3 | # Directory names 4 | DEB_BUILD_DIR=deb-build 5 | PKG_NAME=erlang-sd-notify 6 | DEB_SOURCE_DIR=$(PKG_NAME)-$(VERSION) 7 | 8 | # 9 | PKG_DEST=$(TOPDIR)/$(DEB_BUILD_DIR)/$(DEB_SOURCE_DIR) 10 | 11 | # Where to build the 'pristine' source files 12 | TARBALL_DIR=$(TOPDIR)/$(DEB_BUILD_DIR) 13 | SOURCE_TGZ=$(PKG_NAME)_$(VERSION).orig.tar.gz 14 | 15 | # Place to install the compiled files to 16 | TARGET_DIR=$(TARGET_TOP)/usr/lib/erlang/lib/sd_notify-$(VERSION) 17 | 18 | deb: 19 | rm -rf $(PKG_DEST) 20 | mkdir -p $(PKG_DEST) 21 | tar -c -C $(TOPDIR) --exclude-from=$(TOPDIR)/.gitignore --exclude=.git . | tar -x -C $(PKG_DEST) 22 | rm $(PKG_DEST)/Makefile 23 | tar -zcf $(TARBALL_DIR)/$(SOURCE_TGZ) -C $(TARBALL_DIR) $(DEB_SOURCE_DIR) 24 | tar -c debian | tar -x -C $(PKG_DEST) 25 | cp $(TOPDIR)/LICENSE $(PKG_DEST)/debian/copyright 26 | sed -i 's/%VERSION%/$(VERSION)/g' $(PKG_DEST)/debian/$(PKG_NAME).dirs 27 | cd $(PKG_DEST) && debuild -d -us -uc 28 | 29 | deb-install: 30 | pwd 31 | install -o root -g root -t $(TARGET_DIR)/ebin ../../ebin/*.app 32 | install -o root -g root -t $(TARGET_DIR)/ebin ../../ebin/*.beam -------------------------------------------------------------------------------- /packaging/deb/debian/changelog: -------------------------------------------------------------------------------- 1 | erlang-sd-notify (1.1-1) UNRELEASED; urgency=medium 2 | 3 | * Build for Erlang 23 4 | 5 | -- Gabriele Santomaggio Wen, 8 Jul 2020 11:54:18 +0000 6 | 7 | -------------------------------------------------------------------------------- /packaging/deb/debian/compat: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /packaging/deb/debian/control: -------------------------------------------------------------------------------- 1 | Source: erlang-sd-notify 2 | Maintainer: 3 | Section: misc 4 | Priority: optional 5 | Standards-Version: 3.9.2 6 | Build-Depends: debhelper (>= 9), 7 | erlang-dev (>=17.3-dfsg-4), 8 | rebar3, 9 | 10 | Package: erlang-sd-notify 11 | Architecture: any 12 | Depends: ${shlibs:Depends}, ${misc:Depends}, 13 | erlang-base (>=17.3) | erlang-base-hipe (>=17.3) 14 | Description: Systemd notification support for Erlang. 15 | -------------------------------------------------------------------------------- /packaging/deb/debian/erlang-sd-notify.dirs: -------------------------------------------------------------------------------- 1 | usr/lib/erlang/lib/sd_notify-%VERSION%/ebin 2 | -------------------------------------------------------------------------------- /packaging/deb/debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | %: 3 | dh $@ 4 | 5 | override_dh_auto_install: 6 | make -C packaging/deb TARGET_TOP=$(PWD)/debian/erlang-sd-notify deb-install 7 | -------------------------------------------------------------------------------- /packaging/deb/debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (quilt) 2 | -------------------------------------------------------------------------------- /rebar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/systemd/erlang-sd_notify/44dae67291d51b9aa3147722e83a7e6cb089a7ca/rebar -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {erl_opts, [debug_info, warnings_as_errors]}. -------------------------------------------------------------------------------- /rebar.lock: -------------------------------------------------------------------------------- 1 | []. 2 | -------------------------------------------------------------------------------- /rebar3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/systemd/erlang-sd_notify/44dae67291d51b9aa3147722e83a7e6cb089a7ca/rebar3 -------------------------------------------------------------------------------- /src/sd_notify.app.src: -------------------------------------------------------------------------------- 1 | {application, sd_notify, 2 | [ 3 | {description, "Erlang sd_notify API"}, 4 | {vsn, "git"}, 5 | {registered, []}, 6 | {applications, [ 7 | kernel, 8 | stdlib 9 | ]}, 10 | {mod, { sd_notify, []}}, 11 | {env, []} 12 | ]}. 13 | -------------------------------------------------------------------------------- /src/sd_notify.erl: -------------------------------------------------------------------------------- 1 | %%%---------------------------------------------------------------------- 2 | %%% This is the MIT license. 3 | %%% 4 | %%% Copyright (c) 2014 Peter Lemenkov 5 | %%% 6 | %%% Permission is hereby granted, free of charge, to any person 7 | %%% obtaining a copy of this software and associated documentation 8 | %%% files (the "Software"), to deal in the Software without 9 | %%% restriction, including without limitation the rights to use, 10 | %%% copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | %%% copies of the Software, and to permit persons to whom the 12 | %%% Software is furnished to do so, subject to the following 13 | %%% conditions: 14 | %%% 15 | %%% The above copyright notice and this permission notice shall be 16 | %%% included in all copies or substantial portions of the Software. 17 | %%% 18 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | %%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | %%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | %%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | %%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | %%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | %%% OTHER DEALINGS IN THE SOFTWARE. 26 | %%%---------------------------------------------------------------------- 27 | 28 | -module(sd_notify). 29 | 30 | -export([sd_notify/2, sd_notifyf/3, sd_pid_notify/3, sd_pid_notifyf/4, sd_pid_notify_with_fds/4]). 31 | 32 | -export([ready/0, reloading/0, stopping/0, watchdog/0]). 33 | -export([start_link/0]). 34 | -export([init/1, handle_info/2, terminate/2]). 35 | 36 | % API helpers 37 | 38 | ready() -> sd_pid_notify_with_fds(0, false, <<"READY=1">>, []). 39 | reloading() -> sd_pid_notify_with_fds(0, false, <<"RELOADING=1">>, []). 40 | stopping() -> sd_pid_notify_with_fds(0, false, <<"STOPPING=1">>, []). 41 | watchdog() -> sd_pid_notify_with_fds(0, false, <<"WATCHDOG=1">>, []). 42 | 43 | % gen_server API and callbacks 44 | 45 | start_link() -> 46 | gen_server:start_link({local,?MODULE}, ?MODULE, [], []). 47 | 48 | init([]) -> 49 | WatchdogMs = case os:getenv( "WATCHDOG_USEC" ) of 50 | false -> none; 51 | Value -> 52 | Part = erlang:round(0.8 * erlang:list_to_integer(Value)), 53 | erlang:convert_time_unit(Part, microsecond, millisecond) 54 | end, 55 | erlang:send_after(WatchdogMs, self(), watchdog), 56 | error_logger:info_msg("watchdog: ~p ms", [WatchdogMs]), 57 | {ok, WatchdogMs}. 58 | 59 | handle_info(watchdog, none) -> 60 | {noreply, none}; 61 | handle_info(watchdog, WatchdogMs) -> 62 | watchdog(), 63 | erlang:send_after(WatchdogMs, self(), watchdog), 64 | {noreply, WatchdogMs}. 65 | 66 | terminate(_,_) -> ok. 67 | 68 | % Systemd API 69 | 70 | sd_notify(UnsetEnv, Data) -> 71 | sd_pid_notify_with_fds(0, UnsetEnv, Data, []). 72 | 73 | sd_pid_notify(Pid, UnsetEnv, Data) -> 74 | sd_pid_notify_with_fds(Pid, UnsetEnv, Data, []). 75 | 76 | sd_notifyf(UnsetEnv, Format, Data) -> 77 | sd_pid_notify_with_fds(0, UnsetEnv, lists:flatten(io_lib:format(Format, Data)), []). 78 | 79 | sd_pid_notifyf(Pid, UnsetEnv, Format, Data) -> 80 | sd_pid_notify_with_fds(Pid, UnsetEnv, lists:flatten(io_lib:format(Format, Data)), []). 81 | 82 | sd_pid_notify_with_fds(_Pid, UnsetEnv, Call, _Fds) -> 83 | error_logger:info_msg("systemd: ~p", [Call]), 84 | case os:getenv("NOTIFY_SOCKET") of 85 | false -> {error, not_configured}; 86 | Path -> 87 | case gen_udp:open(0, [local]) of 88 | {error, SocketError} -> 89 | {error, SocketError}; 90 | {ok, Socket} -> 91 | Result = gen_udp:send(Socket, {local,Path}, 0, Call), 92 | gen_udp:close(Socket), 93 | 94 | UnsetEnv == true andalso os:unsetenv("NOTIFY_SOCKET"), 95 | 96 | Result 97 | end 98 | end. 99 | -------------------------------------------------------------------------------- /test/sd_notify_test.erl: -------------------------------------------------------------------------------- 1 | -module(sd_notify_test). 2 | 3 | -include_lib("eunit/include/eunit.hrl"). 4 | 5 | sd_notify_basic_test_() -> 6 | {ok, CWD} = file:get_cwd(), 7 | FakeNotifyUnixSockName = CWD ++ "/fake-sock-" ++ integer_to_list(erlang:phash2(make_ref())), 8 | {ok, FakeNotifyUnixSock} = gen_udp:open(0, [{ifaddr, {local, FakeNotifyUnixSockName}}, {active, false}, list]), 9 | os:putenv("NOTIFY_SOCKET", FakeNotifyUnixSockName), 10 | 11 | {setup, 12 | fun() -> ok end, 13 | fun(_) -> ok = gen_udp:close(FakeNotifyUnixSock), ok = file:delete(FakeNotifyUnixSockName) end, 14 | [ 15 | { 16 | "Try sending message", 17 | fun() -> 18 | TestMessage = integer_to_list(erlang:phash2(make_ref())), 19 | ok = sd_notify:sd_pid_notify_with_fds(0, 0, TestMessage, []), 20 | {ok, {_Address, _Port, Packet}} = gen_udp:recv(FakeNotifyUnixSock, length(TestMessage), 1000), 21 | ?assertEqual(TestMessage, Packet) 22 | end 23 | } 24 | 25 | ] 26 | 27 | }. 28 | 29 | sd_notify_unsetenv_test_() -> 30 | {ok, CWD} = file:get_cwd(), 31 | FakeNotifyUnixSockName = CWD ++ "/fake-sock-" ++ integer_to_list(erlang:phash2(make_ref())), 32 | {ok, FakeNotifyUnixSock} = gen_udp:open(0, [{ifaddr, {local, FakeNotifyUnixSockName}}, {active, false}, list]), 33 | os:putenv("NOTIFY_SOCKET", FakeNotifyUnixSockName), 34 | 35 | {setup, 36 | fun() -> ok end, 37 | fun(_) -> ok = gen_udp:close(FakeNotifyUnixSock), ok = file:delete(FakeNotifyUnixSockName) end, 38 | [ 39 | { 40 | "Try sending message", 41 | fun() -> 42 | TestMessage = integer_to_list(erlang:phash2(make_ref())), 43 | ok = sd_notify:sd_pid_notify_with_fds(0, true, TestMessage, []), 44 | {ok, {_Address, _Port, _Packet}} = gen_udp:recv(FakeNotifyUnixSock, length(TestMessage), 1000), 45 | Ret = os:getenv("NOTIFY_SOCKET"), 46 | ?assertEqual(Ret, false) 47 | end 48 | } 49 | 50 | ] 51 | 52 | }. 53 | 54 | sd_notify_watchdog_test_() -> 55 | {ok, CWD} = file:get_cwd(), 56 | FakeNotifyUnixSockName = CWD ++ "/fake-sock-" ++ integer_to_list(erlang:phash2(make_ref())), 57 | {ok, FakeNotifyUnixSock} = gen_udp:open(0, [{ifaddr, {local, FakeNotifyUnixSockName}}, {active, false}, list]), 58 | os:putenv("NOTIFY_SOCKET", FakeNotifyUnixSockName), 59 | 60 | {setup, 61 | fun() -> ok end, 62 | fun(_) -> ok = gen_udp:close(FakeNotifyUnixSock), ok = file:delete(FakeNotifyUnixSockName) end, 63 | [ 64 | { 65 | "Try sending message", 66 | fun() -> 67 | os:putenv("WATCHDOG_USEC", "500000"), 68 | sd_notify:start_link(), 69 | {ok, {_Address, _Port, Packet}} = gen_udp:recv(FakeNotifyUnixSock, length("WATCHDOG=1"), 1000), 70 | ?assertEqual("WATCHDOG=1", Packet) 71 | end 72 | } 73 | 74 | ] 75 | 76 | }. 77 | --------------------------------------------------------------------------------