forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libtcmu_log.c
381 lines (308 loc) · 8.04 KB
/
libtcmu_log.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright 2016, China Mobile, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libtcmu_log.h"
#include "libtcmu_config.h"
/* tcmu ring buffer for log */
#define LOG_ENTRY_LEN 256 /* rb[0] is reserved for pri */
#define LOG_MSG_LEN (LOG_ENTRY_LEN - 1) /* the length of the log message */
#define LOG_ENTRYS (1024 * 32)
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
struct log_buf {
pthread_cond_t cond;
pthread_mutex_t lock;
bool thread_active;
int init_state;
bool finish_initialize;
unsigned int head;
unsigned int tail;
char buf[LOG_ENTRYS][LOG_ENTRY_LEN];
pthread_t thread_id;
};
static int tcmu_log_level = TCMU_LOG_WARN;
static struct log_buf *tcmu_log_initialize(void);
static struct log_buf *logbuf = NULL;
static int initialized = false;
/* covert log level from tcmu config to syslog */
static inline int to_syslog_level(int level)
{
switch (level) {
case TCMU_CONF_LOG_ERROR:
return TCMU_LOG_ERROR;
case TCMU_CONF_LOG_WARN:
return TCMU_LOG_WARN;
case TCMU_CONF_LOG_INFO:
return TCMU_LOG_INFO;
case TCMU_CONF_LOG_DEBUG:
return TCMU_LOG_DEBUG;
case TCMU_CONF_LOG_DEBUG_SCSI_CMD:
return TCMU_LOG_DEBUG_SCSI_CMD;
default:
return TCMU_LOG_WARN;
}
}
/* get the log level of tcmu-runner */
unsigned int tcmu_get_log_level(void)
{
return tcmu_log_level;
}
void tcmu_set_log_level(int level)
{
tcmu_log_level = to_syslog_level(level);
}
static void open_syslog(const char *ident, int option, int facility)
{
#define ID_MAX_LEN 16
char id[ID_MAX_LEN + 1] = {0}, path[128];
int fd, len = -1;
if (!ident) {
sprintf(path, "/proc/%d/comm", getpid());
fd = open(path, O_RDONLY);
if (fd < 0)
return;
len = read(fd, id, ID_MAX_LEN);
if (len < 0) {
close(fd);
return;
}
close(fd);
} else {
strncpy(id, ident, ID_MAX_LEN);
}
openlog(id, option, facility);
}
static void close_syslog(void)
{
closelog();
}
static inline void log_to_syslog(int pri, const char *logbuf)
{
syslog(pri, "%s", logbuf);
}
static inline uint8_t rb_get_pri(struct log_buf *logbuf, unsigned int cur)
{
return logbuf->buf[cur][0];
}
static inline void rb_set_pri(struct log_buf *logbuf, unsigned int cur, uint8_t pri)
{
logbuf->buf[cur][0] = (char)pri;
}
static inline char *rb_get_msg(struct log_buf *logbuf, unsigned int cur)
{
return logbuf->buf[cur] + 1;
}
static inline bool rb_is_empty(struct log_buf *logbuf)
{
return logbuf->tail == logbuf->head;
}
static inline bool rb_is_full(struct log_buf *logbuf)
{
return logbuf->tail == (logbuf->head + 1) % LOG_ENTRYS;
}
static inline void rb_update_tail(struct log_buf *logbuf)
{
logbuf->tail = (logbuf->tail + 1) % LOG_ENTRYS;
}
static inline void rb_update_head(struct log_buf *logbuf)
{
/* when the ring buffer is full, the oldest log will be dropped */
if (rb_is_full(logbuf))
rb_update_tail(logbuf);
logbuf->head = (logbuf->head + 1) % LOG_ENTRYS;
}
static void
log_internal(int pri,const char *funcname,
int linenr,const char *fmt,
va_list args)
{
unsigned int head;
char *msg;
int n;
if (pri > tcmu_log_level)
return;
/* convert tcmu-runner private level to system level */
if (pri > TCMU_LOG_DEBUG)
pri = TCMU_LOG_DEBUG;
if (!fmt)
return;
if (!initialized && !(logbuf = tcmu_log_initialize()))
return;
pthread_mutex_lock(&logbuf->lock);
head = logbuf->head;
rb_set_pri(logbuf, head, pri);
msg = rb_get_msg(logbuf, head);
n = sprintf(msg, "%s:%d : ", funcname, linenr);
vsnprintf(msg + n, LOG_MSG_LEN - n, fmt, args);
rb_update_head(logbuf);
if (logbuf->thread_active == false)
pthread_cond_signal(&logbuf->cond);
pthread_mutex_unlock(&logbuf->lock);
}
void tcmu_err_message(const char *funcname, int linenr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal(TCMU_LOG_ERROR, funcname, linenr, fmt, args);
va_end(args);
}
void tcmu_warn_message(const char *funcname, int linenr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal(TCMU_LOG_WARN, funcname, linenr, fmt, args);
va_end(args);
}
void tcmu_info_message(const char *funcname, int linenr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal(TCMU_LOG_INFO, funcname, linenr, fmt, args);
va_end(args);
}
void tcmu_dbg_message(const char *funcname, int linenr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal(TCMU_LOG_DEBUG, funcname, linenr, fmt, args);
va_end(args);
}
void tcmu_dbg_scsi_cmd_message(const char *funcname, int linenr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal(TCMU_LOG_DEBUG_SCSI_CMD, funcname, linenr, fmt, args);
va_end(args);
}
static void log_output(int pri, const char *msg)
{
log_to_syslog(pri, msg);
/* log to stdout if tcmu_log_level is DEBUG */
if (pri >= TCMU_LOG_DEBUG)
fprintf(stdout, "%s", msg);
}
static bool log_buf_not_empty_output(struct log_buf *logbuf)
{
unsigned int tail;
uint8_t pri;
char *msg, buf[LOG_MSG_LEN];
if (!logbuf) {
return false;
}
pthread_mutex_lock(&logbuf->lock);
if (rb_is_empty(logbuf)) {
pthread_mutex_unlock(&logbuf->lock);
return false;
}
tail = logbuf->tail;
pri = rb_get_pri(logbuf, tail);
msg = rb_get_msg(logbuf, tail);
memcpy(buf, msg, LOG_MSG_LEN);
rb_update_tail(logbuf);
pthread_mutex_unlock(&logbuf->lock);
/*
* This may block due to rsyslog and syslog-ng, etc.
* And the log productors could still insert their log
* messages into the ring buffer without blocking. But
* the ring buffer may lose some old log rbs if the
* ring buffer is full.
*/
log_output(pri, buf);
return true;
}
static void cancel_log_thread(pthread_t thread)
{
void *join_retval;
int ret;
ret = pthread_cancel(thread);
if (ret) {
return;
}
pthread_join(thread, &join_retval);
}
void tcmu_cancel_log_thread(void)
{
cancel_log_thread(logbuf->thread_id);
}
static void log_thread_cleanup(void *arg)
{
struct log_buf *logbuf = arg;
pthread_cond_destroy(&logbuf->cond);
pthread_mutex_destroy(&logbuf->lock);
free(logbuf);
close_syslog();
}
static void *log_thread_start(void *arg)
{
struct log_buf *logbuf = arg;
pthread_cleanup_push(log_thread_cleanup, arg);
open_syslog(NULL, 0, 0);
pthread_mutex_lock(&logbuf->lock);
if(!logbuf->finish_initialize){
logbuf->finish_initialize = true;
pthread_cond_signal(&logbuf->cond);
}
pthread_mutex_unlock(&logbuf->lock);
while (1) {
pthread_mutex_lock(&logbuf->lock);
logbuf->thread_active = false;
pthread_cond_wait(&logbuf->cond, &logbuf->lock);
logbuf->thread_active = true;
pthread_mutex_unlock(&logbuf->lock);
while (log_buf_not_empty_output(logbuf));
}
pthread_cleanup_pop(1);
return NULL;
}
static struct log_buf *tcmu_log_initialize(void)
{
int ret;
pthread_mutex_lock(&g_mutex);
if (initialized && logbuf != NULL) {
pthread_mutex_unlock(&g_mutex);
return logbuf;
}
logbuf = malloc(sizeof(struct log_buf));
if (!logbuf) {
pthread_mutex_unlock(&g_mutex);
return NULL;
}
logbuf->thread_active = false;
logbuf->finish_initialize = false;
logbuf->head = 0;
logbuf->tail = 0;
pthread_cond_init(&logbuf->cond, NULL);
pthread_mutex_init(&logbuf->lock, NULL);
ret = pthread_create(&logbuf->thread_id, NULL, log_thread_start, logbuf);
if (ret) {
free(logbuf);
pthread_mutex_unlock(&g_mutex);
return NULL;
}
pthread_mutex_lock(&logbuf->lock);
while (!logbuf->finish_initialize)
pthread_cond_wait(&logbuf->cond, &logbuf->lock);
pthread_mutex_unlock(&logbuf->lock);
initialized = true;
pthread_mutex_unlock(&g_mutex);
return logbuf;
}