chiark / gitweb /
treewide: no need to negate errno for log_*_errno()
[elogind.git] / src / socket-proxy / socket-proxyd.c
index d54a05a0875c5d73c4efa2e933b131caecf4a280..d2d7d350150cb117eed0341e27542f947519a694 100644 (file)
@@ -26,7 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <netdb.h>
-#include <sys/fcntl.h>
+#include <fcntl.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
@@ -47,9 +47,6 @@
 
 static const char *arg_remote_host = NULL;
 
-#define _cleanup_freeaddrinfo_ _cleanup_(freeaddrinfop)
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo *, freeaddrinfo);
-
 typedef struct Context {
         sd_event *event;
         sd_resolve *resolve;
@@ -128,7 +125,7 @@ static int connection_create_pipes(Connection *c, int buffer[2], size_t *sz) {
                 return -errno;
         }
 
-        fcntl(buffer[0], F_SETPIPE_SZ, BUFFER_SIZE);
+        (void) fcntl(buffer[0], F_SETPIPE_SZ, BUFFER_SIZE);
 
         r = fcntl(buffer[0], F_GETPIPE_SZ);
         if (r < 0) {
@@ -269,7 +266,7 @@ static int connection_enable_event_sources(Connection *c) {
                 r = 0;
 
         if (r < 0) {
-                log_error("Failed to set up server event source: %s", strerror(-r));
+                log_error_errno(r, "Failed to set up server event source: %m");
                 return r;
         }
 
@@ -281,7 +278,7 @@ static int connection_enable_event_sources(Connection *c) {
                 r = 0;
 
         if (r < 0) {
-                log_error("Failed to set up client event source: %s", strerror(-r));
+                log_error_errno(r, "Failed to set up client event source: %m");
                 return r;
         }
 
@@ -360,13 +357,13 @@ static int connection_start(Connection *c, struct sockaddr *sa, socklen_t salen)
                 if (errno == EINPROGRESS) {
                         r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, EPOLLOUT, connect_cb, c);
                         if (r < 0) {
-                                log_error("Failed to add connection socket: %s", strerror(-r));
+                                log_error_errno(r, "Failed to add connection socket: %m");
                                 goto fail;
                         }
 
                         r = sd_event_source_set_enabled(c->client_event_source, SD_EVENT_ONESHOT);
                         if (r < 0) {
-                                log_error("Failed to enable oneshot event source: %s", strerror(-r));
+                                log_error_errno(r, "Failed to enable oneshot event source: %m");
                                 goto fail;
                         }
                 } else {
@@ -452,7 +449,7 @@ static int resolve_remote(Connection *c) {
         log_debug("Looking up address info for %s:%s", node, service);
         r = sd_resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_cb, c);
         if (r < 0) {
-                log_error("Failed to resolve remote host: %s", strerror(-r));
+                log_error_errno(r, "Failed to resolve remote host: %m");
                 goto fail;
         }
 
@@ -476,7 +473,7 @@ static int add_connection_socket(Context *context, int fd) {
                 return 0;
         }
 
-        r = set_ensure_allocated(&context->connections, trivial_hash_func, trivial_compare_func);
+        r = set_ensure_allocated(&context->connections, NULL);
         if (r < 0) {
                 log_oom();
                 return 0;
@@ -524,14 +521,14 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat
 
                 r = add_connection_socket(context, nfd);
                 if (r < 0) {
-                        log_error("Failed to accept connection, ignoring: %s", strerror(-r));
+                        log_error_errno(r, "Failed to accept connection, ignoring: %m");
                         safe_close(fd);
                 }
         }
 
         r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
         if (r < 0) {
-                log_error("Error while re-enabling listener with ONESHOT: %s", strerror(-r));
+                log_error_errno(r, "Error while re-enabling listener with ONESHOT: %m");
                 sd_event_exit(context->event, r);
                 return r;
         }
@@ -546,7 +543,7 @@ static int add_listen_socket(Context *context, int fd) {
         assert(context);
         assert(fd >= 0);
 
-        r = set_ensure_allocated(&context->listen, trivial_hash_func, trivial_compare_func);
+        r = set_ensure_allocated(&context->listen, NULL);
         if (r < 0) {
                 log_oom();
                 return r;
@@ -554,7 +551,7 @@ static int add_listen_socket(Context *context, int fd) {
 
         r = sd_is_socket(fd, 0, SOCK_STREAM, 1);
         if (r < 0) {
-                log_error("Failed to determine socket type: %s", strerror(-r));
+                log_error_errno(r, "Failed to determine socket type: %m");
                 return r;
         }
         if (r == 0) {
@@ -564,19 +561,19 @@ static int add_listen_socket(Context *context, int fd) {
 
         r = fd_nonblock(fd, true);
         if (r < 0) {
-                log_error("Failed to mark file descriptor non-blocking: %s", strerror(-r));
+                log_error_errno(r, "Failed to mark file descriptor non-blocking: %m");
                 return r;
         }
 
         r = sd_event_add_io(context->event, &source, fd, EPOLLIN, accept_cb, context);
         if (r < 0) {
-                log_error("Failed to add event source: %s", strerror(-r));
+                log_error_errno(r, "Failed to add event source: %m");
                 return r;
         }
 
         r = set_put(context->listen, source);
         if (r < 0) {
-                log_error("Failed to add source to set: %s", strerror(-r));
+                log_error_errno(r, "Failed to add source to set: %m");
                 sd_event_source_unref(source);
                 return r;
         }
@@ -585,24 +582,20 @@ static int add_listen_socket(Context *context, int fd) {
          * watching to accept(). */
         r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
         if (r < 0) {
-                log_error("Failed to enable oneshot mode: %s", strerror(-r));
+                log_error_errno(r, "Failed to enable oneshot mode: %m");
                 return r;
         }
 
         return 0;
 }
 
-static int help(void) {
-
-        printf("%s [HOST:PORT]\n"
-               "%s [SOCKET]\n\n"
+static void help(void) {
+        printf("%1$s [HOST:PORT]\n"
+               "%1$s [SOCKET]\n\n"
                "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n"
                "  -h --help              Show this help\n"
                "     --version           Show package version\n",
-               program_invocation_short_name,
                program_invocation_short_name);
-
-        return 0;
 }
 
 static int parse_argv(int argc, char *argv[]) {
@@ -623,12 +616,13 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
 
                 switch (c) {
 
                 case 'h':
-                        return help();
+                        help();
+                        return 0;
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
@@ -641,7 +635,6 @@ static int parse_argv(int argc, char *argv[]) {
                 default:
                         assert_not_reached("Unhandled option");
                 }
-        }
 
         if (optind >= argc) {
                 log_error("Not enough parameters.");
@@ -670,19 +663,19 @@ int main(int argc, char *argv[]) {
 
         r = sd_event_default(&context.event);
         if (r < 0) {
-                log_error("Failed to allocate event loop: %s", strerror(-r));
+                log_error_errno(r, "Failed to allocate event loop: %m");
                 goto finish;
         }
 
         r = sd_resolve_default(&context.resolve);
         if (r < 0) {
-                log_error("Failed to allocate resolver: %s", strerror(-r));
+                log_error_errno(r, "Failed to allocate resolver: %m");
                 goto finish;
         }
 
         r = sd_resolve_attach_event(context.resolve, context.event, 0);
         if (r < 0) {
-                log_error("Failed to attach resolver: %s", strerror(-r));
+                log_error_errno(r, "Failed to attach resolver: %m");
                 goto finish;
         }
 
@@ -707,7 +700,7 @@ int main(int argc, char *argv[]) {
 
         r = sd_event_loop(context.event);
         if (r < 0) {
-                log_error("Failed to run event loop: %s", strerror(-r));
+                log_error_errno(r, "Failed to run event loop: %m");
                 goto finish;
         }