Skip to content

Commit b9231b5

Browse files
committed
Merge branch 'remove-non-portable-error' of https://github.com/stwnt/liburing
* 'remove-non-portable-error' of https://github.com/stwnt/liburing: examples: Use t_error instead of glibc's error. test: Use t_error instead of glibc's error. Add custom error function for tests.
2 parents 7b09481 + 0a9e226 commit b9231b5

File tree

7 files changed

+67
-34
lines changed

7 files changed

+67
-34
lines changed

examples/send-zerocopy.c

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#include <stdint.h>
66
#include <assert.h>
77
#include <errno.h>
8-
#include <error.h>
98
#include <limits.h>
109
#include <fcntl.h>
1110
#include <unistd.h>
1211
#include <stdbool.h>
12+
#include <stdarg.h>
1313
#include <string.h>
1414

1515
#include <arpa/inet.h>
@@ -57,6 +57,23 @@ static struct sockaddr_storage cfg_dst_addr;
5757

5858
static char payload[IP_MAXPACKET] __attribute__((aligned(4096)));
5959

60+
/*
61+
* Implementation of error(3), prints an error message and exits.
62+
*/
63+
static void t_error(int status, int errnum, const char *format, ...)
64+
{
65+
va_list args;
66+
va_start(args, format);
67+
68+
vfprintf(stderr, format, args);
69+
if (errnum)
70+
fprintf(stderr, ": %s", strerror(errnum));
71+
72+
fprintf(stderr, "\n");
73+
va_end(args);
74+
exit(status);
75+
}
76+
6077
static unsigned long gettimeofday_ms(void)
6178
{
6279
struct timeval tv;
@@ -68,7 +85,7 @@ static unsigned long gettimeofday_ms(void)
6885
static void do_setsockopt(int fd, int level, int optname, int val)
6986
{
7087
if (setsockopt(fd, level, optname, &val, sizeof(val)))
71-
error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
88+
t_error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
7289
}
7390

7491
static void setup_sockaddr(int domain, const char *str_addr,
@@ -84,18 +101,18 @@ static void setup_sockaddr(int domain, const char *str_addr,
84101
addr4->sin_port = htons(cfg_port);
85102
if (str_addr &&
86103
inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
87-
error(1, 0, "ipv4 parse error: %s", str_addr);
104+
t_error(1, 0, "ipv4 parse error: %s", str_addr);
88105
break;
89106
case PF_INET6:
90107
memset(addr6, 0, sizeof(*addr6));
91108
addr6->sin6_family = AF_INET6;
92109
addr6->sin6_port = htons(cfg_port);
93110
if (str_addr &&
94111
inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
95-
error(1, 0, "ipv6 parse error: %s", str_addr);
112+
t_error(1, 0, "ipv6 parse error: %s", str_addr);
96113
break;
97114
default:
98-
error(1, 0, "illegal domain");
115+
t_error(1, 0, "illegal domain");
99116
}
100117
}
101118

@@ -105,12 +122,12 @@ static int do_setup_tx(int domain, int type, int protocol)
105122

106123
fd = socket(domain, type, protocol);
107124
if (fd == -1)
108-
error(1, errno, "socket t");
125+
t_error(1, errno, "socket t");
109126

110127
do_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 1 << 21);
111128

112129
if (connect(fd, (void *) &cfg_dst_addr, cfg_alen))
113-
error(1, errno, "connect");
130+
t_error(1, errno, "connect");
114131
return fd;
115132
}
116133

@@ -125,7 +142,7 @@ static inline struct io_uring_cqe *wait_cqe_fast(struct io_uring *ring)
125142

126143
ret = io_uring_wait_cqe(ring, &cqe);
127144
if (ret)
128-
error(1, ret, "wait cqe");
145+
t_error(1, ret, "wait cqe");
129146
return cqe;
130147
}
131148

@@ -143,25 +160,25 @@ static void do_tx(int domain, int type, int protocol)
143160

144161
ret = io_uring_queue_init(512, &ring, IORING_SETUP_COOP_TASKRUN);
145162
if (ret)
146-
error(1, ret, "io_uring: queue init");
163+
t_error(1, ret, "io_uring: queue init");
147164

148165
if (cfg_fixed_files) {
149166
ret = io_uring_register_files(&ring, &fd, 1);
150167
if (ret < 0)
151-
error(1, ret, "io_uring: files registration");
168+
t_error(1, ret, "io_uring: files registration");
152169
}
153170
if (cfg_reg_ringfd) {
154171
ret = io_uring_register_ring_fd(&ring);
155172
if (ret < 0)
156-
error(1, ret, "io_uring: io_uring_register_ring_fd");
173+
t_error(1, ret, "io_uring: io_uring_register_ring_fd");
157174
}
158175

