PacktPublishing / Linux-System-Programming-Techniques

Linux System Programming Techniques, published by Packt
MIT License
96 stars 44 forks source link

ch10/my-daemon.c wrong order of umask #5

Open larsbergerproductions opened 5 months ago

larsbergerproductions commented 5 months ago

Description

In the my-daemon programs (here, I explicitly refer to ch10/my-daemon-v2.c), the syscall to umask(022) should either be used before creating the pidfile, or the permissions of the pidfile need to be set explicitly for the control client to work as described in the book.

Explanation

This is not the case if the root user uses a umask of e.g. 027 per default (via environment or similar), where the pidfile ends up with permissions 640 (rw-r-----), making it impossible for users other than root to use the control client for querying the status of the daemon.

Steps to reproduce:

$ sudo -i
# umask 027
# ./my-daemon-v2
# exit
$ my-daemon
fopen pidfile: Permission denied
$ ls -ld /var/run/my-daemon.pid
-rw-r----- 1 root root 6 Mar 26 08:19 /run/my-daemon.pid