forked from gsliepen/tinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidfile.c
38 lines (28 loc) · 663 Bytes
/
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
#include "system.h"
#include "pidfile.h"
#include "names.h"
pidfile_t *read_pidfile(void) {
FILE *f = fopen(pidfilename, "r");
if(!f) {
return NULL;
}
pidfile_t *pf = malloc(sizeof(pidfile_t));
int read = fscanf(f, "%20d %64s %128s port %128s", &pf->pid, pf->cookie, pf->host, pf->port);
fclose(f);
if(read != 4) {
free(pf);
pf = NULL;
}
return pf;
}
bool write_pidfile(const char *controlcookie, const char *address) {
const mode_t mask = umask(0);
umask(mask | 077);
FILE *f = fopen(pidfilename, "w");
if(!f) {
return false;
}
umask(mask);
fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, address);
return !fclose(f);
}