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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "Journald.h"
#include <endian.h>
#include <fcntl.h>
#include <iterator>
#include <memory>
#include <string>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <syslog.h>
#include <unistd.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include "include/ceph_assert.h"
#include "common/LogEntry.h"
#include "log/Entry.h"
#include "log/SubsystemMap.h"
#include "msg/msg_fmt.h"
namespace ceph::logging {
namespace {
const struct sockaddr_un sockaddr = {
AF_UNIX,
"/run/systemd/journal/socket",
};
ssize_t sendmsg_fd(int transport_fd, int fd)
{
constexpr size_t control_len = CMSG_LEN(sizeof(int));
char control[control_len];
struct msghdr mh = {
(struct sockaddr*)&sockaddr, // msg_name
sizeof(sockaddr), // msg_namelen
nullptr, // msg_iov
0, // msg_iovlen
&control, // msg_control
control_len, // msg_controllen
};
ceph_assert(transport_fd >= 0);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
*reinterpret_cast<int *>(CMSG_DATA(cmsg)) = fd;
return sendmsg(transport_fd, &mh, MSG_NOSIGNAL);
}
char map_prio(short ceph_prio)
{
if (ceph_prio < 0)
return LOG_ERR;
if (ceph_prio == 0)
return LOG_WARNING;
if (ceph_prio < 5)
return LOG_NOTICE;
if (ceph_prio < 10)
return LOG_INFO;
return LOG_DEBUG;
}
}
namespace detail {
class EntryEncoderBase {
public:
EntryEncoderBase():
m_msg_vec {
{}, {}, {}, { (char *)"\n", 1 },
}
{
std::string id = program_invocation_short_name;
for (auto& c : id) {
if (c == '\n')
c = '_';
}
static_segment = "SYSLOG_IDENTIFIER=" + id + "\n";
m_msg_vec[0].iov_base = static_segment.data();
m_msg_vec[0].iov_len = static_segment.size();
}
constexpr struct iovec *iovec() { return this->m_msg_vec; }
constexpr std::size_t iovec_len()
{
return sizeof(m_msg_vec) / sizeof(m_msg_vec[0]);
}
private:
struct iovec m_msg_vec[4];
std::string static_segment;
protected:
fmt::memory_buffer meta_buf;
struct iovec &meta_vec() { return m_msg_vec[1]; }
struct iovec &msg_vec() { return m_msg_vec[2]; }
};
class EntryEncoder : public EntryEncoderBase {
public:
void encode(const Entry& e, const SubsystemMap *s)
{
meta_buf.clear();
fmt::format_to(std::back_inserter(meta_buf),
R"(PRIORITY={:d}
CEPH_SUBSYS={}
TIMESTAMP={}
CEPH_PRIO={}
THREAD={:016x}
MESSAGE
)",
map_prio(e.m_prio),
s->get_name(e.m_subsys),
e.m_stamp.time_since_epoch().count().count,
e.m_prio,
e.m_thread);
uint64_t msg_len = htole64(e.size());
meta_buf.resize(meta_buf.size() + sizeof(msg_len));
*(reinterpret_cast<uint64_t*>(meta_buf.end()) - 1) = htole64(e.size());
meta_vec().iov_base = meta_buf.data();
meta_vec().iov_len = meta_buf.size();
msg_vec().iov_base = (void *)e.strv().data();
msg_vec().iov_len = e.size();
}
};
class LogEntryEncoder : public EntryEncoderBase {
public:
void encode(const LogEntry& le)
{
meta_buf.clear();
fmt::format_to(std::back_inserter(meta_buf),
R"(PRIORITY={:d}
TIMESTAMP={}
CEPH_NAME={}
CEPH_RANK={}
CEPH_SEQ={}
CEPH_CHANNEL={}
MESSAGE
)",
clog_type_to_syslog_level(le.prio),
le.stamp.to_nsec(),
le.name.to_str(),
le.rank,
le.seq,
le.channel);
uint64_t msg_len = htole64(le.msg.size());
meta_buf.resize(meta_buf.size() + sizeof(msg_len));
*(reinterpret_cast<uint64_t*>(meta_buf.end()) - 1) = htole64(le.msg.size());
meta_vec().iov_base = meta_buf.data();
meta_vec().iov_len = meta_buf.size();
msg_vec().iov_base = (void *)le.msg.data();
msg_vec().iov_len = le.msg.size();
}
};
enum class JournaldClient::MemFileMode {
MEMFD_CREATE,
OPEN_TMPFILE,
OPEN_UNLINK,
};
constexpr const char *mem_file_dir = "/dev/shm";
void JournaldClient::detect_mem_file_mode()
{
int memfd = memfd_create("ceph-journald", MFD_ALLOW_SEALING | MFD_CLOEXEC);
if (memfd >= 0) {
mem_file_mode = MemFileMode::MEMFD_CREATE;
close(memfd);
return;
}
memfd = open(mem_file_dir, O_TMPFILE | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
if (memfd >= 0) {
mem_file_mode = MemFileMode::OPEN_TMPFILE;
close(memfd);
return;
}
mem_file_mode = MemFileMode::OPEN_UNLINK;
}
int JournaldClient::open_mem_file()
{
switch (mem_file_mode) {
case MemFileMode::MEMFD_CREATE:
return memfd_create("ceph-journald", MFD_ALLOW_SEALING | MFD_CLOEXEC);
case MemFileMode::OPEN_TMPFILE:
return open(mem_file_dir, O_TMPFILE | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
case MemFileMode::OPEN_UNLINK:
char mem_file_template[] = "/dev/shm/ceph-journald-XXXXXX";
int fd = mkostemp(mem_file_template, O_CLOEXEC);
unlink(mem_file_template);
return fd;
}
ceph_abort("Unexpected mem_file_mode");
}
JournaldClient::JournaldClient() :
m_msghdr({
(struct sockaddr*)&sockaddr, // msg_name
sizeof(sockaddr), // msg_namelen
})
{
fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
ceph_assertf(fd > 0, "socket creation failed: %s", strerror(errno));
int sendbuf = 2 * 1024 * 1024;
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof(sendbuf));
detect_mem_file_mode();
}
JournaldClient::~JournaldClient()
{
close(fd);
}
int JournaldClient::send()
{
int ret = sendmsg(fd, &m_msghdr, MSG_NOSIGNAL);
if (ret >= 0)
return 0;
/* Fail silently if the journal is not available */
if (errno == ENOENT)
return -1;
if (errno != EMSGSIZE && errno != ENOBUFS) {
std::cerr << "Failed to send log to journald: " << strerror(errno) << std::endl;
return -1;
}
/* Message doesn't fit... Let's dump the data in a memfd and
* just pass a file descriptor of it to the other side.
*/
int buffer_fd = open_mem_file();
if (buffer_fd < 0) {
std::cerr << "Failed to open buffer_fd while sending log to journald: " << strerror(errno) << std::endl;
return -1;
}
ret = writev(buffer_fd, m_msghdr.msg_iov, m_msghdr.msg_iovlen);
if (ret < 0) {
std::cerr << "Failed to write to buffer_fd while sending log to journald: " << strerror(errno) << std::endl;
goto err_close_buffer_fd;
}
if (mem_file_mode == MemFileMode::MEMFD_CREATE) {
ret = fcntl(buffer_fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
if (ret) {
std::cerr << "Failed to seal buffer_fd while sending log to journald: " << strerror(errno) << std::endl;
goto err_close_buffer_fd;
}
}
ret = sendmsg_fd(fd, buffer_fd);
if (ret < 0) {
/* Fail silently if the journal is not available */
if (errno == ENOENT)
goto err_close_buffer_fd;
std::cerr << "Failed to send fd while sending log to journald: " << strerror(errno) << std::endl;
goto err_close_buffer_fd;
}
close(buffer_fd);
return 0;
err_close_buffer_fd:
close(buffer_fd);
return -1;
}
} // namespace ceph::logging::detail
JournaldLogger::JournaldLogger(const SubsystemMap *s) :
m_entry_encoder(std::make_unique<detail::EntryEncoder>()),
m_subs(s)
{
client.m_msghdr.msg_iov = m_entry_encoder->iovec();
client.m_msghdr.msg_iovlen = m_entry_encoder->iovec_len();
}
JournaldLogger::~JournaldLogger() = default;
int JournaldLogger::log_entry(const Entry& e)
{
m_entry_encoder->encode(e, m_subs);
return client.send();
}
JournaldClusterLogger::JournaldClusterLogger() :
m_log_entry_encoder(std::make_unique<detail::LogEntryEncoder>())
{
client.m_msghdr.msg_iov = m_log_entry_encoder->iovec();
client.m_msghdr.msg_iovlen = m_log_entry_encoder->iovec_len();
}
JournaldClusterLogger::~JournaldClusterLogger() = default;
int JournaldClusterLogger::log_log_entry(const LogEntry &le)
{
m_log_entry_encoder->encode(le);
return client.send();
}
}
|