mirror of
https://github.com/odin-lang/Odin.git
synced 2026-02-17 08:34:08 +00:00
Make all handles non-inheritable by default
The sockets are left as non-inheritable because they never should be inherited.
This commit is contained in:
@@ -117,7 +117,7 @@ _wrap_os_addr :: proc "contextless" (addr: linux.Sock_Addr_Any)->(Endpoint) {
|
||||
_create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (Any_Socket, Network_Error) {
|
||||
family := _unwrap_os_family(family)
|
||||
proto, socktype := _unwrap_os_proto_socktype(protocol)
|
||||
sock, errno := linux.socket(family, socktype, {}, proto)
|
||||
sock, errno := linux.socket(family, socktype, {.CLOEXEC}, proto)
|
||||
if errno != .NONE {
|
||||
return {}, Create_Socket_Error(errno)
|
||||
}
|
||||
@@ -132,7 +132,7 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
||||
}
|
||||
// Create new TCP socket
|
||||
os_sock: linux.Fd
|
||||
os_sock, errno = linux.socket(_unwrap_os_family(family_from_endpoint(endpoint)), .STREAM, {}, .TCP)
|
||||
os_sock, errno = linux.socket(_unwrap_os_family(family_from_endpoint(endpoint)), .STREAM, {.CLOEXEC}, .TCP)
|
||||
if errno != .NONE {
|
||||
// TODO(flysand): should return invalid file descriptor here casted as TCP_Socket
|
||||
return {}, Create_Socket_Error(errno)
|
||||
@@ -172,7 +172,7 @@ _listen_tcp :: proc(endpoint: Endpoint, backlog := 1000) -> (TCP_Socket, Network
|
||||
ep_address := _unwrap_os_addr(endpoint)
|
||||
// Create TCP socket
|
||||
os_sock: linux.Fd
|
||||
os_sock, errno = linux.socket(ep_family, .STREAM, {}, .TCP)
|
||||
os_sock, errno = linux.socket(ep_family, .STREAM, {.CLOEXEC}, .TCP)
|
||||
if errno != .NONE {
|
||||
// TODO(flysand): should return invalid file descriptor here casted as TCP_Socket
|
||||
return {}, Create_Socket_Error(errno)
|
||||
|
||||
@@ -29,7 +29,7 @@ File_Flag :: enum {
|
||||
Sync,
|
||||
Trunc,
|
||||
Sparse,
|
||||
Close_On_Exec,
|
||||
Inheritable,
|
||||
|
||||
Unbuffered_IO,
|
||||
}
|
||||
@@ -43,7 +43,15 @@ O_EXCL :: File_Flags{.Excl}
|
||||
O_SYNC :: File_Flags{.Sync}
|
||||
O_TRUNC :: File_Flags{.Trunc}
|
||||
O_SPARSE :: File_Flags{.Sparse}
|
||||
O_CLOEXEC :: File_Flags{.Close_On_Exec}
|
||||
|
||||
/*
|
||||
If specified, the file handle is inherited upon the creation of a child
|
||||
process. By default all handles are created non-inheritable.
|
||||
|
||||
**Note**: The standard file handles (stderr, stdout and stdin) are always
|
||||
initialized as inheritable.
|
||||
*/
|
||||
O_INHERITABLE :: File_Flags{.Inheritable}
|
||||
|
||||
stdin: ^File = nil // OS-Specific
|
||||
stdout: ^File = nil // OS-Specific
|
||||
|
||||
@@ -70,7 +70,7 @@ _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, er
|
||||
// Just default to using O_NOCTTY because needing to open a controlling
|
||||
// terminal would be incredibly rare. This has no effect on files while
|
||||
// allowing us to open serial devices.
|
||||
sys_flags: linux.Open_Flags = {.NOCTTY}
|
||||
sys_flags: linux.Open_Flags = {.NOCTTY, .CLOEXEC}
|
||||
switch flags & O_RDONLY|O_WRONLY|O_RDWR {
|
||||
case O_RDONLY:
|
||||
case O_WRONLY: sys_flags += {.WRONLY}
|
||||
@@ -82,7 +82,7 @@ _open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, er
|
||||
if .Excl in flags { sys_flags += {.EXCL} }
|
||||
if .Sync in flags { sys_flags += {.DSYNC} }
|
||||
if .Trunc in flags { sys_flags += {.TRUNC} }
|
||||
if .Close_On_Exec in flags { sys_flags += {.CLOEXEC} }
|
||||
if .Inheritable in flags { sys_flags -= {.CLOEXEC} }
|
||||
|
||||
fd, errno := linux.open(name_cstr, sys_flags, transmute(linux.Mode)(u32(perm)))
|
||||
if errno != .NONE {
|
||||
|
||||
@@ -79,7 +79,7 @@ _open_internal :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (han
|
||||
share_mode := u32(win32.FILE_SHARE_READ | win32.FILE_SHARE_WRITE)
|
||||
sa := win32.SECURITY_ATTRIBUTES {
|
||||
nLength = size_of(win32.SECURITY_ATTRIBUTES),
|
||||
bInheritHandle = .Close_On_Exec not_in flags,
|
||||
bInheritHandle = .Inheritable in flags,
|
||||
}
|
||||
|
||||
create_mode: u32 = win32.OPEN_EXISTING
|
||||
|
||||
Reference in New Issue
Block a user