mainloop: cleanup

parent 3e92b6f7
...@@ -1913,7 +1913,7 @@ static int freezer_cgroup_events_cb(int fd, uint32_t events, void *cbdata, ...@@ -1913,7 +1913,7 @@ static int freezer_cgroup_events_cb(int fd, uint32_t events, void *cbdata,
static int cg_unified_freeze(struct cgroup_ops *ops, int timeout) static int cg_unified_freeze(struct cgroup_ops *ops, int timeout)
{ {
__do_close int fd = -EBADF; __do_close int fd = -EBADF;
__do_lxc_mainloop_close struct lxc_epoll_descr *descr_ptr = NULL; call_cleaner(lxc_mainloop_close) struct lxc_epoll_descr *descr_ptr = NULL;
int ret; int ret;
struct lxc_epoll_descr descr; struct lxc_epoll_descr descr;
struct hierarchy *h; struct hierarchy *h;
...@@ -1981,7 +1981,7 @@ static int cg_legacy_unfreeze(struct cgroup_ops *ops) ...@@ -1981,7 +1981,7 @@ static int cg_legacy_unfreeze(struct cgroup_ops *ops)
static int cg_unified_unfreeze(struct cgroup_ops *ops, int timeout) static int cg_unified_unfreeze(struct cgroup_ops *ops, int timeout)
{ {
__do_close int fd = -EBADF; __do_close int fd = -EBADF;
__do_lxc_mainloop_close struct lxc_epoll_descr *descr_ptr = NULL; call_cleaner(lxc_mainloop_close)struct lxc_epoll_descr *descr_ptr = NULL;
int ret; int ret;
struct lxc_epoll_descr descr; struct lxc_epoll_descr descr;
struct hierarchy *h; struct hierarchy *h;
......
...@@ -34,7 +34,7 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms) ...@@ -34,7 +34,7 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms)
if (errno == EINTR) if (errno == EINTR)
continue; continue;
return -1; return -errno;
} }
for (i = 0; i < nfds; i++) { for (i = 0; i < nfds; i++) {
...@@ -62,9 +62,9 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms) ...@@ -62,9 +62,9 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms)
int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd, int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
lxc_mainloop_callback_t callback, void *data) lxc_mainloop_callback_t callback, void *data)
{ {
__do_free struct mainloop_handler *handler = NULL;
__do_free struct lxc_list *item = NULL;
struct epoll_event ev; struct epoll_event ev;
struct mainloop_handler *handler;
struct lxc_list *item;
if (fd < 0) if (fd < 0)
return -1; return -1;
...@@ -81,19 +81,15 @@ int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd, ...@@ -81,19 +81,15 @@ int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
ev.data.ptr = handler; ev.data.ptr = handler;
if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev) < 0) if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev) < 0)
goto out_free_handler; return -errno;
item = malloc(sizeof(*item)); item = malloc(sizeof(*item));
if (!item) if (!item)
goto out_free_handler; return ret_errno(ENOMEM);
item->elem = handler; item->elem = move_ptr(handler);
lxc_list_add(&descr->handlers, item); lxc_list_add(&descr->handlers, move_ptr(item));
return 0; return 0;
out_free_handler:
free(handler);
return -1;
} }
int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd) int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
...@@ -107,7 +103,7 @@ int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd) ...@@ -107,7 +103,7 @@ int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
if (handler->fd == fd) { if (handler->fd == fd) {
/* found */ /* found */
if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL)) if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
return -1; return -errno;
lxc_list_del(iterator); lxc_list_del(iterator);
free(iterator->elem); free(iterator->elem);
...@@ -116,21 +112,20 @@ int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd) ...@@ -116,21 +112,20 @@ int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
} }
} }
return -1; return ret_errno(EINVAL);
} }
int lxc_mainloop_open(struct lxc_epoll_descr *descr) int lxc_mainloop_open(struct lxc_epoll_descr *descr)
{ {
/* hint value passed to epoll create */
descr->epfd = epoll_create1(EPOLL_CLOEXEC); descr->epfd = epoll_create1(EPOLL_CLOEXEC);
if (descr->epfd < 0) if (descr->epfd < 0)
return -1; return -errno;
lxc_list_init(&descr->handlers); lxc_list_init(&descr->handlers);
return 0; return 0;
} }
int lxc_mainloop_close(struct lxc_epoll_descr *descr) void lxc_mainloop_close(struct lxc_epoll_descr *descr)
{ {
struct lxc_list *iterator, *next; struct lxc_list *iterator, *next;
...@@ -144,8 +139,5 @@ int lxc_mainloop_close(struct lxc_epoll_descr *descr) ...@@ -144,8 +139,5 @@ int lxc_mainloop_close(struct lxc_epoll_descr *descr)
iterator = next; iterator = next;
} }
if (descr->epfd >= 0) close_prot_errno_disarm(descr->epfd);
return close(descr->epfd);
return 0;
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include "list.h" #include "list.h"
#include "memory_utils.h"
#define LXC_MAINLOOP_ERROR -1 #define LXC_MAINLOOP_ERROR -1
#define LXC_MAINLOOP_CONTINUE 0 #define LXC_MAINLOOP_CONTINUE 0
...@@ -29,15 +30,8 @@ extern int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd); ...@@ -29,15 +30,8 @@ extern int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd);
extern int lxc_mainloop_open(struct lxc_epoll_descr *descr); extern int lxc_mainloop_open(struct lxc_epoll_descr *descr);
extern int lxc_mainloop_close(struct lxc_epoll_descr *descr); extern void lxc_mainloop_close(struct lxc_epoll_descr *descr);
static inline void __auto_lxc_mainloop_close__(struct lxc_epoll_descr **descr) define_cleanup_function(struct lxc_epoll_descr *, lxc_mainloop_close);
{
if (*descr)
lxc_mainloop_close(*descr);
}
#define __do_lxc_mainloop_close \
__attribute__((__cleanup__(__auto_lxc_mainloop_close__)))
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment