Skip to content

Commit 34e9adc

Browse files
author
Xiubo Li
committed
logger: Add logger helper support
Add logger helper support for TCMU, and it just sent log message to the system logger for now. The nonblock logger helper will support in the next series of patches later. Signed-off-by: Xiubo Li <[email protected]>
1 parent 938f6bd commit 34e9adc

19 files changed

+403
-309
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_library(tcmu
4040
libtcmu.c
4141
libtcmu-register.c
4242
tcmuhandler-generated.c
43+
libtcmu_log.c
4344
)
4445
set_target_properties(tcmu
4546
PROPERTIES
@@ -62,6 +63,7 @@ add_library(tcmu_static
6263
libtcmu.c
6364
libtcmu-register.c
6465
tcmuhandler-generated.c
66+
libtcmu_log.c
6567
)
6668
target_include_directories(tcmu_static
6769
PUBLIC ${LIBNL_INCLUDE_DIR}

api.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <endian.h>
3131
#include <errno.h>
3232

33+
#include "libtcmu_log.h"
3334
#include "libtcmu_common.h"
3435
#include "libtcmu_priv.h"
3536

@@ -55,20 +56,20 @@ int tcmu_get_attribute(struct tcmu_device *dev, const char *name)
5556

5657
fd = open(path, O_RDONLY);
5758
if (fd == -1) {
58-
tcmu_errp(dev->ctx, "Could not open configfs to read attribute %s\n", name);
59+
tcmu_err("Could not open configfs to read attribute %s\n", name);
5960
return -EINVAL;
6061
}
6162

6263
ret = read(fd, buf, sizeof(buf));
6364
close(fd);
6465
if (ret == -1) {
65-
tcmu_errp(dev->ctx, "Could not read configfs to read attribute %s\n", name);
66+
tcmu_err("Could not read configfs to read attribute %s\n", name);
6667
return -EINVAL;
6768
}
6869

6970
val = strtoul(buf, NULL, 0);
7071
if (val == ULONG_MAX) {
71-
tcmu_errp(dev->ctx, "could not convert string to value\n");
72+
tcmu_err("could not convert string to value\n");
7273
return -EINVAL;
7374
}
7475

@@ -95,14 +96,14 @@ static char *tcmu_get_wwn(struct tcmu_device *dev)
9596

9697
fd = open(path, O_RDONLY);
9798
if (fd == -1) {
98-
tcmu_errp(dev->ctx, "Could not open configfs to read unit serial\n");
99+
tcmu_err("Could not open configfs to read unit serial\n");
99100
return NULL;
100101
}
101102

102103
ret = read(fd, buf, sizeof(buf));
103104
close(fd);
104105
if (ret == -1) {
105-
tcmu_errp(dev->ctx, "Could not read configfs to read unit serial\n");
106+
tcmu_err("Could not read configfs to read unit serial\n");
106107
return NULL;
107108
}
108109

@@ -112,7 +113,7 @@ static char *tcmu_get_wwn(struct tcmu_device *dev)
112113
/* Skip to the good stuff */
113114
ret = asprintf(&ret_buf, "%s", &buf[28]);
114115
if (ret == -1) {
115-
tcmu_errp(dev->ctx, "could not convert string to value\n");
116+
tcmu_err("could not convert string to value\n");
116117
return NULL;
117118
}
118119

@@ -133,28 +134,28 @@ long long tcmu_get_device_size(struct tcmu_device *dev)
133134

134135
fd = open(path, O_RDONLY);
135136
if (fd == -1) {
136-
tcmu_errp(dev->ctx, "Could not open configfs to read dev info\n");
137+
tcmu_err("Could not open configfs to read dev info\n");
137138
return -EINVAL;
138139
}
139140

140141
ret = read(fd, buf, sizeof(buf));
141142
close(fd);
142143
if (ret == -1) {
143-
tcmu_errp(dev->ctx, "Could not read configfs to read dev info\n");
144+
tcmu_err("Could not read configfs to read dev info\n");
144145
return -EINVAL;
145146
}
146147
buf[sizeof(buf)-1] = '\0'; /* paranoid? Ensure null terminated */
147148

148149
rover = strstr(buf, " Size: ");
149150
if (!rover) {
150-
tcmu_errp(dev->ctx, "Could not find \" Size: \" in %s\n", path);
151+
tcmu_err("Could not find \" Size: \" in %s\n", path);
151152
return -EINVAL;
152153
}
153154
rover += 7; /* get to the value */
154155

155156
size = strtoull(rover, NULL, 0);
156157
if (size == ULLONG_MAX) {
157-
tcmu_errp(dev->ctx, "Could not get size\n");
158+
tcmu_err("Could not get size\n");
158159
return -EINVAL;
159160
}
160161

consumer.c

+8-24
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@
4040
#include "libtcmu.h"
4141
#include "scsi_defs.h"
4242

43-
/*
44-
* Debug API implementation
45-
*/
46-
void dbgp(const char *fmt, ...)
47-
{
48-
va_list va;
49-
va_start(va, fmt);
50-
vprintf(fmt, va);
51-
va_end(va);
52-
}
53-
54-
void errp(const char *fmt, ...)
55-
{
56-
va_list va;
57-
58-
va_start(va, fmt);
59-
vfprintf(stderr, fmt, va);
60-
va_end(va);
61-
}
62-
6343
struct tcmu_device *tcmu_dev_array[128];
6444
size_t dev_array_len = 0;
6545

@@ -147,7 +127,7 @@ static int foo_handle_cmd(
147127
return SAM_STAT_GOOD;
148128

149129
default:
150-
errp("unknown command %x\n", cdb[0]);
130+
tcmu_err("unknown command %x\n", cdb[0]);
151131
return TCMU_NOT_HANDLED;
152132
}
153133
}
@@ -170,12 +150,14 @@ int main(int argc, char **argv)
170150
int i;
171151
int ret;
172152

153+
tcmu_log_open_syslog(TCMU_CONSUMER, 0, 0);
154+
173155
/* If any TCMU devices that exist that match subtype,
174156
handler->added() will now be called from within
175157
tcmulib_initialize(). */
176-
tcmulib_ctx = tcmulib_initialize(&foo_handler, 1, errp);
158+
tcmulib_ctx = tcmulib_initialize(&foo_handler, 1);
177159
if (tcmulib_ctx <= 0) {
178-
errp("tcmulib_initialize failed with %p\n", tcmulib_ctx);
160+
tcmu_err("tcmulib_initialize failed with %p\n", tcmulib_ctx);
179161
exit(1);
180162
}
181163

@@ -193,7 +175,7 @@ int main(int argc, char **argv)
193175
ret = poll(pollfds, dev_array_len+1, -1);
194176

195177
if (ret <= 0) {
196-
errp("poll() returned %d, exiting\n", ret);
178+
tcmu_err("poll() returned %d, exiting\n", ret);
197179
exit(1);
198180
}
199181

@@ -229,5 +211,7 @@ int main(int argc, char **argv)
229211
}
230212
}
231213

