From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2119.outbound.protection.outlook.com [40.107.236.119]) by sourceware.org (Postfix) with ESMTPS id 09A593857C7A for ; Sat, 20 Feb 2021 23:51:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 09A593857C7A ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a7QtEVaDgiwwJmSbxMGm69HPq/id3WwhKLvp9ry9YNgPNlPdTYvwV1YGZveReFAvHEfMej+VcACJcp7v9zQVQqMKNItbKcmhQtXi95ABMrXL4IHXSxrSrkeBN+psmfx8V5Oah9bnr8N+pfReBEo43WokckS7jQB+n3cROLVRm7vyD0rI8Qn3Ks1ZpmP9wEUlY+qojqRJ4myo68ws2a7En7ueT18DE56/CY5w/Jc+/8EkLA+UmVWkdnyQTB2c16kQJB40JB8pcha0626WDsioni3rMGRIyFKdsk4gqFvdVOueKPJYqb1fe14lhE9q994wa6z7ZWHicVK28AFgOLYMYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bV28v8022304f0akO77DlCXK6PFb3gc8wKsd5A7LWzk=; b=QMdE7dV9sAzWJE9cg5vyoLoJQNzEs+K0L80iOPVegms6rMqnZFFKVyoPjQ+DqeVym1gvq7EofpJYdkC1Ixi/tsrPHN/dJgF8gmBz+uW+uKpSNXxYsTgBvYqP6AkO2z5RGRCgyEVehAyjNOd9kkl+9aBO3bX+fFMdXwaUDOhmTlUG9h+l66l8IKIBB1xbUxD4Cpqt7TabQgDdeFzxIBEc9csgqamqYnQBBNkZtg4v6abY7AVqX3iW2vmNwdSnwNahGJdUoN/gdvVWYV8yvcxlz4lJBloh7pNCikgvz3BIq005tHKXaQGrw4GgFAqJx061hPWY/VZ2gyqvuL52ukeRlQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=cornell.edu; dmarc=pass action=none header.from=cornell.edu; dkim=pass header.d=cornell.edu; arc=none Received: from BN7PR04MB4388.namprd04.prod.outlook.com (2603:10b6:406:f8::19) by BN8PR04MB5713.namprd04.prod.outlook.com (2603:10b6:408:76::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3846.25; Sat, 20 Feb 2021 23:51:27 +0000 Received: from BN7PR04MB4388.namprd04.prod.outlook.com ([fe80::f071:e174:ef12:375c]) by BN7PR04MB4388.namprd04.prod.outlook.com ([fe80::f071:e174:ef12:375c%6]) with mapi id 15.20.3846.045; Sat, 20 Feb 2021 23:51:27 +0000 Subject: Re: fstat and similar methods in fhandler_socket_local and fhandler_socket_unix To: cygwin-developers@cygwin.com References: From: Ken Brown Message-ID: Date: Sat, 20 Feb 2021 18:51:24 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [2603:7081:7e41:6a00:8dc0:ca72:dedf:9d4d] X-ClientProxiedBy: MN2PR06CA0005.namprd06.prod.outlook.com (2603:10b6:208:23d::10) To BN7PR04MB4388.namprd04.prod.outlook.com (2603:10b6:406:f8::19) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [IPv6:2603:7081:7e41:6a00:8dc0:ca72:dedf:9d4d] (2603:7081:7e41:6a00:8dc0:ca72:dedf:9d4d) by MN2PR06CA0005.namprd06.prod.outlook.com (2603:10b6:208:23d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27 via Frontend Transport; Sat, 20 Feb 2021 23:51:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4f9488f0-0a6a-4036-9bee-08d8d5fa6fcf X-MS-TrafficTypeDiagnostic: BN8PR04MB5713: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2958; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xaZVtqRTYTt120StKh0iG+jkb2ugW/l3mS+kQ2PtuAjE4dt02HTGK8j/aX1awlWdv2DTrfj0Yo8/0wHf4gEORuvJRUbCr13uIto42b4W4i5UnAL0grnAT7fylSEC/jyMpw/VwnGUzW6/iZGeowNXK71LCCGbPdkdlgeplHmhtoNBVPsLblwdnb7BDTFDowcgGJbk+J69wkxX1VqhsbMb15VZnbt+zWUh8e7qu8jmMxEfbvdH6fXM2KUX1M5tK6yasxEj1hS9zi0qIhE4Fr19o6vDLi7qCa0ERlSul9wGUMwYXWhI2u91yTts1bCudquD4rWm1hUASfyLLL800FlJTzENk99MViqclLdAOsCtGfWMFQ6cbIS10V8YpnHOBAVXpILoKb2PLRByfT3YbdCn9zvg3FE75MHRZPVSJ26+SfOISaqj+yidiJiBu3/WQPLwTetisHN+CD1Z8R/5wHDEVB0HA+c8EweuutjV4BbwTdCuH5o2iXBsCqkS4tNOmnDG7OSYgkOBc3ad0FYI4GixKSMcUIETMyo7zy5HZ3Rk4Abw2Z+ZwWYSBPA49qunARE+Zo7831YU5zH/swaNVYoQ1A419AE7M49UjkuG0KAitSWcUhKaHKSKkQo94wfiE9mDu8ZoozcTBxK93OcuOQvxxULDiXqTnJk2T2YgaABw6slueEmm2wAA+HMOBuQSyQZUmy4LUPSez+vVVrBJN8SllA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BN7PR04MB4388.namprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(396003)(346002)(136003)(39860400002)(366004)(966005)(52116002)(478600001)(16526019)(316002)(186003)(786003)(6916009)(8676002)(31686004)(75432002)(8936002)(53546011)(36756003)(31696002)(2906002)(86362001)(66556008)(66476007)(66946007)(5660300002)(2616005)(83380400001)(6486002)(43740500002)(45980500001)(460985005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?Windows-1252?Q?cY8grXoV4p/jO6RlkvdGZZA8I8N2huo8Xa199+AR5ED9cmrz02LUM1Fs?= =?Windows-1252?Q?ny/uDLgzOFdhMgJloMYfccKvMWIh2dQTs/q6oIsitv6JfePXN/ksAG1x?= =?Windows-1252?Q?OvYsHv4cyu6BaKE4Jbq9lff9SJjzyXbi7L6Jj20rMw8/787XI1Sg1OoU?= =?Windows-1252?Q?cpdOow46kFAqv0gDDFGUi+C5hS/Bz3w5WiUlzQ3iHa9jz8OCUs7ftIEn?= =?Windows-1252?Q?BZal4QHV1HPcqFk8Eu1MaFi7nlhBRl6sZW9XHoFor/KT03tzzgDguknQ?= =?Windows-1252?Q?H0ECcAri+LKyESYj3sen8InPdV7YPdbTCD3hBaYW26PsR3kyB1DV5bt7?= =?Windows-1252?Q?K0G9i7j+94mkC7ukBgGUH2xA8u82fAJYvhn9vzc4qgTVl4d9jfm3nwET?= =?Windows-1252?Q?wILf6vkZUBqpa5hxCefMfNw22aLif4xJP4z1axkuL9zNk9ClGyasFpi3?= =?Windows-1252?Q?9Qv7K2PQJhdb0/rd5dBwXJpWXmcjcC1gQHy/SstomCrlSbnsIQnwJvvH?= =?Windows-1252?Q?WeW6WEcRRvaKboJt2ULAi02hAW9C3GyXkn929FijltLvLto1A/8UvzeF?= =?Windows-1252?Q?VL0qFycRzXDjU5AmU7RELWjfWuWBiUh4e6IIEXsc36Gm7BektMrQwGkG?= =?Windows-1252?Q?h5MzAhemWNUdPFlXRWNyFCE6iipZQ3Dq/e+U7JK+6sKqHOF1KfI+O1p6?= =?Windows-1252?Q?qSqX/5eXnVzvoAV9VxXHgKPLghtu+f31Jhf+DnskDy3mGhvIJAdg8gyE?= =?Windows-1252?Q?Nysrn1zC9SGlGyHAylDQdkbkezuOdDeQ29eSo2+vXfszNGNxZ2A9VvwN?= =?Windows-1252?Q?PSxfPWYJ3CWP8wTFY5hCVh5KDuMIVUApy2VqFqGet/BZ04HS1EoDGhxm?= =?Windows-1252?Q?daYzeLTCA70mbDoutnFLwpObgWWbIlvsaYm4z/+XhVpTIdTvRn2jf9+U?= =?Windows-1252?Q?qTylUfbASZCw39eThCmcc3d1Oj3WlBgJovMEtnJTW6oVfKRlhyr44pyO?= =?Windows-1252?Q?c0dN5lBOJqdzsNGyidRhxc+Ic+TzqU1OlJGU1AWnYc6CNH6Z4TUavBug?= =?Windows-1252?Q?ctCFvCNfYQS29rXN7Q1LK+8D/cghlDJIMhN5dQtu7lcC27I4cqC4nMcl?= =?Windows-1252?Q?R6/09vK5rklZkeNYI7rnI5FAsFwu7KnOdFulqu2ic5oJWCwqc911uUKx?= =?Windows-1252?Q?mZUaL2zUmw04bpJPIO2WTEKaMVVpgry7w6qJ2jOD1i0H/8GK7KMTao29?= =?Windows-1252?Q?Rf/FMDtHhEGlGZn0SuUKZ5Rjf+vCqemWznFAqP9Qx+JN7aEtWYJQngz8?= =?Windows-1252?Q?+HVhlbSF5EJrv/tzGbDMGpFhH6cWU9GtKUp99XcoHHGLDB5Ymb1n+4XC?= =?Windows-1252?Q?Jr1wKeC2mOMnRoBoc89EatkK9fgyfy5J/50cMdGsXXAvLN1vQolQGUR8?= =?Windows-1252?Q?EbG1j5KLfn4TgcawNtnypVfGmqOF75kZM05bV67nAehiRRTnwBnE3q8Z?= =?Windows-1252?Q?g0CYitKe?= X-OriginatorOrg: cornell.edu X-MS-Exchange-CrossTenant-Network-Message-Id: 4f9488f0-0a6a-4036-9bee-08d8d5fa6fcf X-MS-Exchange-CrossTenant-AuthSource: BN7PR04MB4388.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2021 23:51:27.1499 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 5d7e4366-1b9b-45cf-8e79-b14b27df46e1 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: LRfMk5S675gCuZXkpECcAS5q3AzUD4HKQkXnHfQol75ci84cRRSAeUmwJpiAP22kOAQhmR+7StrkDs+31Bcd5A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8PR04MB5713 X-Spam-Status: No, score=1.4 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_INVALID, DKIM_SIGNED, KAM_DMARC_STATUS, KAM_SHORT, MSGID_FROM_MTA_HEADER, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: cygwin-developers@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component developers mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Feb 2021 23:51:31 -0000 On 2/20/2021 6:32 PM, Ken Brown via Cygwin-developers wrote: > fhandler_socket_local::fstat and many similar methods seem to assume that the > fhandler is based on a socket *file*.  When this is not the case, they end up > using handles inappropriately.  Consider the following test case, for example. > > $ cat socket_stat.c > #include > #include > #include > #include > #include > #include > #include > #include > > void print_stat (struct stat); > > int > main () > { >   struct stat st; >   int fd; > >   fd = socket (AF_UNIX, SOCK_STREAM, 0); >   if (fd == -1) >     { >       perror ("socket"); >       exit (1); >     } >   if (fstat (fd, &st) < 0) >     { >       perror ("fstat"); >       exit (1); >     } >   print_stat (st); > } > > /* https://man7.org/linux/man-pages/man2/lstat.2.html */ > void > print_stat (struct stat st) > { >   printf("File type:                "); > >   switch (st.st_mode & S_IFMT) { >   case S_IFBLK:  printf("block device\n");            break; >   case S_IFCHR:  printf("character device\n");        break; >   case S_IFDIR:  printf("directory\n");               break; >   case S_IFIFO:  printf("FIFO/pipe\n");               break; >   case S_IFLNK:  printf("symlink\n");                 break; >   case S_IFREG:  printf("regular file\n");            break; >   case S_IFSOCK: printf("socket\n");                  break; >   default:       printf("unknown?\n");                break; >   } > >   printf("I-node number:            %ld\n", (long) st.st_ino); >   printf("dev:                      %lu\n", st.st_dev); >   printf("rdev:                     %lu\n", st.st_rdev); >   printf("Mode:                     %lo (octal)\n", >          (unsigned long) st.st_mode); > >   printf("Link count:               %ld\n", (long) st.st_nlink); >   printf("Ownership:                UID=%ld   GID=%ld\n", >          (long) st.st_uid, (long) st.st_gid); > >   printf("Preferred I/O block size: %ld bytes\n", >          (long) st.st_blksize); >   printf("File size:                %lld bytes\n", >          (long long) st.st_size); >   printf("Blocks allocated:         %lld\n", >          (long long) st.st_blocks); > >   printf("Last status change:       %s", ctime(&st.st_ctime)); >   printf("Last file access:         %s", ctime(&st.st_atime)); >   printf("Last file modification:   %s", ctime(&st.st_mtime)); >   printf("\n"); > } > > $ gcc -g -O0 -o socket_stat socket_stat.c > > $ ./socket_stat.exe > File type:                socket > I-node number:            4 > dev:                      1966200 > rdev:                     1966200 > Mode:                     140644 (octal) > Link count:               0 > Ownership:                UID=197609   GID=197121 > Preferred I/O block size: 65536 bytes > File size:                0 bytes > Blocks allocated:         0 > Last status change:       Wed Dec 31 19:00:00 1969 > Last file access:         Wed Dec 31 19:00:00 1969 > Last file modification:   Wed Dec 31 19:00:00 1969 > > On Linux the output is: > > File type:                socket > I-node number:            117 > dev:                      0 > rdev:                     0 > Mode:                     140777 (octal) > Link count:               1 > Ownership:                UID=1000   GID=1000 > Preferred I/O block size: 512 bytes > File size:                0 bytes > Blocks allocated:         0 > Last status change:       Sat Feb 20 18:13:50 2021 > Last file access:         Sat Feb 20 18:13:50 2021 > Last file modification:   Sat Feb 20 18:13:50 2021 > > I haven't been able to find any documentation about what fstat(2) should do on a > socket descriptor, so maybe the difference between Cygwin and Linux is > unimportant.  But the code path followed on Cygwin definitely seems wrong. > fhandler_socket_local::fstat calls fstat_fs which ends up using the io_handle as > though it were a file handle.  It seems to me that fstat_fs should only be > called if we have a path_conv handle (which is the case if the fstat call > resulted from a stat call) or if the socket descriptor came from open with > O_PATH set. > > Similar remarks apply to fstatfvs, fchmod,....  In some of these cases, like > fchmod, POSIX explicitly states that the behavior is undefined, so maybe we > should just fail when there's no underlying socket file. Maybe the answer in all these cases is just to revert to the fhandler_socket methods when our fhandler_socket_local is not based on a socket file. Ken