[lxc-devel] [lxc/master] mainloop, ringbuf: cleanup
brauner on Github
lxc-bot at linuxcontainers.org
Fri Mar 20 17:26:42 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200320/c7658001/attachment-0001.bin>
-------------- next part --------------
From eafc1bb6e60394c18cf4b8214e9fe30e1714d67c Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 20 Mar 2020 18:02:42 +0100
Subject: [PATCH 1/2] mainloop: cleanup
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/cgroups/cgfsng.c | 4 ++--
src/lxc/mainloop.c | 32 ++++++++++++--------------------
src/lxc/mainloop.h | 12 +++---------
3 files changed, 17 insertions(+), 31 deletions(-)
diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index 8edaa54c9f..21abd910dd 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -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)
{
__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;
struct lxc_epoll_descr descr;
struct hierarchy *h;
@@ -1981,7 +1981,7 @@ static int cg_legacy_unfreeze(struct cgroup_ops *ops)
static int cg_unified_unfreeze(struct cgroup_ops *ops, int timeout)
{
__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;
struct lxc_epoll_descr descr;
struct hierarchy *h;
diff --git a/src/lxc/mainloop.c b/src/lxc/mainloop.c
index 2ef5a93e6b..6d4c5935ae 100644
--- a/src/lxc/mainloop.c
+++ b/src/lxc/mainloop.c
@@ -34,7 +34,7 @@ int lxc_mainloop(struct lxc_epoll_descr *descr, int timeout_ms)
if (errno == EINTR)
continue;
- return -1;
+ return -errno;
}
for (i = 0; i < nfds; i++) {
@@ -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,
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 mainloop_handler *handler;
- struct lxc_list *item;
if (fd < 0)
return -1;
@@ -81,19 +81,15 @@ int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
ev.data.ptr = handler;
if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev) < 0)
- goto out_free_handler;
+ return -errno;
item = malloc(sizeof(*item));
if (!item)
- goto out_free_handler;
+ return ret_errno(ENOMEM);
- item->elem = handler;
- lxc_list_add(&descr->handlers, item);
+ item->elem = move_ptr(handler);
+ lxc_list_add(&descr->handlers, move_ptr(item));
return 0;
-
-out_free_handler:
- free(handler);
- return -1;
}
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) {
/* found */
if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
- return -1;
+ return -errno;
lxc_list_del(iterator);
free(iterator->elem);
@@ -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)
{
- /* hint value passed to epoll create */
descr->epfd = epoll_create1(EPOLL_CLOEXEC);
if (descr->epfd < 0)
- return -1;
+ return -errno;
lxc_list_init(&descr->handlers);
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;
@@ -144,8 +139,5 @@ int lxc_mainloop_close(struct lxc_epoll_descr *descr)
iterator = next;
}
- if (descr->epfd >= 0)
- return close(descr->epfd);
-
- return 0;
+ close_prot_errno_disarm(descr->epfd);
}
diff --git a/src/lxc/mainloop.h b/src/lxc/mainloop.h
index a161d52aad..8afac60d35 100644
--- a/src/lxc/mainloop.h
+++ b/src/lxc/mainloop.h
@@ -6,6 +6,7 @@
#include <stdint.h>
#include "list.h"
+#include "memory_utils.h"
#define LXC_MAINLOOP_ERROR -1
#define LXC_MAINLOOP_CONTINUE 0
@@ -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_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)
-{
- if (*descr)
- lxc_mainloop_close(*descr);
-}
-
-#define __do_lxc_mainloop_close \
- __attribute__((__cleanup__(__auto_lxc_mainloop_close__)))
+define_cleanup_function(struct lxc_epoll_descr *, lxc_mainloop_close);
#endif
From 55cf04e373662f58bdb40196a9418c7cc1166430 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 20 Mar 2020 18:18:37 +0100
Subject: [PATCH 2/2] ringbuf: fix cleanup operations
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/ringbuf.c | 6 +-----
src/lxc/ringbuf.h | 3 ++-
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/src/lxc/ringbuf.c b/src/lxc/ringbuf.c
index 0ece4fd263..cb7513a1a6 100644
--- a/src/lxc/ringbuf.c
+++ b/src/lxc/ringbuf.c
@@ -20,9 +20,9 @@
int lxc_ringbuf_create(struct lxc_ringbuf *buf, size_t size)
{
+ __do_close int memfd = -EBADF;
char *tmp;
int ret;
- int memfd = -1;
buf->size = size;
buf->r_off = 0;
@@ -63,14 +63,10 @@ int lxc_ringbuf_create(struct lxc_ringbuf *buf, size_t size)
if (tmp == MAP_FAILED || tmp != (buf->addr + buf->size))
goto on_error;
- close(memfd);
-
return 0;
on_error:
lxc_ringbuf_release(buf);
- if (memfd >= 0)
- close(memfd);
return -1;
}
diff --git a/src/lxc/ringbuf.h b/src/lxc/ringbuf.h
index 49041811fe..dbbc7dacae 100644
--- a/src/lxc/ringbuf.h
+++ b/src/lxc/ringbuf.h
@@ -36,7 +36,8 @@ extern int lxc_ringbuf_read(struct lxc_ringbuf *buf, char *out, size_t *len);
static inline void lxc_ringbuf_release(struct lxc_ringbuf *buf)
{
- munmap(buf->addr, buf->size * 2);
+ if (buf->addr)
+ munmap(buf->addr, buf->size * 2);
}
static inline void lxc_ringbuf_clear(struct lxc_ringbuf *buf)
More information about the lxc-devel
mailing list