* fhandler_socket.cc (get_inet_addr): Use NT native functions for

accessing the file representing the local socket.
	(fhandler_socket::bind): Ditto.
	* ntdll.h: Declare NtReadFile.
This commit is contained in:
Corinna Vinschen 2007-08-15 15:17:05 +00:00
parent 3b98bacfd6
commit d85bcb4542
3 changed files with 51 additions and 21 deletions

View File

@ -1,3 +1,10 @@
2007-08-15 Corinna Vinschen <corinna@vinschen.de>
* fhandler_socket.cc (get_inet_addr): Use NT native functions for
accessing the file representing the local socket.
(fhandler_socket::bind): Ditto.
* ntdll.h: Declare NtReadFile.
2007-08-14 Corinna Vinschen <corinna@vinschen.de> 2007-08-14 Corinna Vinschen <corinna@vinschen.de>
* path.cc (symlink_info::check): In case of ERROR_SHARING_VIOLATION and * path.cc (symlink_info::check): In case of ERROR_SHARING_VIOLATION and

View File

@ -39,6 +39,7 @@
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include "cygtls.h" #include "cygtls.h"
#include "cygwin/in6.h" #include "cygwin/in6.h"
#include "ntdll.h"
#define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT) #define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT)
#define EVENT_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE) #define EVENT_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
@ -79,6 +80,11 @@ get_inet_addr (const struct sockaddr *in, int inlen,
} }
else if (in->sa_family == AF_LOCAL) else if (in->sa_family == AF_LOCAL)
{ {
NTSTATUS status;
HANDLE fh;
OBJECT_ATTRIBUTES attr;
IO_STATUS_BLOCK io;
path_conv pc (in->sa_data, PC_SYM_FOLLOW); path_conv pc (in->sa_data, PC_SYM_FOLLOW);
if (pc.error) if (pc.error)
{ {
@ -95,19 +101,22 @@ get_inet_addr (const struct sockaddr *in, int inlen,
set_errno (EBADF); set_errno (EBADF);
return 0; return 0;
} }
HANDLE fh = CreateFile (pc, GENERIC_READ, FILE_SHARE_VALID_FLAGS, status = NtOpenFile (&fh, GENERIC_READ | SYNCHRONIZE,
&sec_none, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, pc.get_object_attr (attr, sec_none_nih), &io,
0); FILE_SHARE_VALID_FLAGS,
if (fh == INVALID_HANDLE_VALUE) FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT);
if (!NT_SUCCESS (status))
{ {
__seterrno (); __seterrno_from_nt_status (status);
return 0; return 0;
} }
int ret = 0; int ret = 0;
DWORD len = 0;
char buf[128]; char buf[128];
memset (buf, 0, sizeof buf); memset (buf, 0, sizeof buf);
if (ReadFile (fh, buf, 128, &len, 0)) status = NtReadFile (fh, NULL, NULL, NULL, &io, buf, 128, NULL, NULL);
NtClose (fh);
if (NT_SUCCESS (status))
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
char ctype; char ctype;
@ -127,8 +136,7 @@ get_inet_addr (const struct sockaddr *in, int inlen,
ret = 1; ret = 1;
} }
else else
__seterrno (); __seterrno_from_nt_status (status);
CloseHandle (fh);
return ret; return ret;
} }
else else
@ -813,35 +821,47 @@ fhandler_socket::bind (const struct sockaddr *name, int namelen)
} }
mode_t mode = adjust_socket_file_mode ((S_IRWXU | S_IRWXG | S_IRWXO) mode_t mode = adjust_socket_file_mode ((S_IRWXU | S_IRWXG | S_IRWXO)
& ~cygheap->umask); & ~cygheap->umask);
DWORD attr = FILE_ATTRIBUTE_SYSTEM; DWORD fattr = FILE_ATTRIBUTE_SYSTEM;
if (!(mode & (S_IWUSR | S_IWGRP | S_IWOTH))) if (!(mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
attr |= FILE_ATTRIBUTE_READONLY; fattr |= FILE_ATTRIBUTE_READONLY;
SECURITY_ATTRIBUTES sa = sec_none; SECURITY_ATTRIBUTES sa = sec_none_nih;
security_descriptor sd; security_descriptor sd;
if (allow_ntsec && pc.has_acls ()) if (allow_ntsec && pc.has_acls ())
set_security_attribute (mode, &sa, sd); set_security_attribute (mode, &sa, sd);
HANDLE fh = CreateFile (pc, GENERIC_WRITE, 0, &sa, CREATE_NEW, attr, 0); NTSTATUS status;
if (fh == INVALID_HANDLE_VALUE) HANDLE fh;
OBJECT_ATTRIBUTES attr;
IO_STATUS_BLOCK io;
status = NtCreateFile (&fh, GENERIC_WRITE | SYNCHRONIZE,
pc.get_object_attr (attr, sa), &io, NULL, fattr,
FILE_SHARE_VALID_FLAGS, FILE_CREATE,
FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT,
NULL, 0);
if (!NT_SUCCESS (status))
{ {
if (GetLastError () == ERROR_ALREADY_EXISTS) if (io.Information == FILE_EXISTS)
set_errno (EADDRINUSE); set_errno (EADDRINUSE);
else else
__seterrno (); __seterrno_from_nt_status (status);
} }
char buf[sizeof (SOCKET_COOKIE) + 80]; char buf[sizeof (SOCKET_COOKIE) + 80];
__small_sprintf (buf, "%s%u %c ", SOCKET_COOKIE, sin.sin_port, get_socket_type () == SOCK_STREAM ? 's' : get_socket_type () == SOCK_DGRAM ? 'd' : '-'); __small_sprintf (buf, "%s%u %c ", SOCKET_COOKIE, sin.sin_port, get_socket_type () == SOCK_STREAM ? 's' : get_socket_type () == SOCK_DGRAM ? 'd' : '-');
af_local_set_secret (strchr (buf, '\0')); af_local_set_secret (strchr (buf, '\0'));
DWORD blen = strlen (buf) + 1; DWORD blen = strlen (buf) + 1;
if (!WriteFile (fh, buf, blen, &blen, 0)) status = NtWriteFile (fh, NULL, NULL, NULL, &io, buf, blen, NULL, 0);
NtClose (fh);
if (!NT_SUCCESS (status))
{ {
__seterrno (); extern NTSTATUS unlink_nt (path_conv &pc);
CloseHandle (fh);
DeleteFile (pc); __seterrno_from_nt_status (status);
unlink_nt (pc);
} }
else else
{ {
CloseHandle (fh);
set_sun_path (un_addr->sun_path); set_sun_path (un_addr->sun_path);
res = 0; res = 0;
} }

View File

@ -811,6 +811,9 @@ extern "C"
NTSTATUS NTAPI NtQueryVolumeInformationFile (HANDLE, IO_STATUS_BLOCK *, NTSTATUS NTAPI NtQueryVolumeInformationFile (HANDLE, IO_STATUS_BLOCK *,
VOID *, ULONG, VOID *, ULONG,
FS_INFORMATION_CLASS); FS_INFORMATION_CLASS);
NTSTATUS NTAPI NtReadFile (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID,
PIO_STATUS_BLOCK, PVOID, ULONG, PLARGE_INTEGER,
PULONG);
NTSTATUS NTAPI NtSetEaFile (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG); NTSTATUS NTAPI NtSetEaFile (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG);
NTSTATUS NTAPI NtSetInformationFile (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, NTSTATUS NTAPI NtSetInformationFile (HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG,
FILE_INFORMATION_CLASS); FILE_INFORMATION_CLASS);