]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-resolve/test-resolve.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / libsystemd / sd-resolve / test-resolve.c
index 33ef6fc0f7469c34863f964bdcf7ac042b240744..880d224a3871ecb0940d44e738cf53a7061c48e1 100644 (file)
@@ -1,21 +1,6 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2005-2008 Lennart Poettering
-  Copyright 2014 Daniel Buch
-
-  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.
-
-  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 systemd; If not, see <http://www.gnu.org/licenses/>.
+  Copyright © 2014 Daniel Buch
 ***/
 
 #include <arpa/inet.h>
@@ -33,6 +18,8 @@
 #include "socket-util.h"
 #include "string-util.h"
 
+#define TEST_TIMEOUT_USEC (20*USEC_PER_SEC)
+
 static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
         const struct addrinfo *i;
 
@@ -63,7 +50,7 @@ static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, c
                 return 0;
         }
 
-        printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
+        printf("Host: %s  Serv: %s\n", strna(host), strna(serv));
         return 0;
 }
 
@@ -86,7 +73,9 @@ int main(int argc, char *argv[]) {
         assert_se(sd_resolve_default(&resolve) >= 0);
 
         /* Test a floating resolver query */
-        sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
+        r = sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
+        if (r < 0)
+                log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
 
         /* Make a name -> address query */
         r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
@@ -101,9 +90,17 @@ int main(int argc, char *argv[]) {
 
         /* Wait until all queries are completed */
         for (;;) {
-                r = sd_resolve_wait(resolve, (uint64_t) -1);
+                r = sd_resolve_wait(resolve, TEST_TIMEOUT_USEC);
                 if (r == 0)
                         break;
+                if (r == -ETIMEDOUT) {
+                        /* Let's catch time-outs here, so that we can run safely in a CI that has no reliable DNS. Note
+                         * that we invoke exit() directly here, as the stuck NSS call will not allow us to exit
+                         * cleanly. */
+
+                        log_notice_errno(r, "sd_resolve_wait() timed out, but that's OK");
+                        exit(EXIT_SUCCESS);
+                }
                 if (r < 0) {
                         log_error_errno(r, "sd_resolve_wait(): %m");
                         assert_not_reached("sd_resolve_wait() failed");