-
Notifications
You must be signed in to change notification settings - Fork 26
/
pidfile.c
98 lines (82 loc) · 1.69 KB
/
pidfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* write pidfile
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <libisns/util.h>
static void
__update_pidfile(int fd)
{
char pidbuf[32];
snprintf(pidbuf, sizeof(pidbuf), "%u\n", getpid());
if (write(fd, pidbuf, strlen(pidbuf)) < 0)
isns_fatal("Error writing pid file: %m\n");
close(fd);
}
static pid_t
__read_pidfile(const char *filename)
{
char pidbuf[32];
FILE *fp;
pid_t pid = -1;
fp = fopen(filename, "r");
if (fp != NULL) {
if (fgets(pidbuf, sizeof(pidbuf), fp))
pid = strtoul(pidbuf, NULL, 0);
fclose(fp);
}
return pid;
}
void
isns_write_pidfile(const char *filename)
{
int fd;
pid_t pid;
fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0644);
if (fd >= 0) {
__update_pidfile(fd);
return;
}
if (errno != EEXIST)
isns_fatal("Error creating pid file %s: %m\n",
filename);
/* If the pid file is stale, remove it.
* Not really needed in real life, but
* highly convenient for debugging :) */
if ((pid = __read_pidfile(filename)) > 0
&& kill(pid, 0) < 0
&& errno == ESRCH) {
isns_debug_general(
"Removing stale PID file %s\n",
filename);
unlink(filename);
}
/* Try again */
fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0644);
if (fd < 0)
isns_fatal("PID file exists; another daemon "
"seems to be running\n");
__update_pidfile(fd);
}
void
isns_update_pidfile(const char *filename)
{
int fd;
fd = open(filename, O_WRONLY);
if (fd < 0) {
isns_fatal("Error opening pid file %s: %m\n",
filename);
}
__update_pidfile(fd);
}
void
isns_remove_pidfile(const char *filename)
{
unlink(filename);
}