]> git.sesse.net Git - cubemap/blobdiff - udpinput.cpp
Fix various close-on-exec bugs.
[cubemap] / udpinput.cpp
index 0035640ad4a91bd6ed2d4d59f3a6db8feb677a6d..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;
@@ -223,7 +232,7 @@ void UDPInput::do_work()
                }
        
                for (size_t stream_index : stream_indices) {    
-                       servers->add_data(stream_index, packet_buf, ret, /*metacube_flags=*/0);
+                       servers->add_data(stream_index, packet_buf, ret, /*metacube_flags=*/0, /*pts=*/RationalPTS());
                }
        }
 }