2018-02-24 03:59:21 +08:00
|
|
|
/* fhandler_socket_unix.cc.
|
|
|
|
|
|
|
|
See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2020-09-30 04:18:15 +08:00
|
|
|
#include "winsup.h"
|
|
|
|
|
|
|
|
GUID __cygwin_socket_guid = {
|
|
|
|
.Data1 = 0xefc1714d,
|
|
|
|
.Data2 = 0x7b19,
|
|
|
|
.Data3 = 0x4407,
|
|
|
|
.Data4 = { 0xba, 0xb3, 0xc5, 0xb1, 0xf9, 0x2c, 0xb8, 0x8c }
|
|
|
|
};
|
|
|
|
|
2018-06-26 22:31:17 +08:00
|
|
|
#ifdef __WITH_AF_UNIX
|
|
|
|
|
2018-03-01 01:56:13 +08:00
|
|
|
#include <w32api/winioctl.h>
|
2018-02-24 03:59:21 +08:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <unistd.h>
|
2018-03-01 01:56:13 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2018-02-24 03:59:21 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <cygwin/acl.h>
|
2018-03-01 01:56:13 +08:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
2018-03-03 01:14:11 +08:00
|
|
|
#include "cygheap.h"
|
2018-03-01 01:56:13 +08:00
|
|
|
#include "shared_info.h"
|
2018-02-24 03:59:21 +08:00
|
|
|
#include "ntdll.h"
|
|
|
|
#include "miscfuncs.h"
|
|
|
|
#include "tls_pbuf.h"
|
|
|
|
|
2018-03-03 01:14:11 +08:00
|
|
|
/*
|
|
|
|
Abstract socket:
|
2018-02-24 03:59:21 +08:00
|
|
|
|
2018-03-03 01:14:11 +08:00
|
|
|
An abstract socket is represented by a symlink in the native
|
2018-08-24 19:36:06 +08:00
|
|
|
NT namespace, within the Cygwin subdir in BasedNamedObjects.
|
2018-03-03 01:14:11 +08:00
|
|
|
So it's globally available but only exists as long as at least on
|
|
|
|
descriptor on the socket is open, as desired.
|
|
|
|
|
|
|
|
The name of the symlink is: "af-unix-<sun_path>"
|
|
|
|
|
|
|
|
<sun_path> is the transposed sun_path string, including the leading
|
|
|
|
NUL. The transposition is simplified in that it uses every byte
|
|
|
|
in the valid sun_path name as is, no extra multibyte conversion.
|
2018-03-07 01:37:09 +08:00
|
|
|
The content of the symlink is the basename of the underlying pipe.
|
2018-03-03 01:14:11 +08:00
|
|
|
|
|
|
|
Named socket:
|
|
|
|
|
|
|
|
A named socket is represented by a reparse point with a Cygwin-specific
|
2018-03-07 01:37:09 +08:00
|
|
|
tag and GUID. The GenericReparseBuffer content is the basename of the
|
2018-03-03 01:14:11 +08:00
|
|
|
underlying pipe.
|
|
|
|
|
|
|
|
Pipe:
|
|
|
|
|
|
|
|
The pipe is named \\.\pipe\cygwin-<installation_key>-unix-[sd]-<uniq_id>
|
|
|
|
|
|
|
|
- <installation_key> is the 8 byte hex Cygwin installation key
|
|
|
|
- [sd] is s for SOCK_STREAM, d for SOCK_DGRAM
|
|
|
|
- <uniq_id> is an 8 byte hex unique number
|
|
|
|
|
2018-03-07 01:42:12 +08:00
|
|
|
Note: We use MAX_PATH below for convenience where sufficient. It's
|
2018-03-19 03:46:43 +08:00
|
|
|
big enough to hold sun_paths as well as pipe names as well as packet
|
|
|
|
headers etc., so we don't have to use tmp_pathbuf as often.
|
2018-03-07 01:55:03 +08:00
|
|
|
|
|
|
|
Every packet sent to a peer is a combination of the socket name of the
|
|
|
|
local socket, the ancillary data, and the actual user data. The data
|
|
|
|
is always sent in this order. The header contains length information
|
|
|
|
for the entire packet, as well as for all three data blocks. The
|
|
|
|
combined maximum size of a packet is 64K, including the header.
|
|
|
|
|
2018-03-07 22:43:26 +08:00
|
|
|
A connecting, bound STREAM socket sends it's local sun_path once after
|
2018-03-07 01:55:03 +08:00
|
|
|
a successful connect. An already connected socket also sends its local
|
|
|
|
sun_path after a successful bind (border case, but still...). These
|
|
|
|
packages don't contain any other data (cmsg_len == 0, data_len == 0).
|
|
|
|
|
2018-03-07 22:43:26 +08:00
|
|
|
A bound DGRAM socket sends its sun_path with each sendmsg/sendto.
|
2018-03-03 01:14:11 +08:00
|
|
|
*/
|
2018-03-07 01:55:03 +08:00
|
|
|
class af_unix_pkt_hdr_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint16_t pckt_len; /* size of packet including header */
|
2018-03-19 03:46:43 +08:00
|
|
|
bool admin_pkg : 1; /* admin packets are marked as such */
|
|
|
|
shut_state shut_info : 2; /* _SHUT_RECV /_SHUT_SEND. */
|
2018-03-07 01:55:03 +08:00
|
|
|
uint8_t name_len; /* size of name, a sockaddr_un */
|
|
|
|
uint16_t cmsg_len; /* size of ancillary data block */
|
|
|
|
uint16_t data_len; /* size of user data */
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
af_unix_pkt_hdr_t (bool a, shut_state s, uint8_t n, uint16_t c, uint16_t d)
|
|
|
|
{ init (a, s, n, c, d); }
|
|
|
|
void init (bool a, shut_state s, uint8_t n, uint16_t c, uint16_t d)
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
admin_pkg = a;
|
2018-03-07 01:55:03 +08:00
|
|
|
shut_info = s;
|
|
|
|
name_len = n;
|
|
|
|
cmsg_len = c;
|
|
|
|
data_len = d;
|
|
|
|
pckt_len = sizeof (*this) + name_len + cmsg_len + data_len;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define AF_UNIX_PKT_OFFSETOF_NAME(phdr) \
|
|
|
|
(sizeof (af_unix_pkt_hdr_t))
|
|
|
|
#define AF_UNIX_PKT_OFFSETOF_CMSG(phdr) \
|
|
|
|
(sizeof (af_unix_pkt_hdr_t) + (phdr)->name_len)
|
|
|
|
#define AF_UNIX_PKT_OFFSETOF_DATA(phdr) \
|
|
|
|
({ \
|
|
|
|
af_unix_pkt_hdr_t *_p = phdr; \
|
|
|
|
sizeof (af_unix_pkt_hdr_t) + (_p)->name_len + (_p)->cmsg_len; \
|
|
|
|
})
|
|
|
|
#define AF_UNIX_PKT_NAME(phdr) \
|
|
|
|
({ \
|
|
|
|
af_unix_pkt_hdr_t *_p = phdr; \
|
|
|
|
(struct sockaddr_un *)(((PBYTE)(_p)) \
|
|
|
|
+ AF_UNIX_PKT_OFFSETOF_NAME (_p)); \
|
|
|
|
})
|
|
|
|
#define AF_UNIX_PKT_CMSG(phdr) \
|
|
|
|
({ \
|
|
|
|
af_unix_pkt_hdr_t *_p = phdr; \
|
2018-03-19 03:46:43 +08:00
|
|
|
(struct cmsghdr *)(((PBYTE)(_p)) + AF_UNIX_PKT_OFFSETOF_CMSG (_p)); \
|
2018-03-07 01:55:03 +08:00
|
|
|
})
|
|
|
|
#define AF_UNIX_PKT_DATA(phdr) \
|
|
|
|
({ \
|
|
|
|
af_unix_pkt_hdr_t _p = phdr; \
|
|
|
|
(void *)(((PBYTE)(_p)) + AF_UNIX_PKT_OFFSETOF_DATA (_p)); \
|
|
|
|
})
|
|
|
|
|
2018-03-07 23:12:55 +08:00
|
|
|
/* Some error conditions on pipes have multiple status codes, unfortunately. */
|
|
|
|
#define STATUS_PIPE_NO_INSTANCE_AVAILABLE(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_INSTANCE_NOT_AVAILABLE \
|
|
|
|
|| _s == STATUS_PIPE_NOT_AVAILABLE \
|
|
|
|
|| _s == STATUS_PIPE_BUSY; })
|
|
|
|
|
|
|
|
#define STATUS_PIPE_IS_CLOSING(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_PIPE_CLOSING \
|
|
|
|
|| _s == STATUS_PIPE_EMPTY; })
|
|
|
|
|
|
|
|
#define STATUS_PIPE_INVALID(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_INVALID_INFO_CLASS \
|
|
|
|
|| _s == STATUS_INVALID_PIPE_STATE \
|
|
|
|
|| _s == STATUS_INVALID_READ_MODE; })
|
|
|
|
|
|
|
|
#define STATUS_PIPE_MORE_DATA(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_BUFFER_OVERFLOW \
|
|
|
|
|| _s == STATUS_MORE_PROCESSING_REQUIRED; })
|
|
|
|
|
2018-03-08 04:55:34 +08:00
|
|
|
/* Default timeout value of connect: 20 secs, as on Linux. */
|
|
|
|
#define AF_UNIX_CONNECT_TIMEOUT (-20 * NS100PERSEC)
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
void
|
|
|
|
sun_name_t::set (const struct sockaddr_un *name, socklen_t namelen)
|
2018-03-06 01:08:47 +08:00
|
|
|
{
|
|
|
|
if (namelen < 0)
|
|
|
|
namelen = 0;
|
|
|
|
un_len = namelen < (__socklen_t) sizeof un ? namelen : sizeof un;
|
2018-03-19 03:46:43 +08:00
|
|
|
un.sun_family = AF_UNIX;
|
|
|
|
if (name && un_len)
|
2018-03-06 01:08:47 +08:00
|
|
|
memcpy (&un, name, un_len);
|
2018-03-19 03:46:43 +08:00
|
|
|
_nul[un_len] = '\0';
|
2018-03-06 01:08:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 23:00:36 +08:00
|
|
|
static HANDLE
|
|
|
|
create_event ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
HANDLE evt = NULL;
|
|
|
|
|
|
|
|
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
|
|
|
|
status = NtCreateEvent (&evt, EVENT_ALL_ACCESS, &attr,
|
|
|
|
NotificationEvent, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2018-03-12 22:26:12 +08:00
|
|
|
/* Called from socket, socketpair, accept4 */
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::create_shmem ()
|
|
|
|
{
|
|
|
|
HANDLE sect;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
LARGE_INTEGER size = { .QuadPart = sizeof (af_unix_shmem_t) };
|
|
|
|
SIZE_T viewsize = sizeof (af_unix_shmem_t);
|
|
|
|
PVOID addr = NULL;
|
|
|
|
|
|
|
|
InitializeObjectAttributes (&attr, NULL, OBJ_INHERIT, NULL, NULL);
|
|
|
|
status = NtCreateSection (§, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY
|
|
|
|
| SECTION_MAP_READ | SECTION_MAP_WRITE,
|
|
|
|
&attr, &size, PAGE_READWRITE, SEC_COMMIT, NULL);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
status = NtMapViewOfSection (sect, NtCurrentProcess (), &addr, 0, viewsize,
|
|
|
|
NULL, &viewsize, ViewShare, 0, PAGE_READWRITE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
NtClose (sect);
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
shmem_handle = sect;
|
|
|
|
shmem = (af_unix_shmem_t *) addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called from dup, fixup_after_fork. Expects shmem_handle to be
|
|
|
|
valid. */
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::reopen_shmem ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
SIZE_T viewsize = PAGESIZE;
|
|
|
|
PVOID addr = NULL;
|
|
|
|
|
|
|
|
status = NtMapViewOfSection (shmem_handle, NtCurrentProcess (), &addr, 0,
|
|
|
|
PAGESIZE, NULL, &viewsize, ViewShare, 0,
|
|
|
|
PAGE_READWRITE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
shmem = (af_unix_shmem_t *) addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-07 23:00:36 +08:00
|
|
|
/* Character length of pipe name, excluding trailing NUL. */
|
|
|
|
#define CYGWIN_PIPE_SOCKET_NAME_LEN 47
|
|
|
|
|
2018-03-07 01:37:09 +08:00
|
|
|
/* Character position encoding the socket type in a pipe name. */
|
|
|
|
#define CYGWIN_PIPE_SOCKET_TYPE_POS 29
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_socket_unix::gen_pipe_name ()
|
|
|
|
{
|
|
|
|
WCHAR pipe_name_buf[CYGWIN_PIPE_SOCKET_NAME_LEN + 1];
|
|
|
|
UNICODE_STRING pipe_name;
|
|
|
|
|
|
|
|
__small_swprintf (pipe_name_buf, L"cygwin-%S-unix-%C-%016_X",
|
|
|
|
&cygheap->installation_key,
|
|
|
|
get_type_char (),
|
2018-03-11 21:54:20 +08:00
|
|
|
get_unique_id ());
|
2018-03-07 01:37:09 +08:00
|
|
|
RtlInitUnicodeString (&pipe_name, pipe_name_buf);
|
|
|
|
pc.set_nt_native_path (&pipe_name);
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:07:39 +08:00
|
|
|
HANDLE
|
2018-03-03 01:14:11 +08:00
|
|
|
fhandler_socket_unix::create_abstract_link (const sun_name_t *sun,
|
|
|
|
PUNICODE_STRING pipe_name)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-03 01:14:11 +08:00
|
|
|
WCHAR name[MAX_PATH];
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
HANDLE fh = NULL;
|
|
|
|
|
|
|
|
PWCHAR p = wcpcpy (name, L"af-unix-");
|
|
|
|
/* NUL bytes have no special meaning in an abstract socket name, so
|
|
|
|
we assume iso-8859-1 for simplicity and transpose the string.
|
|
|
|
transform_chars_af_unix is doing just that. */
|
2018-03-11 04:07:46 +08:00
|
|
|
p = transform_chars_af_unix (p, sun->un.sun_path, sun->un_len);
|
|
|
|
*p = L'\0';
|
2018-03-03 01:14:11 +08:00
|
|
|
RtlInitUnicodeString (&uname, name);
|
|
|
|
InitializeObjectAttributes (&attr, &uname, OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (), NULL);
|
|
|
|
/* Fill symlink with name of pipe */
|
|
|
|
status = NtCreateSymbolicLinkObject (&fh, SYMBOLIC_LINK_ALL_ACCESS,
|
|
|
|
&attr, pipe_name);
|
|
|
|
if (!NT_SUCCESS (status))
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-03 01:14:11 +08:00
|
|
|
if (status == STATUS_OBJECT_NAME_EXISTS
|
|
|
|
|| status == STATUS_OBJECT_NAME_COLLISION)
|
|
|
|
set_errno (EADDRINUSE);
|
|
|
|
else
|
|
|
|
__seterrno_from_nt_status (status);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
2018-03-03 01:14:11 +08:00
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rep_pipe_name_t
|
|
|
|
{
|
|
|
|
USHORT Length;
|
|
|
|
WCHAR PipeName[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_socket_unix::create_reparse_point (const sun_name_t *sun,
|
|
|
|
PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
ULONG access;
|
|
|
|
HANDLE old_trans = NULL, trans = NULL;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE fh = NULL;
|
|
|
|
PREPARSE_GUID_DATA_BUFFER rp;
|
|
|
|
rep_pipe_name_t *rep_pipe_name;
|
|
|
|
|
2018-03-07 01:42:12 +08:00
|
|
|
const DWORD data_len = offsetof (rep_pipe_name_t, PipeName)
|
|
|
|
+ pipe_name->Length + sizeof (WCHAR);
|
2018-02-24 03:59:21 +08:00
|
|
|
|
2018-03-03 01:14:11 +08:00
|
|
|
path_conv pc (sun->un.sun_path, PC_SYM_FOLLOW);
|
2018-02-24 03:59:21 +08:00
|
|
|
if (pc.error)
|
|
|
|
{
|
|
|
|
set_errno (pc.error);
|
2018-03-03 01:14:11 +08:00
|
|
|
return NULL;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
2018-03-03 01:14:11 +08:00
|
|
|
if (pc.exists ())
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-03 01:14:11 +08:00
|
|
|
set_errno (EADDRINUSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* We will overwrite the DACL after the call to NtCreateFile. This
|
|
|
|
requires READ_CONTROL and WRITE_DAC access, otherwise get_file_sd
|
|
|
|
and set_file_sd both have to open the file again.
|
|
|
|
FIXME: On remote NTFS shares open sometimes fails because even the
|
|
|
|
creator of the file doesn't have the right to change the DACL.
|
|
|
|
I don't know what setting that is or how to recognize such a share,
|
|
|
|
so for now we don't request WRITE_DAC on remote drives. */
|
|
|
|
access = DELETE | FILE_GENERIC_WRITE;
|
|
|
|
if (!pc.isremote ())
|
|
|
|
access |= READ_CONTROL | WRITE_DAC | WRITE_OWNER;
|
|
|
|
if ((pc.fs_flags () & FILE_SUPPORTS_TRANSACTIONS))
|
|
|
|
start_transaction (old_trans, trans);
|
|
|
|
|
|
|
|
retry_after_transaction_error:
|
|
|
|
status = NtCreateFile (&fh, DELETE | FILE_GENERIC_WRITE,
|
|
|
|
pc.get_object_attr (attr, sec_none_nih), &io,
|
|
|
|
NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT
|
|
|
|
| FILE_NON_DIRECTORY_FILE
|
|
|
|
| FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_OPEN_REPARSE_POINT,
|
|
|
|
NULL, 0);
|
|
|
|
if (NT_TRANSACTIONAL_ERROR (status) && trans)
|
|
|
|
{
|
|
|
|
stop_transaction (status, old_trans, trans);
|
|
|
|
goto retry_after_transaction_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
if (io.Information == FILE_EXISTS)
|
|
|
|
set_errno (EADDRINUSE);
|
|
|
|
else
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rp = (PREPARSE_GUID_DATA_BUFFER)
|
|
|
|
alloca (REPARSE_GUID_DATA_BUFFER_HEADER_SIZE + data_len);
|
|
|
|
rp->ReparseTag = IO_REPARSE_TAG_CYGUNIX;
|
|
|
|
rp->ReparseDataLength = data_len;
|
|
|
|
rp->Reserved = 0;
|
|
|
|
memcpy (&rp->ReparseGuid, CYGWIN_SOCKET_GUID, sizeof (GUID));
|
|
|
|
rep_pipe_name = (rep_pipe_name_t *) rp->GenericReparseBuffer.DataBuffer;
|
|
|
|
rep_pipe_name->Length = pipe_name->Length;
|
|
|
|
memcpy (rep_pipe_name->PipeName, pipe_name->Buffer, pipe_name->Length);
|
|
|
|
rep_pipe_name->PipeName[pipe_name->Length / sizeof (WCHAR)] = L'\0';
|
|
|
|
status = NtFsControlFile (fh, NULL, NULL, NULL, &io,
|
|
|
|
FSCTL_SET_REPARSE_POINT, rp,
|
|
|
|
REPARSE_GUID_DATA_BUFFER_HEADER_SIZE
|
|
|
|
+ rp->ReparseDataLength, NULL, 0);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
2018-03-07 01:42:12 +08:00
|
|
|
mode_t perms = (S_IRWXU | S_IRWXG | S_IRWXO) & ~cygheap->umask;
|
|
|
|
set_created_file_access (fh, pc, perms);
|
2018-03-03 01:14:11 +08:00
|
|
|
NtClose (fh);
|
|
|
|
/* We don't have to keep the file open, but the caller needs to
|
|
|
|
get a value != NULL to know the file creation went fine. */
|
|
|
|
fh = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
else if (!trans)
|
|
|
|
{
|
|
|
|
FILE_DISPOSITION_INFORMATION fdi = { TRUE };
|
|
|
|
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
status = NtSetInformationFile (fh, &io, &fdi, sizeof fdi,
|
|
|
|
FileDispositionInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("Setting delete dispostion failed, status = %y",
|
|
|
|
status);
|
|
|
|
NtClose (fh);
|
|
|
|
fh = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (trans)
|
|
|
|
stop_transaction (status, old_trans, trans);
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_socket_unix::create_file (const sun_name_t *sun)
|
|
|
|
{
|
|
|
|
if (sun->un_len <= (socklen_t) sizeof (sa_family_t)
|
2018-03-07 01:42:12 +08:00
|
|
|
|| (sun->un_len == 3 && sun->un.sun_path[0] == '\0'))
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (sun->un.sun_path[0] == '\0')
|
|
|
|
return create_abstract_link (sun, pc.get_nt_native_path ());
|
|
|
|
return create_reparse_point (sun, pc.get_nt_native_path ());
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::open_abstract_link (sun_name_t *sun,
|
|
|
|
PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
WCHAR name[MAX_PATH];
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
HANDLE fh;
|
|
|
|
|
|
|
|
PWCHAR p = wcpcpy (name, L"af-unix-");
|
|
|
|
p = transform_chars_af_unix (p, sun->un.sun_path, sun->un_len);
|
|
|
|
*p = L'\0';
|
|
|
|
RtlInitUnicodeString (&uname, name);
|
|
|
|
InitializeObjectAttributes (&attr, &uname, OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (), NULL);
|
|
|
|
status = NtOpenSymbolicLinkObject (&fh, SYMBOLIC_LINK_QUERY, &attr);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
2018-02-24 03:59:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-03 01:14:11 +08:00
|
|
|
if (pipe_name)
|
|
|
|
status = NtQuerySymbolicLinkObject (fh, pipe_name, NULL);
|
|
|
|
NtClose (fh);
|
|
|
|
if (pipe_name)
|
|
|
|
{
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Enforce NUL-terminated pipe name. */
|
|
|
|
pipe_name->Buffer[pipe_name->Length / sizeof (WCHAR)] = L'\0';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::open_reparse_point (sun_name_t *sun,
|
|
|
|
PUNICODE_STRING pipe_name)
|
|
|
|
{
|
2018-02-24 03:59:21 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE fh;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
2018-03-03 01:14:11 +08:00
|
|
|
PREPARSE_GUID_DATA_BUFFER rp;
|
|
|
|
tmp_pathbuf tp;
|
2018-02-24 03:59:21 +08:00
|
|
|
|
2018-03-03 01:14:11 +08:00
|
|
|
path_conv pc (sun->un.sun_path, PC_SYM_FOLLOW);
|
|
|
|
if (pc.error)
|
|
|
|
{
|
|
|
|
set_errno (pc.error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!pc.exists ())
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
pc.get_object_attr (attr, sec_none_nih);
|
|
|
|
do
|
|
|
|
{
|
2018-03-07 01:42:12 +08:00
|
|
|
status = NtOpenFile (&fh, FILE_GENERIC_READ, &attr, &io,
|
2018-02-24 03:59:21 +08:00
|
|
|
FILE_SHARE_VALID_FLAGS,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT
|
2018-03-03 01:14:11 +08:00
|
|
|
| FILE_NON_DIRECTORY_FILE
|
2018-02-24 03:59:21 +08:00
|
|
|
| FILE_OPEN_FOR_BACKUP_INTENT
|
2018-03-03 01:14:11 +08:00
|
|
|
| FILE_OPEN_REPARSE_POINT);
|
2018-02-24 03:59:21 +08:00
|
|
|
if (status == STATUS_SHARING_VIOLATION)
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
/* While we hope that the sharing violation is only temporary, we
|
|
|
|
also could easily get stuck here, waiting for a file in use by
|
|
|
|
some greedy Win32 application. Therefore we should never wait
|
|
|
|
endlessly without checking for signals and thread cancel event. */
|
|
|
|
pthread_testcancel ();
|
|
|
|
if (cygwait (NULL, cw_nowait, cw_sig_eintr) == WAIT_SIGNALED
|
|
|
|
&& !_my_tls.call_signal_handler ())
|
|
|
|
{
|
|
|
|
set_errno (EINTR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
yield ();
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
else if (!NT_SUCCESS (status))
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
while (status == STATUS_SHARING_VIOLATION);
|
2018-03-03 01:14:11 +08:00
|
|
|
rp = (PREPARSE_GUID_DATA_BUFFER) tp.c_get ();
|
|
|
|
status = NtFsControlFile (fh, NULL, NULL, NULL, &io, FSCTL_GET_REPARSE_POINT,
|
|
|
|
NULL, 0, rp, MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
|
2018-02-24 03:59:21 +08:00
|
|
|
NtClose (fh);
|
2018-03-03 01:14:11 +08:00
|
|
|
if (rp->ReparseTag == IO_REPARSE_TAG_CYGUNIX
|
|
|
|
&& memcmp (CYGWIN_SOCKET_GUID, &rp->ReparseGuid, sizeof (GUID)) == 0)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-03 01:14:11 +08:00
|
|
|
if (pipe_name)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-03 01:14:11 +08:00
|
|
|
rep_pipe_name_t *rep_pipe_name = (rep_pipe_name_t *)
|
|
|
|
rp->GenericReparseBuffer.DataBuffer;
|
|
|
|
pipe_name->Length = rep_pipe_name->Length;
|
|
|
|
/* pipe name in reparse point is NUL-terminated */
|
|
|
|
memcpy (pipe_name->Buffer, rep_pipe_name->PipeName,
|
|
|
|
rep_pipe_name->Length + sizeof (WCHAR));
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:14:11 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::open_file (sun_name_t *sun, int &type,
|
|
|
|
PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (sun->un_len <= (socklen_t) sizeof (sa_family_t)
|
2018-03-07 01:42:12 +08:00
|
|
|
|| (sun->un_len == 3 && sun->un.sun_path[0] == '\0'))
|
2018-03-03 01:14:11 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
else if (sun->un.sun_path[0] == '\0')
|
|
|
|
ret = open_abstract_link (sun, pipe_name);
|
|
|
|
else
|
|
|
|
ret = open_reparse_point (sun, pipe_name);
|
|
|
|
if (!ret)
|
2018-03-07 01:37:09 +08:00
|
|
|
switch (pipe_name->Buffer[CYGWIN_PIPE_SOCKET_TYPE_POS])
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
type = SOCK_DGRAM;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
type = SOCK_STREAM;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_socket_unix::autobind (sun_name_t* sun)
|
|
|
|
{
|
|
|
|
uint32_t id;
|
|
|
|
HANDLE fh;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Use only 5 hex digits (up to 2^20 sockets) for Linux compat */
|
|
|
|
set_unique_id ();
|
|
|
|
id = get_unique_id () & 0xfffff;
|
|
|
|
sun->un.sun_path[0] = '\0';
|
|
|
|
sun->un_len = sizeof (sa_family_t)
|
|
|
|
+ 1 /* leading NUL */
|
|
|
|
+ __small_sprintf (sun->un.sun_path + 1, "%5X", id);
|
|
|
|
}
|
|
|
|
while ((fh = create_abstract_link (sun, pc.get_nt_native_path ())) == NULL);
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t
|
|
|
|
fhandler_socket_unix::get_type_char ()
|
|
|
|
{
|
|
|
|
switch (get_socket_type ())
|
|
|
|
{
|
|
|
|
case SOCK_STREAM:
|
|
|
|
return 's';
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
return 'd';
|
|
|
|
default:
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
/* This also sets the pipe to message mode unconditionally. */
|
2018-03-03 01:14:11 +08:00
|
|
|
void
|
2018-03-07 01:55:03 +08:00
|
|
|
fhandler_socket_unix::set_pipe_non_blocking (bool nonblocking)
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
if (get_handle ())
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_PIPE_INFORMATION fpi;
|
|
|
|
|
|
|
|
fpi.ReadMode = FILE_PIPE_MESSAGE_MODE;
|
2018-03-07 01:55:03 +08:00
|
|
|
fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION
|
|
|
|
: FILE_PIPE_QUEUE_OPERATION;
|
2018-03-03 01:14:11 +08:00
|
|
|
status = NtSetInformationFile (get_handle (), &io, &fpi, sizeof fpi,
|
|
|
|
FilePipeInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
2018-03-07 01:55:03 +08:00
|
|
|
debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
/* Apart from being called from bind(), from_bind indicates that the caller
|
|
|
|
already locked state_lock, so send_sock_info doesn't lock, only unlocks
|
|
|
|
state_lock. */
|
2018-03-07 01:55:03 +08:00
|
|
|
int
|
2018-03-19 03:46:43 +08:00
|
|
|
fhandler_socket_unix::send_sock_info (bool from_bind)
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-07 23:08:15 +08:00
|
|
|
sun_name_t *sun;
|
2018-03-19 03:46:43 +08:00
|
|
|
size_t plen;
|
|
|
|
size_t clen = 0;
|
2018-03-07 01:55:03 +08:00
|
|
|
af_unix_pkt_hdr_t *packet;
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
if (!from_bind)
|
|
|
|
{
|
|
|
|
state_lock ();
|
|
|
|
/* When called from connect, initialize credentials. accept4 already
|
|
|
|
did it (copied from listening socket). */
|
|
|
|
if (sock_cred ()->pid == 0)
|
|
|
|
set_cred ();
|
|
|
|
}
|
|
|
|
sun = sun_path ();
|
|
|
|
plen = sizeof *packet + sun->un_len;
|
|
|
|
/* When called from connect/accept4, send SCM_CREDENTIALS, too. */
|
|
|
|
if (!from_bind)
|
|
|
|
{
|
|
|
|
clen = CMSG_SPACE (sizeof (struct ucred));
|
|
|
|
plen += clen;
|
|
|
|
}
|
|
|
|
packet = (af_unix_pkt_hdr_t *) alloca (plen);
|
|
|
|
packet->init (true, _SHUT_NONE, sun->un_len, clen, 0);
|
2018-03-07 23:08:15 +08:00
|
|
|
if (sun)
|
2018-03-19 03:46:43 +08:00
|
|
|
memcpy (AF_UNIX_PKT_NAME (packet), &sun->un, sun->un_len);
|
|
|
|
if (!from_bind)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg = AF_UNIX_PKT_CMSG (packet);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_CREDENTIALS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN (sizeof (struct ucred));
|
|
|
|
memcpy (CMSG_DATA(cmsg), sock_cred (), sizeof (struct ucred));
|
|
|
|
}
|
2018-03-07 01:55:03 +08:00
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
state_unlock ();
|
2018-03-14 18:36:06 +08:00
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
/* The theory: Fire and forget. */
|
2018-03-12 22:26:12 +08:00
|
|
|
io_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
set_pipe_non_blocking (true);
|
|
|
|
status = NtWriteFile (get_handle (), NULL, NULL, NULL, &io, packet,
|
|
|
|
packet->pckt_len, NULL, NULL);
|
|
|
|
set_pipe_non_blocking (is_nonblocking ());
|
2018-03-12 22:26:12 +08:00
|
|
|
io_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("Couldn't send my name: NtWriteFile: %y", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
/* Checks if the next packet in the pipe is an administrative packet.
|
|
|
|
If so, it reads it from the pipe, handles it. Returns an error code. */
|
2018-03-07 01:55:03 +08:00
|
|
|
int
|
2018-03-19 03:46:43 +08:00
|
|
|
fhandler_socket_unix::grab_admin_pkg ()
|
|
|
|
{
|
|
|
|
HANDLE evt;
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
/* MAX_PATH is more than sufficient for admin packets. */
|
|
|
|
PFILE_PIPE_PEEK_BUFFER pbuf = (PFILE_PIPE_PEEK_BUFFER) alloca (MAX_PATH);
|
|
|
|
if (!(evt = create_event ()))
|
|
|
|
return 0;
|
|
|
|
io_lock ();
|
|
|
|
ULONG ret_len = peek_pipe (pbuf, MAX_PATH, evt);
|
|
|
|
if (pbuf->NumberOfMessages == 0 || ret_len < sizeof (af_unix_pkt_hdr_t))
|
|
|
|
{
|
|
|
|
io_unlock ();
|
|
|
|
NtClose (evt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
af_unix_pkt_hdr_t *packet = (af_unix_pkt_hdr_t *) pbuf->Data;
|
|
|
|
if (!packet->admin_pkg)
|
|
|
|
io_unlock ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
packet = (af_unix_pkt_hdr_t *) pbuf;
|
|
|
|
status = NtReadFile (get_handle (), evt, NULL, NULL, &io, packet,
|
|
|
|
MAX_PATH, NULL, NULL);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
/* Very short-lived */
|
|
|
|
status = NtWaitForSingleObject (evt, FALSE, NULL);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
status = io.Status;
|
|
|
|
}
|
|
|
|
io_unlock ();
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
state_lock ();
|
|
|
|
if (packet->shut_info)
|
|
|
|
{
|
|
|
|
/* Peer's shutdown sends the SHUT flags as used by the peer.
|
|
|
|
They have to be reversed for our side. */
|
|
|
|
int shut_info = saw_shutdown ();
|
|
|
|
if (packet->shut_info & _SHUT_RECV)
|
|
|
|
shut_info |= _SHUT_SEND;
|
|
|
|
if (packet->shut_info & _SHUT_SEND)
|
|
|
|
shut_info |= _SHUT_RECV;
|
|
|
|
saw_shutdown (shut_info);
|
|
|
|
/* FIXME: anything else here? */
|
|
|
|
}
|
|
|
|
if (packet->name_len > 0)
|
|
|
|
peer_sun_path (AF_UNIX_PKT_NAME (packet), packet->name_len);
|
|
|
|
if (packet->cmsg_len > 0)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg = (struct cmsghdr *)
|
|
|
|
alloca (packet->cmsg_len);
|
|
|
|
memcpy (cmsg, AF_UNIX_PKT_CMSG (packet), packet->cmsg_len);
|
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET
|
|
|
|
&& cmsg->cmsg_type == SCM_CREDENTIALS)
|
|
|
|
peer_cred ((struct ucred *) CMSG_DATA(cmsg));
|
|
|
|
}
|
|
|
|
state_unlock ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NtClose (evt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns an error code. Locking is not required when called from accept4,
|
|
|
|
user space doesn't know about this socket yet. */
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::recv_peer_info ()
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-07 23:00:36 +08:00
|
|
|
HANDLE evt;
|
2018-03-07 01:55:03 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
af_unix_pkt_hdr_t *packet;
|
|
|
|
struct sockaddr_un *un;
|
|
|
|
ULONG len;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-03-07 23:00:36 +08:00
|
|
|
if (!(evt = create_event ()))
|
|
|
|
return ENOBUFS;
|
2018-03-19 03:46:43 +08:00
|
|
|
len = sizeof *packet + sizeof *un + CMSG_SPACE (sizeof (struct ucred));
|
2018-03-07 01:55:03 +08:00
|
|
|
packet = (af_unix_pkt_hdr_t *) alloca (len);
|
|
|
|
set_pipe_non_blocking (false);
|
2018-03-07 23:00:36 +08:00
|
|
|
status = NtReadFile (get_handle (), evt, NULL, NULL, &io, packet, len,
|
2018-03-07 01:55:03 +08:00
|
|
|
NULL, NULL);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
|
2018-03-08 04:55:34 +08:00
|
|
|
timeout.QuadPart = AF_UNIX_CONNECT_TIMEOUT;
|
2018-03-07 23:00:36 +08:00
|
|
|
ret = cygwait (evt, &timeout, cw_sig_eintr);
|
2018-03-07 01:55:03 +08:00
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
status = io.Status;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
ret = ECONNABORTED;
|
|
|
|
break;
|
|
|
|
case WAIT_SIGNALED:
|
|
|
|
ret = EINTR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 03:46:43 +08:00
|
|
|
set_pipe_non_blocking (is_nonblocking ());
|
|
|
|
NtClose (evt);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (!NT_SUCCESS (status) && ret == 0)
|
|
|
|
ret = geterrno_from_nt_status (status);
|
2018-03-19 03:46:43 +08:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
if (packet->name_len > 0)
|
|
|
|
peer_sun_path (AF_UNIX_PKT_NAME (packet), packet->name_len);
|
|
|
|
if (packet->cmsg_len > 0)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg = (struct cmsghdr *) alloca (packet->cmsg_len);
|
|
|
|
memcpy (cmsg, AF_UNIX_PKT_CMSG (packet), packet->cmsg_len);
|
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET
|
|
|
|
&& cmsg->cmsg_type == SCM_CREDENTIALS)
|
|
|
|
peer_cred ((struct ucred *) CMSG_DATA(cmsg));
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:55:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:37:09 +08:00
|
|
|
NTSTATUS
|
2018-03-07 22:48:21 +08:00
|
|
|
fhandler_socket_unix::npfs_handle (HANDLE &nph)
|
2018-03-07 01:37:09 +08:00
|
|
|
{
|
2018-03-07 22:48:21 +08:00
|
|
|
static NO_COPY SRWLOCK npfs_lock;
|
2018-03-07 01:37:09 +08:00
|
|
|
static NO_COPY HANDLE npfs_dirh;
|
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2018-03-07 01:37:09 +08:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
/* Lockless after first call. */
|
|
|
|
if (npfs_dirh)
|
|
|
|
{
|
|
|
|
nph = npfs_dirh;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
AcquireSRWLockExclusive (&npfs_lock);
|
|
|
|
if (!npfs_dirh)
|
2018-03-07 01:37:09 +08:00
|
|
|
{
|
2018-03-07 22:48:21 +08:00
|
|
|
InitializeObjectAttributes (&attr, &ro_u_npfs, 0, NULL, NULL);
|
|
|
|
status = NtOpenFile (&npfs_dirh, FILE_READ_ATTRIBUTES | SYNCHRONIZE,
|
2018-03-07 01:37:09 +08:00
|
|
|
&attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2018-03-07 23:19:32 +08:00
|
|
|
0);
|
2018-03-03 01:14:11 +08:00
|
|
|
}
|
2018-03-07 22:48:21 +08:00
|
|
|
ReleaseSRWLockExclusive (&npfs_lock);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
nph = npfs_dirh;
|
|
|
|
return status;
|
2018-03-03 01:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
2018-03-09 21:19:36 +08:00
|
|
|
fhandler_socket_unix::create_pipe (bool single_instance)
|
2018-03-03 01:14:11 +08:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
2018-03-07 01:37:09 +08:00
|
|
|
HANDLE npfsh;
|
2018-03-03 01:14:11 +08:00
|
|
|
HANDLE ph;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG sharing;
|
|
|
|
ULONG nonblocking;
|
|
|
|
ULONG max_instances;
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
status = npfs_handle (npfsh);
|
2018-03-07 01:37:09 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-03 06:39:15 +08:00
|
|
|
access = GENERIC_READ | FILE_READ_ATTRIBUTES
|
|
|
|
| GENERIC_WRITE | FILE_WRITE_ATTRIBUTES
|
|
|
|
| SYNCHRONIZE;
|
2018-03-03 01:14:11 +08:00
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
2018-03-07 01:37:09 +08:00
|
|
|
InitializeObjectAttributes (&attr, pc.get_nt_native_path (),
|
|
|
|
OBJ_INHERIT | OBJ_CASE_INSENSITIVE,
|
|
|
|
npfsh, NULL);
|
2018-03-03 01:14:11 +08:00
|
|
|
nonblocking = is_nonblocking () ? FILE_PIPE_COMPLETE_OPERATION
|
|
|
|
: FILE_PIPE_QUEUE_OPERATION;
|
2018-03-09 21:19:36 +08:00
|
|
|
max_instances = single_instance ? 1 : -1;
|
2018-03-03 01:14:11 +08:00
|
|
|
timeout.QuadPart = -500000;
|
|
|
|
status = NtCreateNamedPipeFile (&ph, access, &attr, &io, sharing,
|
2018-03-07 01:42:12 +08:00
|
|
|
FILE_CREATE, 0,
|
2018-03-03 01:14:11 +08:00
|
|
|
FILE_PIPE_MESSAGE_TYPE,
|
|
|
|
FILE_PIPE_MESSAGE_MODE,
|
|
|
|
nonblocking, max_instances,
|
2018-03-07 01:28:15 +08:00
|
|
|
rmem (), wmem (), &timeout);
|
2018-03-03 01:14:11 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2018-03-07 01:42:12 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2018-03-03 01:14:11 +08:00
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_socket_unix::create_pipe_instance ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
2018-03-07 01:37:09 +08:00
|
|
|
HANDLE npfsh;
|
2018-03-03 01:14:11 +08:00
|
|
|
HANDLE ph;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG sharing;
|
|
|
|
ULONG nonblocking;
|
|
|
|
ULONG max_instances;
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
status = npfs_handle (npfsh);
|
2018-03-07 01:37:09 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-03 06:39:15 +08:00
|
|
|
access = GENERIC_READ | FILE_READ_ATTRIBUTES
|
|
|
|
| GENERIC_WRITE | FILE_WRITE_ATTRIBUTES
|
|
|
|
| SYNCHRONIZE;
|
2018-03-03 01:14:11 +08:00
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
2018-03-03 06:39:15 +08:00
|
|
|
/* NPFS doesn't understand reopening by handle, unfortunately. */
|
2018-03-03 01:14:11 +08:00
|
|
|
InitializeObjectAttributes (&attr, pc.get_nt_native_path (), OBJ_INHERIT,
|
2018-03-07 01:37:09 +08:00
|
|
|
npfsh, NULL);
|
2018-03-03 01:14:11 +08:00
|
|
|
nonblocking = is_nonblocking () ? FILE_PIPE_COMPLETE_OPERATION
|
|
|
|
: FILE_PIPE_QUEUE_OPERATION;
|
|
|
|
max_instances = (get_socket_type () == SOCK_DGRAM) ? 1 : -1;
|
|
|
|
timeout.QuadPart = -500000;
|
|
|
|
status = NtCreateNamedPipeFile (&ph, access, &attr, &io, sharing,
|
2018-03-07 01:42:12 +08:00
|
|
|
FILE_OPEN, 0,
|
2018-03-03 01:14:11 +08:00
|
|
|
FILE_PIPE_MESSAGE_TYPE,
|
|
|
|
FILE_PIPE_MESSAGE_MODE,
|
|
|
|
nonblocking, max_instances,
|
2018-03-07 01:28:15 +08:00
|
|
|
rmem (), wmem (), &timeout);
|
2018-03-03 01:14:11 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2018-03-07 01:42:12 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2018-03-03 01:14:11 +08:00
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
NTSTATUS
|
2018-03-19 03:46:43 +08:00
|
|
|
fhandler_socket_unix::open_pipe (PUNICODE_STRING pipe_name, bool xchg_sock_info)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE npfsh;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG sharing;
|
2018-03-09 21:19:36 +08:00
|
|
|
HANDLE ph = NULL;
|
2018-03-07 01:55:03 +08:00
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
status = npfs_handle (npfsh);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
return status;
|
|
|
|
access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
|
|
|
|
InitializeObjectAttributes (&attr, pipe_name, OBJ_INHERIT, npfsh, NULL);
|
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
status = NtOpenFile (&ph, access, &attr, &io, sharing, 0);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (ph);
|
2018-03-19 03:46:43 +08:00
|
|
|
if (xchg_sock_info)
|
2020-10-04 23:05:33 +08:00
|
|
|
{
|
|
|
|
/* FIXME: Should we check for errors? */
|
|
|
|
send_sock_info (false);
|
|
|
|
recv_peer_info ();
|
|
|
|
}
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
return status;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
struct conn_wait_info_t
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
fhandler_socket_unix *fh;
|
|
|
|
UNICODE_STRING pipe_name;
|
|
|
|
WCHAR pipe_name_buf[CYGWIN_PIPE_SOCKET_NAME_LEN + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Just hop to the wait_pipe_thread method. */
|
|
|
|
DWORD WINAPI
|
|
|
|
connect_wait_func (LPVOID param)
|
|
|
|
{
|
|
|
|
conn_wait_info_t *wait_info = (conn_wait_info_t *) param;
|
|
|
|
return wait_info->fh->wait_pipe_thread (&wait_info->pipe_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a waiter thread to wait for a pipe instance to become available.
|
|
|
|
in blocking mode, wait for the thread to finish. In nonblocking mode
|
|
|
|
just return with errno set to EINPROGRESS. */
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::wait_pipe (PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
conn_wait_info_t *wait_info;
|
|
|
|
DWORD waitret, err;
|
|
|
|
int ret = -1;
|
2018-03-07 23:19:32 +08:00
|
|
|
HANDLE thr, evt;
|
|
|
|
PVOID param;
|
2018-03-07 01:55:03 +08:00
|
|
|
|
2018-03-07 23:19:32 +08:00
|
|
|
if (!(cwt_termination_evt = create_event ()))
|
2018-03-11 21:56:02 +08:00
|
|
|
return -1;
|
|
|
|
wait_info = (conn_wait_info_t *) cmalloc (HEAP_3_FHANDLER, sizeof *wait_info);
|
|
|
|
if (!wait_info)
|
|
|
|
return -1;
|
2018-03-07 01:55:03 +08:00
|
|
|
wait_info->fh = this;
|
|
|
|
RtlInitEmptyUnicodeString (&wait_info->pipe_name, wait_info->pipe_name_buf,
|
|
|
|
sizeof wait_info->pipe_name_buf);
|
|
|
|
RtlCopyUnicodeString (&wait_info->pipe_name, pipe_name);
|
|
|
|
|
|
|
|
cwt_param = (PVOID) wait_info;
|
|
|
|
connect_wait_thr = CreateThread (NULL, PREFERRED_IO_BLKSIZE,
|
|
|
|
connect_wait_func, cwt_param, 0, NULL);
|
|
|
|
if (!connect_wait_thr)
|
|
|
|
{
|
|
|
|
cfree (wait_info);
|
|
|
|
__seterrno ();
|
2018-03-07 23:19:32 +08:00
|
|
|
goto out;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
if (is_nonblocking ())
|
|
|
|
{
|
|
|
|
set_errno (EINPROGRESS);
|
2018-03-07 23:19:32 +08:00
|
|
|
goto out;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
waitret = cygwait (connect_wait_thr, cw_infinite, cw_cancel | cw_sig_eintr);
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
|
|
|
GetExitCodeThread (connect_wait_thr, &err);
|
|
|
|
else
|
2018-03-07 23:19:32 +08:00
|
|
|
{
|
|
|
|
SetEvent (cwt_termination_evt);
|
2018-03-19 03:46:43 +08:00
|
|
|
NtWaitForSingleObject (connect_wait_thr, FALSE, NULL);
|
2018-03-07 23:19:32 +08:00
|
|
|
GetExitCodeThread (connect_wait_thr, &err);
|
|
|
|
waitret = WAIT_SIGNALED;
|
|
|
|
}
|
|
|
|
thr = InterlockedExchangePointer (&connect_wait_thr, NULL);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (thr)
|
2018-03-19 03:46:43 +08:00
|
|
|
NtClose (thr);
|
2018-03-07 23:19:32 +08:00
|
|
|
param = InterlockedExchangePointer (&cwt_param, NULL);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (param)
|
|
|
|
cfree (param);
|
|
|
|
switch (waitret)
|
|
|
|
{
|
|
|
|
case WAIT_CANCELED:
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case WAIT_SIGNALED:
|
|
|
|
set_errno (EINTR);
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-12 22:26:12 +08:00
|
|
|
so_error (err);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (err)
|
|
|
|
set_errno (err);
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-07 23:19:32 +08:00
|
|
|
out:
|
|
|
|
evt = InterlockedExchangePointer (&cwt_termination_evt, NULL);
|
|
|
|
if (evt)
|
|
|
|
NtClose (evt);
|
2018-03-07 01:55:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::connect_pipe (PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
/* Try connecting first. If it doesn't work, wait for the pipe
|
|
|
|
to become available. */
|
2018-03-09 21:19:36 +08:00
|
|
|
status = open_pipe (pipe_name, get_socket_type () != SOCK_DGRAM);
|
2018-03-07 23:12:55 +08:00
|
|
|
if (STATUS_PIPE_NO_INSTANCE_AVAILABLE (status))
|
2018-03-07 01:55:03 +08:00
|
|
|
return wait_pipe (pipe_name);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
2018-03-12 22:26:12 +08:00
|
|
|
so_error (get_errno ());
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
so_error (0);
|
2018-03-07 01:55:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::listen_pipe ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
HANDLE evt = NULL;
|
2018-03-07 23:23:44 +08:00
|
|
|
DWORD waitret = WAIT_OBJECT_0;
|
2020-10-04 00:37:36 +08:00
|
|
|
int ret = -1;
|
2018-03-07 01:55:03 +08:00
|
|
|
|
|
|
|
io.Status = STATUS_PENDING;
|
2018-03-07 23:00:36 +08:00
|
|
|
if (!is_nonblocking () && !(evt = create_event ()))
|
|
|
|
return -1;
|
2018-03-07 01:55:03 +08:00
|
|
|
status = NtFsControlFile (get_handle (), evt, NULL, NULL, &io,
|
|
|
|
FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
2018-03-07 23:23:44 +08:00
|
|
|
waitret = cygwait (evt ?: get_handle (), cw_infinite,
|
|
|
|
cw_cancel | cw_sig_eintr);
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
2018-03-07 01:55:03 +08:00
|
|
|
status = io.Status;
|
|
|
|
}
|
|
|
|
if (evt)
|
|
|
|
NtClose (evt);
|
2018-03-07 23:23:44 +08:00
|
|
|
if (waitret == WAIT_CANCELED)
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
else if (waitret == WAIT_SIGNALED)
|
|
|
|
set_errno (EINTR);
|
|
|
|
else if (status == STATUS_PIPE_LISTENING)
|
2018-03-07 01:55:03 +08:00
|
|
|
set_errno (EAGAIN);
|
2020-10-04 00:37:36 +08:00
|
|
|
else if (status == STATUS_SUCCESS || status == STATUS_PIPE_CONNECTED)
|
|
|
|
ret = 0;
|
|
|
|
else
|
2018-03-07 01:55:03 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2020-10-04 00:37:36 +08:00
|
|
|
return ret;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
ULONG
|
|
|
|
fhandler_socket_unix::peek_pipe (PFILE_PIPE_PEEK_BUFFER pbuf, ULONG psize,
|
|
|
|
HANDLE evt)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
status = NtFsControlFile (get_handle (), evt, NULL, NULL, &io,
|
|
|
|
FSCTL_PIPE_PEEK, NULL, 0, pbuf, psize);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
/* Very short-lived */
|
|
|
|
status = NtWaitForSingleObject (evt ?: get_handle (), FALSE, NULL);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
status = io.Status;
|
|
|
|
}
|
|
|
|
return NT_SUCCESS (status) ? (io.Information
|
|
|
|
- offsetof (FILE_PIPE_PEEK_BUFFER, Data))
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::disconnect_pipe (HANDLE ph)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
status = NtFsControlFile (ph, NULL, NULL, NULL, &io, FSCTL_PIPE_DISCONNECT,
|
|
|
|
NULL, 0, NULL, 0);
|
2018-03-07 23:23:44 +08:00
|
|
|
/* Short-lived. Don't use cygwait. We don't want to be interrupted. */
|
|
|
|
if (status == STATUS_PENDING
|
2018-03-19 03:46:43 +08:00
|
|
|
&& NtWaitForSingleObject (ph, FALSE, NULL) == WAIT_OBJECT_0)
|
2018-03-07 01:55:03 +08:00
|
|
|
status = io.Status;
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:36:06 +08:00
|
|
|
void
|
2018-03-19 03:46:43 +08:00
|
|
|
fhandler_socket_unix::init_cred ()
|
2018-03-14 18:36:06 +08:00
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
struct ucred *scred = shmem->sock_cred ();
|
|
|
|
struct ucred *pcred = shmem->peer_cred ();
|
|
|
|
scred->pid = pcred->pid = (pid_t) 0;
|
|
|
|
scred->uid = pcred->uid = (uid_t) -1;
|
|
|
|
scred->gid = pcred->gid = (gid_t) -1;
|
2018-03-14 18:36:06 +08:00
|
|
|
}
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
void
|
|
|
|
fhandler_socket_unix::set_cred ()
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
struct ucred *scred = shmem->sock_cred ();
|
|
|
|
scred->pid = myself->pid;
|
|
|
|
scred->uid = myself->uid;
|
|
|
|
scred->gid = myself->gid;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 20:21:05 +08:00
|
|
|
void
|
|
|
|
fhandler_socket_unix::fixup_helper ()
|
|
|
|
{
|
|
|
|
if (shmem_handle)
|
|
|
|
reopen_shmem ();
|
|
|
|
connect_wait_thr = NULL;
|
|
|
|
cwt_termination_evt = NULL;
|
|
|
|
cwt_param = NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-11 04:09:28 +08:00
|
|
|
/* ========================== public methods ========================= */
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
void
|
|
|
|
fhandler_socket_unix::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
|
|
|
fhandler_socket::fixup_after_fork (parent);
|
|
|
|
if (backing_file_handle && backing_file_handle != INVALID_HANDLE_VALUE)
|
|
|
|
fork_fixup (parent, backing_file_handle, "backing_file_handle");
|
2018-03-12 22:26:12 +08:00
|
|
|
if (shmem_handle)
|
2020-02-28 20:21:05 +08:00
|
|
|
fork_fixup (parent, shmem_handle, "shmem_handle");
|
|
|
|
fixup_helper ();
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-11 04:09:28 +08:00
|
|
|
void
|
|
|
|
fhandler_socket_unix::fixup_after_exec ()
|
|
|
|
{
|
|
|
|
if (!close_on_exec ())
|
2020-02-28 20:21:05 +08:00
|
|
|
fixup_helper ();
|
2018-03-11 04:09:28 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
void
|
|
|
|
fhandler_socket_unix::set_close_on_exec (bool val)
|
|
|
|
{
|
|
|
|
fhandler_base::set_close_on_exec (val);
|
|
|
|
if (backing_file_handle && backing_file_handle != INVALID_HANDLE_VALUE)
|
|
|
|
set_no_inheritance (backing_file_handle, val);
|
2018-03-12 22:26:12 +08:00
|
|
|
if (shmem_handle)
|
|
|
|
set_no_inheritance (shmem_handle, val);
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_socket_unix::fhandler_socket_unix ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_socket_unix::~fhandler_socket_unix ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::dup (fhandler_base *child, int flags)
|
|
|
|
{
|
2021-02-24 00:48:46 +08:00
|
|
|
if (get_flags () & O_PATH)
|
|
|
|
/* We're viewing the socket as a disk file, but fhandler_base::dup
|
|
|
|
suffices here. */
|
|
|
|
return fhandler_base::dup (child, flags);
|
|
|
|
|
2018-03-11 04:12:27 +08:00
|
|
|
if (fhandler_socket::dup (child, flags))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
fhandler_socket_unix *fhs = (fhandler_socket_unix *) child;
|
2018-03-11 04:12:27 +08:00
|
|
|
if (backing_file_handle && backing_file_handle != INVALID_HANDLE_VALUE
|
|
|
|
&& !DuplicateHandle (GetCurrentProcess (), backing_file_handle,
|
|
|
|
GetCurrentProcess (), &fhs->backing_file_handle,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
fhs->close ();
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), shmem_handle,
|
|
|
|
GetCurrentProcess (), &fhs->shmem_handle,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
fhs->close ();
|
2020-02-28 19:39:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (fhs->reopen_shmem () < 0)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
fhs->close ();
|
2018-03-12 22:26:12 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-19 03:46:43 +08:00
|
|
|
fhs->sun_path (sun_path ());
|
|
|
|
fhs->peer_sun_path (peer_sun_path ());
|
2018-03-07 01:55:03 +08:00
|
|
|
fhs->connect_wait_thr = NULL;
|
2018-03-07 23:19:32 +08:00
|
|
|
fhs->cwt_termination_evt = NULL;
|
2018-03-07 01:55:03 +08:00
|
|
|
fhs->cwt_param = NULL;
|
2018-03-11 04:12:27 +08:00
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
/* Waiter thread method. Here we wait for a pipe instance to become
|
|
|
|
available and connect to it, if so. This function is running
|
|
|
|
asynchronously if called on a non-blocking pipe. The important
|
|
|
|
things to do:
|
|
|
|
|
|
|
|
- Set the peer pipe handle if successful
|
2018-03-07 22:43:26 +08:00
|
|
|
- Send own sun_path to peer if successful
|
2018-03-07 01:55:03 +08:00
|
|
|
- Set connect_state
|
|
|
|
- Set so_error for later call to select
|
|
|
|
*/
|
|
|
|
DWORD
|
|
|
|
fhandler_socket_unix::wait_pipe_thread (PUNICODE_STRING pipe_name)
|
|
|
|
{
|
|
|
|
HANDLE npfsh;
|
2018-03-07 23:19:32 +08:00
|
|
|
HANDLE evt;
|
2018-03-07 01:55:03 +08:00
|
|
|
LONG error = 0;
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG pwbuf_size;
|
|
|
|
PFILE_PIPE_WAIT_FOR_BUFFER pwbuf;
|
|
|
|
LONGLONG stamp;
|
|
|
|
|
2018-03-07 22:48:21 +08:00
|
|
|
status = npfs_handle (npfsh);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
error = geterrno_from_nt_status (status);
|
|
|
|
goto out;
|
|
|
|
}
|
2018-03-07 23:19:32 +08:00
|
|
|
if (!(evt = create_event ()))
|
|
|
|
goto out;
|
2018-03-07 01:55:03 +08:00
|
|
|
pwbuf_size = offsetof (FILE_PIPE_WAIT_FOR_BUFFER, Name) + pipe_name->Length;
|
|
|
|
pwbuf = (PFILE_PIPE_WAIT_FOR_BUFFER) alloca (pwbuf_size);
|
2018-03-08 04:55:34 +08:00
|
|
|
pwbuf->Timeout.QuadPart = AF_UNIX_CONNECT_TIMEOUT;
|
2018-03-07 01:55:03 +08:00
|
|
|
pwbuf->NameLength = pipe_name->Length;
|
|
|
|
pwbuf->TimeoutSpecified = TRUE;
|
|
|
|
memcpy (pwbuf->Name, pipe_name->Buffer, pipe_name->Length);
|
2018-11-27 20:47:02 +08:00
|
|
|
stamp = get_clock (CLOCK_MONOTONIC)->n100secs ();
|
2018-03-07 01:55:03 +08:00
|
|
|
do
|
|
|
|
{
|
2018-03-07 23:19:32 +08:00
|
|
|
status = NtFsControlFile (npfsh, evt, NULL, NULL, &io, FSCTL_PIPE_WAIT,
|
2018-03-07 01:55:03 +08:00
|
|
|
pwbuf, pwbuf_size, NULL, 0);
|
2018-03-07 23:19:32 +08:00
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
HANDLE w[2] = { evt, cwt_termination_evt };
|
|
|
|
switch (WaitForMultipleObjects (2, w, FALSE, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
status = io.Status;
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
default:
|
|
|
|
status = STATUS_THREAD_IS_TERMINATING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:55:03 +08:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_SUCCESS:
|
|
|
|
{
|
2018-03-09 21:19:36 +08:00
|
|
|
status = open_pipe (pipe_name, get_socket_type () != SOCK_DGRAM);
|
2018-03-07 23:12:55 +08:00
|
|
|
if (STATUS_PIPE_NO_INSTANCE_AVAILABLE (status))
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
|
|
|
/* Another concurrent connect grabbed the pipe instance
|
|
|
|
under our nose. Fix the timeout value and go waiting
|
|
|
|
again, unless the timeout has passed. */
|
2018-11-27 20:47:02 +08:00
|
|
|
pwbuf->Timeout.QuadPart -=
|
|
|
|
stamp - get_clock (CLOCK_MONOTONIC)->n100secs ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (pwbuf->Timeout.QuadPart >= 0)
|
|
|
|
{
|
|
|
|
status = STATUS_IO_TIMEOUT;
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!NT_SUCCESS (status))
|
|
|
|
error = geterrno_from_nt_status (status);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATUS_OBJECT_NAME_NOT_FOUND:
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
break;
|
|
|
|
case STATUS_IO_TIMEOUT:
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
case STATUS_INSUFFICIENT_RESOURCES:
|
|
|
|
error = ENOBUFS;
|
|
|
|
break;
|
2018-03-07 23:19:32 +08:00
|
|
|
case STATUS_THREAD_IS_TERMINATING:
|
|
|
|
error = EINTR;
|
|
|
|
break;
|
2018-03-07 01:55:03 +08:00
|
|
|
case STATUS_INVALID_DEVICE_REQUEST:
|
|
|
|
default:
|
|
|
|
error = EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 23:12:55 +08:00
|
|
|
while (STATUS_PIPE_NO_INSTANCE_AVAILABLE (status));
|
2018-03-07 01:55:03 +08:00
|
|
|
out:
|
|
|
|
PVOID param = InterlockedExchangePointer (&cwt_param, NULL);
|
|
|
|
if (param)
|
|
|
|
cfree (param);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_lock ();
|
2018-03-19 03:06:43 +08:00
|
|
|
state_lock ();
|
2018-03-12 22:26:12 +08:00
|
|
|
so_error (error);
|
2018-03-07 01:55:03 +08:00
|
|
|
connect_state (error ? connect_failed : connected);
|
2018-03-19 03:06:43 +08:00
|
|
|
state_unlock ();
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::socket (int af, int type, int protocol, int flags)
|
|
|
|
{
|
2018-02-27 00:56:47 +08:00
|
|
|
if (type != SOCK_STREAM && type != SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (protocol != 0)
|
|
|
|
{
|
|
|
|
set_errno (EPROTONOSUPPORT);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
if (create_shmem () < 0)
|
|
|
|
return -1;
|
2018-03-07 01:28:15 +08:00
|
|
|
rmem (262144);
|
|
|
|
wmem (262144);
|
2018-03-19 03:46:43 +08:00
|
|
|
set_addr_family (AF_UNIX);
|
2018-03-03 01:14:11 +08:00
|
|
|
set_socket_type (type);
|
2020-10-04 22:18:54 +08:00
|
|
|
set_flags (O_RDWR | O_BINARY);
|
2018-03-03 01:14:11 +08:00
|
|
|
if (flags & SOCK_NONBLOCK)
|
|
|
|
set_nonblocking (true);
|
|
|
|
if (flags & SOCK_CLOEXEC)
|
|
|
|
set_close_on_exec (true);
|
2018-03-19 03:46:43 +08:00
|
|
|
init_cred ();
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (NULL);
|
2018-03-03 01:14:11 +08:00
|
|
|
set_unique_id ();
|
|
|
|
set_ino (get_unique_id ());
|
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::socketpair (int af, int type, int protocol, int flags,
|
2018-02-27 00:53:50 +08:00
|
|
|
fhandler_socket *fh_out)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-09 21:19:36 +08:00
|
|
|
HANDLE pipe;
|
|
|
|
sun_name_t sun;
|
|
|
|
fhandler_socket_unix *fh = (fhandler_socket_unix *) fh_out;
|
|
|
|
|
2018-02-27 00:56:47 +08:00
|
|
|
if (type != SOCK_STREAM && type != SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (protocol != 0)
|
|
|
|
{
|
|
|
|
set_errno (EPROTONOSUPPORT);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-09 21:19:36 +08:00
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
if (create_shmem () < 0)
|
|
|
|
return -1;
|
|
|
|
if (fh->create_shmem () < 0)
|
|
|
|
goto fh_shmem_failed;
|
2018-03-09 21:19:36 +08:00
|
|
|
/* socket() on both sockets */
|
|
|
|
rmem (262144);
|
|
|
|
fh->rmem (262144);
|
|
|
|
wmem (262144);
|
|
|
|
fh->wmem (262144);
|
2018-03-19 03:46:43 +08:00
|
|
|
set_addr_family (AF_UNIX);
|
|
|
|
fh->set_addr_family (AF_UNIX);
|
2018-03-09 21:19:36 +08:00
|
|
|
set_socket_type (type);
|
|
|
|
fh->set_socket_type (type);
|
2018-03-19 03:46:43 +08:00
|
|
|
set_cred ();
|
|
|
|
fh->set_cred ();
|
2018-03-09 21:19:36 +08:00
|
|
|
set_unique_id ();
|
|
|
|
set_ino (get_unique_id ());
|
|
|
|
/* bind/listen 1st socket */
|
|
|
|
gen_pipe_name ();
|
|
|
|
pipe = create_pipe (true);
|
|
|
|
if (!pipe)
|
2018-03-19 03:46:43 +08:00
|
|
|
goto create_pipe_failed;
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (pipe);
|
2018-03-19 03:46:43 +08:00
|
|
|
sun_path (&sun);
|
|
|
|
fh->peer_sun_path (&sun);
|
2018-03-09 21:19:36 +08:00
|
|
|
connect_state (listener);
|
2018-03-19 03:46:43 +08:00
|
|
|
/* connect 2nd socket, even for DGRAM. There's no difference as far
|
|
|
|
as socketpairs are concerned. */
|
|
|
|
if (fh->open_pipe (pc.get_nt_native_path (), false) < 0)
|
|
|
|
goto fh_open_pipe_failed;
|
2018-03-09 21:19:36 +08:00
|
|
|
fh->connect_state (connected);
|
|
|
|
if (flags & SOCK_NONBLOCK)
|
|
|
|
{
|
|
|
|
set_nonblocking (true);
|
|
|
|
fh->set_nonblocking (true);
|
|
|
|
}
|
|
|
|
if (flags & SOCK_CLOEXEC)
|
|
|
|
{
|
|
|
|
set_close_on_exec (true);
|
|
|
|
fh->set_close_on_exec (true);
|
|
|
|
}
|
|
|
|
return 0;
|
2018-03-19 03:46:43 +08:00
|
|
|
|
|
|
|
fh_open_pipe_failed:
|
|
|
|
NtClose (pipe);
|
|
|
|
create_pipe_failed:
|
2020-02-28 19:40:49 +08:00
|
|
|
NtUnmapViewOfSection (NtCurrentProcess (), fh->shmem);
|
2018-03-19 03:46:43 +08:00
|
|
|
NtClose (fh->shmem_handle);
|
|
|
|
fh_shmem_failed:
|
2020-02-28 19:40:49 +08:00
|
|
|
NtUnmapViewOfSection (NtCurrentProcess (), shmem);
|
2018-03-19 03:46:43 +08:00
|
|
|
NtClose (shmem_handle);
|
|
|
|
return -1;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
/* Bind creates the backing file, generates the pipe name and sets
|
|
|
|
bind_state. On DGRAM sockets it also creates the pipe. On STREAM
|
|
|
|
sockets either listen or connect will do that. */
|
2018-02-24 03:59:21 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::bind (const struct sockaddr *name, int namelen)
|
|
|
|
{
|
2018-03-03 06:40:36 +08:00
|
|
|
sun_name_t sun (name, namelen);
|
|
|
|
bool unnamed = (sun.un_len == sizeof sun.un.sun_family);
|
|
|
|
HANDLE pipe = NULL;
|
2018-03-03 01:14:11 +08:00
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
if (sun.un.sun_family != AF_UNIX)
|
2018-03-03 06:40:36 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2018-03-03 01:14:11 +08:00
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (binding_state () == bind_pending)
|
|
|
|
{
|
|
|
|
set_errno (EALREADY);
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (binding_state () == bound)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
binding_state (bind_pending);
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_unlock ();
|
2018-03-03 06:40:36 +08:00
|
|
|
gen_pipe_name ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (get_socket_type () == SOCK_DGRAM)
|
|
|
|
{
|
2018-03-09 21:19:36 +08:00
|
|
|
pipe = create_pipe (true);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (!pipe)
|
|
|
|
{
|
|
|
|
binding_state (unbound);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (pipe);
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
|
|
|
backing_file_handle = unnamed ? autobind (&sun) : create_file (&sun);
|
|
|
|
if (!backing_file_handle)
|
2018-03-03 06:40:36 +08:00
|
|
|
{
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (NULL);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (pipe)
|
|
|
|
NtClose (pipe);
|
|
|
|
binding_state (unbound);
|
2018-03-03 06:40:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-19 03:46:43 +08:00
|
|
|
state_lock ();
|
|
|
|
sun_path (&sun);
|
|
|
|
/* If we're already connected, send socket info to peer. In this case
|
|
|
|
send_sock_info calls state_unlock */
|
2018-03-07 01:55:03 +08:00
|
|
|
if (connect_state () == connected)
|
2018-03-19 03:46:43 +08:00
|
|
|
send_sock_info (true);
|
|
|
|
else
|
|
|
|
state_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
binding_state (bound);
|
2018-03-03 06:40:36 +08:00
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
/* Create pipe on non-DGRAM sockets and set conn_state to listener. */
|
2018-02-24 03:59:21 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::listen (int backlog)
|
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
if (get_socket_type () == SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
set_errno (EOPNOTSUPP);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
while (binding_state () == bind_pending)
|
|
|
|
yield ();
|
|
|
|
if (binding_state () == unbound)
|
|
|
|
{
|
|
|
|
set_errno (EDESTADDRREQ);
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
bind_unlock ();
|
|
|
|
conn_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (connect_state () != unconnected && connect_state () != connect_failed)
|
|
|
|
{
|
|
|
|
set_errno (connect_state () == listener ? EADDRINUSE : EINVAL);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-09 21:19:36 +08:00
|
|
|
HANDLE pipe = create_pipe (false);
|
|
|
|
if (!pipe)
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-09 21:19:36 +08:00
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (pipe);
|
2018-03-19 03:46:43 +08:00
|
|
|
state_lock ();
|
|
|
|
set_cred ();
|
|
|
|
state_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
connect_state (listener);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::accept4 (struct sockaddr *peer, int *len, int flags)
|
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
if (get_socket_type () != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
set_errno (EOPNOTSUPP);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect_state () != listener
|
|
|
|
|| (peer && (!len || *len < (int) sizeof (sa_family_t))))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (listen_pipe () == 0)
|
|
|
|
{
|
|
|
|
/* Our handle is now connected with a client. This handle is used
|
|
|
|
for the accepted socket. Our handle has to be replaced with a
|
|
|
|
new instance handle for the next accept. */
|
2018-03-12 22:26:12 +08:00
|
|
|
io_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
HANDLE accepted = get_handle ();
|
|
|
|
HANDLE new_inst = create_pipe_instance ();
|
|
|
|
int error = ENOBUFS;
|
2018-03-07 23:23:44 +08:00
|
|
|
if (!new_inst)
|
2018-03-12 22:26:12 +08:00
|
|
|
io_unlock ();
|
2018-03-07 23:23:44 +08:00
|
|
|
else
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
|
|
|
/* Set new io handle. */
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (new_inst);
|
2018-03-12 22:26:12 +08:00
|
|
|
io_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
/* Prepare new file descriptor. */
|
|
|
|
cygheap_fdnew fd;
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
fhandler_socket_unix *sock = (fhandler_socket_unix *)
|
|
|
|
build_fh_dev (dev ());
|
|
|
|
if (sock)
|
|
|
|
{
|
2018-03-12 22:26:12 +08:00
|
|
|
if (sock->create_shmem () < 0)
|
|
|
|
goto create_shmem_failed;
|
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
sock->set_addr_family (AF_UNIX);
|
2018-03-07 01:55:03 +08:00
|
|
|
sock->set_socket_type (get_socket_type ());
|
|
|
|
if (flags & SOCK_NONBLOCK)
|
|
|
|
sock->set_nonblocking (true);
|
|
|
|
if (flags & SOCK_CLOEXEC)
|
|
|
|
sock->set_close_on_exec (true);
|
|
|
|
sock->set_unique_id ();
|
|
|
|
sock->set_ino (sock->get_unique_id ());
|
|
|
|
sock->pc.set_nt_native_path (pc.get_nt_native_path ());
|
|
|
|
sock->connect_state (connected);
|
|
|
|
sock->binding_state (binding_state ());
|
2019-03-30 15:12:02 +08:00
|
|
|
sock->set_handle (accepted);
|
2018-03-07 01:55:03 +08:00
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
sock->sun_path (sun_path ());
|
|
|
|
sock->sock_cred (sock_cred ());
|
|
|
|
/* Send this socket info to connecting socket. */
|
|
|
|
sock->send_sock_info (false);
|
|
|
|
/* Fetch the packet sent by send_sock_info called by
|
|
|
|
connecting peer. */
|
|
|
|
error = sock->recv_peer_info ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (error == 0)
|
|
|
|
{
|
2018-03-07 23:23:44 +08:00
|
|
|
__try
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-07 23:23:44 +08:00
|
|
|
if (peer)
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
sun_name_t *sun = sock->peer_sun_path ();
|
2018-03-07 23:23:44 +08:00
|
|
|
if (sun)
|
|
|
|
{
|
|
|
|
memcpy (peer, &sun->un,
|
|
|
|
MIN (*len, sun->un_len));
|
|
|
|
*len = sun->un_len;
|
|
|
|
}
|
|
|
|
else if (len)
|
|
|
|
*len = 0;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
2018-03-07 23:23:44 +08:00
|
|
|
fd = sock;
|
|
|
|
if (fd <= 2)
|
|
|
|
set_std_handle (fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
__except (NO_ERROR)
|
|
|
|
{
|
|
|
|
error = EFAULT;
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
2018-03-07 23:23:44 +08:00
|
|
|
__endtry
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
create_shmem_failed:
|
2018-03-07 01:55:03 +08:00
|
|
|
delete sock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Ouch! We can't handle the client if we couldn't
|
|
|
|
create a new instance to accept more connections.*/
|
|
|
|
disconnect_pipe (accepted);
|
|
|
|
set_errno (error);
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::connect (const struct sockaddr *name, int namelen)
|
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
sun_name_t sun (name, namelen);
|
|
|
|
int peer_type;
|
|
|
|
WCHAR pipe_name_buf[CYGWIN_PIPE_SOCKET_NAME_LEN + 1];
|
|
|
|
UNICODE_STRING pipe_name;
|
|
|
|
|
|
|
|
/* Test and set connection state. */
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_lock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (connect_state () == connect_pending)
|
|
|
|
{
|
|
|
|
set_errno (EALREADY);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect_state () == listener)
|
|
|
|
{
|
|
|
|
set_errno (EADDRINUSE);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect_state () == connected && get_socket_type () != SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
set_errno (EISCONN);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
connect_state (connect_pending);
|
2018-03-12 22:26:12 +08:00
|
|
|
conn_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
/* Check validity of name */
|
|
|
|
if (sun.un_len <= (int) sizeof (sa_family_t))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sun.un.sun_family != AF_UNIX)
|
|
|
|
{
|
|
|
|
set_errno (EAFNOSUPPORT);
|
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sun.un_len == 3 && sun.un.sun_path[0] == '\0')
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Check if peer address exists. */
|
|
|
|
RtlInitEmptyUnicodeString (&pipe_name, pipe_name_buf, sizeof pipe_name_buf);
|
|
|
|
if (open_file (&sun, peer_type, &pipe_name) < 0)
|
|
|
|
{
|
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (peer_type != get_socket_type ())
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
connect_state (unconnected);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-19 03:46:43 +08:00
|
|
|
peer_sun_path (&sun);
|
2018-03-07 01:55:03 +08:00
|
|
|
if (get_socket_type () != SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
if (connect_pipe (&pipe_name) < 0)
|
|
|
|
{
|
|
|
|
if (get_errno () != EINPROGRESS)
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
peer_sun_path (NULL);
|
2018-03-07 01:55:03 +08:00
|
|
|
connect_state (connect_failed);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connect_state (connected);
|
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::getsockname (struct sockaddr *name, int *namelen)
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
sun_name_t *sun = sun_path ();
|
2018-02-24 03:59:21 +08:00
|
|
|
|
2018-03-19 03:46:43 +08:00
|
|
|
memcpy (name, sun, MIN (*namelen, sun->un_len));
|
|
|
|
*namelen = sun->un_len;
|
2018-02-24 03:59:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::getpeername (struct sockaddr *name, int *namelen)
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
sun_name_t *sun = peer_sun_path ();
|
|
|
|
memcpy (name, sun, MIN (*namelen, sun->un_len));
|
|
|
|
*namelen = sun->un_len;
|
2018-02-24 03:59:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::shutdown (int how)
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
if (how < SHUT_RD || how > SHUT_RDWR)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Convert SHUT_RD/SHUT_WR/SHUT_RDWR to _SHUT_RECV/_SHUT_SEND bits. */
|
|
|
|
++how;
|
|
|
|
state_lock ();
|
|
|
|
int old_shutdown_mask = saw_shutdown ();
|
|
|
|
int new_shutdown_mask = old_shutdown_mask | how;
|
|
|
|
if (new_shutdown_mask != old_shutdown_mask)
|
|
|
|
saw_shutdown (new_shutdown_mask);
|
|
|
|
state_unlock ();
|
|
|
|
if (new_shutdown_mask != old_shutdown_mask)
|
|
|
|
{
|
|
|
|
/* Send shutdown info to peer. Note that it's not necessarily fatal
|
|
|
|
if the info isn't sent here. The info will be reproduced by any
|
|
|
|
followup package sent to the peer. */
|
|
|
|
af_unix_pkt_hdr_t packet (true, (shut_state) new_shutdown_mask, 0, 0, 0);
|
|
|
|
io_lock ();
|
|
|
|
set_pipe_non_blocking (true);
|
|
|
|
status = NtWriteFile (get_handle (), NULL, NULL, NULL, &io, &packet,
|
|
|
|
packet.pckt_len, NULL, NULL);
|
|
|
|
set_pipe_non_blocking (is_nonblocking ());
|
|
|
|
io_unlock ();
|
|
|
|
}
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("Couldn't send shutdown info: NtWriteFile: %y", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 00:48:46 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::open (int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
/* We don't support opening sockets unless O_PATH is specified. */
|
|
|
|
if (flags & O_PATH)
|
|
|
|
return open_fs (flags, mode);
|
|
|
|
|
|
|
|
set_errno (EOPNOTSUPP);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
int
|
|
|
|
fhandler_socket_unix::close ()
|
|
|
|
{
|
2021-02-24 00:48:46 +08:00
|
|
|
if (get_flags () & O_PATH)
|
|
|
|
return fhandler_base::close ();
|
|
|
|
|
2018-03-07 23:19:32 +08:00
|
|
|
HANDLE evt = InterlockedExchangePointer (&cwt_termination_evt, NULL);
|
2018-03-07 01:55:03 +08:00
|
|
|
HANDLE thr = InterlockedExchangePointer (&connect_wait_thr, NULL);
|
|
|
|
if (thr)
|
|
|
|
{
|
2018-03-07 23:19:32 +08:00
|
|
|
if (evt)
|
|
|
|
SetEvent (evt);
|
2018-03-19 03:46:43 +08:00
|
|
|
NtWaitForSingleObject (thr, FALSE, NULL);
|
|
|
|
NtClose (thr);
|
2018-03-07 01:55:03 +08:00
|
|
|
}
|
2018-03-07 23:19:32 +08:00
|
|
|
if (evt)
|
|
|
|
NtClose (evt);
|
2018-03-07 01:55:03 +08:00
|
|
|
PVOID param = InterlockedExchangePointer (&cwt_param, NULL);
|
|
|
|
if (param)
|
|
|
|
cfree (param);
|
2018-03-19 03:46:43 +08:00
|
|
|
HANDLE hdl = InterlockedExchangePointer (&get_handle (), NULL);
|
|
|
|
if (hdl)
|
|
|
|
NtClose (hdl);
|
|
|
|
if (backing_file_handle && backing_file_handle != INVALID_HANDLE_VALUE)
|
|
|
|
NtClose (backing_file_handle);
|
2018-03-12 22:26:12 +08:00
|
|
|
HANDLE shm = InterlockedExchangePointer (&shmem_handle, NULL);
|
|
|
|
if (shm)
|
|
|
|
NtClose (shm);
|
|
|
|
param = InterlockedExchangePointer ((PVOID *) &shmem, NULL);
|
|
|
|
if (param)
|
2020-02-28 19:40:49 +08:00
|
|
|
NtUnmapViewOfSection (NtCurrentProcess (), param);
|
2018-03-03 01:14:11 +08:00
|
|
|
return 0;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::getpeereid (pid_t *pid, uid_t *euid, gid_t *egid)
|
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
if (get_socket_type () != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect_state () != connected)
|
2018-03-07 01:55:03 +08:00
|
|
|
set_errno (ENOTCONN);
|
|
|
|
else
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-07 01:55:03 +08:00
|
|
|
__try
|
|
|
|
{
|
2018-03-19 03:06:43 +08:00
|
|
|
state_lock ();
|
2018-03-19 03:46:43 +08:00
|
|
|
struct ucred *pcred = peer_cred ();
|
2018-03-07 01:55:03 +08:00
|
|
|
if (pid)
|
2018-03-19 03:46:43 +08:00
|
|
|
*pid = pcred->pid;
|
2018-03-07 01:55:03 +08:00
|
|
|
if (euid)
|
2018-03-19 03:46:43 +08:00
|
|
|
*euid = pcred->uid;
|
2018-03-07 01:55:03 +08:00
|
|
|
if (egid)
|
2018-03-19 03:46:43 +08:00
|
|
|
*egid = pcred->gid;
|
2018-03-19 03:06:43 +08:00
|
|
|
state_unlock ();
|
2018-03-07 01:55:03 +08:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
2018-03-07 01:55:03 +08:00
|
|
|
return ret;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::recvmsg (struct msghdr *msg, int flags)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-14 18:36:06 +08:00
|
|
|
set_errno (EAFNOSUPPORT);
|
|
|
|
return -1;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::recvfrom (void *ptr, size_t len, int flags,
|
|
|
|
struct sockaddr *from, int *fromlen)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-08 04:53:56 +08:00
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
iov.iov_base = ptr;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = from;
|
|
|
|
msg.msg_namelen = from && fromlen ? *fromlen : 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
ret = recvmsg (&msg, flags);
|
|
|
|
if (ret >= 0 && from && fromlen)
|
|
|
|
*fromlen = msg.msg_namelen;
|
|
|
|
return ret;
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __reg3
|
|
|
|
fhandler_socket_unix::read (void *ptr, size_t& len)
|
|
|
|
{
|
|
|
|
set_errno (EAFNOSUPPORT);
|
|
|
|
len = 0;
|
2018-03-08 04:53:56 +08:00
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
iov.iov_base = ptr;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
len = recvmsg (&msg, 0);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t __stdcall
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::readv (const struct iovec *const iov, int iovcnt,
|
|
|
|
ssize_t tot)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-08 04:53:56 +08:00
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = (struct iovec *) iov;
|
|
|
|
msg.msg_iovlen = iovcnt;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
return recvmsg (&msg, 0);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::sendmsg (const struct msghdr *msg, int flags)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
|
|
|
set_errno (EAFNOSUPPORT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::sendto (const void *in_ptr, size_t len, int flags,
|
|
|
|
const struct sockaddr *to, int tolen)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-08 04:53:56 +08:00
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
iov.iov_base = (void *) in_ptr;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = (void *) to;
|
|
|
|
msg.msg_namelen = to ? tolen : 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
return sendmsg (&msg, flags);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t __stdcall
|
|
|
|
fhandler_socket_unix::write (const void *ptr, size_t len)
|
|
|
|
{
|
2018-03-08 04:53:56 +08:00
|
|
|
struct iovec iov;
|
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
iov.iov_base = (void *) ptr;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
return sendmsg (&msg, 0);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t __stdcall
|
2018-03-08 04:53:56 +08:00
|
|
|
fhandler_socket_unix::writev (const struct iovec *const iov, int iovcnt,
|
|
|
|
ssize_t tot)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
2018-03-08 04:53:56 +08:00
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = (struct iovec *) iov;
|
|
|
|
msg.msg_iovlen = iovcnt;
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
return sendmsg (&msg, 0);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::setsockopt (int level, int optname, const void *optval,
|
|
|
|
socklen_t optlen)
|
|
|
|
{
|
|
|
|
/* Preprocessing setsockopt. */
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case SOL_SOCKET:
|
|
|
|
switch (optname)
|
|
|
|
{
|
|
|
|
case SO_PASSCRED:
|
2018-03-19 03:46:43 +08:00
|
|
|
if (optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool val;
|
|
|
|
val = !!*(int *) optval;
|
|
|
|
/* Using bind_lock here to make sure the autobind below is
|
|
|
|
covered. This is the only place to set so_passcred anyway. */
|
|
|
|
bind_lock ();
|
|
|
|
if (val && binding_state () == unbound)
|
|
|
|
{
|
|
|
|
sun_name_t sun;
|
|
|
|
|
|
|
|
binding_state (bind_pending);
|
|
|
|
backing_file_handle = autobind (&sun);
|
|
|
|
if (!backing_file_handle)
|
|
|
|
{
|
|
|
|
binding_state (unbound);
|
|
|
|
bind_unlock ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sun_path (&sun);
|
|
|
|
binding_state (bound);
|
|
|
|
}
|
|
|
|
so_passcred (val);
|
|
|
|
bind_unlock ();
|
2018-02-24 03:59:21 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SO_REUSEADDR:
|
2018-03-19 03:46:43 +08:00
|
|
|
if (optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
reuseaddr (!!*(int *) optval);
|
2018-02-24 03:59:21 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SO_RCVBUF:
|
2018-03-19 03:46:43 +08:00
|
|
|
if (optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
rmem (*(int *) optval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SO_SNDBUF:
|
2018-03-19 03:46:43 +08:00
|
|
|
if (optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
wmem (*(int *) optval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
|
|
|
if (optlen < (socklen_t) sizeof (struct timeval))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!timeval_to_ms ((struct timeval *) optval,
|
|
|
|
(optname == SO_RCVTIMEO) ? rcvtimeo ()
|
|
|
|
: sndtimeo ()))
|
|
|
|
{
|
|
|
|
set_errno (EDOM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* AF_UNIX sockets simply ignore all other SOL_SOCKET options. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_errno (ENOPROTOOPT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::getsockopt (int level, int optname, const void *optval,
|
|
|
|
socklen_t *optlen)
|
|
|
|
{
|
|
|
|
/* Preprocessing getsockopt.*/
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case SOL_SOCKET:
|
|
|
|
switch (optname)
|
|
|
|
{
|
|
|
|
case SO_ERROR:
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
if (*optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
int *e = (int *) optval;
|
2018-03-07 01:55:03 +08:00
|
|
|
LONG err;
|
|
|
|
|
2018-03-12 22:26:12 +08:00
|
|
|
err = so_error (0);
|
2018-03-07 01:55:03 +08:00
|
|
|
*e = err;
|
2018-02-24 03:59:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SO_PASSCRED:
|
2018-03-19 03:46:43 +08:00
|
|
|
{
|
|
|
|
if (*optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int *e = (int *) optval;
|
|
|
|
*e = so_passcred ();
|
|
|
|
break;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
|
|
|
|
case SO_PEERCRED:
|
|
|
|
{
|
|
|
|
struct ucred *cred = (struct ucred *) optval;
|
|
|
|
|
|
|
|
if (*optlen < (socklen_t) sizeof *cred)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret = getpeereid (&cred->pid, &cred->uid, &cred->gid);
|
|
|
|
if (!ret)
|
|
|
|
*optlen = (socklen_t) sizeof *cred;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SO_REUSEADDR:
|
|
|
|
{
|
2018-03-12 22:26:12 +08:00
|
|
|
unsigned int *reuse = (unsigned int *) optval;
|
2018-02-24 03:59:21 +08:00
|
|
|
|
2018-03-12 22:26:12 +08:00
|
|
|
if (*optlen < (socklen_t) sizeof *reuse)
|
2018-02-24 03:59:21 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-12 22:26:12 +08:00
|
|
|
*reuse = reuseaddr ();
|
|
|
|
*optlen = (socklen_t) sizeof *reuse;
|
2018-02-24 03:59:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-08 04:56:42 +08:00
|
|
|
case SO_RCVBUF:
|
|
|
|
case SO_SNDBUF:
|
|
|
|
if (*optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*(int *) optval = (optname == SO_RCVBUF) ? rmem () : wmem ();
|
|
|
|
break;
|
|
|
|
|
2018-02-24 03:59:21 +08:00
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
|
|
|
{
|
|
|
|
struct timeval *time_out = (struct timeval *) optval;
|
|
|
|
|
|
|
|
if (*optlen < (socklen_t) sizeof *time_out)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
DWORD ms = (optname == SO_RCVTIMEO) ? rcvtimeo () : sndtimeo ();
|
|
|
|
if (ms == 0 || ms == INFINITE)
|
|
|
|
{
|
|
|
|
time_out->tv_sec = 0;
|
|
|
|
time_out->tv_usec = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
time_out->tv_sec = ms / MSPERSEC;
|
|
|
|
time_out->tv_usec = ((ms % MSPERSEC) * USPERSEC) / MSPERSEC;
|
|
|
|
}
|
|
|
|
*optlen = (socklen_t) sizeof *time_out;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SO_TYPE:
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
if (*optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
unsigned int *type = (unsigned int *) optval;
|
|
|
|
*type = get_socket_type ();
|
|
|
|
*optlen = (socklen_t) sizeof *type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AF_UNIX sockets simply ignore all other SOL_SOCKET options. */
|
|
|
|
|
|
|
|
case SO_LINGER:
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
if (*optlen < (socklen_t) sizeof (struct linger))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
struct linger *linger = (struct linger *) optval;
|
|
|
|
memset (linger, 0, sizeof *linger);
|
|
|
|
*optlen = (socklen_t) sizeof *linger;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
if (*optlen < (socklen_t) sizeof (int))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
unsigned int *val = (unsigned int *) optval;
|
|
|
|
*val = 0;
|
|
|
|
*optlen = (socklen_t) sizeof *val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_errno (ENOPROTOOPT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
2018-03-07 01:42:12 +08:00
|
|
|
int ret = -1;
|
2018-02-24 03:59:21 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIOASYNC:
|
|
|
|
#ifdef __x86_64__
|
|
|
|
case _IOW('f', 125, int):
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case FIONREAD:
|
|
|
|
#ifdef __x86_64__
|
|
|
|
case _IOR('f', 127, int):
|
|
|
|
#endif
|
|
|
|
case FIONBIO:
|
2018-03-07 01:55:03 +08:00
|
|
|
{
|
|
|
|
const bool was_nonblocking = is_nonblocking ();
|
|
|
|
set_nonblocking (*(int *) p);
|
|
|
|
const bool now_nonblocking = is_nonblocking ();
|
|
|
|
if (was_nonblocking != now_nonblocking)
|
|
|
|
set_pipe_non_blocking (now_nonblocking);
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
case SIOCATMARK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = fhandler_socket::ioctl (cmd, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::fcntl (int cmd, intptr_t arg)
|
|
|
|
{
|
2021-02-24 00:48:46 +08:00
|
|
|
if (get_flags () & O_PATH)
|
|
|
|
/* We're viewing the socket as a disk file, but
|
|
|
|
fhandler_base::fcntl suffices here. */
|
|
|
|
return fhandler_base::fcntl (cmd, arg);
|
|
|
|
|
2018-03-07 01:55:03 +08:00
|
|
|
int ret = -1;
|
2018-02-24 03:59:21 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_SETOWN:
|
|
|
|
break;
|
|
|
|
case F_GETOWN:
|
|
|
|
break;
|
2018-03-07 01:55:03 +08:00
|
|
|
case F_SETFL:
|
|
|
|
{
|
|
|
|
const bool was_nonblocking = is_nonblocking ();
|
|
|
|
const int allowed_flags = O_APPEND | O_NONBLOCK_MASK;
|
|
|
|
int new_flags = arg & allowed_flags;
|
|
|
|
if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
|
|
|
|
new_flags &= ~OLD_O_NDELAY;
|
|
|
|
set_flags ((get_flags () & ~allowed_flags) | new_flags);
|
|
|
|
const bool now_nonblocking = is_nonblocking ();
|
|
|
|
if (was_nonblocking != now_nonblocking)
|
|
|
|
set_pipe_non_blocking (now_nonblocking);
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
default:
|
|
|
|
ret = fhandler_socket::fcntl (cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket_unix::fstat (struct stat *buf)
|
|
|
|
{
|
2021-02-22 22:08:04 +08:00
|
|
|
if (!dev ().isfs ())
|
|
|
|
/* fstat called on a socket. */
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::fstat (buf);
|
2021-02-22 22:08:04 +08:00
|
|
|
|
|
|
|
/* stat/lstat on a socket file or fstat on a socket opened w/ O_PATH. */
|
|
|
|
int ret = fhandler_base::fstat_fs (buf);
|
2018-02-24 03:59:21 +08:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFSOCK;
|
|
|
|
buf->st_size = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket_unix::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
2021-02-22 22:21:00 +08:00
|
|
|
if (!dev ().isfs ())
|
|
|
|
/* fstatvfs called on a socket. */
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::fstatvfs (sfs);
|
2021-02-22 22:21:00 +08:00
|
|
|
|
|
|
|
/* statvfs on a socket file or fstatvfs on a socket opened w/ O_PATH. */
|
|
|
|
if (get_flags () & O_PATH)
|
|
|
|
/* We already have a handle. */
|
|
|
|
{
|
|
|
|
HANDLE h = get_handle ();
|
|
|
|
if (h)
|
|
|
|
return fstatvfs_by_handle (h, sfs);
|
|
|
|
}
|
2018-02-24 03:59:21 +08:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
fh.get_device () = FH_FS;
|
|
|
|
return fh.fstatvfs (sfs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::fchmod (mode_t newmode)
|
|
|
|
{
|
2021-02-22 22:34:36 +08:00
|
|
|
if (!dev ().isfs ())
|
|
|
|
/* fchmod called on a socket. */
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::fchmod (newmode);
|
2021-02-22 22:34:36 +08:00
|
|
|
|
|
|
|
/* chmod on a socket file. [We won't get here if fchmod is called
|
|
|
|
on a socket opened w/ O_PATH.] */
|
2018-02-24 03:59:21 +08:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
fh.get_device () = FH_FS;
|
2018-02-27 00:58:46 +08:00
|
|
|
/* Kludge: Don't allow to remove read bit on socket files for
|
|
|
|
user/group/other, if the accompanying write bit is set. It would
|
|
|
|
be nice to have exact permissions on a socket file, but it's
|
|
|
|
necessary that somebody able to access the socket can always read
|
|
|
|
the contents of the socket file to avoid spurious "permission
|
|
|
|
denied" messages. */
|
|
|
|
newmode |= (newmode & (S_IWUSR | S_IWGRP | S_IWOTH)) << 1;
|
|
|
|
return fh.fchmod (S_IFSOCK | newmode);
|
2018-02-24 03:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::fchown (uid_t uid, gid_t gid)
|
|
|
|
{
|
2021-02-22 22:50:34 +08:00
|
|
|
if (!dev ().isfs ())
|
|
|
|
/* fchown called on a socket. */
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::fchown (uid, gid);
|
2021-02-22 22:50:34 +08:00
|
|
|
|
|
|
|
/* chown/lchown on a socket file. [We won't get here if fchown is
|
|
|
|
called on a socket opened w/ O_PATH.] */
|
2018-02-24 03:59:21 +08:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
return fh.fchown (uid, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::facl (int cmd, int nentries, aclent_t *aclbufp)
|
|
|
|
{
|
2021-02-22 23:13:09 +08:00
|
|
|
if (!dev ().isfs ())
|
|
|
|
/* facl called on a socket. */
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::facl (cmd, nentries, aclbufp);
|
2021-02-22 23:13:09 +08:00
|
|
|
|
|
|
|
/* facl on a socket file. [We won't get here if facl is called on a
|
|
|
|
socket opened w/ O_PATH.] */
|
2018-02-24 03:59:21 +08:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
return fh.facl (cmd, nentries, aclbufp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket_unix::link (const char *newpath)
|
|
|
|
{
|
2018-03-19 03:46:43 +08:00
|
|
|
if (sun_path ()
|
|
|
|
&& (sun_path ()->un_len <= (socklen_t) sizeof (sa_family_t)
|
|
|
|
|| sun_path ()->un.sun_path[0] == '\0'))
|
2018-02-24 03:59:21 +08:00
|
|
|
return fhandler_socket::link (newpath);
|
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
return fh.link (newpath);
|
|
|
|
}
|
2018-06-26 22:31:17 +08:00
|
|
|
|
|
|
|
#endif /* __WITH_AF_UNIX */
|