2000-10-27 17:50:33 +08:00
|
|
|
/* fhandler_socket.cc. See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2018-03-01 01:56:13 +08:00
|
|
|
#include <sys/socket.h>
|
2012-07-06 21:52:19 +08:00
|
|
|
#include <asm/byteorder.h>
|
2002-10-21 09:00:58 +08:00
|
|
|
#include <unistd.h>
|
2012-03-08 17:36:11 +08:00
|
|
|
#include <sys/param.h>
|
2018-02-14 17:20:42 +08:00
|
|
|
#include <sys/statvfs.h>
|
2016-01-07 01:41:36 +08:00
|
|
|
#include <cygwin/acl.h>
|
2018-03-01 01:56:13 +08:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
2018-02-22 04:40:01 +08:00
|
|
|
#include "tls_pbuf.h"
|
2000-10-27 17:50:33 +08:00
|
|
|
|
2002-06-27 03:25:09 +08:00
|
|
|
extern "C" {
|
2018-02-22 04:40:01 +08:00
|
|
|
int sscanf (const char *, const char *, ...);
|
2002-06-27 03:25:09 +08:00
|
|
|
} /* End of "C" section */
|
|
|
|
|
2000-10-27 17:50:33 +08:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_socket */
|
|
|
|
|
2004-04-09 16:43:29 +08:00
|
|
|
fhandler_socket::fhandler_socket () :
|
|
|
|
fhandler_base (),
|
2018-02-22 04:40:01 +08:00
|
|
|
uid (myself->uid),
|
|
|
|
gid (myself->gid),
|
|
|
|
mode (S_IFSOCK | S_IRWXU | S_IRWXG | S_IRWXO),
|
2018-02-07 23:16:51 +08:00
|
|
|
_rcvtimeo (INFINITE),
|
|
|
|
_sndtimeo (INFINITE),
|
2004-04-09 20:09:45 +08:00
|
|
|
status ()
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_socket::~fhandler_socket ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-22 23:54:08 +08:00
|
|
|
char *
|
|
|
|
fhandler_socket::get_proc_fd_name (char *buf)
|
2005-02-20 04:03:18 +08:00
|
|
|
{
|
2018-02-22 23:54:08 +08:00
|
|
|
__small_sprintf (buf, "socket:[%lu]", get_plain_ino ());
|
|
|
|
return buf;
|
2005-02-20 04:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-02-22 23:54:08 +08:00
|
|
|
fhandler_socket::getpeereid (pid_t *pid, uid_t *euid, gid_t *egid)
|
2005-02-20 04:03:18 +08:00
|
|
|
{
|
2018-02-22 23:54:08 +08:00
|
|
|
set_errno (EINVAL);
|
2007-02-27 20:58:56 +08:00
|
|
|
return -1;
|
2005-02-20 04:03:18 +08:00
|
|
|
}
|
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
/* Definitions of old ifreq stuff used prior to Cygwin 1.7.0. */
|
|
|
|
#define OLD_SIOCGIFFLAGS _IOW('s', 101, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFADDR _IOW('s', 102, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFBRDADDR _IOW('s', 103, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFNETMASK _IOW('s', 104, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFHWADDR _IOW('s', 105, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFMETRIC _IOW('s', 106, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFMTU _IOW('s', 107, struct __old_ifreq)
|
|
|
|
#define OLD_SIOCGIFINDEX _IOW('s', 108, struct __old_ifreq)
|
|
|
|
|
|
|
|
#define CONV_OLD_TO_NEW_SIO(old) (((old)&0xff00ffff)|(((long)sizeof(struct ifreq)&IOCPARM_MASK)<<16))
|
|
|
|
|
|
|
|
struct __old_ifreq {
|
2011-06-06 13:02:13 +08:00
|
|
|
#define __OLD_IFNAMSIZ 16
|
2007-01-22 06:54:05 +08:00
|
|
|
union {
|
|
|
|
char ifrn_name[__OLD_IFNAMSIZ]; /* if name, e.g. "en0" */
|
|
|
|
} ifr_ifrn;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct sockaddr ifru_addr;
|
|
|
|
struct sockaddr ifru_broadaddr;
|
|
|
|
struct sockaddr ifru_netmask;
|
|
|
|
struct sockaddr ifru_hwaddr;
|
|
|
|
short ifru_flags;
|
|
|
|
int ifru_metric;
|
|
|
|
int ifru_mtu;
|
|
|
|
int ifru_ifindex;
|
|
|
|
} ifr_ifru;
|
|
|
|
};
|
|
|
|
|
2000-10-27 17:50:33 +08:00
|
|
|
int
|
|
|
|
fhandler_socket::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
2007-06-21 23:57:54 +08:00
|
|
|
extern int get_ifconf (struct ifconf *ifc, int what); /* net.cc */
|
2000-10-27 17:50:33 +08:00
|
|
|
int res;
|
2001-02-08 06:50:50 +08:00
|
|
|
struct ifconf ifc, *ifcp;
|
2007-01-22 06:54:05 +08:00
|
|
|
struct ifreq *ifrp;
|
2002-06-27 03:25:09 +08:00
|
|
|
|
2000-10-27 17:50:33 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SIOCGIFCONF:
|
2001-02-08 06:50:50 +08:00
|
|
|
ifcp = (struct ifconf *) p;
|
|
|
|
if (!ifcp)
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-01-22 06:54:05 +08:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
|
|
|
{
|
|
|
|
ifc.ifc_len = ifcp->ifc_len / sizeof (struct __old_ifreq)
|
2007-02-20 08:16:18 +08:00
|
|
|
* sizeof (struct ifreq);
|
2007-01-22 06:54:05 +08:00
|
|
|
ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
|
|
|
|
}
|
|
|
|
else
|
2007-02-20 08:16:18 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
ifc.ifc_len = ifcp->ifc_len;
|
|
|
|
ifc.ifc_buf = ifcp->ifc_buf;
|
|
|
|
}
|
2007-06-21 23:57:54 +08:00
|
|
|
res = get_ifconf (&ifc, cmd);
|
2000-10-27 17:50:33 +08:00
|
|
|
if (res)
|
2002-09-30 12:35:18 +08:00
|
|
|
debug_printf ("error in get_ifconf");
|
2007-01-22 06:54:05 +08:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
2007-02-20 08:16:18 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
struct __old_ifreq *ifr = (struct __old_ifreq *) ifcp->ifc_buf;
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp, ++ifr)
|
|
|
|
{
|
|
|
|
memcpy (&ifr->ifr_ifrn, &ifrp->ifr_ifrn, sizeof ifr->ifr_ifrn);
|
|
|
|
ifr->ifr_name[__OLD_IFNAMSIZ - 1] = '\0';
|
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru, sizeof ifr->ifr_ifru);
|
|
|
|
}
|
|
|
|
ifcp->ifc_len = ifc.ifc_len / sizeof (struct ifreq)
|
|
|
|
* sizeof (struct __old_ifreq);
|
|
|
|
}
|
|
|
|
else
|
2007-02-20 08:16:18 +08:00
|
|
|
ifcp->ifc_len = ifc.ifc_len;
|
2000-10-27 17:50:33 +08:00
|
|
|
break;
|
2007-01-22 06:54:05 +08:00
|
|
|
case OLD_SIOCGIFFLAGS:
|
|
|
|
case OLD_SIOCGIFADDR:
|
|
|
|
case OLD_SIOCGIFBRDADDR:
|
|
|
|
case OLD_SIOCGIFNETMASK:
|
|
|
|
case OLD_SIOCGIFHWADDR:
|
|
|
|
case OLD_SIOCGIFMETRIC:
|
|
|
|
case OLD_SIOCGIFMTU:
|
|
|
|
case OLD_SIOCGIFINDEX:
|
|
|
|
cmd = CONV_OLD_TO_NEW_SIO (cmd);
|
|
|
|
/*FALLTHRU*/
|
2000-10-27 17:50:33 +08:00
|
|
|
case SIOCGIFFLAGS:
|
|
|
|
case SIOCGIFBRDADDR:
|
|
|
|
case SIOCGIFNETMASK:
|
|
|
|
case SIOCGIFADDR:
|
2001-02-08 06:50:50 +08:00
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
case SIOCGIFMETRIC:
|
|
|
|
case SIOCGIFMTU:
|
2007-01-16 20:01:36 +08:00
|
|
|
case SIOCGIFINDEX:
|
2007-01-22 06:54:05 +08:00
|
|
|
case SIOCGIFFRNDLYNAM:
|
2007-06-15 00:02:32 +08:00
|
|
|
case SIOCGIFDSTADDR:
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
if (!p)
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
2002-09-30 12:35:18 +08:00
|
|
|
debug_printf ("ifr == NULL");
|
2000-10-27 17:50:33 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
if (cmd > SIOCGIFINDEX && CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
|
|
|
{
|
|
|
|
debug_printf ("cmd not supported on this platform");
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ifc.ifc_len = 64 * sizeof (struct ifreq);
|
|
|
|
ifc.ifc_buf = (caddr_t) alloca (ifc.ifc_len);
|
|
|
|
if (cmd == SIOCGIFFRNDLYNAM)
|
|
|
|
{
|
|
|
|
struct ifreq_frndlyname *iff = (struct ifreq_frndlyname *)
|
2007-02-20 08:16:18 +08:00
|
|
|
alloca (64 * sizeof (struct ifreq_frndlyname));
|
2007-01-22 06:54:05 +08:00
|
|
|
for (int i = 0; i < 64; ++i)
|
|
|
|
ifc.ifc_req[i].ifr_frndlyname = &iff[i];
|
|
|
|
}
|
|
|
|
|
2007-06-21 23:57:54 +08:00
|
|
|
res = get_ifconf (&ifc, cmd);
|
2000-10-27 17:50:33 +08:00
|
|
|
if (res)
|
|
|
|
{
|
2002-09-30 12:35:18 +08:00
|
|
|
debug_printf ("error in get_ifconf");
|
2000-10-27 17:50:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
if (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ)
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
struct __old_ifreq *ifr = (struct __old_ifreq *) p;
|
|
|
|
debug_printf (" name: %s", ifr->ifr_name);
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp)
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
debug_printf ("testname: %s", ifrp->ifr_name);
|
|
|
|
if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
|
|
|
|
sizeof ifr->ifr_ifru);
|
2001-02-08 06:50:50 +08:00
|
|
|
break;
|
2007-01-22 06:54:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ifreq *ifr = (struct ifreq *) p;
|
|
|
|
debug_printf (" name: %s", ifr->ifr_name);
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp)
|
|
|
|
{
|
|
|
|
debug_printf ("testname: %s", ifrp->ifr_name);
|
|
|
|
if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
|
|
|
|
{
|
|
|
|
if (cmd == SIOCGIFFRNDLYNAM)
|
|
|
|
/* The application has to care for the space. */
|
|
|
|
memcpy (ifr->ifr_frndlyname, ifrp->ifr_frndlyname,
|
|
|
|
sizeof (struct ifreq_frndlyname));
|
|
|
|
else
|
|
|
|
memcpy (&ifr->ifr_ifru, &ifrp->ifr_ifru,
|
|
|
|
sizeof ifr->ifr_ifru);
|
2007-01-16 20:01:36 +08:00
|
|
|
break;
|
2000-10-27 17:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((caddr_t) ifrp >= ifc.ifc_buf + ifc.ifc_len)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-02-22 23:30:08 +08:00
|
|
|
res = fhandler_base::ioctl (cmd, p);
|
2000-10-27 17:50:33 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
syscall_printf ("%d = ioctl_socket(%x, %p)", res, cmd, p);
|
2000-10-27 17:50:33 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_socket::fcntl (int cmd, intptr_t arg)
|
2000-10-27 17:50:33 +08:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int request, current;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_SETFL:
|
|
|
|
{
|
2001-09-08 05:32:07 +08:00
|
|
|
/* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
|
2001-08-07 08:01:42 +08:00
|
|
|
Set only the flag that has been passed in. If both are set, just
|
|
|
|
record O_NONBLOCK. */
|
2013-04-23 17:44:36 +08:00
|
|
|
int new_flags = arg & O_NONBLOCK_MASK;
|
2001-08-07 08:01:42 +08:00
|
|
|
if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
|
|
|
|
new_flags = O_NONBLOCK;
|
2001-09-08 05:32:07 +08:00
|
|
|
current = get_flags () & O_NONBLOCK_MASK;
|
2001-08-14 15:41:45 +08:00
|
|
|
request = new_flags ? 1 : 0;
|
2001-09-08 05:32:07 +08:00
|
|
|
if (!!current != !!new_flags && (res = ioctl (FIONBIO, &request)))
|
|
|
|
break;
|
2001-08-14 15:41:45 +08:00
|
|
|
set_flags ((get_flags () & ~O_NONBLOCK_MASK) | new_flags);
|
2001-09-08 05:32:07 +08:00
|
|
|
break;
|
2000-10-27 17:50:33 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = fhandler_base::fcntl (cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-22 23:54:08 +08:00
|
|
|
int
|
|
|
|
fhandler_socket::open (int flags, mode_t mode)
|
2001-06-23 04:53:45 +08:00
|
|
|
{
|
2018-02-22 23:54:08 +08:00
|
|
|
set_errno (ENXIO);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket::fstat (struct stat *buf)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2018-02-27 22:30:00 +08:00
|
|
|
res = fhandler_base::fstat (buf);
|
2018-02-22 23:54:08 +08:00
|
|
|
if (!res)
|
2009-11-23 20:21:39 +08:00
|
|
|
{
|
2018-02-23 20:32:51 +08:00
|
|
|
buf->st_dev = FHDEV (DEV_SOCK_MAJOR, 0);
|
2018-02-22 23:54:08 +08:00
|
|
|
if (!(buf->st_ino = get_plain_ino ()))
|
|
|
|
sscanf (get_name (), "/proc/%*d/fd/socket:[%lld]",
|
|
|
|
(long long *) &buf->st_ino);
|
|
|
|
buf->st_uid = uid;
|
|
|
|
buf->st_gid = gid;
|
|
|
|
buf->st_mode = mode;
|
|
|
|
buf->st_size = 0;
|
2009-11-23 20:21:39 +08:00
|
|
|
}
|
2018-02-22 23:54:08 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_socket::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
memset (sfs, 0, sizeof (*sfs));
|
|
|
|
sfs->f_bsize = sfs->f_frsize = 4096;
|
|
|
|
sfs->f_namemax = NAME_MAX;
|
|
|
|
return 0;
|
2001-06-23 04:53:45 +08:00
|
|
|
}
|
2002-01-02 00:25:31 +08:00
|
|
|
|
2005-02-24 01:39:46 +08:00
|
|
|
int
|
2018-02-22 23:54:08 +08:00
|
|
|
fhandler_socket::fchmod (mode_t newmode)
|
2005-02-24 01:39:46 +08:00
|
|
|
{
|
2018-02-22 23:54:08 +08:00
|
|
|
mode = (newmode & ~S_IFMT) | S_IFSOCK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::fchown (uid_t newuid, gid_t newgid)
|
|
|
|
{
|
|
|
|
bool perms = check_token_membership (&well_known_admins_sid);
|
|
|
|
|
|
|
|
/* Admin rulez */
|
|
|
|
if (!perms)
|
|
|
|
{
|
|
|
|
/* Otherwise, new uid == old uid or current uid is fine */
|
|
|
|
if (newuid == ILLEGAL_UID || newuid == uid || newuid == myself->uid)
|
|
|
|
perms = true;
|
|
|
|
/* Otherwise, new gid == old gid or current gid is fine */
|
|
|
|
else if (newgid == ILLEGAL_GID || newgid == gid || newgid == myself->gid)
|
|
|
|
perms = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Last but not least, newgid in supplementary group list is fine */
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
gid_t *gids = (gid_t *) tp.w_get ();
|
|
|
|
int num = getgroups (65536 / sizeof (*gids), gids);
|
|
|
|
|
|
|
|
for (int idx = 0; idx < num; ++idx)
|
|
|
|
if (newgid == gids[idx])
|
|
|
|
{
|
|
|
|
perms = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perms)
|
|
|
|
{
|
|
|
|
if (newuid != ILLEGAL_UID)
|
|
|
|
uid = newuid;
|
|
|
|
if (newgid != ILLEGAL_GID)
|
|
|
|
gid = newgid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_errno (EPERM);
|
2014-08-22 17:21:33 +08:00
|
|
|
return -1;
|
2005-02-24 01:39:46 +08:00
|
|
|
}
|
2018-02-22 23:54:08 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::facl (int cmd, int nentries, aclent_t *aclbufp)
|
|
|
|
{
|
|
|
|
set_errno (EOPNOTSUPP);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::link (const char *newpath)
|
|
|
|
{
|
|
|
|
return fhandler_base::link (newpath);
|
|
|
|
}
|