1 /* fhandler_socket.cc. See fhandler.h for a description of the fhandler classes.
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009, 2010, 2011, 2012 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
12 /* #define DEBUG_NEST_ON 1 */
14 #define __INSIDE_CYGWIN_NET__
18 #include <asm/byteorder.h>
21 #define USE_SYS_TYPES_FD_SET
27 #include "cygwin/version.h"
28 #include "perprocess.h"
33 #include "shared_info.h"
37 #include <sys/param.h>
40 #include "cygwin/in6.h"
42 #include "miscfuncs.h"
44 #define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT)
45 #define EVENT_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
47 extern bool fdsock (cygheap_fdmanip
& fd
, const device
*, SOCKET soc
);
49 int sscanf (const char *, const char *, ...);
50 } /* End of "C" section */
52 fhandler_dev_random
* entropy_source
;
55 adjust_socket_file_mode (mode_t mode
)
57 /* Kludge: Don't allow to remove read bit on socket files for
58 user/group/other, if the accompanying write bit is set. It would
59 be nice to have exact permissions on a socket file, but it's
60 necessary that somebody able to access the socket can always read
61 the contents of the socket file to avoid spurious "permission
63 return mode
| ((mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) << 1);
66 /* cygwin internal: map sockaddr into internet domain address */
68 get_inet_addr (const struct sockaddr
*in
, int inlen
,
69 struct sockaddr_storage
*out
, int *outlen
,
70 int *type
= NULL
, int *secret
= NULL
)
73 int* secret_ptr
= (secret
? : secret_buf
);
75 switch (in
->sa_family
)
81 memcpy (out
, in
, inlen
);
85 set_errno (EAFNOSUPPORT
);
88 /* AF_LOCAL/AF_UNIX only */
89 path_conv
pc (in
->sa_data
, PC_SYM_FOLLOW
);
100 /* Do NOT test for the file being a socket file here. The socket file
101 creation is not an atomic operation, so there is a chance that socket
102 files which are just in the process of being created are recognized
103 as non-socket files. To work around this problem we now create the
104 file with all sharing disabled. If the below NtOpenFile fails
105 with STATUS_SHARING_VIOLATION we know that the file already exists,
106 but the creating process isn't finished yet. So we yield and try
107 again, until we can either open the file successfully, or some error
108 other than STATUS_SHARING_VIOLATION occurs.
109 Since we now don't know if the file is actually a socket file, we
110 perform this check here explicitely. */
113 OBJECT_ATTRIBUTES attr
;
116 pc
.get_object_attr (attr
, sec_none_nih
);
119 status
= NtOpenFile (&fh
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
,
120 FILE_SHARE_VALID_FLAGS
,
121 FILE_SYNCHRONOUS_IO_NONALERT
122 | FILE_OPEN_FOR_BACKUP_INTENT
123 | FILE_NON_DIRECTORY_FILE
);
124 if (status
== STATUS_SHARING_VIOLATION
)
126 /* While we hope that the sharing violation is only temporary, we
127 also could easily get stuck here, waiting for a file in use by
128 some greedy Win32 application. Therefore we should never wait
129 endlessly without checking for signals and thread cancel event. */
130 pthread_testcancel ();
131 /* Using IsEventSignalled like this is racy since another thread could
132 be waiting for signal_arrived. */
133 if (IsEventSignalled (signal_arrived
)
134 && !_my_tls
.call_signal_handler ())
141 else if (!NT_SUCCESS (status
))
143 __seterrno_from_nt_status (status
);
147 while (status
== STATUS_SHARING_VIOLATION
);
148 /* Now test for the SYSTEM bit. */
149 FILE_BASIC_INFORMATION fbi
;
150 status
= NtQueryInformationFile (fh
, &io
, &fbi
, sizeof fbi
,
151 FileBasicInformation
);
152 if (!NT_SUCCESS (status
))
154 __seterrno_from_nt_status (status
);
157 if (!(fbi
.FileAttributes
& FILE_ATTRIBUTE_SYSTEM
))
163 /* Eventually check the content and fetch the required information. */
165 memset (buf
, 0, sizeof buf
);
166 status
= NtReadFile (fh
, NULL
, NULL
, NULL
, &io
, buf
, 128, NULL
, NULL
);
168 if (NT_SUCCESS (status
))
170 struct sockaddr_in sin
;
172 sin
.sin_family
= AF_INET
;
173 if (strncmp (buf
, SOCKET_COOKIE
, strlen (SOCKET_COOKIE
)))
178 sscanf (buf
+ strlen (SOCKET_COOKIE
), "%hu %c %08x-%08x-%08x-%08x",
181 secret_ptr
, secret_ptr
+ 1, secret_ptr
+ 2, secret_ptr
+ 3);
182 sin
.sin_port
= htons (sin
.sin_port
);
183 sin
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
184 memcpy (out
, &sin
, sizeof sin
);
185 *outlen
= sizeof sin
;
187 *type
= (ctype
== 's' ? SOCK_STREAM
:
188 ctype
== 'd' ? SOCK_DGRAM
192 __seterrno_from_nt_status (status
);
196 /**********************************************************************/
197 /* fhandler_socket */
199 fhandler_socket::fhandler_socket () :
204 prot_info_ptr (NULL
),
206 peer_sun_path (NULL
),
209 need_fork_fixup (true);
212 fhandler_socket::~fhandler_socket ()
215 cfree (prot_info_ptr
);
219 cfree (peer_sun_path
);
223 fhandler_socket::get_proc_fd_name (char *buf
)
225 __small_sprintf (buf
, "socket:[%d]", get_socket ());
230 fhandler_socket::open (int flags
, mode_t mode
)
237 fhandler_socket::af_local_set_sockpair_cred ()
239 sec_pid
= sec_peer_pid
= getpid ();
240 sec_uid
= sec_peer_uid
= geteuid32 ();
241 sec_gid
= sec_peer_gid
= getegid32 ();
245 fhandler_socket::af_local_setblocking (bool &async
, bool &nonblocking
)
248 nonblocking
= is_nonblocking ();
251 WSAAsyncSelect (get_socket (), winmsg
, 0, 0);
252 WSAEventSelect (get_socket (), wsock_evt
, EVENT_MASK
);
254 set_nonblocking (false);
259 fhandler_socket::af_local_unsetblocking (bool async
, bool nonblocking
)
262 set_nonblocking (true);
265 WSAAsyncSelect (get_socket (), winmsg
, WM_ASYNCIO
, ASYNC_MASK
);
271 fhandler_socket::af_local_recv_secret ()
273 int out
[4] = { 0, 0, 0, 0 };
274 int rest
= sizeof out
;
275 char *ptr
= (char *) out
;
278 int ret
= recvfrom (ptr
, rest
, 0, NULL
, NULL
);
286 debug_printf ("Received af_local secret: %08x-%08x-%08x-%08x",
287 out
[0], out
[1], out
[2], out
[3]);
288 if (out
[0] != connect_secret
[0] || out
[1] != connect_secret
[1]
289 || out
[2] != connect_secret
[2] || out
[3] != connect_secret
[3])
291 debug_printf ("Receiving af_local secret mismatch");
296 debug_printf ("Receiving af_local secret failed");
301 fhandler_socket::af_local_send_secret ()
303 int rest
= sizeof connect_secret
;
304 char *ptr
= (char *) connect_secret
;
307 int ret
= sendto (ptr
, rest
, 0, NULL
, 0);
313 debug_printf ("Sending af_local secret %s", rest
== 0 ? "succeeded"
319 fhandler_socket::af_local_recv_cred ()
321 struct ucred out
= { (pid_t
) 0, (__uid32_t
) -1, (__gid32_t
) -1 };
322 int rest
= sizeof out
;
323 char *ptr
= (char *) &out
;
326 int ret
= recvfrom (ptr
, rest
, 0, NULL
, NULL
);
334 debug_printf ("Received eid credentials: pid: %d, uid: %d, gid: %d",
335 out
.pid
, out
.uid
, out
.gid
);
336 sec_peer_pid
= out
.pid
;
337 sec_peer_uid
= out
.uid
;
338 sec_peer_gid
= out
.gid
;
341 debug_printf ("Receiving eid credentials failed");
346 fhandler_socket::af_local_send_cred ()
348 struct ucred in
= { sec_pid
, sec_uid
, sec_gid
};
349 int rest
= sizeof in
;
350 char *ptr
= (char *) &in
;
353 int ret
= sendto (ptr
, rest
, 0, NULL
, 0);
360 debug_printf ("Sending eid credentials succeeded");
362 debug_printf ("Sending eid credentials failed");
367 fhandler_socket::af_local_connect ()
369 /* This keeps the test out of select. */
370 if (get_addr_family () != AF_LOCAL
|| get_socket_type () != SOCK_STREAM
)
373 debug_printf ("af_local_connect called");
374 bool orig_async_io
, orig_is_nonblocking
;
375 af_local_setblocking (orig_async_io
, orig_is_nonblocking
);
376 if (!af_local_send_secret () || !af_local_recv_secret ()
377 || !af_local_send_cred () || !af_local_recv_cred ())
379 debug_printf ("accept from unauthorized server");
380 ::shutdown (get_socket (), SD_BOTH
);
381 WSASetLastError (WSAECONNREFUSED
);
384 af_local_unsetblocking (orig_async_io
, orig_is_nonblocking
);
389 fhandler_socket::af_local_accept ()
391 debug_printf ("af_local_accept called");
392 bool orig_async_io
, orig_is_nonblocking
;
393 af_local_setblocking (orig_async_io
, orig_is_nonblocking
);
394 if (!af_local_recv_secret () || !af_local_send_secret ()
395 || !af_local_recv_cred () || !af_local_send_cred ())
397 debug_printf ("connect from unauthorized client");
398 ::shutdown (get_socket (), SD_BOTH
);
399 ::closesocket (get_socket ());
400 WSASetLastError (WSAECONNABORTED
);
403 af_local_unsetblocking (orig_async_io
, orig_is_nonblocking
);
408 fhandler_socket::af_local_set_cred ()
411 sec_uid
= geteuid32 ();
412 sec_gid
= getegid32 ();
413 sec_peer_pid
= (pid_t
) 0;
414 sec_peer_uid
= (__uid32_t
) -1;
415 sec_peer_gid
= (__gid32_t
) -1;
419 fhandler_socket::af_local_copy (fhandler_socket
*sock
)
421 sock
->connect_secret
[0] = connect_secret
[0];
422 sock
->connect_secret
[1] = connect_secret
[1];
423 sock
->connect_secret
[2] = connect_secret
[2];
424 sock
->connect_secret
[3] = connect_secret
[3];
425 sock
->sec_pid
= sec_pid
;
426 sock
->sec_uid
= sec_uid
;
427 sock
->sec_gid
= sec_gid
;
428 sock
->sec_peer_pid
= sec_peer_pid
;
429 sock
->sec_peer_uid
= sec_peer_uid
;
430 sock
->sec_peer_gid
= sec_peer_gid
;
434 fhandler_socket::af_local_set_secret (char *buf
)
438 void *buf
= malloc (sizeof (fhandler_dev_random
));
439 entropy_source
= new (buf
) fhandler_dev_random ();
440 entropy_source
->dev () = *urandom_dev
;
442 if (entropy_source
&&
443 !entropy_source
->open (O_RDONLY
))
445 delete entropy_source
;
446 entropy_source
= NULL
;
450 size_t len
= sizeof (connect_secret
);
451 entropy_source
->read (connect_secret
, len
);
452 if (len
!= sizeof (connect_secret
))
453 bzero ((char*) connect_secret
, sizeof (connect_secret
));
455 __small_sprintf (buf
, "%08x-%08x-%08x-%08x",
456 connect_secret
[0], connect_secret
[1],
457 connect_secret
[2], connect_secret
[3]);
460 /* Maximum number of concurrently opened sockets from all Cygwin processes
461 per session. Note that shared sockets (through dup/fork/exec) are
462 counted as one socket. */
463 #define NUM_SOCKS (32768 / sizeof (wsa_event))
465 #define LOCK_EVENTS WaitForSingleObject (wsock_mtx, INFINITE)
466 #define UNLOCK_EVENTS ReleaseMutex (wsock_mtx)
468 static wsa_event wsa_events
[NUM_SOCKS
] __attribute__((section (".cygwin_dll_common"), shared
));
470 static LONG socket_serial_number
__attribute__((section (".cygwin_dll_common"), shared
));
472 static HANDLE wsa_slot_mtx
;
475 sock_shared_name (PWCHAR buf
, LONG num
)
477 __small_swprintf (buf
, L
"socket.%d", num
);
482 search_wsa_event_slot (LONG new_serial_number
)
484 WCHAR name
[32], searchname
[32];
485 UNICODE_STRING uname
;
486 OBJECT_ATTRIBUTES attr
;
491 RtlInitUnicodeString (&uname
, sock_shared_name (name
, 0));
492 InitializeObjectAttributes (&attr
, &uname
, OBJ_INHERIT
| OBJ_OPENIF
,
493 get_session_parent_dir (),
494 everyone_sd (CYG_MUTANT_ACCESS
));
495 status
= NtCreateMutant (&wsa_slot_mtx
, CYG_MUTANT_ACCESS
, &attr
, FALSE
);
496 if (!NT_SUCCESS (status
))
497 api_fatal ("Couldn't create/open shared socket mutex %S, %p",
500 switch (WaitForSingleObject (wsa_slot_mtx
, INFINITE
))
506 api_fatal ("WFSO failed for shared socket mutex, %E");
509 unsigned int slot
= new_serial_number
% NUM_SOCKS
;
510 while (wsa_events
[slot
].serial_number
)
513 RtlInitUnicodeString (&uname
, sock_shared_name (searchname
,
514 wsa_events
[slot
].serial_number
));
515 InitializeObjectAttributes (&attr
, &uname
, 0, get_session_parent_dir (),
517 status
= NtOpenMutant (&searchmtx
, READ_CONTROL
, &attr
);
518 if (!NT_SUCCESS (status
))
520 /* Mutex still exists, attached socket is active, try next slot. */
522 slot
= (slot
+ 1) % NUM_SOCKS
;
523 if (slot
== (new_serial_number
% NUM_SOCKS
))
525 /* Did the whole array once. Too bad. */
526 debug_printf ("No free socket slot");
527 ReleaseMutex (wsa_slot_mtx
);
531 memset (&wsa_events
[slot
], 0, sizeof (wsa_event
));
532 wsa_events
[slot
].serial_number
= new_serial_number
;
533 ReleaseMutex (wsa_slot_mtx
);
534 return wsa_events
+ slot
;
538 fhandler_socket::init_events ()
540 LONG new_serial_number
;
542 UNICODE_STRING uname
;
543 OBJECT_ATTRIBUTES attr
;
549 InterlockedIncrement (&socket_serial_number
);
550 if (!new_serial_number
) /* 0 is reserved for global mutex */
551 InterlockedIncrement (&socket_serial_number
);
552 RtlInitUnicodeString (&uname
, sock_shared_name (name
, new_serial_number
));
553 InitializeObjectAttributes (&attr
, &uname
, OBJ_INHERIT
| OBJ_OPENIF
,
554 get_session_parent_dir (),
555 everyone_sd (CYG_MUTANT_ACCESS
));
556 status
= NtCreateMutant (&wsock_mtx
, CYG_MUTANT_ACCESS
, &attr
, FALSE
);
557 if (!NT_SUCCESS (status
))
559 debug_printf ("NtCreateMutant(%S), %p", &uname
, status
);
563 if (status
== STATUS_OBJECT_NAME_EXISTS
)
566 while (status
== STATUS_OBJECT_NAME_EXISTS
);
567 if ((wsock_evt
= CreateEvent (&sec_all
, TRUE
, FALSE
, NULL
))
568 == WSA_INVALID_EVENT
)
570 debug_printf ("CreateEvent, %E");
575 if (WSAEventSelect (get_socket (), wsock_evt
, EVENT_MASK
) == SOCKET_ERROR
)
577 debug_printf ("WSAEventSelect, %E");
578 set_winsock_errno ();
583 wsock_events
= search_wsa_event_slot (new_serial_number
);
584 /* sock type not yet set here. */
585 if (pc
.dev
== FH_UDP
|| pc
.dev
== FH_DGRAM
)
586 wsock_events
->events
= FD_WRITE
;
591 fhandler_socket::evaluate_events (const long event_mask
, long &events
,
597 WSANETWORKEVENTS evts
= { 0 };
598 if (!(WSAEnumNetworkEvents (get_socket (), wsock_evt
, &evts
)))
600 if (evts
.lNetworkEvents
)
603 wsock_events
->events
|= evts
.lNetworkEvents
;
604 events_now
= (wsock_events
->events
& event_mask
);
605 if (evts
.lNetworkEvents
& FD_CONNECT
)
606 wsock_events
->connect_errorcode
= evts
.iErrorCode
[FD_CONNECT_BIT
];
608 if ((evts
.lNetworkEvents
& FD_OOB
) && wsock_events
->owner
)
609 kill (wsock_events
->owner
, SIGURG
);
614 if ((events
= events_now
) != 0
615 || (events
= (wsock_events
->events
& event_mask
)) != 0)
617 if (events
& FD_CONNECT
)
620 if ((wsa_err
= wsock_events
->connect_errorcode
) != 0)
622 WSASetLastError (wsa_err
);
626 wsock_events
->events
|= FD_WRITE
;
627 wsock_events
->events
&= ~FD_CONNECT
;
628 wsock_events
->connect_errorcode
= 0;
630 /* This test makes the accept function behave as on Linux when
631 accept is called on a socket for which shutdown for the read side
632 has been called. The second half of this code is in the shutdown
633 method. See there for more info. */
634 if ((event_mask
& FD_ACCEPT
) && (events
& FD_CLOSE
))
636 WSASetLastError (WSAEINVAL
);
640 wsock_events
->events
&= ~(events
& ~(FD_WRITE
| FD_CLOSE
));
648 fhandler_socket::wait_for_events (const long event_mask
, const DWORD flags
)
656 while (!(ret
= evaluate_events (event_mask
, events
, !(flags
& MSG_PEEK
)))
659 if (is_nonblocking () || (flags
& MSG_DONTWAIT
))
661 WSASetLastError (WSAEWOULDBLOCK
);
665 WSAEVENT ev
[2] = { wsock_evt
, signal_arrived
};
666 switch (WSAWaitForMultipleEvents (2, ev
, FALSE
, 50, FALSE
))
668 case WSA_WAIT_TIMEOUT
:
669 pthread_testcancel ();
671 case WSA_WAIT_EVENT_0
:
674 case WSA_WAIT_EVENT_0
+ 1:
675 if (_my_tls
.call_signal_handler ())
677 WSASetLastError (WSAEINTR
);
681 WSASetLastError (WSAEFAULT
);
690 fhandler_socket::release_events ()
696 /* Called from net.cc:fdsock() if a freshly created socket is not
697 inheritable. In that case we use fixup_before_fork_exec. See
698 the comment in fdsock() for a description of the problem. */
700 fhandler_socket::init_fixup_before ()
702 prot_info_ptr
= (LPWSAPROTOCOL_INFOW
)
703 cmalloc_abort (HEAP_BUF
, sizeof (WSAPROTOCOL_INFOW
));
704 cygheap
->fdtab
.inc_need_fixup_before ();
708 fhandler_socket::fixup_before_fork_exec (DWORD win_pid
)
710 SOCKET ret
= WSADuplicateSocketW (get_socket (), win_pid
, prot_info_ptr
);
712 set_winsock_errno ();
714 debug_printf ("WSADuplicateSocket succeeded (%lx)", prot_info_ptr
->dwProviderReserved
);
719 fhandler_socket::fixup_after_fork (HANDLE parent
)
721 fork_fixup (parent
, wsock_mtx
, "wsock_mtx");
722 fork_fixup (parent
, wsock_evt
, "wsock_evt");
724 if (!need_fixup_before ())
726 fhandler_base::fixup_after_fork (parent
);
730 SOCKET new_sock
= WSASocketW (FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
,
731 FROM_PROTOCOL_INFO
, prot_info_ptr
, 0,
732 WSA_FLAG_OVERLAPPED
);
733 if (new_sock
== INVALID_SOCKET
)
735 set_winsock_errno ();
736 set_io_handle ((HANDLE
) INVALID_SOCKET
);
740 /* Even though the original socket was not inheritable, the duplicated
741 socket is potentially inheritable again. */
742 SetHandleInformation ((HANDLE
) new_sock
, HANDLE_FLAG_INHERIT
, 0);
743 set_io_handle ((HANDLE
) new_sock
);
744 debug_printf ("WSASocket succeeded (%lx)", new_sock
);
749 fhandler_socket::fixup_after_exec ()
751 if (need_fixup_before () && !close_on_exec ())
752 fixup_after_fork (NULL
);
756 fhandler_socket::dup (fhandler_base
*child
, int flags
)
758 debug_printf ("here");
759 fhandler_socket
*fhs
= (fhandler_socket
*) child
;
761 if (!DuplicateHandle (GetCurrentProcess (), wsock_mtx
,
762 GetCurrentProcess (), &fhs
->wsock_mtx
,
763 0, TRUE
, DUPLICATE_SAME_ACCESS
))
768 if (!DuplicateHandle (GetCurrentProcess (), wsock_evt
,
769 GetCurrentProcess (), &fhs
->wsock_evt
,
770 0, TRUE
, DUPLICATE_SAME_ACCESS
))
773 NtClose (fhs
->wsock_mtx
);
776 if (get_addr_family () == AF_LOCAL
)
778 fhs
->set_sun_path (get_sun_path ());
779 fhs
->set_peer_sun_path (get_peer_sun_path ());
781 if (!need_fixup_before ())
783 int ret
= fhandler_base::dup (child
, flags
);
786 NtClose (fhs
->wsock_evt
);
787 NtClose (fhs
->wsock_mtx
);
792 cygheap
->user
.deimpersonate ();
793 fhs
->init_fixup_before ();
794 fhs
->set_io_handle (get_io_handle ());
795 int ret
= fhs
->fixup_before_fork_exec (GetCurrentProcessId ());
796 cygheap
->user
.reimpersonate ();
799 fhs
->fixup_after_fork (GetCurrentProcess ());
800 if (fhs
->get_io_handle() != (HANDLE
) INVALID_SOCKET
)
803 cygheap
->fdtab
.dec_need_fixup_before ();
804 NtClose (fhs
->wsock_evt
);
805 NtClose (fhs
->wsock_mtx
);
810 fhandler_socket::fstat (struct __stat64
*buf
)
813 if (get_device () == FH_UNIX
)
815 res
= fhandler_base::fstat_fs (buf
);
818 buf
->st_mode
= (buf
->st_mode
& ~S_IFMT
) | S_IFSOCK
;
824 res
= fhandler_base::fstat (buf
);
828 buf
->st_ino
= (__ino64_t
) ((DWORD
) get_handle ());
829 buf
->st_mode
= S_IFSOCK
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
837 fhandler_socket::fstatvfs (struct statvfs
*sfs
)
839 if (get_device () == FH_UNIX
)
841 fhandler_disk_file
fh (pc
);
842 fh
.get_device () = FH_FS
;
843 return fh
.fstatvfs (sfs
);
850 fhandler_socket::fchmod (mode_t mode
)
852 if (get_device () == FH_UNIX
)
854 fhandler_disk_file
fh (pc
);
855 fh
.get_device () = FH_FS
;
856 int ret
= fh
.fchmod (S_IFSOCK
| adjust_socket_file_mode (mode
));
864 fhandler_socket::fchown (__uid32_t uid
, __gid32_t gid
)
866 if (get_device () == FH_UNIX
)
868 fhandler_disk_file
fh (pc
);
869 return fh
.fchown (uid
, gid
);
876 fhandler_socket::facl (int cmd
, int nentries
, __aclent32_t
*aclbufp
)
878 if (get_device () == FH_UNIX
)
880 fhandler_disk_file
fh (pc
);
881 return fh
.facl (cmd
, nentries
, aclbufp
);
888 fhandler_socket::link (const char *newpath
)
890 if (get_device () == FH_UNIX
)
892 fhandler_disk_file
fh (pc
);
893 return fh
.link (newpath
);
895 return fhandler_base::link (newpath
);
899 fhandler_socket::bind (const struct sockaddr
*name
, int namelen
)
903 if (name
->sa_family
== AF_LOCAL
)
905 #define un_addr ((struct sockaddr_un *) name)
906 struct sockaddr_in sin
;
907 int len
= sizeof sin
;
909 if (strlen (un_addr
->sun_path
) >= UNIX_PATH_LEN
)
911 set_errno (ENAMETOOLONG
);
914 sin
.sin_family
= AF_INET
;
916 sin
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
917 if (::bind (get_socket (), (sockaddr
*) &sin
, len
))
919 syscall_printf ("AF_LOCAL: bind failed");
920 set_winsock_errno ();
923 if (::getsockname (get_socket (), (sockaddr
*) &sin
, &len
))
925 syscall_printf ("AF_LOCAL: getsockname failed");
926 set_winsock_errno ();
930 sin
.sin_port
= ntohs (sin
.sin_port
);
931 debug_printf ("AF_LOCAL: socket bound to port %u", sin
.sin_port
);
933 path_conv
pc (un_addr
->sun_path
, PC_SYM_FOLLOW
);
936 set_errno (pc
.error
);
941 set_errno (EADDRINUSE
);
944 mode_t mode
= adjust_socket_file_mode ((S_IRWXU
| S_IRWXG
| S_IRWXO
)
946 DWORD fattr
= FILE_ATTRIBUTE_SYSTEM
;
947 if (!(mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) && !pc
.has_acls ())
948 fattr
|= FILE_ATTRIBUTE_READONLY
;
949 SECURITY_ATTRIBUTES sa
= sec_none_nih
;
952 OBJECT_ATTRIBUTES attr
;
954 ULONG access
= DELETE
| FILE_GENERIC_WRITE
;
956 /* If the filesystem supports ACLs, we will overwrite the DACL after the
957 call to NtCreateFile. This requires a handle with READ_CONTROL and
958 WRITE_DAC access, otherwise get_file_sd and set_file_sd both have to
960 FIXME: On remote NTFS shares open sometimes fails because even the
961 creator of the file doesn't have the right to change the DACL.
962 I don't know what setting that is or how to recognize such a share,
963 so for now we don't request WRITE_DAC on remote drives. */
964 if (pc
.has_acls () && !pc
.isremote ())
965 access
|= READ_CONTROL
| WRITE_DAC
;
967 status
= NtCreateFile (&fh
, access
, pc
.get_object_attr (attr
, sa
), &io
,
968 NULL
, fattr
, 0, FILE_CREATE
,
969 FILE_NON_DIRECTORY_FILE
970 | FILE_SYNCHRONOUS_IO_NONALERT
971 | FILE_OPEN_FOR_BACKUP_INTENT
,
973 if (!NT_SUCCESS (status
))
975 if (io
.Information
== FILE_EXISTS
)
976 set_errno (EADDRINUSE
);
978 __seterrno_from_nt_status (status
);
983 set_file_attribute (fh
, pc
, ILLEGAL_UID
, ILLEGAL_GID
,
984 S_JUSTCREATED
| mode
);
985 char buf
[sizeof (SOCKET_COOKIE
) + 80];
986 __small_sprintf (buf
, "%s%u %c ", SOCKET_COOKIE
, sin
.sin_port
,
987 get_socket_type () == SOCK_STREAM
? 's'
988 : get_socket_type () == SOCK_DGRAM
? 'd' : '-');
989 af_local_set_secret (strchr (buf
, '\0'));
990 DWORD blen
= strlen (buf
) + 1;
991 status
= NtWriteFile (fh
, NULL
, NULL
, NULL
, &io
, buf
, blen
, NULL
, 0);
992 if (!NT_SUCCESS (status
))
994 __seterrno_from_nt_status (status
);
995 FILE_DISPOSITION_INFORMATION fdi
= { TRUE
};
996 status
= NtSetInformationFile (fh
, &io
, &fdi
, sizeof fdi
,
997 FileDispositionInformation
);
998 if (!NT_SUCCESS (status
))
999 debug_printf ("Setting delete dispostion failed, status = %p",
1004 set_sun_path (un_addr
->sun_path
);
1013 if (!saw_reuseaddr ())
1015 /* If the application didn't explicitely request SO_REUSEADDR,
1016 enforce POSIX standard socket binding behaviour by setting the
1017 SO_EXCLUSIVEADDRUSE socket option. See cygwin_setsockopt()
1018 for a more detailed description.
1020 KB 870562: Note that a bug in Win2K SP1-3 and XP up to SP1 only
1021 enables this option for users in the local administrators group. */
1023 int ret
= ::setsockopt (get_socket (), SOL_SOCKET
,
1025 (const char *) &on
, sizeof on
);
1026 debug_printf ("%d = setsockopt(SO_EXCLUSIVEADDRUSE), %E", ret
);
1028 if (::bind (get_socket (), name
, namelen
))
1029 set_winsock_errno ();
1039 fhandler_socket::connect (const struct sockaddr
*name
, int namelen
)
1041 bool in_progress
= false;
1042 struct sockaddr_storage sst
;
1046 pthread_testcancel ();
1048 if (get_inet_addr (name
, namelen
, &sst
, &namelen
, &type
, connect_secret
)
1050 return SOCKET_ERROR
;
1052 if (get_addr_family () == AF_LOCAL
&& get_socket_type () != type
)
1054 WSASetLastError (WSAEPROTOTYPE
);
1055 set_winsock_errno ();
1056 return SOCKET_ERROR
;
1059 int res
= ::connect (get_socket (), (struct sockaddr
*) &sst
, namelen
);
1060 if (!is_nonblocking ()
1061 && res
== SOCKET_ERROR
1062 && WSAGetLastError () == WSAEWOULDBLOCK
)
1063 res
= wait_for_events (FD_CONNECT
| FD_CLOSE
, 0);
1069 err
= WSAGetLastError ();
1070 /* Special handling for connect to return the correct error code
1071 when called on a non-blocking socket. */
1072 if (is_nonblocking ())
1074 if (err
== WSAEWOULDBLOCK
|| err
== WSAEALREADY
)
1077 if (err
== WSAEWOULDBLOCK
)
1078 WSASetLastError (err
= WSAEINPROGRESS
);
1080 if (err
== WSAEINVAL
)
1081 WSASetLastError (err
= WSAEISCONN
);
1082 set_winsock_errno ();
1085 if (get_addr_family () == AF_LOCAL
&& (!res
|| in_progress
))
1086 set_peer_sun_path (name
->sa_data
);
1088 if (get_addr_family () == AF_LOCAL
&& get_socket_type () == SOCK_STREAM
)
1090 af_local_set_cred (); /* Don't move into af_local_connect since
1091 af_local_connect is called from select,
1092 possibly running under another identity. */
1093 if (!res
&& af_local_connect ())
1095 set_winsock_errno ();
1096 return SOCKET_ERROR
;
1100 if (err
== WSAEINPROGRESS
|| err
== WSAEALREADY
)
1101 connect_state (connect_pending
);
1103 connect_state (connect_failed
);
1105 connect_state (connected
);
1111 fhandler_socket::listen (int backlog
)
1113 int res
= ::listen (get_socket (), backlog
);
1114 if (res
&& WSAGetLastError () == WSAEINVAL
)
1116 /* It's perfectly valid to call listen on an unbound INET socket.
1117 In this case the socket is automatically bound to an unused
1118 port number, listening on all interfaces. On WinSock, listen
1119 fails with WSAEINVAL when it's called on an unbound socket.
1120 So we have to bind manually here to have POSIX semantics. */
1121 if (get_addr_family () == AF_INET
)
1123 struct sockaddr_in sin
;
1124 sin
.sin_family
= AF_INET
;
1126 sin
.sin_addr
.s_addr
= INADDR_ANY
;
1127 if (!::bind (get_socket (), (struct sockaddr
*) &sin
, sizeof sin
))
1128 res
= ::listen (get_socket (), backlog
);
1130 else if (get_addr_family () == AF_INET6
)
1132 struct sockaddr_in6 sin6
=
1134 sin6_family
: AF_INET6
,
1137 sin6_addr
: {{IN6ADDR_ANY_INIT
}},
1140 if (!::bind (get_socket (), (struct sockaddr
*) &sin6
, sizeof sin6
))
1141 res
= ::listen (get_socket (), backlog
);
1146 if (get_addr_family () == AF_LOCAL
&& get_socket_type () == SOCK_STREAM
)
1147 af_local_set_cred ();
1148 connect_state (connected
);
1152 set_winsock_errno ();
1157 fhandler_socket::accept4 (struct sockaddr
*peer
, int *len
, int flags
)
1159 /* Allows NULL peer and len parameters. */
1160 struct sockaddr_storage lpeer
;
1161 int llen
= sizeof (struct sockaddr_storage
);
1163 pthread_testcancel ();
1166 while (!(res
= wait_for_events (FD_ACCEPT
| FD_CLOSE
, 0))
1167 && (res
= ::accept (get_socket (), (struct sockaddr
*) &lpeer
, &llen
))
1169 && WSAGetLastError () == WSAEWOULDBLOCK
)
1171 if (res
== (int) INVALID_SOCKET
)
1172 set_winsock_errno ();
1175 cygheap_fdnew res_fd
;
1176 if (res_fd
>= 0 && fdsock (res_fd
, &dev (), res
))
1178 fhandler_socket
*sock
= (fhandler_socket
*) res_fd
;
1179 sock
->set_addr_family (get_addr_family ());
1180 sock
->set_socket_type (get_socket_type ());
1181 sock
->async_io (false); /* fdsock switches async mode off. */
1182 if (get_addr_family () == AF_LOCAL
)
1184 sock
->set_sun_path (get_sun_path ());
1185 sock
->set_peer_sun_path (get_peer_sun_path ());
1186 if (get_socket_type () == SOCK_STREAM
)
1188 /* Don't forget to copy credentials from accepting
1189 socket to accepted socket and start transaction
1190 on accepted socket! */
1191 af_local_copy (sock
);
1192 res
= sock
->af_local_accept ();
1196 set_winsock_errno ();
1201 sock
->set_nonblocking (flags
& SOCK_NONBLOCK
);
1202 if (flags
& SOCK_CLOEXEC
)
1203 sock
->set_close_on_exec (true);
1204 /* No locking necessary at this point. */
1205 sock
->wsock_events
->events
= wsock_events
->events
| FD_WRITE
;
1206 sock
->wsock_events
->owner
= wsock_events
->owner
;
1207 sock
->connect_state (connected
);
1211 if (get_addr_family () == AF_LOCAL
)
1213 /* FIXME: Right now we have no way to determine the
1214 bound socket name of the peer's socket. For now
1215 we just fake an unbound socket on the other side. */
1216 static struct sockaddr_un un
= { AF_LOCAL
, "" };
1217 memcpy (peer
, &un
, MIN (*len
, (int) sizeof (un
.sun_family
)));
1218 *len
= (int) sizeof (un
.sun_family
);
1222 memcpy (peer
, &lpeer
, MIN (*len
, llen
));
1235 debug_printf ("res %d", res
);
1240 fhandler_socket::getsockname (struct sockaddr
*name
, int *namelen
)
1244 if (get_addr_family () == AF_LOCAL
)
1246 struct sockaddr_un sun
;
1247 sun
.sun_family
= AF_LOCAL
;
1248 sun
.sun_path
[0] = '\0';
1249 if (get_sun_path ())
1250 strncat (sun
.sun_path
, get_sun_path (), UNIX_PATH_LEN
- 1);
1251 memcpy (name
, &sun
, MIN (*namelen
, (int) SUN_LEN (&sun
) + 1));
1252 *namelen
= (int) SUN_LEN (&sun
) + (get_sun_path () ? 1 : 0);
1257 /* Always use a local big enough buffer and truncate later as necessary
1258 per POSIX. WinSock unfortunaltey only returns WSAEFAULT if the buffer
1260 struct sockaddr_storage sock
;
1261 int len
= sizeof sock
;
1262 res
= ::getsockname (get_socket (), (struct sockaddr
*) &sock
, &len
);
1265 memcpy (name
, &sock
, MIN (*namelen
, len
));
1270 if (WSAGetLastError () == WSAEINVAL
)
1272 /* WinSock returns WSAEINVAL if the socket is locally
1273 unbound. Per SUSv3 this is not an error condition.
1274 We're faking a valid return value here by creating the
1275 same content in the sockaddr structure as on Linux. */
1276 memset (&sock
, 0, sizeof sock
);
1277 sock
.ss_family
= get_addr_family ();
1278 switch (get_addr_family ())
1282 len
= (int) sizeof (struct sockaddr_in
);
1286 len
= (int) sizeof (struct sockaddr_in6
);
1289 WSASetLastError (WSAEOPNOTSUPP
);
1294 memcpy (name
, &sock
, MIN (*namelen
, len
));
1299 set_winsock_errno ();
1307 fhandler_socket::getpeername (struct sockaddr
*name
, int *namelen
)
1309 /* Always use a local big enough buffer and truncate later as necessary
1310 per POSIX. WinSock unfortunately only returns WSAEFAULT if the buffer
1312 struct sockaddr_storage sock
;
1313 int len
= sizeof sock
;
1314 int res
= ::getpeername (get_socket (), (struct sockaddr
*) &sock
, &len
);
1316 set_winsock_errno ();
1317 else if (get_addr_family () == AF_LOCAL
)
1319 struct sockaddr_un sun
;
1320 memset (&sun
, 0, sizeof sun
);
1321 sun
.sun_family
= AF_LOCAL
;
1322 sun
.sun_path
[0] = '\0';
1323 if (get_peer_sun_path ())
1324 strncat (sun
.sun_path
, get_peer_sun_path (), UNIX_PATH_LEN
- 1);
1325 memcpy (name
, &sun
, MIN (*namelen
, (int) SUN_LEN (&sun
) + 1));
1326 *namelen
= (int) SUN_LEN (&sun
) + (get_peer_sun_path () ? 1 : 0);
1330 memcpy (name
, &sock
, MIN (*namelen
, len
));
1338 fhandler_socket::read (void *in_ptr
, size_t& len
)
1340 WSABUF wsabuf
= { len
, (char *) in_ptr
};
1341 WSAMSG wsamsg
= { NULL
, 0, &wsabuf
, 1, { 0, NULL
}, 0 };
1342 len
= recv_internal (&wsamsg
);
1346 fhandler_socket::readv (const struct iovec
*const iov
, const int iovcnt
,
1349 WSABUF wsabuf
[iovcnt
];
1350 WSABUF
*wsaptr
= wsabuf
+ iovcnt
;
1351 const struct iovec
*iovptr
= iov
+ iovcnt
;
1352 while (--wsaptr
>= wsabuf
)
1354 wsaptr
->len
= (--iovptr
)->iov_len
;
1355 wsaptr
->buf
= (char *) iovptr
->iov_base
;
1357 WSAMSG wsamsg
= { NULL
, 0, wsabuf
, iovcnt
, { 0, NULL
}, 0 };
1358 return recv_internal (&wsamsg
);
1362 #define WSAID_WSARECVMSG \
1363 {0xf689d7c8,0x6f1f,0x436b,{0x8a,0x53,0xe5,0x4f,0xe3,0x51,0xc3,0x22}};
1364 typedef int (WSAAPI
*LPFN_WSARECVMSG
)(SOCKET
,LPWSAMSG
,LPDWORD
,LPWSAOVERLAPPED
,
1365 LPWSAOVERLAPPED_COMPLETION_ROUTINE
);
1366 int WSAAPI
WSASendMsg(SOCKET
,LPWSAMSG
,DWORD
,LPDWORD
, LPWSAOVERLAPPED
,
1367 LPWSAOVERLAPPED_COMPLETION_ROUTINE
);
1370 /* There's no DLL which exports the symbol WSARecvMsg. One has to call
1371 WSAIoctl as below to fetch the function pointer. Why on earth did the
1372 MS developers decide not to export a normal symbol for these extension
1375 get_ext_funcptr (SOCKET sock
, void *funcptr
)
1378 const GUID guid
= WSAID_WSARECVMSG
;
1379 return WSAIoctl (sock
, SIO_GET_EXTENSION_FUNCTION_POINTER
,
1380 (void *) &guid
, sizeof (GUID
), funcptr
, sizeof (void *),
1385 fhandler_socket::recv_internal (LPWSAMSG wsamsg
)
1388 DWORD ret
= 0, wret
;
1389 int evt_mask
= FD_READ
| ((wsamsg
->dwFlags
& MSG_OOB
) ? FD_OOB
: 0);
1390 LPWSABUF
&wsabuf
= wsamsg
->lpBuffers
;
1391 ULONG
&wsacnt
= wsamsg
->dwBufferCount
;
1392 bool use_recvmsg
= false;
1393 static NO_COPY LPFN_WSARECVMSG WSARecvMsg
;
1395 DWORD wait_flags
= wsamsg
->dwFlags
;
1396 bool waitall
= !!(wait_flags
& MSG_WAITALL
);
1397 wsamsg
->dwFlags
&= (MSG_OOB
| MSG_PEEK
| MSG_DONTROUTE
);
1398 if (wsamsg
->Control
.len
> 0)
1401 && get_ext_funcptr (get_socket (), &WSARecvMsg
) == SOCKET_ERROR
)
1403 set_winsock_errno ();
1404 return SOCKET_ERROR
;
1410 if (get_socket_type () != SOCK_STREAM
)
1412 WSASetLastError (WSAEOPNOTSUPP
);
1413 set_winsock_errno ();
1414 return SOCKET_ERROR
;
1416 if (is_nonblocking () || (wsamsg
->dwFlags
& (MSG_OOB
| MSG_PEEK
)))
1420 /* Note: Don't call WSARecvFrom(MSG_PEEK) without actually having data
1421 waiting in the buffers, otherwise the event handling gets messed up
1423 while (!(res
= wait_for_events (evt_mask
| FD_CLOSE
, wait_flags
))
1424 || saw_shutdown_read ())
1427 res
= WSARecvMsg (get_socket (), wsamsg
, &wret
, NULL
, NULL
);
1428 /* This is working around a really weird problem in WinSock.
1430 Assume you create a socket, fork the process (thus duplicating
1431 the socket), connect the socket in the child, then call recv
1432 on the original socket handle in the parent process.
1433 In this scenario, calls to WinSock's recvfrom and WSARecvFrom
1434 in the parent will fail with WSAEINVAL, regardless whether both
1435 address parameters, name and namelen, are NULL or point to valid
1436 storage. However, calls to recv and WSARecv succeed as expected.
1437 Per MSDN, WSAEINVAL in the context of recv means "The socket has not
1438 been bound". It is as if the recvfrom functions test if the socket
1439 is bound locally, but in the parent process, WinSock doesn't know
1440 about that and fails, while the same test is omitted in the recv
1443 This also covers another weird case: WinSock returns WSAEFAULT if
1444 namelen is a valid pointer while name is NULL. Both parameters are
1445 ignored for TCP sockets, so this only occurs when using UDP socket. */
1446 else if (!wsamsg
->name
|| get_socket_type () == SOCK_STREAM
)
1447 res
= WSARecv (get_socket (), wsabuf
, wsacnt
, &wret
, &wsamsg
->dwFlags
,
1450 res
= WSARecvFrom (get_socket (), wsabuf
, wsacnt
, &wret
,
1451 &wsamsg
->dwFlags
, wsamsg
->name
, &wsamsg
->namelen
,
1458 while (wret
&& wsacnt
)
1460 if (wsabuf
->len
> wret
)
1462 wsabuf
->len
-= wret
;
1463 wsabuf
->buf
+= wret
;
1468 wret
-= wsabuf
->len
;
1476 else if (WSAGetLastError () != WSAEWOULDBLOCK
)
1482 /* According to SUSv3, errno isn't set in that case and no error
1483 condition is returned. */
1484 if (WSAGetLastError () == WSAEMSGSIZE
)
1489 /* ESHUTDOWN isn't defined for recv in SUSv3. Simply EOF is returned
1491 if (WSAGetLastError () == WSAESHUTDOWN
)
1494 set_winsock_errno ();
1495 return SOCKET_ERROR
;
1503 fhandler_socket::recvfrom (void *ptr
, size_t len
, int flags
,
1504 struct sockaddr
*from
, int *fromlen
)
1506 pthread_testcancel ();
1508 WSABUF wsabuf
= { len
, (char *) ptr
};
1509 WSAMSG wsamsg
= { from
, from
&& fromlen
? *fromlen
: 0,
1513 ssize_t ret
= recv_internal (&wsamsg
);
1515 *fromlen
= wsamsg
.namelen
;
1520 fhandler_socket::recvmsg (struct msghdr
*msg
, int flags
)
1522 pthread_testcancel ();
1524 /* TODO: Descriptor passing on AF_LOCAL sockets. */
1526 /* Disappointing but true: Even if WSARecvMsg is supported, it's only
1527 supported for datagram and raw sockets. */
1528 if (!wincap
.has_recvmsg () || get_socket_type () == SOCK_STREAM
1529 || get_addr_family () == AF_LOCAL
)
1531 msg
->msg_controllen
= 0;
1532 if (!CYGWIN_VERSION_CHECK_FOR_USING_ANCIENT_MSGHDR
)
1536 WSABUF wsabuf
[msg
->msg_iovlen
];
1537 WSABUF
*wsaptr
= wsabuf
+ msg
->msg_iovlen
;
1538 const struct iovec
*iovptr
= msg
->msg_iov
+ msg
->msg_iovlen
;
1539 while (--wsaptr
>= wsabuf
)
1541 wsaptr
->len
= (--iovptr
)->iov_len
;
1542 wsaptr
->buf
= (char *) iovptr
->iov_base
;
1544 WSAMSG wsamsg
= { (struct sockaddr
*) msg
->msg_name
, msg
->msg_namelen
,
1545 wsabuf
, msg
->msg_iovlen
,
1546 { msg
->msg_controllen
, (char *) msg
->msg_control
},
1548 ssize_t ret
= recv_internal (&wsamsg
);
1551 msg
->msg_namelen
= wsamsg
.namelen
;
1552 msg
->msg_controllen
= wsamsg
.Control
.len
;
1553 if (!CYGWIN_VERSION_CHECK_FOR_USING_ANCIENT_MSGHDR
)
1554 msg
->msg_flags
= wsamsg
.dwFlags
;
1560 fhandler_socket::write (const void *ptr
, size_t len
)
1562 WSABUF wsabuf
= { len
, (char *) ptr
};
1563 WSAMSG wsamsg
= { NULL
, 0, &wsabuf
, 1, { 0, NULL
}, 0 };
1564 return send_internal (&wsamsg
, 0);
1568 fhandler_socket::writev (const struct iovec
*const iov
, const int iovcnt
,
1571 WSABUF wsabuf
[iovcnt
];
1572 WSABUF
*wsaptr
= wsabuf
;
1573 const struct iovec
*iovptr
= iov
;
1574 for (int i
= 0; i
< iovcnt
; ++i
)
1576 wsaptr
->len
= iovptr
->iov_len
;
1577 (wsaptr
++)->buf
= (char *) (iovptr
++)->iov_base
;
1579 WSAMSG wsamsg
= { NULL
, 0, wsabuf
, iovcnt
, { 0, NULL
}, 0 };
1580 return send_internal (&wsamsg
, 0);
1584 fhandler_socket::send_internal (struct _WSAMSG
*wsamsg
, int flags
)
1587 DWORD ret
= 0, err
= 0, sum
= 0, off
= 0;
1589 bool use_sendmsg
= false;
1590 DWORD wait_flags
= flags
& MSG_DONTWAIT
;
1591 bool nosignal
= !!(flags
& MSG_NOSIGNAL
);
1593 flags
&= (MSG_OOB
| MSG_DONTROUTE
);
1594 if (wsamsg
->Control
.len
> 0)
1596 for (DWORD i
= 0; i
< wsamsg
->dwBufferCount
;
1597 off
>= wsamsg
->lpBuffers
[i
].len
&& (++i
, off
= 0))
1599 /* CV 2009-12-02: Don't split datagram messages. */
1600 /* FIXME: Look for a way to split a message into the least number of
1601 pieces to minimize the number of WsaSendTo calls. */
1602 if (get_socket_type () == SOCK_STREAM
)
1604 buf
.buf
= wsamsg
->lpBuffers
[i
].buf
+ off
;
1605 buf
.len
= wsamsg
->lpBuffers
[i
].len
- off
;
1606 /* See net.cc:fdsock() and MSDN KB 823764 */
1607 if (buf
.len
>= (unsigned) wmem ())
1608 buf
.len
= (unsigned) wmem ();
1614 res
= WSASendMsg (get_socket (), wsamsg
, flags
, &ret
, NULL
, NULL
);
1615 else if (get_socket_type () == SOCK_STREAM
)
1616 res
= WSASendTo (get_socket (), &buf
, 1, &ret
, flags
,
1617 wsamsg
->name
, wsamsg
->namelen
, NULL
, NULL
);
1619 res
= WSASendTo (get_socket (), wsamsg
->lpBuffers
,
1620 wsamsg
->dwBufferCount
, &ret
, flags
,
1621 wsamsg
->name
, wsamsg
->namelen
, NULL
, NULL
);
1622 if (res
&& (err
= WSAGetLastError ()) == WSAEWOULDBLOCK
)
1625 wsock_events
->events
&= ~FD_WRITE
;
1629 while (res
&& err
== WSAEWOULDBLOCK
1630 && !(res
= wait_for_events (FD_WRITE
| FD_CLOSE
, wait_flags
)));
1636 if (get_socket_type () != SOCK_STREAM
)
1639 else if (is_nonblocking () || err
!= WSAEWOULDBLOCK
)
1645 else if (res
== SOCKET_ERROR
)
1647 set_winsock_errno ();
1649 /* Special handling for EPIPE and SIGPIPE.
1651 EPIPE is generated if the local end has been shut down on a connection
1652 oriented socket. In this case the process will also receive a SIGPIPE
1653 unless MSG_NOSIGNAL is set. */
1654 if ((get_errno () == ECONNABORTED
|| get_errno () == ESHUTDOWN
)
1655 && get_socket_type () == SOCK_STREAM
)
1667 fhandler_socket::sendto (const void *ptr
, size_t len
, int flags
,
1668 const struct sockaddr
*to
, int tolen
)
1670 struct sockaddr_storage sst
;
1672 pthread_testcancel ();
1674 if (to
&& get_inet_addr (to
, tolen
, &sst
, &tolen
) == SOCKET_ERROR
)
1675 return SOCKET_ERROR
;
1677 WSABUF wsabuf
= { len
, (char *) ptr
};
1678 WSAMSG wsamsg
= { to
? (struct sockaddr
*) &sst
: NULL
, tolen
,
1682 return send_internal (&wsamsg
, flags
);
1686 fhandler_socket::sendmsg (const struct msghdr
*msg
, int flags
)
1688 /* TODO: Descriptor passing on AF_LOCAL sockets. */
1690 struct sockaddr_storage sst
;
1693 pthread_testcancel ();
1696 && get_inet_addr ((struct sockaddr
*) msg
->msg_name
, msg
->msg_namelen
,
1697 &sst
, &len
) == SOCKET_ERROR
)
1698 return SOCKET_ERROR
;
1700 WSABUF wsabuf
[msg
->msg_iovlen
];
1701 WSABUF
*wsaptr
= wsabuf
;
1702 const struct iovec
*iovptr
= msg
->msg_iov
;
1703 for (int i
= 0; i
< msg
->msg_iovlen
; ++i
)
1705 wsaptr
->len
= iovptr
->iov_len
;
1706 (wsaptr
++)->buf
= (char *) (iovptr
++)->iov_base
;
1708 WSAMSG wsamsg
= { msg
->msg_name
? (struct sockaddr
*) &sst
: NULL
, len
,
1709 wsabuf
, msg
->msg_iovlen
,
1710 /* Disappointing but true: Even if WSASendMsg is
1711 supported, it's only supported for datagram and
1713 { !wincap
.has_sendmsg ()
1714 || get_socket_type () == SOCK_STREAM
1715 || get_addr_family () == AF_LOCAL
1716 ? 0 : msg
->msg_controllen
, (char *) msg
->msg_control
},
1718 return send_internal (&wsamsg
, flags
);
1722 fhandler_socket::shutdown (int how
)
1724 int res
= ::shutdown (get_socket (), how
);
1726 /* Linux allows to call shutdown for any socket, even if it's not connected.
1727 This also disables to call accept on this socket, if shutdown has been
1728 called with the SHUT_RD or SHUT_RDWR parameter. In contrast, WinSock
1729 only allows to call shutdown on a connected socket. The accept function
1730 is in no way affected. So, what we do here is to fake success, and to
1731 change the event settings so that an FD_CLOSE event is triggered for the
1732 calling Cygwin function. The evaluate_events method handles the call
1733 from accept specially to generate a Linux-compatible behaviour. */
1734 if (res
&& WSAGetLastError () != WSAENOTCONN
)
1735 set_winsock_errno ();
1742 saw_shutdown_read (true);
1743 wsock_events
->events
|= FD_CLOSE
;
1744 SetEvent (wsock_evt
);
1747 saw_shutdown_write (true);
1750 saw_shutdown_read (true);
1751 saw_shutdown_write (true);
1752 wsock_events
->events
|= FD_CLOSE
;
1753 SetEvent (wsock_evt
);
1761 fhandler_socket::close ()
1764 /* TODO: CV - 2008-04-16. Lingering disabled. The original problem
1765 could be no longer reproduced on NT4, XP, 2K8. Any return of a
1766 spurious "Connection reset by peer" *could* be caused by disabling
1767 the linger code here... */
1769 /* HACK to allow a graceful shutdown even if shutdown() hasn't been
1770 called by the application. Note that this isn't the ultimate
1771 solution but it helps in many cases. */
1772 struct linger linger
;
1774 linger
.l_linger
= 240; /* secs. default 2MSL value according to MSDN. */
1775 setsockopt (get_socket (), SOL_SOCKET
, SO_LINGER
,
1776 (const char *)&linger
, sizeof linger
);
1779 while ((res
= closesocket (get_socket ())) != 0)
1781 if (WSAGetLastError () != WSAEWOULDBLOCK
)
1783 set_winsock_errno ();
1787 if (WaitForSingleObject (signal_arrived
, 10) == WAIT_OBJECT_0
)
1793 WSASetLastError (0);
1796 debug_printf ("%d = fhandler_socket::close()", res
);
1800 /* Definitions of old ifreq stuff used prior to Cygwin 1.7.0. */
1801 #define OLD_SIOCGIFFLAGS _IOW('s', 101, struct __old_ifreq)
1802 #define OLD_SIOCGIFADDR _IOW('s', 102, struct __old_ifreq)
1803 #define OLD_SIOCGIFBRDADDR _IOW('s', 103, struct __old_ifreq)
1804 #define OLD_SIOCGIFNETMASK _IOW('s', 104, struct __old_ifreq)
1805 #define OLD_SIOCGIFHWADDR _IOW('s', 105, struct __old_ifreq)
1806 #define OLD_SIOCGIFMETRIC _IOW('s', 106, struct __old_ifreq)
1807 #define OLD_SIOCGIFMTU _IOW('s', 107, struct __old_ifreq)
1808 #define OLD_SIOCGIFINDEX _IOW('s', 108, struct __old_ifreq)
1810 #define CONV_OLD_TO_NEW_SIO(old) (((old)&0xff00ffff)|(((long)sizeof(struct ifreq)&IOCPARM_MASK)<<16))
1812 struct __old_ifreq
{
1813 #define __OLD_IFNAMSIZ 16
1815 char ifrn_name
[__OLD_IFNAMSIZ
]; /* if name, e.g. "en0" */
1819 struct sockaddr ifru_addr
;
1820 struct sockaddr ifru_broadaddr
;
1821 struct sockaddr ifru_netmask
;
1822 struct sockaddr ifru_hwaddr
;
1831 fhandler_socket::ioctl (unsigned int cmd
, void *p
)
1833 extern int get_ifconf (struct ifconf
*ifc
, int what
); /* net.cc */
1835 struct ifconf ifc
, *ifcp
;
1841 ifcp
= (struct ifconf
*) p
;
1847 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ
)
1849 ifc
.ifc_len
= ifcp
->ifc_len
/ sizeof (struct __old_ifreq
)
1850 * sizeof (struct ifreq
);
1851 ifc
.ifc_buf
= (caddr_t
) alloca (ifc
.ifc_len
);
1855 ifc
.ifc_len
= ifcp
->ifc_len
;
1856 ifc
.ifc_buf
= ifcp
->ifc_buf
;
1858 res
= get_ifconf (&ifc
, cmd
);
1860 debug_printf ("error in get_ifconf");
1861 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ
)
1863 struct __old_ifreq
*ifr
= (struct __old_ifreq
*) ifcp
->ifc_buf
;
1864 for (ifrp
= ifc
.ifc_req
;
1865 (caddr_t
) ifrp
< ifc
.ifc_buf
+ ifc
.ifc_len
;
1868 memcpy (&ifr
->ifr_ifrn
, &ifrp
->ifr_ifrn
, sizeof ifr
->ifr_ifrn
);
1869 ifr
->ifr_name
[__OLD_IFNAMSIZ
- 1] = '\0';
1870 memcpy (&ifr
->ifr_ifru
, &ifrp
->ifr_ifru
, sizeof ifr
->ifr_ifru
);
1872 ifcp
->ifc_len
= ifc
.ifc_len
/ sizeof (struct ifreq
)
1873 * sizeof (struct __old_ifreq
);
1876 ifcp
->ifc_len
= ifc
.ifc_len
;
1878 case OLD_SIOCGIFFLAGS
:
1879 case OLD_SIOCGIFADDR
:
1880 case OLD_SIOCGIFBRDADDR
:
1881 case OLD_SIOCGIFNETMASK
:
1882 case OLD_SIOCGIFHWADDR
:
1883 case OLD_SIOCGIFMETRIC
:
1884 case OLD_SIOCGIFMTU
:
1885 case OLD_SIOCGIFINDEX
:
1886 cmd
= CONV_OLD_TO_NEW_SIO (cmd
);
1889 case SIOCGIFBRDADDR
:
1890 case SIOCGIFNETMASK
:
1896 case SIOCGIFFRNDLYNAM
:
1897 case SIOCGIFDSTADDR
:
1901 debug_printf ("ifr == NULL");
1906 if (cmd
> SIOCGIFINDEX
&& CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ
)
1908 debug_printf ("cmd not supported on this platform");
1912 ifc
.ifc_len
= 64 * sizeof (struct ifreq
);
1913 ifc
.ifc_buf
= (caddr_t
) alloca (ifc
.ifc_len
);
1914 if (cmd
== SIOCGIFFRNDLYNAM
)
1916 struct ifreq_frndlyname
*iff
= (struct ifreq_frndlyname
*)
1917 alloca (64 * sizeof (struct ifreq_frndlyname
));
1918 for (int i
= 0; i
< 64; ++i
)
1919 ifc
.ifc_req
[i
].ifr_frndlyname
= &iff
[i
];
1922 res
= get_ifconf (&ifc
, cmd
);
1925 debug_printf ("error in get_ifconf");
1929 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ
)
1931 struct __old_ifreq
*ifr
= (struct __old_ifreq
*) p
;
1932 debug_printf (" name: %s", ifr
->ifr_name
);
1933 for (ifrp
= ifc
.ifc_req
;
1934 (caddr_t
) ifrp
< ifc
.ifc_buf
+ ifc
.ifc_len
;
1937 debug_printf ("testname: %s", ifrp
->ifr_name
);
1938 if (! strcmp (ifrp
->ifr_name
, ifr
->ifr_name
))
1940 memcpy (&ifr
->ifr_ifru
, &ifrp
->ifr_ifru
,
1941 sizeof ifr
->ifr_ifru
);
1948 struct ifreq
*ifr
= (struct ifreq
*) p
;
1949 debug_printf (" name: %s", ifr
->ifr_name
);
1950 for (ifrp
= ifc
.ifc_req
;
1951 (caddr_t
) ifrp
< ifc
.ifc_buf
+ ifc
.ifc_len
;
1954 debug_printf ("testname: %s", ifrp
->ifr_name
);
1955 if (! strcmp (ifrp
->ifr_name
, ifr
->ifr_name
))
1957 if (cmd
== SIOCGIFFRNDLYNAM
)
1958 /* The application has to care for the space. */
1959 memcpy (ifr
->ifr_frndlyname
, ifrp
->ifr_frndlyname
,
1960 sizeof (struct ifreq_frndlyname
));
1962 memcpy (&ifr
->ifr_ifru
, &ifrp
->ifr_ifru
,
1963 sizeof ifr
->ifr_ifru
);
1968 if ((caddr_t
) ifrp
>= ifc
.ifc_buf
+ ifc
.ifc_len
)
1976 res
= WSAAsyncSelect (get_socket (), winmsg
, WM_ASYNCIO
,
1977 *(int *) p
? ASYNC_MASK
: 0);
1978 syscall_printf ("Async I/O on socket %s",
1979 *(int *) p
? "started" : "cancelled");
1980 async_io (*(int *) p
!= 0);
1981 /* If async_io is switched off, revert the event handling. */
1982 if (*(int *) p
== 0)
1983 WSAEventSelect (get_socket (), wsock_evt
, EVENT_MASK
);
1986 res
= ioctlsocket (get_socket (), FIONREAD
, (unsigned long *) p
);
1987 if (res
== SOCKET_ERROR
)
1988 set_winsock_errno ();
1991 /* Sockets are always non-blocking internally. So we just note the
1995 syscall_printf ("socket is now %sblocking",
1996 *(int *) p
? "non" : "");
1997 set_nonblocking (*(int *) p
);
2001 res
= ioctlsocket (get_socket (), cmd
, (unsigned long *) p
);
2004 syscall_printf ("%d = ioctl_socket(%x, %x)", res
, cmd
, p
);
2009 fhandler_socket::fcntl (int cmd
, void *arg
)
2012 int request
, current
;
2018 pid_t pid
= (pid_t
) arg
;
2020 wsock_events
->owner
= pid
;
2022 debug_printf ("owner set to %d", pid
);
2026 res
= wsock_events
->owner
;
2030 /* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
2031 Set only the flag that has been passed in. If both are set, just
2032 record O_NONBLOCK. */
2033 int new_flags
= (int) arg
& O_NONBLOCK_MASK
;
2034 if ((new_flags
& OLD_O_NDELAY
) && (new_flags
& O_NONBLOCK
))
2035 new_flags
= O_NONBLOCK
;
2036 current
= get_flags () & O_NONBLOCK_MASK
;
2037 request
= new_flags
? 1 : 0;
2038 if (!!current
!= !!new_flags
&& (res
= ioctl (FIONBIO
, &request
)))
2040 set_flags ((get_flags () & ~O_NONBLOCK_MASK
) | new_flags
);
2044 res
= fhandler_base::fcntl (cmd
, arg
);
2051 fhandler_socket::set_close_on_exec (bool val
)
2053 set_no_inheritance (wsock_mtx
, val
);
2054 set_no_inheritance (wsock_evt
, val
);
2055 if (need_fixup_before ())
2057 close_on_exec (val
);
2058 debug_printf ("set close_on_exec for %s to %d", get_name (), val
);
2061 fhandler_base::set_close_on_exec (val
);
2065 fhandler_socket::set_sun_path (const char *path
)
2067 sun_path
= path
? cstrdup (path
) : NULL
;
2071 fhandler_socket::set_peer_sun_path (const char *path
)
2073 peer_sun_path
= path
? cstrdup (path
) : NULL
;
2077 fhandler_socket::getpeereid (pid_t
*pid
, __uid32_t
*euid
, __gid32_t
*egid
)
2079 if (get_addr_family () != AF_LOCAL
|| get_socket_type () != SOCK_STREAM
)
2084 if (connect_state () != connected
)
2086 set_errno (ENOTCONN
);
2089 if (sec_peer_pid
== (pid_t
) 0)
2091 set_errno (ENOTCONN
); /* Usually when calling getpeereid on
2092 accepting (instead of accepted) socket. */
2097 if (efault
.faulted (EFAULT
))
2100 *pid
= sec_peer_pid
;
2102 *euid
= sec_peer_uid
;
2104 *egid
= sec_peer_gid
;