Skip to content

Commit a1ff046

Browse files
aidangarskedanielinux
authored andcommitted
return short write (sent) when sent > 0 instead of -1/EAGAIN
- return sent if progress was made - use PTHREAD_MUTEX_INITIALIZER instead of racy mutex_initialized guard - removed extra newline in unit_tests_api.c
1 parent 556573e commit a1ff046

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

src/port/posix/bsd_socket.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
static __thread int in_the_stack = 1;
4747
static struct wolfIP *IPSTACK = NULL;
48-
pthread_mutex_t wolfIP_mutex;
48+
pthread_mutex_t wolfIP_mutex = PTHREAD_MUTEX_INITIALIZER;
4949

5050
struct wolfip_fd_entry;
5151
int wolfIP_sock_poll(struct wolfIP *ipstack, struct pollfd *fds, nfds_t nfds, int timeout);
@@ -1451,6 +1451,8 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
14511451
}
14521452
if (ret == -EAGAIN) {
14531453
if (nonblock) {
1454+
if (sent > 0)
1455+
break;
14541456
errno = EAGAIN;
14551457
pthread_mutex_unlock(&wolfIP_mutex);
14561458
return -1;
@@ -1470,7 +1472,7 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
14701472
if (internal_fd < 0) {
14711473
pthread_mutex_unlock(&wolfIP_mutex);
14721474
errno = EBADF;
1473-
return -1;
1475+
return (sent > 0) ? (ssize_t)sent : -1;
14741476
}
14751477
}
14761478
continue;
@@ -1519,6 +1521,8 @@ ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
15191521
}
15201522
if (ret == -EAGAIN) {
15211523
if (nonblock) {
1524+
if (sent > 0)
1525+
break;
15221526
errno = EAGAIN;
15231527
pthread_mutex_unlock(&wolfIP_mutex);
15241528
return -1;
@@ -1575,6 +1579,8 @@ ssize_t write(int sockfd, const void *buf, size_t len) {
15751579
}
15761580
if (ret == -EAGAIN) {
15771581
if (nonblock) {
1582+
if (sent > 0)
1583+
break;
15781584
errno = EAGAIN;
15791585
pthread_mutex_unlock(&wolfIP_mutex);
15801586
return -1;
@@ -1679,13 +1685,6 @@ void __attribute__((constructor)) init_wolfip_posix() {
16791685
#if WOLFIP_POSIX_TCPDUMP
16801686
static int tcpdump_atexit_registered;
16811687
#endif
1682-
{
1683-
static int mutex_initialized = 0;
1684-
if (!mutex_initialized) {
1685-
pthread_mutex_init(&wolfIP_mutex, NULL);
1686-
mutex_initialized = 1;
1687-
}
1688-
}
16891688
pthread_mutex_lock(&wolfIP_mutex);
16901689
if (IPSTACK) {
16911690
pthread_mutex_unlock(&wolfIP_mutex);

src/test/unit/unit_tests_api.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,5 +3304,3 @@ START_TEST(test_dns_wrapper_apis)
33043304
ck_assert_uint_eq(s.dns_query_type, DNS_QUERY_TYPE_PTR);
33053305
}
33063306
END_TEST
3307-
3308-

0 commit comments

Comments
 (0)