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
|
/* Copyright (c) 2003-2004, Roger Dingledine
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2021, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file pidfile.c
* \brief Record this process's PID to disk.
**/
#include "orconfig.h"
#include "lib/process/pidfile.h"
#include "lib/log/log.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
/** Write the current process ID, followed by NL, into <b>filename</b>.
* Return 0 on success, -1 on failure.
*/
int
write_pidfile(const char *filename)
{
FILE *pidfile;
if ((pidfile = fopen(filename, "w")) == NULL) {
log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
strerror(errno));
return -1;
} else {
#ifdef _WIN32
int pid = (int)_getpid();
#else
int pid = (int)getpid();
#endif
int rv = 0;
if (fprintf(pidfile, "%d\n", pid) < 0)
rv = -1;
if (fclose(pidfile) < 0)
rv = -1;
return rv;
}
}
|