]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
master: service-listen - Use dovecot coding standard for systemd code
authorAki Tuomi <aki.tuomi@open-xchange.com>
Tue, 23 Feb 2021 14:08:28 +0000 (16:08 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Wed, 3 Mar 2021 09:53:46 +0000 (09:53 +0000)
src/master/service-listen.c

index 0d81146119dbd7456aa6b52e1b50c6c1880f9c5c..1e121b5b656e0a373660c6d5641c31db5c5f2b22 100644 (file)
@@ -167,7 +167,7 @@ systemd_listen_fd(const struct ip_addr *ip, in_port_t port, int *fd_r)
 
        fd_max = SD_LISTEN_FDS_START + sd_fds - 1;
        for (fd = SD_LISTEN_FDS_START; fd <= fd_max; fd++) {
-               if (sd_is_socket_inet(fd, ip->family, SOCK_STREAM, 1, port)) {
+               if (sd_is_socket_inet(fd, ip->family, SOCK_STREAM, 1, port) > 0) {
                        *fd_r = fd;
                        return 0;
                }
@@ -244,7 +244,7 @@ static int service_listen(struct service *service)
 }
 
 #ifdef HAVE_SYSTEMD
-static int get_socket_info(int fd, unsigned int *family, in_port_t *port)
+static int get_socket_info(int fd, sa_family_t *family_r, in_port_t *port_r)
 {
        union sockaddr_union {
                struct sockaddr sa;
@@ -253,9 +253,10 @@ static int get_socket_info(int fd, unsigned int *family, in_port_t *port)
        } sockaddr;
        socklen_t l;
 
-       // FIXME(Stephan): why -1?
-       if (port) *port = -1;
-       if (family) *family = -1;
+       if (port_r != NULL)
+               *port_r = 0;
+       if (family_r != NULL)
+               *family_r = AF_UNSPEC;
 
        i_zero(&sockaddr);
        l = sizeof(sockaddr);
@@ -263,18 +264,19 @@ static int get_socket_info(int fd, unsigned int *family, in_port_t *port)
        if (getsockname(fd, &sockaddr.sa, &l) < 0)
              return -errno;
 
-       if (family) *family = sockaddr.sa.sa_family;
-       if (port) {
+       if (family_r != NULL)
+               *family_r = sockaddr.sa.sa_family;
+       if (port_r != NULL) {
                if (sockaddr.sa.sa_family == AF_INET) {
                        if (l < sizeof(struct sockaddr_in))
                                return -EINVAL;
 
-                       *port = ntohs(sockaddr.in4.sin_port);
+                       *port_r = ntohs(sockaddr.in4.sin_port);
                } else {
                        if (l < sizeof(struct sockaddr_in6))
                                return -EINVAL;
 
-                       *port = ntohs(sockaddr.in6.sin6_port);
+                       *port_r = ntohs(sockaddr.in6.sin6_port);
                }
        }
        return 0;
@@ -297,9 +299,9 @@ static int services_verify_systemd(struct service_list *service_list)
        fd_max = SD_LISTEN_FDS_START + sd_fds - 1;
        for (fd = SD_LISTEN_FDS_START; fd <= fd_max; fd++) {
                if (sd_is_socket_inet(fd, 0, SOCK_STREAM, 1, 0) > 0) {
-                       int found = FALSE;
+                       bool found = FALSE;
                        in_port_t port;
-                       unsigned int family;
+                       sa_family_t family;
                        get_socket_info(fd, &family, &port);
                        
                        array_foreach(&service_list->services, services) {
@@ -315,14 +317,19 @@ static int services_verify_systemd(struct service_list *service_list)
                                                break;
                                        }
                                }
-                               if (found) break;
+                               if (found)
+                                       break;
                        }
                        if (!found) {
-                               i_error("systemd listens on port %d, but it's not configured in Dovecot. Closing.",port);
-                               if (shutdown(fd, SHUT_RDWR) < 0 && errno != ENOTCONN)
-                                       i_error("shutdown() failed: %m");
+                               i_error("systemd listens on port %d, "
+                                       "but it's not configured in Dovecot. "
+                                       "Closing.", port);
+                               if (shutdown(fd, SHUT_RDWR) < 0 &&
+                                   errno != ENOTCONN)
+                                       i_error("shutdown(%d) failed: %m", fd);
                                if (dup2(dev_null_fd, fd) < 0)
-                                       i_error("dup2() failed: %m");
+                                       i_error("dup2(%d, %d) failed: %m",
+                                               dev_null_fd, fd);
                        }
                }
        }