Skip to content

Commit 22d3a48

Browse files
committed
Deal with umask when creating semaphores
1 parent bea038d commit 22d3a48

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* Export `umask` to set the calling process's file mode creation mask.
66

7+
* When creating a semaphore, `Semaphore(...)` ignores the calling process's file mode
8+
creation mask for the access permissions while `open(Semaphore, ...)` masks the access
9+
permissions against the process `umask`.
10+
711
* Standard C types are no longer prefixed by `_typeof_`. For example, Julia equivalent of
812
C `mode_t` is given by constant `IPC.mode_t`.
913

src/semaphores.jl

+14-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Semaphore(name, value; perms=0o600, volatile=true) -> sem
2222
creates a new named semaphore identified by the string `name` of the form
2323
`"/somename"` and initial value set to `value`. An instance of
2424
`Semaphore{String}` is returned. Keyword `perms` can be used to specify access
25-
permissions. The permissions settings are masked against the process `umask`.
25+
permissions.
2626
Keyword `volatile` specifies whether the semaphore should be unlinked when the
2727
returned object is finalized.
2828
@@ -52,8 +52,9 @@ open(Semaphore, name, flags, mode, value, volatile) -> sem
5252
where `flags` may have the bits `IPC.O_CREAT` and `IPC.O_EXCL` set, `mode`
5353
specifies the granted access permissions, `value` is the initial semaphore
5454
value and `volatile` is a boolean indicating whether the semaphore should be
55-
unlinked when the returned object `sem` is finalized. The values of `mode` and
56-
`value` are ignored if an existing named semaphore is open.
55+
unlinked when the returned object `sem` is finalized. The values of `mode` and
56+
`value` are ignored if an existing named semaphore is open. The permissions
57+
settings in `mode` are masked against the process `umask`.
5758
5859
5960
## Anonymous Semaphores
@@ -68,7 +69,7 @@ Semaphore(mem, value; offset=0, volatile=true) -> sem
6869
initializes an anonymous semaphore backed by memory object `mem` with initial
6970
value set to `value` and returns an instance of `Semaphore{typeof(mem)}`.
7071
Keyword `offset` can be used to specify the address (in bytes) of the semaphore
71-
data relative to `pointer(mem)`. Keyword `volatile` specify whether the
72+
data relative to `pointer(mem)`. Keyword `volatile` specify whether the
7273
semaphore should be destroyed when the returned object is finalized.
7374
7475
```julia
@@ -81,7 +82,7 @@ position (in bytes) specified by keyword `offset`.
8182
8283
The number of bytes needed to store an anonymous semaphore is given by
8384
`sizeof(Semaphore)` and anonymous semaphore must be aligned in memory at
84-
multiples of the word size (that is `Sys.WORD_SIZE >> 3` in bytes). Memory
85+
multiples of the word size (that is `Sys.WORD_SIZE >> 3` in bytes). Memory
8586
objects used to store an anonymous semaphore must implement two methods:
8687
`pointer(mem)` and `sizeof(mem)` to yield respectively the base address and the
8788
size (in bytes) of the associated memory.
@@ -96,7 +97,12 @@ function Semaphore(name::AbstractString, value::Integer;
9697
val = _check_semaphore_value(value)
9798
mode = maskmode(perms)
9899
flags = O_CREAT | O_EXCL
99-
return open(Semaphore, name, flags, mode, val, volatile)
100+
old_mask = umask(0) # clear file mode creation mask remembering previous settings
101+
try
102+
return open(Semaphore, name, flags, mode, val, volatile)
103+
finally
104+
umask(old_mask) # restore file mode creation mask
105+
end
100106
end
101107

102108
function Semaphore(name::AbstractString)
@@ -112,7 +118,7 @@ function Base.open(::Type{Semaphore}, name::AbstractString, flags::Integer,
112118
systemerror("sem_open", ptr == SEM_FAILED)
113119
sem = Semaphore{String}(ptr, name)
114120
if volatile
115-
finalizer(_close_and_unlink, sem,)
121+
finalizer(_close_and_unlink, sem)
116122
else
117123
finalizer(_close, sem)
118124
end
@@ -125,8 +131,7 @@ function Semaphore(mem::M, value::Integer;
125131
volatile::Bool = true) where {M}
126132
val = _check_semaphore_value(value)
127133
ptr = _get_semaphore_address(mem, offset)
128-
systemerror("sem_init",
129-
_sem_init(ptr, true, val) != SUCCESS)
134+
systemerror("sem_init", _sem_init(ptr, true, val) != SUCCESS)
130135
sem = Semaphore{M}(ptr, mem)
131136
if volatile
132137
finalizer(_destroy, sem)

0 commit comments

Comments
 (0)