]> git.ozlabs.org Git - ccan-lca-2011.git/commitdiff
lca2011: simpler testing by not having oserver_setup exit.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 21 Jan 2011 03:41:16 +0000 (14:11 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 21 Jan 2011 03:41:16 +0000 (14:11 +1030)
Note the use of close_noerr here to preserve the errno.

ccan/oserver/_info
ccan/oserver/oserver.c

index b906502beba1d4579f1fdac53ab6ed3a226fb1ae..a9c74fc961c698afcd6742e0db6371570ba44138 100644 (file)
@@ -20,6 +20,8 @@
  *             int fd, sockfd;
  *
  *             sockfd = oserver_setup();
+ *             if (sockfd < 0)
+ *                     err(1, "Failed to set up server socket");
  *
  *             fd = accept(sockfd, NULL, NULL);
  *             if (fd < 0)
@@ -40,6 +42,7 @@ int main(int argc, char *argv[])
                printf("ccan/read_write_all\n");
                printf("ccan/str\n");
                printf("ccan/foreach\n");
+               printf("ccan/noerr\n");
                return 0;
        }
 
index 5f120cbf2bd4342a3ae34cc7ee8a57e74ebc16fa..b7495f307f6f552b93246d9975af2eefdd74cb27 100644 (file)
@@ -1,5 +1,6 @@
 #include <ccan/oserver/oserver.h>
 #include <ccan/read_write_all/read_write_all.h>
+#include <ccan/noerr/noerr.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -47,7 +48,7 @@ int oserver_setup(void)
 
        sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sockfd < 0)
-               err(1, "Creating TCP socket");
+               return -1;
 
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
                warn("Setting socket reuse");
@@ -55,10 +56,14 @@ int oserver_setup(void)
        u.in.sin_family = AF_INET;
        u.in.sin_port = htons(OSERVER_PORT);
        u.in.sin_addr.s_addr = INADDR_ANY;
-       if (bind(sockfd, &u.addr, sizeof(u.in)) == -1)
-               err(1, "Binding TCP socket");
+       if (bind(sockfd, &u.addr, sizeof(u.in)) == -1) {
+               close_noerr(sockfd);
+               return -1;
+       }
 
-       if (listen(sockfd, 0) != 0)
-               err(1, "Listening on TCP socket");
+       if (listen(sockfd, 0) != 0) {
+               close_noerr(sockfd);
+               return -1;
+       }
        return sockfd;
 }