1N/Ahdr,sys poll,socket,netinet/in
1N/Alib select,poll,socket
1N/Alib htons,htonl sys/types.h sys/socket.h netinet/in.h
1N/Alib getaddrinfo sys/types.h sys/socket.h netdb.h
1N/Atyp fd_set sys/socket.h sys/select.h
1N/Atyp socklen_t unistd.h sys/socket.h = unsigned int
1N/Atst pipe_socketpair note{ use socketpair() for peekable pipe() }end execute{
1N/A #include <ast.h>
1N/A #include <signal.h>
1N/A #include <sys/types.h>
1N/A #include <sys/socket.h>
1N/A #ifndef SHUT_RD
1N/A #define SHUT_RD 0
1N/A #endif
1N/A #ifndef SHUT_WR
1N/A #define SHUT_WR 1
1N/A #endif
1N/A static void handler(sig)
1N/A int sig;
1N/A {
1N/A _exit(0);
1N/A }
1N/A int main()
1N/A {
1N/A int n;
1N/A int pfd[2];
1N/A int sfd[2];
1N/A char buf[256];
1N/A pid_t pid;
1N/A static char msg[] = "hello world\n";
1N/A close(0);
1N/A if (pipe(pfd) < 0 ||
1N/A socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
1N/A shutdown(sfd[1], SHUT_RD) < 0 ||
1N/A shutdown(sfd[0], SHUT_WR) < 0)
1N/A return(1);
1N/A if ((pid = fork()) < 0)
1N/A return(1);
1N/A if (pid)
1N/A {
1N/A close(pfd[1]);
1N/A close(sfd[1]);
1N/A wait(&n);
1N/A if (sfpkrd(pfd[0], buf, sizeof(buf), '\n', -1, 1) >= 0 ||
1N/A sfpkrd(sfd[0], buf, sizeof(buf), '\n', -1, 1) < 0)
1N/A return(1);
1N/A }
1N/A else
1N/A {
1N/A close(pfd[0]);
1N/A close(sfd[0]);
1N/A write(pfd[1], msg, sizeof(msg) - 1);
1N/A write(sfd[1], msg, sizeof(msg) - 1);
1N/A return(0);
1N/A }
1N/A close(pfd[0]);
1N/A close(sfd[0]);
1N/A signal(SIGPIPE, handler);
1N/A if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
1N/A shutdown(sfd[1], SHUT_RD) < 0 ||
1N/A shutdown(sfd[0], SHUT_WR) < 0)
1N/A return(1);
1N/A close(sfd[0]);
1N/A write(sfd[1], msg, sizeof(msg) - 1);
1N/A return(1);
1N/A }
1N/A}end
1N/Atst socketpair_devfd note{ /dev/fd/N handles socketpair() }end execute{
1N/A #include <ast.h>
1N/A #include <fs3d.h>
1N/A #include <sys/types.h>
1N/A #include <sys/socket.h>
1N/A int main()
1N/A {
1N/A int devfd;
1N/A int n;
1N/A int sfd[2];
1N/A fs3d(FS3D_OFF);
1N/A close(0);
1N/A open("/dev/null", O_RDONLY);
1N/A if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
1N/A return(1);
1N/A close(n);
1N/A if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
1N/A shutdown(sfd[0], 1) < 0 ||
1N/A shutdown(sfd[1], 0) < 0)
1N/A return(1);
1N/A close(0);
1N/A dup(sfd[0]);
1N/A close(sfd[0]);
1N/A if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
1N/A return(1);
1N/A return(0);
1N/A }
1N/A}end
1N/Atst socketpair_shutdown_mode note{ fchmod() after socketpair() shutdown() }end execute{
1N/A #include <ast.h>
1N/A #include <sys/types.h>
1N/A #include <sys/stat.h>
1N/A #include <sys/socket.h>
1N/A int main()
1N/A {
1N/A int sfd[2];
1N/A struct stat st0;
1N/A struct stat st1;
1N/A if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
1N/A shutdown(sfd[0], 1) < 0 ||
1N/A shutdown(sfd[1], 0) < 0)
1N/A return(1);
1N/A if (fstat(sfd[0], &st0) < 0 || fstat(sfd[1], &st1) < 0)
1N/A return(1);
1N/A if ((st0.st_mode & (S_IRUSR|S_IWUSR)) == S_IRUSR &&
1N/A (st1.st_mode & (S_IRUSR|S_IWUSR)) == S_IWUSR)
1N/A return(1);
1N/A if (fchmod(sfd[0], S_IRUSR) < 0 ||
1N/A fstat(sfd[0], &st0) < 0 ||
1N/A (st0.st_mode & (S_IRUSR|S_IWUSR)) != S_IRUSR)
1N/A return(1);
1N/A if (fchmod(sfd[1], S_IWUSR) < 0 ||
1N/A fstat(sfd[1], &st1) < 0 ||
1N/A (st1.st_mode & (S_IRUSR|S_IWUSR)) != S_IWUSR)
1N/A return(1);
1N/A return(0);
1N/A }
1N/A}end
1N/Acat{
1N/A #pragma prototyped
1N/A #ifdef _lib_poll
1N/A # define poll _SYS_poll
1N/A #else
1N/A # undef _hdr_poll
1N/A # undef _sys_poll
1N/A #endif /* _lib_poll */
1N/A #ifdef _hdr_poll
1N/A # include <poll.h>
1N/A #else
1N/A # ifdef _sys_poll
1N/A # include <sys/poll.h>
1N/A # endif /* _sys_poll */
1N/A #endif /* _hdr_poll */
1N/A #ifdef _lib_poll
1N/A # undef poll
1N/A extern int poll(struct pollfd*,unsigned long,int);
1N/A #endif /* _lib_poll */
1N/A #ifdef _lib_select
1N/A # ifndef FD_ZERO
1N/A # define FD_ZERO(x) (*(x)=0)
1N/A # endif /* FD_ZERO */
1N/A # ifndef FD_SET
1N/A # define FD_SET(n,x) (*(x)|=(1L<<(n)))
1N/A # endif /* FD_SET */
1N/A # ifndef _typ_fd_set
1N/A typedef long fd_set;
1N/A # endif /*_typ_fd_set */
1N/A #endif /* _lib_select */
1N/A}end