Skip to content

Commit 7aab6e5

Browse files
committed
Fix from review
1 parent 76bbe89 commit 7aab6e5

4 files changed

Lines changed: 23 additions & 48 deletions

File tree

src/crl.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,13 +1712,7 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
17121712
SignalSetup(crl, MONITOR_SETUP_E);
17131713
return NULL;
17141714
}
1715-
#ifdef FD_CLOEXEC
1716-
{
1717-
int fdFlags = fcntl(crl->mfd, F_GETFD);
1718-
if (fdFlags >= 0)
1719-
(void)fcntl(crl->mfd, F_SETFD, fdFlags | FD_CLOEXEC);
1720-
}
1721-
#endif
1715+
wc_set_cloexec(crl->mfd);
17221716

17231717
/* listen for custom shutdown event */
17241718
EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, EV_ADD, 0, 0, NULL);
@@ -1856,11 +1850,7 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
18561850
#ifdef FD_CLOEXEC
18571851
if (crl->mfd < 0 && errno == EINVAL) {
18581852
crl->mfd = eventfd(0, 0);
1859-
if (crl->mfd >= 0) {
1860-
int fdFlags = fcntl(crl->mfd, F_GETFD);
1861-
if (fdFlags >= 0)
1862-
(void)fcntl(crl->mfd, F_SETFD, fdFlags | FD_CLOEXEC);
1863-
}
1853+
wc_set_cloexec(crl->mfd);
18641854
}
18651855
#endif
18661856
if (crl->mfd < 0) {
@@ -1873,23 +1863,11 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
18731863
notifyFd = inotify_init1(IN_CLOEXEC);
18741864
if (notifyFd < 0 && (errno == ENOSYS || errno == EINVAL)) {
18751865
notifyFd = inotify_init();
1876-
#ifdef FD_CLOEXEC
1877-
if (notifyFd >= 0) {
1878-
int fdFlags = fcntl(notifyFd, F_GETFD);
1879-
if (fdFlags >= 0)
1880-
(void)fcntl(notifyFd, F_SETFD, fdFlags | FD_CLOEXEC);
1881-
}
1882-
#endif
1866+
wc_set_cloexec(notifyFd);
18831867
}
18841868
#else
18851869
notifyFd = inotify_init();
1886-
#ifdef FD_CLOEXEC
1887-
if (notifyFd >= 0) {
1888-
int fdFlags = fcntl(notifyFd, F_GETFD);
1889-
if (fdFlags >= 0)
1890-
(void)fcntl(notifyFd, F_SETFD, fdFlags | FD_CLOEXEC);
1891-
}
1892-
#endif
1870+
wc_set_cloexec(notifyFd);
18931871
#endif
18941872
if (notifyFd < 0) {
18951873
WOLFSSL_MSG("inotify failed");

wolfcrypt/benchmark/benchmark.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,11 +1596,7 @@ static const char* bench_result_words3[][5] = {
15961596
PERF_FLAG_FD_CLOEXEC); \
15971597
if (cycles < 0 && errno == EINVAL) { \
15981598
cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
1599-
if (cycles >= 0) { \
1600-
int _fdFlags = fcntl(cycles, F_GETFD); \
1601-
if (_fdFlags >= 0) \
1602-
(void)fcntl(cycles, F_SETFD, _fdFlags | FD_CLOEXEC); \
1603-
} \
1599+
wc_set_cloexec(cycles); \
16041600
} \
16051601
} while (0);
16061602

wolfcrypt/src/wc_port.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,17 +5148,27 @@ char* wolfSSL_strnstr(const char* s1, const char* s2, unsigned int n)
51485148
#define SOCK_CLOEXEC 0
51495149
#endif
51505150

5151+
void wc_set_cloexec(int fd)
5152+
{
5153+
#ifdef FD_CLOEXEC
5154+
int fdFlags;
5155+
if (fd < 0)
5156+
return;
5157+
fdFlags = fcntl(fd, F_GETFD);
5158+
if (fdFlags >= 0)
5159+
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
5160+
#else
5161+
(void)fd;
5162+
#endif
5163+
}
5164+
51515165
int wc_open_cloexec(const char* path, int flags)
51525166
{
51535167
int fd = open(path, flags | O_CLOEXEC);
51545168
#ifdef FD_CLOEXEC
51555169
if (fd < 0 && errno == EINVAL) {
51565170
fd = open(path, flags);
5157-
if (fd >= 0) {
5158-
int fdFlags = fcntl(fd, F_GETFD);
5159-
if (fdFlags >= 0)
5160-
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
5161-
}
5171+
wc_set_cloexec(fd);
51625172
}
51635173
#endif
51645174
return fd;
@@ -5170,11 +5180,7 @@ int wc_socket_cloexec(int domain, int type, int protocol)
51705180
#ifdef FD_CLOEXEC
51715181
if (fd < 0 && errno == EINVAL) {
51725182
fd = socket(domain, type, protocol);
5173-
if (fd >= 0) {
5174-
int fdFlags = fcntl(fd, F_GETFD);
5175-
if (fdFlags >= 0)
5176-
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
5177-
}
5183+
wc_set_cloexec(fd);
51785184
}
51795185
#endif
51805186
return fd;
@@ -5192,13 +5198,7 @@ int wc_accept_cloexec(int sockfd, void* addr, void* addrlen)
51925198
return fd;
51935199
#endif
51945200
fd = accept(sockfd, (struct sockaddr*)addr, (socklen_t*)addrlen);
5195-
#ifdef FD_CLOEXEC
5196-
if (fd >= 0) {
5197-
int fdFlags = fcntl(fd, F_GETFD);
5198-
if (fdFlags >= 0)
5199-
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
5200-
}
5201-
#endif
5201+
wc_set_cloexec(fd);
52025202
return fd;
52035203
}
52045204

wolfssl/wolfcrypt/wc_port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
18611861
#endif
18621862

18631863
#if (defined(__unix__) || defined(__APPLE__)) && !defined(WOLFSSL_ZEPHYR)
1864+
WOLFSSL_LOCAL void wc_set_cloexec(int fd);
18641865
WOLFSSL_LOCAL int wc_open_cloexec(const char* path, int flags);
18651866
WOLFSSL_LOCAL int wc_socket_cloexec(int domain, int type, int protocol);
18661867
WOLFSSL_LOCAL int wc_accept_cloexec(int sockfd, void* addr, void* addrlen);

0 commit comments

Comments
 (0)