Skip to content

Commit

Permalink
Support setting socket permissions through :mode param (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewildgoose committed Oct 11, 2021
1 parent 0e5ece7 commit a693393
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/beam_notify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ defmodule BEAMNotify do
* `:dispatcher` - a function to call when a notification comes in
* `:path` - the path to use for the named socket. A path in the system
temporary directory is the default.
* `:mode` - the permissions to apply to the socket. Should be an octal number
eg: 0o660 for read/write owner/group, no access to everyone else
* `:report_env` - set to `true` to report environment variables in addition
to commandline argument. Defaults to `false`
* `:recbuf` - receive buffer size. If you're sending a particular large
Expand All @@ -33,6 +35,7 @@ defmodule BEAMNotify do
@type options() :: [
name: binary() | atom(),
path: Path.t(),
mode: non_neg_integer(),
dispatcher: dispatcher(),
report_env: boolean(),
recbuf: non_neg_integer()
Expand Down Expand Up @@ -95,6 +98,7 @@ defmodule BEAMNotify do
socket_path = socket_path(options)
dispatcher = Keyword.get(options, :dispatcher, &null_dispatcher/2)
recbuf = Keyword.get(options, :recbuf, 8192)
mode = Keyword.get(options, :mode)

# Blindly try to remove an old file just in case it exists from a previous run
_ = File.rm(socket_path)
Expand All @@ -109,6 +113,8 @@ defmodule BEAMNotify do
{:recbuf, recbuf}
])

if mode, do: File.chmod(socket_path, mode)

state = %{
socket_path: socket_path,
socket: socket,
Expand Down
8 changes: 8 additions & 0 deletions test/beam_notify_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ defmodule BEAMNotifyTest do
assert_receive {["hello", "explicit", "path"], %{}}
end

test "explicit socket permissions" do
# Unusual mode
mode = 0o711
pid = start_supervised!({BEAMNotify, path: "test_socket", mode: mode})

assert mode == Bitwise.band(File.stat!("test_socket").mode(), 0o777)
end

test "sending a message via a script", context do
pid = start_supervised!(beam_notify_child_spec(context))

Expand Down

0 comments on commit a693393

Please sign in to comment.