1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 18:55:40 +03:00

tree-wide: make macros for converting fds to pointers and back generic and use them everywhere

This commit is contained in:
Lennart Poettering 2015-11-17 00:51:24 +01:00
parent b16fee15ff
commit 23e096cc60
6 changed files with 22 additions and 23 deletions

View File

@ -68,7 +68,7 @@ int asynchronous_sync(void) {
}
static void *close_thread(void *p) {
assert_se(close_nointr(PTR_TO_INT(p)) != -EBADF);
assert_se(close_nointr(PTR_TO_FD(p)) != -EBADF);
return NULL;
}
@ -84,7 +84,7 @@ int asynchronous_close(int fd) {
if (fd >= 0) {
PROTECT_ERRNO;
r = asynchronous_job(close_thread, INT_TO_PTR(fd));
r = asynchronous_job(close_thread, FD_TO_PTR(fd));
if (r < 0)
assert_se(close_nointr(fd) != -EBADF);
}

View File

@ -28,6 +28,10 @@
#include "macro.h"
/* Make sure we can distinguish fd 0 and NULL */
#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
int close_nointr(int fd);
int safe_close(int fd);
void safe_close_pair(int p[]);

View File

@ -36,10 +36,6 @@
#define MAKE_SET(s) ((Set*) s)
#define MAKE_FDSET(s) ((FDSet*) s)
/* Make sure we can distinguish fd 0 and NULL */
#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
FDSet *fdset_new(void) {
return MAKE_FDSET(set_new(NULL));
}

View File

@ -48,9 +48,7 @@ static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *u
assert(s);
assert(g);
translated_fd = PTR_TO_INT(hashmap_get(g->translate_fds, INT_TO_PTR(fd+1)));
assert(translated_fd > 0);
translated_fd--;
translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd)));
if ((revents & (EPOLLIN|EPOLLOUT)) == (EPOLLIN|EPOLLOUT))
action = CURL_POLL_INOUT;
@ -79,7 +77,7 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
assert(curl);
assert(g);
io = hashmap_get(g->ios, INT_TO_PTR(s+1));
io = hashmap_get(g->ios, FD_TO_PTR(s));
if (action == CURL_POLL_REMOVE) {
if (io) {
@ -91,8 +89,8 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
sd_event_source_set_enabled(io, SD_EVENT_OFF);
sd_event_source_unref(io);
hashmap_remove(g->ios, INT_TO_PTR(s+1));
hashmap_remove(g->translate_fds, INT_TO_PTR(fd+1));
hashmap_remove(g->ios, FD_TO_PTR(s));
hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
safe_close(fd);
}
@ -143,17 +141,17 @@ static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, v
sd_event_source_set_description(io, "curl-io");
r = hashmap_put(g->ios, INT_TO_PTR(s+1), io);
r = hashmap_put(g->ios, FD_TO_PTR(s), io);
if (r < 0) {
log_oom();
sd_event_source_unref(io);
return -1;
}
r = hashmap_put(g->translate_fds, INT_TO_PTR(fd+1), INT_TO_PTR(s+1));
r = hashmap_put(g->translate_fds, FD_TO_PTR(fd), FD_TO_PTR(s));
if (r < 0) {
log_oom();
hashmap_remove(g->ios, INT_TO_PTR(s+1));
hashmap_remove(g->ios, FD_TO_PTR(s));
sd_event_source_unref(io);
return -1;
}
@ -229,7 +227,7 @@ CurlGlue *curl_glue_unref(CurlGlue *g) {
fd = sd_event_source_get_io_fd(io);
assert(fd >= 0);
hashmap_remove(g->translate_fds, INT_TO_PTR(fd+1));
hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
safe_close(fd);
sd_event_source_unref(io);

View File

@ -24,6 +24,7 @@
#include <sys/mman.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "hashmap.h"
#include "list.h"
#include "log.h"
@ -289,7 +290,7 @@ static void fd_free(FileDescriptor *f) {
window_free(f->windows);
if (f->cache)
assert_se(hashmap_remove(f->cache->fds, INT_TO_PTR(f->fd + 1)));
assert_se(hashmap_remove(f->cache->fds, FD_TO_PTR(f->fd)));
free(f);
}
@ -301,7 +302,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) {
assert(m);
assert(fd >= 0);
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
f = hashmap_get(m->fds, FD_TO_PTR(fd));
if (f)
return f;
@ -316,7 +317,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) {
f->cache = m;
f->fd = fd;
r = hashmap_put(m->fds, UINT_TO_PTR(fd + 1), f);
r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
if (r < 0) {
free(f);
return NULL;
@ -429,7 +430,7 @@ static int find_mmap(
assert(fd >= 0);
assert(size > 0);
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
f = hashmap_get(m->fds, FD_TO_PTR(fd));
if (!f)
return 0;
@ -679,7 +680,7 @@ bool mmap_cache_got_sigbus(MMapCache *m, int fd) {
mmap_cache_process_sigbus(m);
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
f = hashmap_get(m->fds, FD_TO_PTR(fd));
if (!f)
return false;
@ -698,7 +699,7 @@ void mmap_cache_close_fd(MMapCache *m, int fd) {
mmap_cache_process_sigbus(m);
f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
f = hashmap_get(m->fds, FD_TO_PTR(fd));
if (!f)
return;

View File

@ -493,7 +493,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
return PAM_SESSION_ERR;
}
r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL);
if (r != PAM_SUCCESS) {
pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
safe_close(session_fd);