chiark / gitweb /
journald: use greedy_realloc in one place
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 11 Oct 2013 23:33:30 +0000 (19:33 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 13 Oct 2013 21:56:54 +0000 (17:56 -0400)
src/journal/journald-server.c

index 29429f6..518e9ec 100644 (file)
@@ -1145,13 +1145,13 @@ int process_event(Server *s, struct epoll_event *ev) {
                 }
 
                 for (;;) {
-                        struct msghdr msghdr;
-                        struct iovec iovec;
                         struct ucred *ucred = NULL;
                         struct timeval *tv = NULL;
                         struct cmsghdr *cmsg;
                         char *label = NULL;
                         size_t label_len = 0;
+
+                        struct iovec iovec;
                         union {
                                 struct cmsghdr cmsghdr;
 
@@ -1168,7 +1168,14 @@ int process_event(Server *s, struct epoll_event *ev) {
                                             CMSG_SPACE(sizeof(struct timeval)) +
                                             CMSG_SPACE(sizeof(int)) + /* fd */
                                             CMSG_SPACE(NAME_MAX)]; /* selinux label */
-                        } control;
+                        } control = {};
+                        struct msghdr msghdr = {
+                                .msg_iov = &iovec,
+                                .msg_iovlen = 1,
+                                .msg_control = &control,
+                                .msg_controllen = sizeof(control),
+                        };
+
                         ssize_t n;
                         int v;
                         int *fds = NULL;
@@ -1179,36 +1186,14 @@ int process_event(Server *s, struct epoll_event *ev) {
                                 return -errno;
                         }
 
-                        if (s->buffer_size < (size_t) v) {
-                                void *b;
-                                size_t l;
-
-                                l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
-                                b = realloc(s->buffer, l+1);
+                        if (!GREEDY_REALLOC(s->buffer, s->buffer_size, LINE_MAX + (size_t) v))
+                                return log_oom();
 
-                                if (!b) {
-                                        log_error("Couldn't increase buffer.");
-                                        return -ENOMEM;
-                                }
-
-                                s->buffer_size = l;
-                                s->buffer = b;
-                        }
-
-                        zero(iovec);
                         iovec.iov_base = s->buffer;
                         iovec.iov_len = s->buffer_size;
 
-                        zero(control);
-                        zero(msghdr);
-                        msghdr.msg_iov = &iovec;
-                        msghdr.msg_iovlen = 1;
-                        msghdr.msg_control = &control;
-                        msghdr.msg_controllen = sizeof(control);
-
                         n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
                         if (n < 0) {
-
                                 if (errno == EINTR || errno == EAGAIN)
                                         return 1;