chiark / gitweb /
move _cleanup_ attribute in front of the type
[elogind.git] / src / nss-myhostname / nss-myhostname.c
index 83180ad80532713dc6bb9ca2c23e21629568b026..16ccb3ee39db42fe95c8da3609411bdb89c01d17 100644 (file)
@@ -1,23 +1,22 @@
 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
-  This file is part of nss-myhostname.
+  This file is part of systemd.
 
   Copyright 2008-2011 Lennart Poettering
 
-  nss-myhostname is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public License
-  as published by the Free Software Foundation; either version 2.1 of
-  the License, or (at your option) any later version.
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
 
-  nss-myhostname is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lesser General Public License for more details.
 
-  You should have received a copy of the GNU Lesser General Public
-  License along with nss-myhostname; If not, see
-  <http://www.gnu.org/licenses/>.
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include <limits.h>
@@ -33,6 +32,8 @@
 #include <arpa/inet.h>
 
 #include "ifconf.h"
+#include "macro.h"
+#include "util.h"
 
 /* We use 127.0.0.2 as IPv4 address. This has the advantage over
  * 127.0.0.1 that it can be translated back to the local hostname. For
@@ -43,8 +44,6 @@
 #define LOCALADDRESS_IPV6 &in6addr_loopback
 #define LOOPBACK_INTERFACE "lo"
 
-#define ALIGN(a) (((a+sizeof(void*)-1)/sizeof(void*))*sizeof(void*))
-
 enum nss_status _nss_myhostname_gethostbyname4_r(
                 const char *name,
                 struct gaih_addrtuple **pat,
@@ -97,14 +96,13 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
                 int32_t *ttlp) {
 
         unsigned lo_ifi;
-        char hn[HOST_NAME_MAX+1];
+        char hn[HOST_NAME_MAX+1] = {};
         size_t l, idx, ms;
         char *r_name;
         struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
         struct address *addresses = NULL, *a;
         unsigned n_addresses = 0, n;
 
-        memset(hn, 0, sizeof(hn));
         if (gethostname(hn, sizeof(hn)-1) < 0) {
                 *errnop = errno;
                 *h_errnop = NO_RECOVERY;
@@ -177,7 +175,11 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
         /* Verify the size matches */
         assert(idx == ms);
 
-        *pat = r_tuple_prev;
+        /* Nscd expects us to store the first record in **pat. */
+        if (*pat)
+                **pat = *r_tuple_prev;
+        else
+                *pat = r_tuple_prev;
 
         if (ttlp)
                 *ttlp = 0;
@@ -309,7 +311,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
                 int32_t *ttlp,
                 char **canonp) {
 
-        char hn[HOST_NAME_MAX+1];
+        char hn[HOST_NAME_MAX+1] = {};
 
         if (af == AF_UNSPEC)
                 af = AF_INET;
@@ -320,7 +322,6 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
                 return NSS_STATUS_UNAVAIL;
         }
 
-        memset(hn, 0, sizeof(hn));
         if (gethostname(hn, sizeof(hn)-1) < 0) {
                 *errnop = errno;
                 *h_errnop = NO_RECOVERY;
@@ -377,8 +378,9 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r(
                 int *errnop, int *h_errnop,
                 int32_t *ttlp) {
 
-        char hn[HOST_NAME_MAX+1];
-        struct address *addresses = NULL, *a;
+        char hn[HOST_NAME_MAX+1] = {};
+        _cleanup_free_ struct address *addresses = NULL;
+        struct address *a;
         unsigned n_addresses = 0, n;
 
         if (len != PROTO_ADDRESS_SIZE(af)) {
@@ -416,13 +418,9 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r(
         *errnop = ENOENT;
         *h_errnop = HOST_NOT_FOUND;
 
-        free(addresses);
         return NSS_STATUS_NOTFOUND;
 
 found:
-        free(addresses);
-
-        memset(hn, 0, sizeof(hn));
         if (gethostname(hn, sizeof(hn)-1) < 0) {
                 *errnop = errno;
                 *h_errnop = NO_RECOVERY;