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