diff seobeo/s_network.c @ 244:b8aa08503378

[tools] Add sandboxed online LaTeX editor Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Mon, 03 Aug 2026 16:56:25 -0700
parents 0e7b9464248d
children 117c4d53c9a4
line wrap: on
line diff
--- a/seobeo/s_network.c	Mon Aug 03 15:26:44 2026 -0700
+++ b/seobeo/s_network.c	Mon Aug 03 16:56:25 2026 -0700
@@ -17,6 +17,13 @@
 #endif
 }
 
+static void Seobeo_Socket_Set_Close_On_Exec(int socket_fd)
+{
+  int flags = fcntl(socket_fd, F_GETFD, 0);
+  if (flags >= 0)
+    fcntl(socket_fd, F_SETFD, flags | FD_CLOEXEC);
+}
+
 static ssize_t Seobeo_Socket_Write(
     int socket_fd,
     const void *buffer,
@@ -52,9 +59,14 @@
     free_server_info = free_server_info->ai_next
   )
   {
+    int socket_type = free_server_info->ai_socktype;
+#ifdef SOCK_CLOEXEC
+    socket_type |= SOCK_CLOEXEC;
+#endif
     if((socket_fd = socket(free_server_info->ai_family,
-                        free_server_info->ai_socktype, free_server_info->ai_protocol)) == -1)
+                        socket_type, free_server_info->ai_protocol)) == -1)
     { perror("socket"); continue; }
+    Seobeo_Socket_Set_Close_On_Exec(socket_fd);
     Seobeo_Socket_Disable_Sigpipe(socket_fd);
 
      if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
@@ -126,9 +138,14 @@
   { perror("getaddrinfo"); return NULL; }
 
 
+  int socket_type = server_infos->ai_socktype;
+#ifdef SOCK_CLOEXEC
+  socket_type |= SOCK_CLOEXEC;
+#endif
   if((socket_fd = socket(server_infos->ai_family,
-                      server_infos->ai_socktype, server_infos->ai_protocol)) == -1)
+                      socket_type, server_infos->ai_protocol)) == -1)
   { perror("socket"); return NULL; }
+  Seobeo_Socket_Set_Close_On_Exec(socket_fd);
   Seobeo_Socket_Disable_Sigpipe(socket_fd);
 
   if (connect(socket_fd, server_infos->ai_addr, server_infos->ai_addrlen) != 0)
@@ -178,14 +195,16 @@
   socklen_t addrlen = sizeof addr;
   char client_inet_addr[INET6_ADDRSTRLEN];
 
-  int client_fd = accept(p_server_handle->socket,
-                         (struct sockaddr*)&addr,
-                         &addrlen);
+  int client_fd = accept(
+      p_server_handle->socket,
+      (struct sockaddr*)&addr,
+      &addrlen);
   inet_ntop(
       addr.ss_family,
       Seobeo_Get_IP4_Or_IP6((struct sockaddr *)&addr),
       client_inet_addr, sizeof client_inet_addr);
   if (client_fd == -1) return NULL;
+  Seobeo_Socket_Set_Close_On_Exec(client_fd);
   Seobeo_Socket_Disable_Sigpipe(client_fd);
 
   // Set non blocking...