X Tutup
Skip to content

Commit 23e096c

Browse files
committed
tree-wide: make macros for converting fds to pointers and back generic and use them everywhere
1 parent b16fee1 commit 23e096c

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

src/basic/async.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int asynchronous_sync(void) {
6868
}
6969

7070
static void *close_thread(void *p) {
71-
assert_se(close_nointr(PTR_TO_INT(p)) != -EBADF);
71+
assert_se(close_nointr(PTR_TO_FD(p)) != -EBADF);
7272
return NULL;
7373
}
7474

@@ -84,7 +84,7 @@ int asynchronous_close(int fd) {
8484
if (fd >= 0) {
8585
PROTECT_ERRNO;
8686

87-
r = asynchronous_job(close_thread, INT_TO_PTR(fd));
87+
r = asynchronous_job(close_thread, FD_TO_PTR(fd));
8888
if (r < 0)
8989
assert_se(close_nointr(fd) != -EBADF);
9090
}

src/basic/fd-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
#include "macro.h"
3030

31+
/* Make sure we can distinguish fd 0 and NULL */
32+
#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
33+
#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
34+
3135
int close_nointr(int fd);
3236
int safe_close(int fd);
3337
void safe_close_pair(int p[]);

src/basic/fdset.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
#define MAKE_SET(s) ((Set*) s)
3737
#define MAKE_FDSET(s) ((FDSet*) s)
3838

39-
/* Make sure we can distinguish fd 0 and NULL */
40-
#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
41-
#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
42-
4339
FDSet *fdset_new(void) {
4440
return MAKE_FDSET(set_new(NULL));
4541
}

src/import/curl-util.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *u
4848
assert(s);
4949
assert(g);
5050

51-
translated_fd = PTR_TO_INT(hashmap_get(g->translate_fds, INT_TO_PTR(fd+1)));
52-
assert(translated_fd > 0);
53-
translated_fd--;
51+
translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd)));
5452

5553
if ((revents & (EPOLLIN|EPOLLOUT)) == (EPOLLIN|EPOLLOUT))
5654
action = CURL_POLL_INOUT;
@@ -79,7 +77,7 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
7977
assert(curl);
8078
assert(g);
8179

82-
io = hashmap_get(g->ios, INT_TO_PTR(s+1));
80+
io = hashmap_get(g->ios, FD_TO_PTR(s));
8381

8482
if (action == CURL_POLL_REMOVE) {
8583
if (io) {
@@ -91,8 +89,8 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
9189
sd_event_source_set_enabled(io, SD_EVENT_OFF);
9290
sd_event_source_unref(io);
9391

94-
hashmap_remove(g->ios, INT_TO_PTR(s+1));
95-
hashmap_remove(g->translate_fds, INT_TO_PTR(fd+1));
92+
hashmap_remove(g->ios, FD_TO_PTR(s));
93+
hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
9694

9795
safe_close(fd);
9896
}
@@ -143,17 +141,17 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
143141

144142
sd_event_source_set_description(io, "curl-io");
145143

146-
r = hashmap_put(g->ios, INT_TO_PTR(s+1), io);
144+
r = hashmap_put(g->ios, FD_TO_PTR(s), io);
147145
if (r < 0) {
148146
log_oom();
149147
sd_event_source_unref(io);
150148
return -1;
151149
}
152150

153-
r = hashmap_put(g->translate_fds, INT_TO_PTR(fd+1), INT_TO_PTR(s+1));
151+
r = hashmap_put(g->translate_fds, FD_TO_PTR(fd), FD_TO_PTR(s));
154152
if (r < 0) {
155153
log_oom();
156-
hashmap_remove(g->ios, INT_TO_PTR(s+1));
154+
hashmap_remove(g->ios, FD_TO_PTR(s));
157155
sd_event_source_unref(io);
158156
return -1;
159157
}
@@ -229,7 +227,7 @@ CurlGlue *curl_glue_unref(CurlGlue *g) {
229227
fd = sd_event_source_get_io_fd(io);
230228
assert(fd >= 0);
231229

232-
hashmap_remove(g->translate_fds, INT_TO_PTR(fd+1));
230+
hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
233231

234232
safe_close(fd);
235233
sd_event_source_unref(io);

src/journal/mmap-cache.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sys/mman.h>
2525

2626
#include "alloc-util.h"
27+
#include "fd-util.h"
2728
#include "hashmap.h"
2829
#include "list.h"
2930
#include "log.h"
@@ -289,7 +290,7 @@ static void fd_free(FileDescriptor *f) {
289290
window_free(f->windows);
290291

291292
if (f->cache)
292-
assert_se(hashmap_remove(f->cache->fds, INT_TO_PTR(f->fd + 1)));
293+
assert_se(hashmap_remove(f->cache->fds, FD_TO_PTR(f->fd)));
293294

294295
free(f);
295296
}
@@ -301,7 +302,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) {
301302
assert(m);
302303
assert(fd >= 0);
303304

304-
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
305+
f = hashmap_get(m->fds, FD_TO_PTR(fd));
305306
if (f)
306307
return f;
307308

@@ -316,7 +317,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) {
316317
f->cache = m;
317318
f->fd = fd;
318319

319-
r = hashmap_put(m->fds, UINT_TO_PTR(fd + 1), f);
320+
r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
320321
if (r < 0) {
321322
free(f);
322323
return NULL;
@@ -429,7 +430,7 @@ static int find_mmap(
429430
assert(fd >= 0);
430431
assert(size > 0);
431432

432-
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
433+
f = hashmap_get(m->fds, FD_TO_PTR(fd));
433434
if (!f)
434435
return 0;
435436

@@ -679,7 +680,7 @@ bool mmap_cache_got_sigbus(MMapCache *m, int fd) {
679680

680681
mmap_cache_process_sigbus(m);
681682

682-
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
683+
f = hashmap_get(m->fds, FD_TO_PTR(fd));
683684
if (!f)
684685
return false;
685686

@@ -698,7 +699,7 @@ void mmap_cache_close_fd(MMapCache *m, int fd) {
698699

699700
mmap_cache_process_sigbus(m);
700701

701-
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
702+
f = hashmap_get(m->fds, FD_TO_PTR(fd));
702703
if (!f)
703704
return;
704705

src/login/pam_systemd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
493493
return PAM_SESSION_ERR;
494494
}
495495

496-
r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
496+
r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL);
497497
if (r != PAM_SUCCESS) {
498498
pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
499499
safe_close(session_fd);

0 commit comments

Comments
 (0)
X Tutup