-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathUtil.cc
149 lines (122 loc) · 2.81 KB
/
Util.cc
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string>
#include <time.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <univalue.h>
#include <stdio.h>
#include "Util.h"
using namespace std;
std::string addressToStr(const struct sockaddr *sockaddr, socklen_t socklen)
{
char name[1025] = "";
if (!getnameinfo(sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
return std::string(name);
return string("");
}
std::string formatTime(const std::string& fmt, time_t t)
{
// convert unix seconds-since-epoch to split-out struct
struct tm tm;
gmtime_r(&t, &tm);
// build formatted time string
char timeStr[fmt.size() + 256];
strftime(timeStr, sizeof(timeStr), fmt.c_str(), &tm);
return string(timeStr);
}
std::string isoTimeStr(time_t t)
{
return formatTime("%FT%TZ", t);
}
bool readJsonFile(const std::string& filename, UniValue& jval)
{
jval.clear();
FILE *f = fopen(filename.c_str(), "r");
if (!f)
return false;
string jdata;
char buf[4096];
while (!feof(f)) {
int bread = fread(buf, 1, sizeof(buf), f);
if (ferror(f)) {
fclose(f);
return false;
}
string s(buf, bread);
jdata += s;
}
if (ferror(f)) {
fclose(f);
return false;
}
fclose(f);
if (!jval.read(jdata))
return false;
return true;
}
int write_pid_file(const std::string& pidFn)
{
char str[32], *s;
size_t bytes;
int fd;
struct flock lock;
int err;
const char *pid_fn = pidFn.c_str();
/* build file data */
sprintf(str, "%u\n", (unsigned int) getpid());
/* open non-exclusively (works on NFS v2) */
fd = open(pid_fn, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
err = errno;
syslog(LOG_DAEMON|LOG_ERR, "Cannot open PID file %s: %s",
pid_fn, strerror(err));
return -err;
}
/* lock */
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLK, &lock) != 0) {
err = errno;
if (err == EAGAIN) {
syslog(LOG_DAEMON|LOG_ERR, "PID file %s is already locked",
pid_fn);
} else {
syslog(LOG_DAEMON|LOG_ERR, "Cannot lock PID file %s: %s",
pid_fn, strerror(err));
}
close(fd);
return -err;
}
/* write file data */
bytes = strlen(str);
s = str;
while (bytes > 0) {
ssize_t rc = write(fd, s, bytes);
if (rc < 0) {
err = errno;
syslog(LOG_DAEMON|LOG_ERR, "PID number write failed: %s",
strerror(err));
goto err_out;
}
bytes -= rc;
s += rc;
}
/* make sure file data is written to disk */
if (fsync(fd) < 0) {
err = errno;
syslog(LOG_DAEMON|LOG_ERR, "PID file fsync failed: %s", strerror(err));
goto err_out;
}
return fd;
err_out:
unlink(pid_fn);
close(fd);
return -err;
}