73 lines
1.9 KiB
C
73 lines
1.9 KiB
C
#define _GNU_SOURCE
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include "lock.h"
|
|
#include "logr.h"
|
|
|
|
void
|
|
lock_pidfile (const char *pidfile_name)
|
|
{
|
|
/* Acquire a file descriptor for the lock file.
|
|
*/
|
|
logg(3, "creating pid file `%s'...\n", pidfile_name);
|
|
int pidfile_fd = open(pidfile_name, O_RDWR | O_CREAT | O_FSYNC, 0640);
|
|
if ( pidfile_fd < 0 )
|
|
{
|
|
logg_exit("failed to create pid file: %s)", strerror(errno));
|
|
}
|
|
|
|
/* Attempt to lock the file.
|
|
*/
|
|
struct flock pidfile_lock = {
|
|
.l_type = F_WRLCK,
|
|
.l_whence = SEEK_END,
|
|
.l_start = 0,
|
|
.l_len = 0,
|
|
.l_pid = 0
|
|
};
|
|
logg(3, "locking pid file...\n");
|
|
if (-1 == fcntl(pidfile_fd, F_SETLK, &pidfile_lock))
|
|
{
|
|
logg_exit("failed to lock pid file: %s\n", strerror(errno));
|
|
}
|
|
logg(3, "lock acquired.\n");
|
|
|
|
/* Convert the process id into a string, preparatory to writing it
|
|
* into the pid file.
|
|
*/
|
|
char *pid_str;
|
|
int pid_str_size = asprintf(&pid_str, "%d\n", getpid());
|
|
if (pid_str_size < 0)
|
|
{
|
|
logg_exit("failed to allocate pid string: %s\n", strerror(errno));
|
|
}
|
|
logg(3, "pid_str is `%s' and its size is %d.\n", pid_str, pid_str_size);
|
|
|
|
/* GNU's libc info sternly states "always call `write' in a loop".
|
|
* Even for a few bytes?
|
|
*/
|
|
ssize_t bytes_left = pid_str_size;
|
|
ssize_t written = 0;
|
|
while (bytes_left > 0)
|
|
{
|
|
written = write(pidfile_fd, pid_str, bytes_left);
|
|
if (-1 == written)
|
|
{
|
|
logg_exit("failed to write to pid file: %s\n", strerror(errno));
|
|
}
|
|
bytes_left -= written;
|
|
}
|
|
free(pid_str);
|
|
logg(2, "acquired pid file `%s'.\n", pidfile_name);
|
|
|
|
return;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
/*---------------------------------------------------------------------------*/
|