Use actual port in tincd logs / tinc get Port / invitations
[tinc] / src / pidfile.c
1 #include "system.h"
2
3 #include "pidfile.h"
4 #include "names.h"
5
6 pidfile_t *read_pidfile(void) {
7         FILE *f = fopen(pidfilename, "r");
8
9         if(!f) {
10                 return NULL;
11         }
12
13         pidfile_t *pf = malloc(sizeof(pidfile_t));
14         int read = fscanf(f, "%20d %64s %128s port %128s", &pf->pid, pf->cookie, pf->host, pf->port);
15         fclose(f);
16
17         if(read != 4) {
18                 free(pf);
19                 pf = NULL;
20         }
21
22         return pf;
23 }
24
25
26 bool write_pidfile(const char *controlcookie, const char *address) {
27         const mode_t mask = umask(0);
28         umask(mask | 077);
29         FILE *f = fopen(pidfilename, "w");
30
31         if(!f) {
32                 return false;
33         }
34
35         umask(mask);
36         fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, address);
37         return !fclose(f);
38 }