214+
tcmu_log_close_syslog();
215+
232216
return 0;
233217
}

file_example.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,29 @@ static int file_open(struct tcmu_device *dev)
190190

191191
block_size = tcmu_get_attribute(dev, "hw_block_size");
192192
if (block_size < 0) {
193-
errp("Could not get device block size\n");
193+
tcmu_err("Could not get device block size\n");
194194
goto err;
195195
}
196196
tcmu_set_dev_block_size(dev, block_size);
197197

198198
size = tcmu_get_device_size(dev);
199199
if (size < 0) {
200-
errp("Could not get device size\n");
200+
tcmu_err("Could not get device size\n");
201201
goto err;
202202
}
203203

204204
tcmu_set_dev_num_lbas(dev, size / block_size);
205205

206206
config = strchr(tcmu_get_dev_cfgstring(dev), '/');
207207
if (!config) {
208-
errp("no configuration found in cfgstring\n");
208+
tcmu_err("no configuration found in cfgstring\n");
209209
goto err;
210210
}
211211
config += 1; /* get past '/' */
212212

213213
state->fd = open(config, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
214214
if (state->fd == -1) {
215-
errp("could not open %s: %m\n", config);
215+
tcmu_err("could not open %s: %m\n", config);
216216
goto err;
217217
}
218218

@@ -348,7 +348,7 @@ static int file_handle_cmd(
348348

349349
ret = pread(state->fd, buf, length, offset);
350350
if (ret == -1) {
351-
errp("read failed: %m\n");
351+
tcmu_err("read failed: %m\n");
352352
free(buf);
353353
return set_medium_error(sense);
354354
}
@@ -377,7 +377,7 @@ static int file_handle_cmd(
377377

378378
ret = pwrite(state->fd, iovec->iov_base, to_copy, offset);
379379
if (ret == -1) {
380-
errp("Could not write: %m\n");
380+
tcmu_err("Could not write: %m\n");
381381
return set_medium_error(sense);
382382
}
383383

@@ -390,7 +390,7 @@ static int file_handle_cmd(
390390
}
391391
break;
392392
default:
393-
errp("unknown command %x\n", cdb[0]);
393+
tcmu_err("unknown command %x\n", cdb[0]);
394394
return TCMU_NOT_HANDLED;
395395
}
396396
}

0 commit comments

Comments
 (0)