From 711961ac442d0404f0c4dd0e5d01e9d206392c68 Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Fri, 20 Sep 2019 14:28:59 +0200 Subject: [PATCH] Add meaningful error codes Change-Id: Ic633e51ba49e8e9261ce4e9f33efec8a66f6079a --- src/shared/logretrieve.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/shared/logretrieve.c b/src/shared/logretrieve.c index 21997c7..d18a03c 100644 --- a/src/shared/logretrieve.c +++ b/src/shared/logretrieve.c @@ -28,7 +28,7 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, bool is_pip assert(conf); if (validate_buffers(&enabled_buffers) < 0) - return 1; + return -EINVAL; struct fd_info **fdi_ptrs = calloc(bit_count(enabled_buffers) + 1 /* NULL terminator */, sizeof *fdi_ptrs); if (!fdi_ptrs) @@ -162,8 +162,9 @@ int do_print(struct fd_info **data_fds, int fd_count, write_callback callback, v int epoll_cnt = 0; __attribute__((cleanup(close_fd))) int epollfd = epoll_create1(0); if (epollfd < 0) { + int ret = -errno; DBG("epoll_create failed: %m"); - return -1; + return ret; } __attribute__ ((cleanup(sort_vector_free))) struct sort_vector logs; @@ -175,7 +176,7 @@ int do_print(struct fd_info **data_fds, int fd_count, write_callback callback, v clock_gettime(get_proper_clock(sort_by), &logs.start); if (!sort_vector_finalize(&logs)) { ERR("Error: unable to allocate memory\n"); - return -1; + return -ENOMEM; } for (int nfds = 0; nfds < fd_count; ++nfds) { @@ -187,11 +188,12 @@ int do_print(struct fd_info **data_fds, int fd_count, write_callback callback, v continue; } else if (r < 0) { ERR("Error while preparing for printing: %s", strerror(-r)); - return -1; + return r; } int fd = data_fds[nfds]->fd; - if (fd_set_flags(fd, O_NONBLOCK) < 0) - return -1; + r = fd_set_flags(fd, O_NONBLOCK); + if (r < 0) + return r; struct epoll_event ev = { .data.ptr = data_fds[nfds], .events = EPOLLIN, @@ -203,7 +205,7 @@ int do_print(struct fd_info **data_fds, int fd_count, write_callback callback, v __attribute__((cleanup(free_ptr))) struct epoll_event *evs = calloc(epoll_cnt, sizeof *evs); if (!evs) { ERR("Error: no memory for epoll array"); - return -1; + return -ENOMEM; } while (epoll_cnt > 0) { @@ -212,7 +214,7 @@ int do_print(struct fd_info **data_fds, int fd_count, write_callback callback, v nfds = epoll_wait(epollfd, evs, epoll_cnt, 100); while (nfds < 0 && errno == EINTR); if (nfds < 0) - return -1; + return -errno; for (int i = 0; i < nfds; ++i) { struct fd_info *fdi = evs[i].data.ptr; -- 2.7.4