1 /* Test of passing file descriptors.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
28 #include <sys/socket.h>
37 #if defined __CYGWIN__
38 /* Cygwin does not support file-descriptor passing: As on Cygwin 3.5.3,
39 the only cmsg_type that winsup/cygwin/fhandler/socket_unix.cc handles
40 is SCM_CREDENTIALS. Not SCM_RIGHTS. */
52 /* Avoid hanging on failure. */
54 signal (SIGALRM
, SIG_DFL
);
58 fdnull
= open ("/dev/null", O_RDWR
);
61 perror ("Could not open /dev/null");
65 ret
= socketpair (AF_UNIX
, SOCK_STREAM
, 0, pair
);
68 perror ("socket pair failed");
80 ret
= sendfd (pair
[1], fdnull
);
91 ASSERT (close (pair
[1]) == 0);
92 fd
= recvfd (pair
[0], 0);
98 ret
= waitpid (pid
, &status
, 0);
106 if (!WIFEXITED (status
))
108 fprintf (stderr
, "Child does not normally exit\n");
111 ret
= WEXITSTATUS (status
);
114 fprintf (stderr
, "Send fd fail\n");
118 /* try to stat new fd */
119 ret
= fstat (fd
, &st
);
126 /* Check behavior when sender no longer around */
128 fd
= recvfd (pair
[0], 0);
130 ASSERT (errno
== ENOTCONN
);
132 return test_exit_status
;
136 ASSERT(sendfd (0, 0) == -1);
137 ASSERT(errno
== ENOSYS
);
140 ASSERT(recvfd (0, 0) == -1);
141 ASSERT(errno
== ENOSYS
);
143 if (test_exit_status
!= EXIT_SUCCESS
)
144 return test_exit_status
;
145 fputs ("skipping test: socketpair not supported on this system\n",