159176
iov.iov_base = payload;
160177
iov.iov_len = cfg_payload_len;
161178

162179
ret = io_uring_register_buffers(&ring, &iov, 1);
163180
if (ret)
164-
error(1, ret, "io_uring: buffer registration");
181+
t_error(1, ret, "io_uring: buffer registration");
165182

166183
tstop = gettimeofday_ms() + cfg_runtime_ms;
167184
do {
@@ -193,14 +210,14 @@ static void do_tx(int domain, int type, int protocol)
193210

194211
ret = io_uring_submit(&ring);
195212
if (ret != cfg_nr_reqs)
196-
error(1, ret, "submit");
213+
t_error(1, ret, "submit");
197214

198215
for (i = 0; i < cfg_nr_reqs; i++) {
199216
cqe = wait_cqe_fast(&ring);
200217

201218
if (cqe->flags & IORING_CQE_F_NOTIF) {
202219
if (cqe->flags & IORING_CQE_F_MORE)
203-
error(1, -EINVAL, "F_MORE notif");
220+
t_error(1, -EINVAL, "F_MORE notif");
204221
compl_cqes--;
205222
i--;
206223
io_uring_cqe_seen(&ring, cqe);
@@ -217,7 +234,7 @@ static void do_tx(int domain, int type, int protocol)
217234
fprintf(stderr, "Connection failure");
218235
goto out_fail;
219236
} else if (cqe->res != -EAGAIN) {
220-
error(1, cqe->res, "send failed");
237+
t_error(1, cqe->res, "send failed");
221238
}
222239
io_uring_cqe_seen(&ring, cqe);
223240
}
@@ -226,7 +243,7 @@ static void do_tx(int domain, int type, int protocol)
226243
out_fail:
227244
shutdown(fd, SHUT_RDWR);
228245
if (close(fd))
229-
error(1, errno, "close");
246+
t_error(1, errno, "close");
230247

231248
fprintf(stderr, "tx=%lu (MB=%lu), tx/s=%lu (MB/s=%lu)\n",
232249
packets, bytes >> 20,
@@ -254,7 +271,7 @@ static void do_test(int domain, int type, int protocol)
254271

255272
static void usage(const char *filepath)
256273
{
257-
error(1, 0, "Usage: %s [-n<N>] [-z<val>] [-s<payload size>] "
274+
t_error(1, 0, "Usage: %s [-n<N>] [-z<val>] [-s<payload size>] "
258275
"(-4|-6) [-t<time s>] -D<dst_ip> udp", filepath);
259276
}
260277

@@ -276,13 +293,13 @@ static void parse_opts(int argc, char **argv)
276293
switch (c) {
277294
case '4':
278295
if (cfg_family != PF_UNSPEC)
279-
error(1, 0, "Pass one of -4 or -6");
296+
t_error(1, 0, "Pass one of -4 or -6");
280297
cfg_family = PF_INET;
281298
cfg_alen = sizeof(struct sockaddr_in);
282299
break;
283300
case '6':
284301
if (cfg_family != PF_UNSPEC)
285-
error(1, 0, "Pass one of -4 or -6");
302+
t_error(1, 0, "Pass one of -4 or -6");
286303
cfg_family = PF_INET6;
287304
cfg_alen = sizeof(struct sockaddr_in6);
288305
break;
@@ -311,9 +328,9 @@ static void parse_opts(int argc, char **argv)
311328
}
312329

313330
if (cfg_nr_reqs > MAX_SUBMIT_NR)
314-
error(1, 0, "-n: submit batch nr exceeds max (%d)", MAX_SUBMIT_NR);
331+
t_error(1, 0, "-n: submit batch nr exceeds max (%d)", MAX_SUBMIT_NR);
315332
if (cfg_payload_len > max_payload_len)
316-
error(1, 0, "-s: payload exceeds max (%d)", max_payload_len);
333+
t_error(1, 0, "-s: payload exceeds max (%d)", max_payload_len);
317334

318335
setup_sockaddr(cfg_family, daddr, &cfg_dst_addr);
319336

@@ -333,7 +350,7 @@ int main(int argc, char **argv)
333350
else if (!strcmp(cfg_test, "udp"))
334351
do_test(cfg_family, SOCK_DGRAM, 0);
335352
else
336-
error(1, 0, "unknown cfg_test %s", cfg_test);
353+
t_error(1, 0, "unknown cfg_test %s", cfg_test);
337354

338355
return 0;
339356
}

test/defer-taskrun.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <unistd.h>
55
#include <stdlib.h>
66
#include <string.h>
7-
#include <error.h>
87
#include <sys/eventfd.h>
98
#include <signal.h>
109
#include <poll.h>

test/helpers.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdio.h>
99
#include <fcntl.h>
1010
#include <unistd.h>
11+
#include <stdarg.h>
1112
#include <sys/types.h>
1213

1314
#include <arpa/inet.h>
@@ -300,3 +301,20 @@ unsigned __io_uring_flush_sq(struct io_uring *ring)
300301
*/
301302
return tail - *sq->khead;
302303
}
304+
305+
/*
306+
* Implementation of error(3), prints an error message and exits.
307+
*/
308+
void t_error(int status, int errnum, const char *format, ...)
309+
{
310+
va_list args;
311+
va_start(args, format);
312+
313+
vfprintf(stderr, format, args);
314+
if (errnum)
315+
fprintf(stderr, ": %s", strerror(errnum));
316+
317+
fprintf(stderr, "\n");
318+
va_end(args);
319+
exit(status);
320+
}

test/helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ unsigned __io_uring_flush_sq(struct io_uring *ring);
8989

9090
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
9191

92+
void t_error(int status, int errnum, const char *format, ...);
93+
9294
#ifdef __cplusplus
9395
}
9496
#endif

