shm_open not working under systemd
TL;DR: shm_open
, which opens shared memory, will fail with fd != -1
if it does not have permission to read/write to /dev/shm/<name>
.
I had a binary which worked when run manually, but failed when run as a systemd service with:
The failing assertion happened here:
Looking at the man page for shm_open
, I found that this function typically creates shared memory on the filesystem under /dev/shm
.
Looking at /dev/shm
quickly illuminated the problem. To test, I had manually run the command as the user vagrant
, which had created the shared memory owned by vagrant
:
This was preventing me from subsequently running the command as a systemd service, which runs as the user somethingd
.
To fix this, I deleted the shared memory:
Then I was able to run the systemd service without getting an assertion.
Checking the shared memory again now shows it is owned by somethingd
: