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 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 /* #define DEBUG_NEST_ON 1 */
13 #define __INSIDE_CYGWIN_NET__
17 #include <asm/byteorder.h>
20 #define USE_SYS_TYPES_FD_SET
25 #include "cygwin/version.h"
26 #include "perprocess.h"
31 #include "shared_info.h"
37 #include "cygwin/in6.h"
40 #define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT)
41 #define EVENT_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
43 extern bool fdsock (cygheap_fdmanip& fd, const device *, SOCKET soc);
45 int sscanf (const char *, const char *, ...);
46 } /* End of "C" section */
48 fhandler_dev_random* entropy_source;
51 adjust_socket_file_mode (mode_t mode)
53 /* Kludge: Don't allow to remove read bit on socket files for
54 user/group/other, if the accompanying write bit is set. It would
55 be nice to have exact permissions on a socket file, but it's
56 necessary that somebody able to access the socket can always read
57 the contents of the socket file to avoid spurious "permission
59 return mode | ((mode & (S_IWUSR | S_IWGRP | S_IWOTH)) << 1);
62 /* cygwin internal: map sockaddr into internet domain address */
64 get_inet_addr (const struct sockaddr *in, int inlen,
65 struct sockaddr_storage *out, int *outlen,
66 int *type = NULL, int *secret = NULL)
69 int* secret_ptr = (secret ? : secret_buf);
71 if (in->sa_family == AF_INET || in->sa_family == AF_INET6)
73 memcpy (out, in, inlen);
77 else if (in->sa_family == AF_LOCAL)
81 OBJECT_ATTRIBUTES attr;
84 path_conv pc (in->sa_data, PC_SYM_FOLLOW);
100 status = NtOpenFile (&fh, GENERIC_READ | SYNCHRONIZE,
101 pc.get_object_attr (attr, sec_none_nih), &io,
102 FILE_SHARE_VALID_FLAGS,
103 FILE_SYNCHRONOUS_IO_NONALERT
104 | FILE_OPEN_FOR_BACKUP_INTENT);
105 if (!NT_SUCCESS (status))
107 __seterrno_from_nt_status (status);
112 memset (buf, 0, sizeof buf);
113 status = NtReadFile (fh, NULL, NULL, NULL, &io, buf, 128, NULL, NULL);
115 if (NT_SUCCESS (status))
117 struct sockaddr_in sin;
119 sin.sin_family = AF_INET;
120 sscanf (buf + strlen (SOCKET_COOKIE), "%hu %c %08x-%08x-%08x-%08x",
123 secret_ptr, secret_ptr + 1, secret_ptr + 2, secret_ptr + 3);
124 sin.sin_port = htons (sin.sin_port);
125 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
126 memcpy (out, &sin, sizeof sin);
127 *outlen = sizeof sin;
129 *type = (ctype == 's' ? SOCK_STREAM :
130 ctype == 'd' ? SOCK_DGRAM
135 __seterrno_from_nt_status (status);
140 set_errno (EAFNOSUPPORT);
145 /**********************************************************************/
146 /* fhandler_socket */
148 fhandler_socket::fhandler_socket () :
156 need_fork_fixup (true);
159 fhandler_socket::~fhandler_socket ()
166 fhandler_socket::get_proc_fd_name (char *buf)
168 __small_sprintf (buf, "socket:[%d]", get_socket ());
173 fhandler_socket::open (int flags, mode_t mode)
180 fhandler_socket::af_local_set_sockpair_cred ()
182 sec_pid = sec_peer_pid = getpid ();
183 sec_uid = sec_peer_uid = geteuid32 ();
184 sec_gid = sec_peer_gid = getegid32 ();
188 fhandler_socket::af_local_setblocking (bool &async, bool &nonblocking)
191 nonblocking = is_nonblocking ();
194 WSAAsyncSelect (get_socket (), winmsg, 0, 0);
195 WSAEventSelect (get_socket (), wsock_evt, EVENT_MASK);
197 set_nonblocking (false);
202 fhandler_socket::af_local_unsetblocking (bool async, bool nonblocking)
205 set_nonblocking (true);
208 WSAAsyncSelect (get_socket (), winmsg, WM_ASYNCIO, ASYNC_MASK);
214 fhandler_socket::af_local_recv_secret ()
216 int out[4] = { 0, 0, 0, 0 };
217 int rest = sizeof out;
218 char *ptr = (char *) out;
221 int ret = recvfrom (ptr, rest, 0, NULL, NULL);
229 debug_printf ("Received af_local secret: %08x-%08x-%08x-%08x",
230 out[0], out[1], out[2], out[3]);
231 if (out[0] != connect_secret[0] || out[1] != connect_secret[1]
232 || out[2] != connect_secret[2] || out[3] != connect_secret[3])
234 debug_printf ("Receiving af_local secret mismatch");
239 debug_printf ("Receiving af_local secret failed");
244 fhandler_socket::af_local_send_secret ()
246 int rest = sizeof connect_secret;
247 char *ptr = (char *) connect_secret;
250 int ret = sendto (ptr, rest, 0, NULL, 0);
256 debug_printf ("Sending af_local secret %s", rest == 0 ? "succeeded"
262 fhandler_socket::af_local_recv_cred ()
264 struct ucred out = { (pid_t) 0, (__uid32_t) -1, (__gid32_t) -1 };
265 int rest = sizeof out;
266 char *ptr = (char *) &out;
269 int ret = recvfrom (ptr, rest, 0, NULL, NULL);
277 debug_printf ("Received eid credentials: pid: %d, uid: %d, gid: %d",
278 out.pid, out.uid, out.gid);
279 sec_peer_pid = out.pid;
280 sec_peer_uid = out.uid;
281 sec_peer_gid = out.gid;
284 debug_printf ("Receiving eid credentials failed");
289 fhandler_socket::af_local_send_cred ()
291 struct ucred in = { sec_pid, sec_uid, sec_gid };
292 int rest = sizeof in;
293 char *ptr = (char *) ∈
296 int ret = sendto (ptr, rest, 0, NULL, 0);
303 debug_printf ("Sending eid credentials succeeded");
305 debug_printf ("Sending eid credentials failed");
310 fhandler_socket::af_local_connect ()
312 /* This keeps the test out of select. */
313 if (get_addr_family () != AF_LOCAL || get_socket_type () != SOCK_STREAM)
316 debug_printf ("af_local_connect called");
317 bool orig_async_io, orig_is_nonblocking;
318 af_local_setblocking (orig_async_io, orig_is_nonblocking);
319 if (!af_local_send_secret () || !af_local_recv_secret ()
320 || !af_local_send_cred () || !af_local_recv_cred ())
322 debug_printf ("accept from unauthorized server");
323 ::shutdown (get_socket (), SD_BOTH);
324 WSASetLastError (WSAECONNREFUSED);
327 af_local_unsetblocking (orig_async_io, orig_is_nonblocking);
332 fhandler_socket::af_local_accept ()
334 debug_printf ("af_local_accept called");
335 bool orig_async_io, orig_is_nonblocking;
336 af_local_setblocking (orig_async_io, orig_is_nonblocking);
337 if (!af_local_recv_secret () || !af_local_send_secret ()
338 || !af_local_recv_cred () || !af_local_send_cred ())
340 debug_printf ("connect from unauthorized client");
341 ::shutdown (get_socket (), SD_BOTH);
342 ::closesocket (get_socket ());
343 WSASetLastError (WSAECONNABORTED);
346 af_local_unsetblocking (orig_async_io, orig_is_nonblocking);
351 fhandler_socket::af_local_set_cred ()
354 sec_uid = geteuid32 ();
355 sec_gid = getegid32 ();
356 sec_peer_pid = (pid_t) 0;
357 sec_peer_uid = (__uid32_t) -1;
358 sec_peer_gid = (__gid32_t) -1;
362 fhandler_socket::af_local_copy (fhandler_socket *sock)
364 sock->connect_secret[0] = connect_secret[0];
365 sock->connect_secret[1] = connect_secret[1];
366 sock->connect_secret[2] = connect_secret[2];
367 sock->connect_secret[3] = connect_secret[3];
368 sock->sec_pid = sec_pid;
369 sock->sec_uid = sec_uid;
370 sock->sec_gid = sec_gid;
371 sock->sec_peer_pid = sec_peer_pid;
372 sock->sec_peer_uid = sec_peer_uid;
373 sock->sec_peer_gid = sec_peer_gid;
377 fhandler_socket::af_local_set_secret (char *buf)
381 void *buf = malloc (sizeof (fhandler_dev_random));
382 entropy_source = new (buf) fhandler_dev_random ();
383 entropy_source->dev () = *urandom_dev;
385 if (entropy_source &&
386 !entropy_source->open (O_RDONLY))
388 delete entropy_source;
389 entropy_source = NULL;
393 size_t len = sizeof (connect_secret);
394 entropy_source->read (connect_secret, len);
395 if (len != sizeof (connect_secret))
396 bzero ((char*) connect_secret, sizeof (connect_secret));
398 __small_sprintf (buf, "%08x-%08x-%08x-%08x",
399 connect_secret [0], connect_secret [1],
400 connect_secret [2], connect_secret [3]);
403 /* Maximum number of concurrently opened sockets from all Cygwin processes
404 per session. Note that shared sockets (through dup/fork/exec) are
405 counted as one socket. */
406 #define NUM_SOCKS (32768 / sizeof (wsa_event))
408 #define LOCK_EVENTS WaitForSingleObject (wsock_mtx, INFINITE)
409 #define UNLOCK_EVENTS ReleaseMutex (wsock_mtx)
411 static wsa_event wsa_events[NUM_SOCKS] __attribute__((section (".cygwin_dll_common"), shared)) = { 0 };
413 static LONG socket_serial_number __attribute__((section (".cygwin_dll_common"), shared)) = 0;
415 static HANDLE wsa_slot_mtx;
418 sock_shared_name (PWCHAR buf, LONG num)
420 __small_swprintf (buf, L"socket.%d", num);
425 search_wsa_event_slot (LONG new_serial_number)
427 WCHAR name[32], searchname[32];
428 UNICODE_STRING uname;
429 OBJECT_ATTRIBUTES attr;
434 RtlInitUnicodeString (&uname, sock_shared_name (name, 0));
435 InitializeObjectAttributes (&attr, &uname, OBJ_INHERIT | OBJ_OPENIF,
436 get_session_parent_dir (),
437 everyone_sd (CYG_MUTANT_ACCESS));
438 status = NtCreateMutant (&wsa_slot_mtx, CYG_MUTANT_ACCESS, &attr, FALSE);
439 if (!NT_SUCCESS (status))
440 api_fatal ("Couldn't create/open shared socket mutex %S, %p",
443 switch (WaitForSingleObject (wsa_slot_mtx, INFINITE))
449 api_fatal ("WFSO failed for shared socket mutex, %E");
452 unsigned int slot = new_serial_number % NUM_SOCKS;
453 while (wsa_events[slot].serial_number)
456 RtlInitUnicodeString (&uname, sock_shared_name (searchname,
457 wsa_events[slot].serial_number));
458 InitializeObjectAttributes (&attr, &uname, 0, get_session_parent_dir (),
460 status = NtOpenMutant (&searchmtx, READ_CONTROL, &attr);
461 if (!NT_SUCCESS (status))
463 /* Mutex still exists, attached socket is active, try next slot. */
465 slot = (slot + 1) % NUM_SOCKS;
466 if (slot == (new_serial_number % NUM_SOCKS))
468 /* Did the whole array once. Too bad. */
469 debug_printf ("No free socket slot");
470 ReleaseMutex (wsa_slot_mtx);
474 memset (&wsa_events[slot], 0, sizeof (wsa_event));
475 wsa_events[slot].serial_number = new_serial_number;
476 ReleaseMutex (wsa_slot_mtx);
477 return wsa_events + slot;
481 fhandler_socket::init_events ()
483 LONG new_serial_number;
485 UNICODE_STRING uname;
486 OBJECT_ATTRIBUTES attr;
492 InterlockedIncrement (&socket_serial_number);
493 if (!new_serial_number) /* 0 is reserved for global mutex */
494 InterlockedIncrement (&socket_serial_number);
495 RtlInitUnicodeString (&uname, sock_shared_name (name, new_serial_number));
496 InitializeObjectAttributes (&attr, &uname, OBJ_INHERIT | OBJ_OPENIF,
497 get_session_parent_dir (),
498 everyone_sd (CYG_MUTANT_ACCESS));
499 status = NtCreateMutant (&wsock_mtx, CYG_MUTANT_ACCESS, &attr, FALSE);
500 if (!NT_SUCCESS (status))
502 debug_printf ("NtCreateMutant(%S), %p", &uname, status);
506 if (status == STATUS_OBJECT_NAME_EXISTS)
509 while (status == STATUS_OBJECT_NAME_EXISTS);
510 if ((wsock_evt = CreateEvent (&sec_all, TRUE, FALSE, NULL))
511 == WSA_INVALID_EVENT)
513 debug_printf ("CreateEvent, %E");
518 if (WSAEventSelect (get_socket (), wsock_evt, EVENT_MASK) == SOCKET_ERROR)
520 debug_printf ("WSAEventSelect, %E");
521 set_winsock_errno ();
526 wsock_events = search_wsa_event_slot (new_serial_number);
527 /* sock type not yet set here. */
528 if (pc.dev == FH_UDP || pc.dev == FH_DGRAM)
529 wsock_events->events = FD_WRITE;
534 fhandler_socket::evaluate_events (const long event_mask, long &events,
540 WSANETWORKEVENTS evts = { 0 };
541 if (!(WSAEnumNetworkEvents (get_socket (), wsock_evt, &evts)))
543 if (evts.lNetworkEvents)
546 wsock_events->events |= evts.lNetworkEvents;
547 events_now = (wsock_events->events & event_mask);
548 if (evts.lNetworkEvents & FD_CONNECT)
549 wsock_events->connect_errorcode = evts.iErrorCode[FD_CONNECT_BIT];
551 if ((evts.lNetworkEvents & FD_OOB) && wsock_events->owner)
552 kill (wsock_events->owner, SIGURG);
557 if ((events = events_now) != 0
558 || (events = (wsock_events->events & event_mask)) != 0)
560 if (events & FD_CONNECT)
563 if ((wsa_err = wsock_events->connect_errorcode) != 0)
565 WSASetLastError (wsa_err);
569 wsock_events->events |= FD_WRITE;
570 wsock_events->events &= ~FD_CONNECT;
571 wsock_events->connect_errorcode = 0;
574 wsock_events->events &= ~(events & ~(FD_WRITE | FD_CLOSE));
582 fhandler_socket::wait_for_events (const long event_mask)
590 while (!(ret = evaluate_events (event_mask, events, true)) && !events)
592 if (is_nonblocking ())
594 WSASetLastError (WSAEWOULDBLOCK);
598 WSAEVENT ev[2] = { wsock_evt, signal_arrived };
599 switch (WSAWaitForMultipleEvents (2, ev, FALSE, 50, FALSE))
601 case WSA_WAIT_TIMEOUT:
602 case WSA_WAIT_EVENT_0:
605 case WSA_WAIT_EVENT_0 + 1:
606 if (_my_tls.call_signal_handler ())
608 sig_dispatch_pending ();
611 WSASetLastError (WSAEINTR);
615 WSASetLastError (WSAEFAULT);
624 fhandler_socket::release_events ()
631 fhandler_socket::fixup_after_fork (HANDLE parent)
633 fork_fixup (parent, wsock_mtx, "wsock_mtx");
634 fork_fixup (parent, wsock_evt, "wsock_evt");
635 fhandler_base::fixup_after_fork (parent);
639 fhandler_socket::dup (fhandler_base *child)
641 debug_printf ("here");
642 fhandler_socket *fhs = (fhandler_socket *) child;
644 if (!DuplicateHandle (hMainProc, wsock_mtx, hMainProc, &fhs->wsock_mtx, 0,
645 TRUE, DUPLICATE_SAME_ACCESS))
650 if (!DuplicateHandle (hMainProc, wsock_evt, hMainProc, &fhs->wsock_evt, 0,
651 TRUE, DUPLICATE_SAME_ACCESS))
654 NtClose (fhs->wsock_mtx);
657 fhs->wsock_events = wsock_events;
659 fhs->addr_family = addr_family;
660 fhs->set_socket_type (get_socket_type ());
661 if (get_addr_family () == AF_LOCAL)
663 fhs->set_sun_path (get_sun_path ());
664 if (get_socket_type () == SOCK_STREAM)
666 fhs->sec_pid = sec_pid;
667 fhs->sec_uid = sec_uid;
668 fhs->sec_gid = sec_gid;
669 fhs->sec_peer_pid = sec_peer_pid;
670 fhs->sec_peer_uid = sec_peer_uid;
671 fhs->sec_peer_gid = sec_peer_gid;
674 fhs->connect_state (connect_state ());
675 int ret = fhandler_base::dup (child);
678 NtClose (fhs->wsock_evt);
679 NtClose (fhs->wsock_mtx);
685 fhandler_socket::fstat (struct __stat64 *buf)
688 if (get_device () == FH_UNIX)
690 res = fhandler_base::fstat_fs (buf);
693 buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFSOCK;
698 res = fhandler_base::fstat (buf);
702 buf->st_ino = (__ino64_t) ((DWORD) get_handle ());
703 buf->st_mode = S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO;
710 fhandler_socket::fstatvfs (struct statvfs *sfs)
712 if (get_device () == FH_UNIX)
714 fhandler_disk_file fh (pc);
715 fh.get_device () = FH_FS;
716 return fh.fstatvfs (sfs);
723 fhandler_socket::fchmod (mode_t mode)
725 if (get_device () == FH_UNIX)
727 fhandler_disk_file fh (pc);
728 fh.get_device () = FH_FS;
729 int ret = fh.fchmod (S_IFSOCK | adjust_socket_file_mode (mode));
737 fhandler_socket::fchown (__uid32_t uid, __gid32_t gid)
739 if (get_device () == FH_UNIX)
741 fhandler_disk_file fh (pc);
742 return fh.fchown (uid, gid);
749 fhandler_socket::facl (int cmd, int nentries, __aclent32_t *aclbufp)
751 if (get_device () == FH_UNIX)
753 fhandler_disk_file fh (pc);
754 return fh.facl (cmd, nentries, aclbufp);
761 fhandler_socket::link (const char *newpath)
763 if (get_device () == FH_UNIX)
765 fhandler_disk_file fh (pc);
766 return fh.link (newpath);
768 return fhandler_base::link (newpath);
772 address_in_use (const struct sockaddr *addr)
774 switch (addr->sa_family)
781 struct sockaddr_in *in = (struct sockaddr_in *) addr;
783 if (GetTcpTable (NULL, &size, FALSE) == ERROR_INSUFFICIENT_BUFFER)
785 tab = (PMIB_TCPTABLE) alloca (size += 16 * sizeof (PMIB_TCPROW));
786 if (!GetTcpTable (tab, &size, FALSE))
787 for (i = tab->dwNumEntries, entry = tab->table; i > 0;
789 if (entry->dwLocalAddr == in->sin_addr.s_addr
790 && entry->dwLocalPort == in->sin_port
791 && entry->dwState >= MIB_TCP_STATE_LISTEN
792 && entry->dwState <= MIB_TCP_STATE_LAST_ACK)
799 /* This test works on XP SP2 and above which should cover almost
801 PMIB_TCP6TABLE_OWNER_PID tab;
802 PMIB_TCP6ROW_OWNER_PID entry;
804 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) addr;
806 if (GetExtendedTcpTable (NULL, &size, FALSE, AF_INET6,
807 TCP_TABLE_OWNER_PID_ALL, 0)
808 == ERROR_INSUFFICIENT_BUFFER)
810 tab = (PMIB_TCP6TABLE_OWNER_PID)
811 alloca (size += 16 * sizeof (PMIB_TCP6ROW_OWNER_PID));
812 if (!GetExtendedTcpTable (tab, &size, FALSE, AF_INET6,
813 TCP_TABLE_OWNER_PID_ALL, 0))
814 for (i = tab->dwNumEntries, entry = tab->table; i > 0;
816 if (IN6_ARE_ADDR_EQUAL (entry->ucLocalAddr,
817 in6->sin6_addr.s6_addr)
818 /* FIXME: Is testing for the scope required. too?!? */
819 && entry->dwLocalPort == in6->sin6_port
820 && entry->dwState >= MIB_TCP_STATE_LISTEN
821 && entry->dwState <= MIB_TCP_STATE_LAST_ACK)
833 fhandler_socket::bind (const struct sockaddr *name, int namelen)
837 if (name->sa_family == AF_LOCAL)
839 #define un_addr ((struct sockaddr_un *) name)
840 struct sockaddr_in sin;
841 int len = sizeof sin;
843 if (strlen (un_addr->sun_path) >= UNIX_PATH_LEN)
845 set_errno (ENAMETOOLONG);
848 sin.sin_family = AF_INET;
850 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
851 if (::bind (get_socket (), (sockaddr *) &sin, len))
853 syscall_printf ("AF_LOCAL: bind failed");
854 set_winsock_errno ();
857 if (::getsockname (get_socket (), (sockaddr *) &sin, &len))
859 syscall_printf ("AF_LOCAL: getsockname failed");
860 set_winsock_errno ();
864 sin.sin_port = ntohs (sin.sin_port);
865 debug_printf ("AF_LOCAL: socket bound to port %u", sin.sin_port);
867 path_conv pc (un_addr->sun_path, PC_SYM_FOLLOW);
870 set_errno (pc.error);
875 set_errno (EADDRINUSE);
878 mode_t mode = adjust_socket_file_mode ((S_IRWXU | S_IRWXG | S_IRWXO)
880 DWORD fattr = FILE_ATTRIBUTE_SYSTEM;
881 if (!(mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
882 fattr |= FILE_ATTRIBUTE_READONLY;
883 SECURITY_ATTRIBUTES sa = sec_none_nih;
884 security_descriptor sd;
886 set_security_attribute (mode, &sa, sd);
889 OBJECT_ATTRIBUTES attr;
891 status = NtCreateFile (&fh, DELETE | FILE_GENERIC_WRITE,
892 pc.get_object_attr (attr, sa), &io, NULL, fattr,
893 FILE_SHARE_VALID_FLAGS, FILE_CREATE,
894 FILE_NON_DIRECTORY_FILE
895 | FILE_SYNCHRONOUS_IO_NONALERT
896 | FILE_OPEN_FOR_BACKUP_INTENT,
898 if (!NT_SUCCESS (status))
900 if (io.Information == FILE_EXISTS)
901 set_errno (EADDRINUSE);
903 __seterrno_from_nt_status (status);
907 char buf[sizeof (SOCKET_COOKIE) + 80];
908 __small_sprintf (buf, "%s%u %c ", SOCKET_COOKIE, sin.sin_port,
909 get_socket_type () == SOCK_STREAM ? 's'
910 : get_socket_type () == SOCK_DGRAM ? 'd' : '-');
911 af_local_set_secret (strchr (buf, '\0'));
912 DWORD blen = strlen (buf) + 1;
913 status = NtWriteFile (fh, NULL, NULL, NULL, &io, buf, blen, NULL, 0);
914 if (!NT_SUCCESS (status))
916 __seterrno_from_nt_status (status);
917 FILE_DISPOSITION_INFORMATION fdi = { TRUE };
918 status = NtSetInformationFile (fh, &io, &fdi, sizeof fdi,
919 FileDispositionInformation);
920 if (!NT_SUCCESS (status))
921 debug_printf ("Setting delete dispostion failed, status = %p",
926 set_sun_path (un_addr->sun_path);
935 /* If the application didn't explicitely call setsockopt (SO_REUSEADDR),
936 enforce exclusive local address use using the SO_EXCLUSIVEADDRUSE
937 socket option, to emulate POSIX socket behaviour more closely.
939 KB 870562: Note that this option is only available since NT4 SP4.
940 Also note that a bug in Win2K SP1-3 and XP up to SP1 only enables
941 this option for users in the local administrators group. */
942 if (wincap.has_exclusiveaddruse ())
944 if (!saw_reuseaddr ())
947 int ret = ::setsockopt (get_socket (), SOL_SOCKET,
949 (const char *) &on, sizeof on);
950 debug_printf ("%d = setsockopt (SO_EXCLUSIVEADDRUSE), %E", ret);
952 else if (!wincap.has_enhanced_socket_security ())
954 debug_printf ("SO_REUSEADDR set");
955 /* There's a bug in SO_REUSEADDR handling in WinSock.
956 Per standards, we must not be able to reuse a complete
957 duplicate of a local TCP address (same IP, same port),
958 even if SO_REUSEADDR has been set. That's unfortunately
961 So we're testing here if the local address is already in
962 use and don't bind, if so. This only works for OSes with
963 IP Helper support and is, of course, still prone to races.
965 However, we don't have to do this on systems supporting
966 "enhanced socket security" (2K3 and later). On these
967 systems the default binding behaviour is exactly as you'd
968 expect for SO_REUSEADDR, while setting SO_REUSEADDR re-enables
969 the wrong behaviour. So all we have to do on these newer
970 systems is never to set SO_REUSEADDR but only to note that
971 it has been set for the above SO_EXCLUSIVEADDRUSE setting.
972 See setsockopt() in net.cc. */
973 if (get_socket_type () == SOCK_STREAM
974 && wincap.has_ip_helper_lib ()
975 && address_in_use (name))
977 debug_printf ("Local address in use, don't bind");
978 set_errno (EADDRINUSE);
983 if (::bind (get_socket (), name, namelen))
984 set_winsock_errno ();
994 fhandler_socket::connect (const struct sockaddr *name, int namelen)
997 bool in_progress = false;
998 struct sockaddr_storage sst;
1002 if (!get_inet_addr (name, namelen, &sst, &namelen, &type, connect_secret))
1005 if (get_addr_family () == AF_LOCAL && get_socket_type () != type)
1007 WSASetLastError (WSAEPROTOTYPE);
1008 set_winsock_errno ();
1012 res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen);
1013 if (!is_nonblocking ()
1014 && res == SOCKET_ERROR
1015 && WSAGetLastError () == WSAEWOULDBLOCK)
1016 res = wait_for_events (FD_CONNECT | FD_CLOSE);
1022 err = WSAGetLastError ();
1023 /* Special handling for connect to return the correct error code
1024 when called on a non-blocking socket. */
1025 if (is_nonblocking ())
1027 if (err == WSAEWOULDBLOCK || err == WSAEALREADY)
1030 if (err == WSAEWOULDBLOCK)
1031 WSASetLastError (err = WSAEINPROGRESS);
1033 if (err == WSAEINVAL)
1034 WSASetLastError (err = WSAEISCONN);
1035 set_winsock_errno ();
1038 if (get_addr_family () == AF_LOCAL && (!res || in_progress))
1039 set_sun_path (name->sa_data);
1041 if (get_addr_family () == AF_LOCAL && get_socket_type () == SOCK_STREAM)
1043 af_local_set_cred (); /* Don't move into af_local_connect since
1044 af_local_connect is called from select,
1045 possibly running under another identity. */
1046 if (!res && af_local_connect ())
1048 set_winsock_errno ();
1053 if (err == WSAEINPROGRESS || err == WSAEALREADY)
1054 connect_state (connect_pending);
1056 connect_state (connect_failed);
1058 connect_state (connected);
1064 fhandler_socket::listen (int backlog)
1066 int res = ::listen (get_socket (), backlog);
1067 if (res && WSAGetLastError () == WSAEINVAL)
1069 /* It's perfectly valid to call listen on an unbound INET socket.
1070 In this case the socket is automatically bound to an unused
1071 port number, listening on all interfaces. On Winsock, listen
1072 fails with WSAEINVAL when it's called on an unbound socket.
1073 So we have to bind manually here to have POSIX semantics. */
1074 if (get_addr_family () == AF_INET)
1076 struct sockaddr_in sin;
1077 sin.sin_family = AF_INET;
1079 sin.sin_addr.s_addr = INADDR_ANY;
1080 if (!::bind (get_socket (), (struct sockaddr *) &sin, sizeof sin))
1081 res = ::listen (get_socket (), backlog);
1083 else if (get_addr_family () == AF_INET6)
1085 struct sockaddr_in6 sin6 =
1087 sin6_family: AF_INET6,
1090 sin6_addr: IN6ADDR_ANY_INIT,
1093 if (!::bind (get_socket (), (struct sockaddr *) &sin6, sizeof sin6))
1094 res = ::listen (get_socket (), backlog);
1099 if (get_addr_family () == AF_LOCAL && get_socket_type () == SOCK_STREAM)
1100 af_local_set_cred ();
1101 connect_state (connected);
1105 set_winsock_errno ();
1110 fhandler_socket::accept (struct sockaddr *peer, int *len)
1112 /* Allows NULL peer and len parameters. */
1113 struct sockaddr_in peer_dummy;
1116 peer = (struct sockaddr *) &peer_dummy;
1119 len_dummy = sizeof (struct sockaddr_in);
1123 /* accept on NT fails if len < sizeof (sockaddr_in)
1124 * some programs set len to
1125 * sizeof (name.sun_family) + strlen (name.sun_path) for UNIX domain
1127 if (len && ((unsigned) *len < sizeof (struct sockaddr_in)))
1128 *len = sizeof (struct sockaddr_in);
1132 while (!(res = wait_for_events (FD_ACCEPT | FD_CLOSE))
1133 && (res = ::accept (get_socket (), peer, len)) == SOCKET_ERROR
1134 && WSAGetLastError () == WSAEWOULDBLOCK)
1136 if (res == (int) INVALID_SOCKET)
1137 set_winsock_errno ();
1140 cygheap_fdnew res_fd;
1141 if (res_fd >= 0 && fdsock (res_fd, &dev (), res))
1143 fhandler_socket *sock = (fhandler_socket *) res_fd;
1144 sock->set_addr_family (get_addr_family ());
1145 sock->set_socket_type (get_socket_type ());
1146 sock->async_io (async_io ());
1147 sock->set_nonblocking (is_nonblocking ());
1148 if (get_addr_family () == AF_LOCAL)
1150 sock->set_sun_path (get_sun_path ());
1151 if (get_socket_type () == SOCK_STREAM)
1153 /* Don't forget to copy credentials from accepting
1154 socket to accepted socket and start transaction
1155 on accepted socket! */
1156 af_local_copy (sock);
1157 res = sock->af_local_accept ();
1161 set_winsock_errno ();
1166 /* No locking necessary at this point. */
1167 sock->wsock_events->events = wsock_events->events | FD_WRITE;
1168 sock->wsock_events->owner = wsock_events->owner;
1169 sock->connect_state (connected);
1180 debug_printf ("res %d", res);
1185 fhandler_socket::getsockname (struct sockaddr *name, int *namelen)
1189 if (get_addr_family () == AF_LOCAL)
1191 struct sockaddr_un *sun = (struct sockaddr_un *) name;
1192 memset (sun, 0, *namelen);
1193 sun->sun_family = AF_LOCAL;
1195 if (!get_sun_path ())
1196 sun->sun_path[0] = '\0';
1198 /* According to SUSv2 "If the actual length of the address is
1199 greater than the length of the supplied sockaddr structure, the
1200 stored address will be truncated." We play it save here so
1201 that the path always has a trailing 0 even if it's truncated. */
1202 strncpy (sun->sun_path, get_sun_path (),
1203 *namelen - sizeof *sun + sizeof sun->sun_path - 1);
1205 *namelen = sizeof *sun - sizeof sun->sun_path
1206 + strlen (sun->sun_path) + 1;
1211 res = ::getsockname (get_socket (), name, namelen);
1214 if (WSAGetLastError () == WSAEINVAL)
1216 /* Winsock returns WSAEINVAL if the socket is locally
1217 unbound. Per SUSv3 this is not an error condition.
1218 We're faking a valid return value here by creating the
1219 same content in the sockaddr structure as on Linux. */
1220 switch (get_addr_family ())
1224 *namelen = sizeof (struct sockaddr_in);
1228 *namelen = sizeof (struct sockaddr_in6);
1231 WSASetLastError (WSAEOPNOTSUPP);
1236 memset (name, 0, *namelen);
1237 name->sa_family = get_addr_family ();
1241 set_winsock_errno ();
1249 fhandler_socket::getpeername (struct sockaddr *name, int *namelen)
1251 int res = ::getpeername (get_socket (), name, namelen);
1253 set_winsock_errno ();
1259 fhandler_socket::readv (const struct iovec *const iov, const int iovcnt,
1266 msg_iov: (struct iovec *) iov, // const_cast
1273 return recvmsg (&msg, 0);
1277 fhandler_socket::recv_internal (WSABUF *wsabuf, DWORD wsacnt, DWORD flags,
1278 struct sockaddr *from, int *fromlen)
1281 DWORD ret = 0, wret;
1282 int evt_mask = FD_READ | ((flags & MSG_OOB) ? FD_OOB : 0);
1284 bool waitall = (flags & MSG_WAITALL);
1285 flags &= (MSG_OOB | MSG_PEEK | MSG_DONTROUTE);
1288 if (get_socket_type () != SOCK_STREAM)
1290 WSASetLastError (WSAEOPNOTSUPP);
1291 set_winsock_errno ();
1292 return SOCKET_ERROR;
1294 if (is_nonblocking () || (flags & (MSG_OOB | MSG_PEEK)))
1298 /* Note: Don't call WSARecvFrom(MSG_PEEK) without actually having data
1299 waiting in the buffers, otherwise the event handling gets messed up
1301 while (!(res = wait_for_events (evt_mask | FD_CLOSE))
1302 || saw_shutdown_read ())
1304 res = WSARecvFrom (get_socket (), wsabuf, wsacnt, &wret,
1305 &flags, from, fromlen, NULL, NULL);
1311 while (wret && wsacnt)
1313 if (wsabuf->len > wret)
1315 wsabuf->len -= wret;
1316 wsabuf->buf += wret;
1321 wret -= wsabuf->len;
1329 else if (WSAGetLastError () != WSAEWOULDBLOCK)
1333 if (!ret && res == SOCKET_ERROR)
1335 /* According to SUSv3, errno isn't set in that case and no error
1336 condition is returned. */
1337 if (WSAGetLastError () == WSAEMSGSIZE)
1340 /* ESHUTDOWN isn't defined for recv in SUSv3. Simply EOF is returned
1342 if (WSAGetLastError () == WSAESHUTDOWN)
1345 set_winsock_errno ();
1346 return SOCKET_ERROR;
1353 fhandler_socket::recvfrom (void *ptr, size_t len, int flags,
1354 struct sockaddr *from, int *fromlen)
1356 WSABUF wsabuf = { len, (char *) ptr };
1357 return recv_internal (&wsabuf, 1, flags, from, fromlen);
1361 fhandler_socket::recvmsg (struct msghdr *msg, int flags)
1363 if (CYGWIN_VERSION_CHECK_FOR_USING_ANCIENT_MSGHDR)
1364 ((struct OLD_msghdr *) msg)->msg_accrightslen = 0;
1367 msg->msg_controllen = 0;
1370 if (get_addr_family () == AF_LOCAL)
1372 /* On AF_LOCAL sockets the (fixed-size) name of the shared memory
1373 area used for descriptor passing is transmitted first.
1374 If this string is empty, no descriptors are passed and we can
1375 go ahead recv'ing the normal data blocks. Otherwise start
1376 special handling for descriptor passing. */
1380 WSABUF wsabuf[msg->msg_iovlen];
1381 WSABUF *wsaptr = wsabuf + msg->msg_iovlen;
1382 const struct iovec *iovptr = msg->msg_iov + msg->msg_iovlen;
1383 while (--wsaptr >= wsabuf)
1385 wsaptr->len = (--iovptr)->iov_len;
1386 wsaptr->buf = (char *) iovptr->iov_base;
1389 struct sockaddr *from = (struct sockaddr *) msg->msg_name;
1390 int *fromlen = from ? &msg->msg_namelen : NULL;
1392 return recv_internal (wsabuf, msg->msg_iovlen, flags, from, fromlen);
1396 fhandler_socket::writev (const struct iovec *const iov, const int iovcnt,
1403 msg_iov: (struct iovec *) iov, // const_cast
1410 return sendmsg (&msg, 0);
1414 fhandler_socket::send_internal (struct _WSABUF *wsabuf, DWORD wsacnt, int flags,
1415 const struct sockaddr *to, int tolen)
1418 DWORD ret = 0, err = 0, sum = 0, off = 0;
1421 for (DWORD i = 0; i < wsacnt; off >= wsabuf[i].len && (++i, off = 0))
1423 buf.buf = wsabuf[i].buf + off;
1424 buf.len = wsabuf[i].len - off;
1425 if (buf.len > 65536) /* See KB 823764 */
1430 if ((res = WSASendTo (get_socket (), &buf, 1, &ret,
1431 flags & (MSG_OOB | MSG_DONTROUTE), to, tolen,
1433 && (err = WSAGetLastError ()) == WSAEWOULDBLOCK)
1436 wsock_events->events &= ~FD_WRITE;
1440 while (res && err == WSAEWOULDBLOCK
1441 && !(res = wait_for_events (FD_WRITE | FD_CLOSE)));
1448 else if (is_nonblocking () || err != WSAEWOULDBLOCK)
1454 else if (res == SOCKET_ERROR)
1456 set_winsock_errno ();
1458 /* Special handling for EPIPE and SIGPIPE.
1460 EPIPE is generated if the local end has been shut down on a connection
1461 oriented socket. In this case the process will also receive a SIGPIPE
1462 unless MSG_NOSIGNAL is set. */
1463 if (get_errno () == ESHUTDOWN && get_socket_type () == SOCK_STREAM)
1466 if (! (flags & MSG_NOSIGNAL))
1475 fhandler_socket::sendto (const void *ptr, size_t len, int flags,
1476 const struct sockaddr *to, int tolen)
1478 struct sockaddr_storage sst;
1480 if (to && !get_inet_addr (to, tolen, &sst, &tolen))
1481 return SOCKET_ERROR;
1483 /* Never write more than 64K at once to workaround a problem with
1484 Winsock, which creates a temporary buffer with the total incoming
1485 buffer size and copies the whole content over, regardless of
1486 the size of the internal send buffer. A buffer full condition
1487 is only recognized in subsequent calls and, if len is big enough,
1488 the call even might fail with an out-of-memory condition. */
1489 WSABUF wsabuf = { len, (char *) ptr };
1490 return send_internal (&wsabuf, 1, flags,
1491 (to ? (const struct sockaddr *) &sst : NULL), tolen);
1495 fhandler_socket::sendmsg (const struct msghdr *msg, int flags)
1497 if (get_addr_family () == AF_LOCAL)
1499 /* For AF_LOCAL/AF_UNIX sockets, if descriptors are given, start
1500 the special handling for descriptor passing. Otherwise just
1501 transmit an empty string to tell the receiver that no
1502 descriptor passing is done. */
1506 WSABUF wsabuf[msg->msg_iovlen];
1507 WSABUF *wsaptr = wsabuf;
1508 const struct iovec *iovptr = msg->msg_iov;
1509 for (int i = 0; i < msg->msg_iovlen; ++i)
1511 wsaptr->len = iovptr->iov_len;
1512 (wsaptr++)->buf = (char *) (iovptr++)->iov_base;
1515 return send_internal (wsabuf, msg->msg_iovlen, flags,
1516 (struct sockaddr *) msg->msg_name, msg->msg_namelen);
1520 fhandler_socket::shutdown (int how)
1522 int res = ::shutdown (get_socket (), how);
1525 set_winsock_errno ();
1530 saw_shutdown_read (true);
1533 saw_shutdown_write (true);
1536 saw_shutdown_read (true);
1537 saw_shutdown_write (true);
1544 fhandler_socket::close ()
1547 /* TODO: CV - 2008-04-16. Lingering disabled. The original problem
1548 could be no longer reproduced on NT4, XP, 2K8. Any return of a
1549 spurious "Connection reset by peer" *could* be caused by disabling
1550 the linger code here... */
1552 /* HACK to allow a graceful shutdown even if shutdown() hasn't been
1553 called by the application. Note that this isn't the ultimate
1554 solution but it helps in many cases. */
1555 struct linger linger;
1557 linger.l_linger = 240; /* secs. default 2MSL value according to MSDN. */
1558 setsockopt (get_socket (), SOL_SOCKET, SO_LINGER,
1559 (const char *)&linger, sizeof linger);
1562 while ((res = closesocket (get_socket ())) != 0)
1564 if (WSAGetLastError () != WSAEWOULDBLOCK)
1566 set_winsock_errno ();
1570 if (WaitForSingleObject (signal_arrived, 10) == WAIT_OBJECT_0)
1576 WSASetLastError (0);
1579 debug_printf ("%d = fhandler_socket::close()", res);
1583 /* Definitions of old ifreq stuff used prior to Cygwin 1.7.0. */
1584 #define OLD_SIOCGIFFLAGS _IOW('s', 101, struct __old_ifreq)
1585 #define OLD_SIOCGIFADDR _IOW('s', 102, struct __old_ifreq)
1586 #define OLD_SIOCGIFBRDADDR _IOW('s', 103, struct __old_ifreq)
1587 #define OLD_SIOCGIFNETMASK _IOW('s', 104, struct __old_ifreq)
1588 #define OLD_SIOCGIFHWADDR _IOW('s', 105, struct __old_ifreq)
1589 #define OLD_SIOCGIFMETRIC _IOW('s', 106, struct __old_ifreq)
1590 #define OLD_SIOCGIFMTU _IOW('s', 107, struct __old_ifreq)
1591 #define OLD_SIOCGIFINDEX _IOW('s', 108, struct __old_ifreq)
1593 #define CONV_OLD_TO_NEW_SIO(old) (((old)&0xff00ffff)|(((long)sizeof(struct ifreq)&IOCPARM_MASK)<<16))
1595 struct __old_ifreq {
1596 #define __OLD_IFNAMSIZ 16
1598 char ifrn_name[__OLD_IFNAMSIZ]; /* if name, e.g. "en0" */
1602 struct sockaddr ifru_addr;
1603 struct sockaddr ifru_broadaddr;
1604 struct sockaddr ifru_netmask;
1605 struct sockaddr ifru_hwaddr;
1614 fhandler_socket::ioctl (unsigned int cmd, void *p)
1616 extern int get_ifconf (struct ifconf *ifc, int what); /* net.cc */
1618 struct ifconf ifc, *ifcp;
1624 ifcp = (struct ifconf *) p;
1630 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
1632 ifc.ifc_len = ifcp->ifc_len / sizeof (struct __old_ifreq)
1633 * sizeof (struct ifreq);
1634 ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
1638 ifc.ifc_len = ifcp->ifc_len;
1639 ifc.ifc_buf = ifcp->ifc_buf;
1641 res = get_ifconf (&ifc, cmd);
1643 debug_printf ("error in get_ifconf");
1644 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
1646 struct __old_ifreq *ifr = (struct __old_ifreq *) ifcp->ifc_buf;
1647 for (ifrp = ifc.ifc_req;
1648 (caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
1651 memcpy (&ifr->ifr_ifrn, &ifrp->ifr_ifrn, sizeof ifr->ifr_ifrn);
1652 ifr->ifr_name[__OLD_IFNAMSIZ - 1] = '\0';
1653 memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru, sizeof ifr->ifr_ifru);
1655 ifcp->ifc_len = ifc.ifc_len / sizeof (struct ifreq)
1656 * sizeof (struct __old_ifreq);
1659 ifcp->ifc_len = ifc.ifc_len;
1661 case OLD_SIOCGIFFLAGS:
1662 case OLD_SIOCGIFADDR:
1663 case OLD_SIOCGIFBRDADDR:
1664 case OLD_SIOCGIFNETMASK:
1665 case OLD_SIOCGIFHWADDR:
1666 case OLD_SIOCGIFMETRIC:
1667 case OLD_SIOCGIFMTU:
1668 case OLD_SIOCGIFINDEX:
1669 cmd = CONV_OLD_TO_NEW_SIO (cmd);
1672 case SIOCGIFBRDADDR:
1673 case SIOCGIFNETMASK:
1679 case SIOCGIFFRNDLYNAM:
1680 case SIOCGIFDSTADDR:
1684 debug_printf ("ifr == NULL");
1689 if (cmd > SIOCGIFINDEX && CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
1691 debug_printf ("cmd not supported on this platform");
1695 ifc.ifc_len = 64 * sizeof (struct ifreq);
1696 ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
1697 if (cmd == SIOCGIFFRNDLYNAM)
1699 struct ifreq_frndlyname *iff = (struct ifreq_frndlyname *)
1700 alloca (64 * sizeof (struct ifreq_frndlyname));
1701 for (int i = 0; i < 64; ++i)
1702 ifc.ifc_req[i].ifr_frndlyname = &iff[i];
1705 res = get_ifconf (&ifc, cmd);
1708 debug_printf ("error in get_ifconf");
1712 if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
1714 struct __old_ifreq *ifr = (struct __old_ifreq *) p;
1715 debug_printf (" name: %s", ifr->ifr_name);
1716 for (ifrp = ifc.ifc_req;
1717 (caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
1720 debug_printf ("testname: %s", ifrp->ifr_name);
1721 if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
1723 memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
1724 sizeof ifr->ifr_ifru);
1731 struct ifreq *ifr = (struct ifreq *) p;
1732 debug_printf (" name: %s", ifr->ifr_name);
1733 for (ifrp = ifc.ifc_req;
1734 (caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
1737 debug_printf ("testname: %s", ifrp->ifr_name);
1738 if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
1740 if (cmd == SIOCGIFFRNDLYNAM)
1741 /* The application has to care for the space. */
1742 memcpy (ifr->ifr_frndlyname, ifrp->ifr_frndlyname,
1743 sizeof (struct ifreq_frndlyname));
1745 memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
1746 sizeof ifr->ifr_ifru);
1751 if ((caddr_t) ifrp >= ifc.ifc_buf + ifc.ifc_len)
1759 res = WSAAsyncSelect (get_socket (), winmsg, WM_ASYNCIO,
1760 *(int *) p ? ASYNC_MASK : 0);
1761 syscall_printf ("Async I/O on socket %s",
1762 *(int *) p ? "started" : "cancelled");
1763 async_io (*(int *) p != 0);
1764 /* If async_io is switched off, revert the event handling. */
1765 if (*(int *) p == 0)
1766 WSAEventSelect (get_socket (), wsock_evt, EVENT_MASK);
1769 res = ioctlsocket (get_socket (), FIONREAD, (unsigned long *) p);
1770 if (res == SOCKET_ERROR)
1771 set_winsock_errno ();
1774 /* Sockets are always non-blocking internally. So we just note the
1778 syscall_printf ("socket is now %sblocking",
1779 *(int *) p ? "non" : "");
1780 set_nonblocking (*(int *) p);
1784 res = ioctlsocket (get_socket (), cmd, (unsigned long *) p);
1787 syscall_printf ("%d = ioctl_socket (%x, %x)", res, cmd, p);
1792 fhandler_socket::fcntl (int cmd, void *arg)
1795 int request, current;
1801 pid_t pid = (pid_t) arg;
1803 wsock_events->owner = pid;
1805 debug_printf ("owner set to %d", pid);
1809 res = wsock_events->owner;
1813 /* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
1814 Set only the flag that has been passed in. If both are set, just
1815 record O_NONBLOCK. */
1816 int new_flags = (int) arg & O_NONBLOCK_MASK;
1817 if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
1818 new_flags = O_NONBLOCK;
1819 current = get_flags () & O_NONBLOCK_MASK;
1820 request = new_flags ? 1 : 0;
1821 if (!!current != !!new_flags && (res = ioctl (FIONBIO, &request)))
1823 set_flags ((get_flags () & ~O_NONBLOCK_MASK) | new_flags);
1827 res = fhandler_base::fcntl (cmd, arg);
1834 fhandler_socket::set_close_on_exec (bool val)
1836 set_no_inheritance (wsock_mtx, val);
1837 set_no_inheritance (wsock_evt, val);
1838 fhandler_base::set_close_on_exec (val);
1839 debug_printf ("set close_on_exec for %s to %d", get_name (), val);
1843 fhandler_socket::set_sun_path (const char *path)
1845 sun_path = path ? cstrdup (path) : NULL;
1849 fhandler_socket::getpeereid (pid_t *pid, __uid32_t *euid, __gid32_t *egid)
1851 if (get_addr_family () != AF_LOCAL || get_socket_type () != SOCK_STREAM)
1856 if (connect_state () != connected)
1858 set_errno (ENOTCONN);
1861 if (sec_peer_pid == (pid_t) 0)
1863 set_errno (ENOTCONN); /* Usually when calling getpeereid on
1864 accepting (instead of accepted) socket. */
1869 if (efault.faulted (EFAULT))
1872 *pid = sec_peer_pid;
1874 *euid = sec_peer_uid;
1876 *egid = sec_peer_gid;