]> git.sesse.net Git - cubemap/blobdiff - udpinput.cpp
Fix various close-on-exec bugs.
[cubemap] / udpinput.cpp
index a2915e8333dff36c1b3cc1fa2efcb1cfbf1d27cf..873b0b952911a226d8f08255f80e2d092e163801 100644 (file)
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <poll.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -120,6 +121,10 @@ UDPInput::UDPInput(const InputProto &serialized)
        : url(serialized.url()),
          sock(serialized.sock())
 {
+       // Set back the close-on-exec flag for the socket.
+       // (This can't leak into a child, since we haven't been started yet.)
+       fcntl(sock, F_SETFD, O_CLOEXEC);
+
        // Should be verified by the caller.
        string protocol;
        bool ok = parse_url(url, &protocol, &user, &host, &port, &path);
@@ -139,6 +144,10 @@ UDPInput::UDPInput(const InputProto &serialized)
 
 InputProto UDPInput::serialize() const
 {
+       // Unset the close-on-exec flag for the socket.
+       // (This can't leak into a child, since there's only one thread left.)
+       fcntl(sock, F_SETFD, 0);
+
        InputProto serialized;
        serialized.set_url(url);
        serialized.set_sock(sock);
@@ -210,7 +219,7 @@ void UDPInput::do_work()
                        ret = recv(sock, packet_buf, sizeof(packet_buf), 0);
                } while (ret == -1 && errno == EINTR);
 
-               if (ret <= 0) {
+               if (ret < 0) {  // Note that zero-byte packets are legal.
                        log_perror("recv");
                        close_socket();
                        continue;