Fix epoll_ctl erroring out 52/110052/1
authorMichal Bloch <m.bloch@samsung.com>
Thu, 12 Jan 2017 14:49:10 +0000 (15:49 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 12 Jan 2017 15:13:20 +0000 (16:13 +0100)
Do not call epoll_ctl on FDs representing actual files,
which causes the call to error out with EPERM.

This use of this mechanism was twofold; first, pipes
need to actually be added to epoll (to see if they're
full). The second use was to discover whether a descriptor
represents a file. This information is known a priori,
which removes the need to make calls doomed to fail.

Change-Id: Ic11c211ee88e70899998e794847b79afc0a90d9a
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logger/logger.c

index d862706..ca5f006 100644 (file)
@@ -588,20 +588,23 @@ static int print_out_logs(struct reader* reader, struct log_buffer* buffer)
        int priority;
        char * tag;
        struct epoll_event ev = { .events = EPOLLOUT, .data.fd = reader->file.fd };
-       int epoll_fd;
+       int epoll_fd = -1;
        int from = reader->current;
-       int is_file = 0;
        int plaintext = 0;
 
-       epoll_fd = epoll_create1(0);
-       r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, reader->file.fd, &ev);
-       if (r == -1 && errno == EPERM)
-               is_file = 1;
+       if (!reader->file.path) { // pipe, not file
+               epoll_fd = epoll_create1(0);
+               r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, reader->file.fd, &ev);
+               if (r < 0) {
+                       close(epoll_fd);
+                       return 0;
+               }
+       }
 
-       plaintext = is_file && (reader->buf_id == LOG_ID_KMSG);
+       plaintext = reader->file.path && (reader->buf_id == LOG_ID_KMSG);
 
        if (reader->partial_log_size) {
-               if (!is_file && epoll_wait(epoll_fd, &ev, 1, 0) < 1)
+               if (!reader->file.path && epoll_wait(epoll_fd, &ev, 1, 0) < 1)
                        goto cleanup;
 
                do {
@@ -639,7 +642,7 @@ static int print_out_logs(struct reader* reader, struct log_buffer* buffer)
                if (!log_should_print_line(reader->file.format, tag, priority))
                        continue;
 
-               if (!is_file) {
+               if (!reader->file.path) {
                        if (epoll_wait(epoll_fd, &ev, 1, 0) < 1)
                                goto cleanup;
 
@@ -692,7 +695,7 @@ static int print_out_logs(struct reader* reader, struct log_buffer* buffer)
                }
        }
 
-       if (is_file && !plaintext)
+       if (reader->file.path && !plaintext)
                ret = write_blob_to_file(reader, &g_file_buffer);
 
        if (reader->dumpcount)
@@ -701,7 +704,8 @@ static int print_out_logs(struct reader* reader, struct log_buffer* buffer)
                ret = -1;
 
 cleanup:
-       close(epoll_fd);
+       if (!reader->file.path)
+               close(epoll_fd);
        reader->current = from;
        return ret;
 }