]> git.sesse.net Git - cubemap/blobdiff - client.cpp
Set close-on-exec on all file descriptors we open.
[cubemap] / client.cpp
index d7bec0986983cb438322941f636a59bd45210003..26806d6fdcf53d88a2ae758adbbbce7c10e1b214 100644 (file)
@@ -1,8 +1,10 @@
 #include <stdio.h>
 #include <arpa/inet.h>
+#include <fcntl.h>
 #include <netinet/in.h>
 #include <stdint.h>
 #include <sys/socket.h>
+#include <unistd.h>
 
 #include "client.h"
 #include "log.h"
@@ -59,6 +61,8 @@ Client::Client(const ClientProto &serialized, const vector<shared_ptr<const stri
          request(serialized.request()),
          url(serialized.url()),
          stream(stream),
+         close_after_response(serialized.close_after_response()),
+         http_11(serialized.http_11()),
          header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
          stream_pos(serialized.stream_pos()),
          stream_pos_end(serialized.stream_pos_end()),
@@ -66,6 +70,10 @@ Client::Client(const ClientProto &serialized, const vector<shared_ptr<const stri
          bytes_lost(serialized.bytes_lost()),
          num_loss_events(serialized.num_loss_events())
 {
+       // 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, 1);
+
        if (stream != nullptr) {
                if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
                        if (stream->pacing_rate != ~0U) {
@@ -113,6 +121,10 @@ Client::Client(const ClientProto &serialized, const vector<shared_ptr<const stri
 
 ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) 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);
+
        ClientProto serialized;
        serialized.set_sock(sock);
        serialized.set_remote_addr(remote_addr);
@@ -132,12 +144,14 @@ ClientProto Client::serialize(unordered_map<const string *, size_t> *short_respo
                serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
        }
 
-       serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
+       serialized.set_header_or_short_response_bytes_sent(header_or_short_response_bytes_sent);
        serialized.set_stream_pos(stream_pos);
        serialized.set_stream_pos_end(stream_pos_end);
        serialized.set_bytes_sent(bytes_sent);
        serialized.set_bytes_lost(bytes_lost);
        serialized.set_num_loss_events(num_loss_events);
+       serialized.set_http_11(http_11);
+       serialized.set_close_after_response(close_after_response);
 
        if (tls_context != nullptr) {
                bool small_version = false;