test/poll.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <signal.h>
1212
#include <poll.h>
1313
#include <sys/wait.h>
14-
#include <error.h>
1514
#include <assert.h>
1615

1716
#include "helpers.h"
@@ -20,7 +19,7 @@
2019
static void do_setsockopt(int fd, int level, int optname, int val)
2120
{
2221
if (setsockopt(fd, level, optname, &val, sizeof(val)))
23-
error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
22+
t_error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
2423
}
2524

2625
static bool check_cq_empty(struct io_uring *ring)

test/send-zerocopy.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <stdint.h>
55
#include <assert.h>
66
#include <errno.h>
7-
#include <error.h>
87
#include <limits.h>
98
#include <fcntl.h>
109
#include <unistd.h>

test/single-issuer.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stdlib.h>
66
#include <string.h>
77
#include <fcntl.h>
8-
#include <error.h>
98
#include <sys/types.h>
109
#include <sys/wait.h>
1110

@@ -56,13 +55,13 @@ static int try_submit(struct io_uring *ring)
5655
return ret;
5756

5857
if (ret != 1)
59-
error(1, ret, "submit %i", ret);
58+
t_error(1, ret, "submit %i", ret);
6059
ret = io_uring_wait_cqe(ring, &cqe);
6160
if (ret)
62-
error(1, ret, "wait fail %i", ret);
61+
t_error(1, ret, "wait fail %i", ret);
6362

6463
if (cqe->res || cqe->user_data != 42)
65-
error(1, ret, "invalid cqe");
64+
t_error(1, ret, "invalid cqe");
6665

6766
io_uring_cqe_seen(ring, cqe);
6867
return 0;
@@ -105,7 +104,7 @@ int main(int argc, char *argv[])
105104
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
106105
IORING_SETUP_R_DISABLED);
107106
if (ret)
108-
error(1, ret, "ring init (2) %i", ret);
107+
t_error(1, ret, "ring init (2) %i", ret);
109108

110109
if (!fork_t()) {
111110
io_uring_enable_rings(&ring);
@@ -121,7 +120,7 @@ int main(int argc, char *argv[])
121120
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
122121
IORING_SETUP_R_DISABLED);
123122
if (ret)
124-
error(1, ret, "ring init (3) %i", ret);
123+
t_error(1, ret, "ring init (3) %i", ret);
125124

126125
io_uring_enable_rings(&ring);
127126
if (!fork_t()) {
@@ -136,7 +135,7 @@ int main(int argc, char *argv[])
136135
/* test that anyone can submit to a SQPOLL|SINGLE_ISSUER ring */
137136
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_SQPOLL);
138137
if (ret)
139-
error(1, ret, "ring init (4) %i", ret);
138+
t_error(1, ret, "ring init (4) %i", ret);
140139

141140
ret = try_submit(&ring);
142141
if (ret) {
@@ -156,7 +155,7 @@ int main(int argc, char *argv[])
156155
/* test that IORING_ENTER_REGISTERED_RING doesn't break anything */
157156
ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER);
158157
if (ret)
159-
error(1, ret, "ring init (5) %i", ret);
158+
t_error(1, ret, "ring init (5) %i", ret);
160159

161160
if (!fork_t()) {
162161
ret = try_submit(&ring);

0 commit comments

Comments
 (0)