Use a non-blocking lock on the PID file
This commit is contained in:
parent
620735523f
commit
ea96ef48cb
8
daemon.c
8
daemon.c
@ -247,11 +247,13 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
int pidFile = -1;
|
int pidFile = -1;
|
||||||
if (pidPath) {
|
if (pidPath) {
|
||||||
pidFile = open(
|
pidFile = open(pidPath, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
|
||||||
pidPath, O_WRONLY | O_CREAT | O_EXLOCK | O_CLOEXEC, 0600
|
|
||||||
);
|
|
||||||
if (pidFile < 0) err(EX_CANTCREAT, "%s", pidPath);
|
if (pidFile < 0) err(EX_CANTCREAT, "%s", pidPath);
|
||||||
|
|
||||||
|
error = flock(pidFile, LOCK_EX | LOCK_NB);
|
||||||
|
if (error && errno != EWOULDBLOCK) err(EX_IOERR, "%s", pidPath);
|
||||||
|
if (error) errx(EX_CANTCREAT, "%s: file is locked", pidPath);
|
||||||
|
|
||||||
error = ftruncate(pidFile, 0);
|
error = ftruncate(pidFile, 0);
|
||||||
if (error) err(EX_IOERR, "%s", pidPath);
|
if (error) err(EX_IOERR, "%s", pidPath);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user