xref: /illumos-gate/usr/src/contrib/ast/src/cmd/ksh93/features/poll (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1ref	-lsocket -lnsl
2hdr,sys	poll,socket,netinet/in
3lib	select,poll,socket
4lib	htons,htonl sys/types.h sys/socket.h netinet/in.h
5lib	getaddrinfo sys/types.h sys/socket.h netdb.h
6typ	fd_set sys/socket.h sys/select.h
7typ	socklen_t unistd.h sys/socket.h = unsigned int
8tst	pipe_socketpair note{ use socketpair() for peekable pipe() }end execute{
9	#include <ast.h>
10	#include <signal.h>
11	#include <sys/types.h>
12	#include <sys/socket.h>
13	#ifndef SHUT_RD
14	#define SHUT_RD		0
15	#endif
16	#ifndef SHUT_WR
17	#define SHUT_WR		1
18	#endif
19	static void handler(sig)
20	int	sig;
21	{
22		_exit(0);
23	}
24	int main()
25	{
26		int		n;
27		int		pfd[2];
28		int		sfd[2];
29		char		buf[256];
30		pid_t		pid;
31		static char	msg[] = "hello world\n";
32		close(0);
33		if (pipe(pfd) < 0 ||
34		    socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
35		    shutdown(sfd[1], SHUT_RD) < 0 ||
36		    shutdown(sfd[0], SHUT_WR) < 0)
37			return(1);
38		if ((pid = fork()) < 0)
39			return(1);
40		if (pid)
41		{
42			close(pfd[1]);
43			close(sfd[1]);
44			wait(&n);
45			if (sfpkrd(pfd[0], buf, sizeof(buf), '\n', -1, 1) >= 0 ||
46			    sfpkrd(sfd[0], buf, sizeof(buf), '\n', -1, 1) < 0)
47				return(1);
48		}
49		else
50		{
51			close(pfd[0]);
52			close(sfd[0]);
53			write(pfd[1], msg, sizeof(msg) - 1);
54			write(sfd[1], msg, sizeof(msg) - 1);
55			return(0);
56		}
57		close(pfd[0]);
58		close(sfd[0]);
59		signal(SIGPIPE, handler);
60		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
61		    shutdown(sfd[1], SHUT_RD) < 0 ||
62		    shutdown(sfd[0], SHUT_WR) < 0)
63			return(1);
64		close(sfd[0]);
65		write(sfd[1], msg, sizeof(msg) - 1);
66		return(1);
67	}
68}end
69tst	socketpair_devfd note{ /dev/fd/N handles socketpair() }end execute{
70	#include <ast.h>
71	#include <fs3d.h>
72	#include <sys/types.h>
73	#include <sys/socket.h>
74	int main()
75	{
76		int		devfd;
77		int		n;
78		int		sfd[2];
79		fs3d(FS3D_OFF);
80		close(0);
81		open("/dev/null", O_RDONLY);
82		if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
83			return(1);
84		close(n);
85		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
86		    shutdown(sfd[0], 1) < 0 ||
87		    shutdown(sfd[1], 0) < 0)
88			return(1);
89		close(0);
90		dup(sfd[0]);
91		close(sfd[0]);
92		if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
93			return(1);
94		return(0);
95	}
96}end
97tst	socketpair_shutdown_mode note{ fchmod() after socketpair() shutdown() }end execute{
98	#include <ast.h>
99	#include <sys/types.h>
100	#include <sys/stat.h>
101	#include <sys/socket.h>
102	int main()
103	{
104		int		sfd[2];
105		struct stat	st0;
106		struct stat	st1;
107		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
108		    shutdown(sfd[0], 1) < 0 ||
109		    shutdown(sfd[1], 0) < 0)
110			return(1);
111		if (fstat(sfd[0], &st0) < 0 || fstat(sfd[1], &st1) < 0)
112			return(1);
113		if ((st0.st_mode & (S_IRUSR|S_IWUSR)) == S_IRUSR &&
114		    (st1.st_mode & (S_IRUSR|S_IWUSR)) == S_IWUSR)
115			return(1);
116		if (fchmod(sfd[0], S_IRUSR) < 0 ||
117		    fstat(sfd[0], &st0) < 0 ||
118		    (st0.st_mode & (S_IRUSR|S_IWUSR)) != S_IRUSR)
119			return(1);
120		if (fchmod(sfd[1], S_IWUSR) < 0 ||
121		    fstat(sfd[1], &st1) < 0 ||
122		    (st1.st_mode & (S_IRUSR|S_IWUSR)) != S_IWUSR)
123			return(1);
124		return(0);
125	}
126}end
127cat{
128	#pragma prototyped
129	#ifdef _lib_poll
130	#   define poll _SYS_poll
131	#else
132	#   undef _hdr_poll
133	#   undef _sys_poll
134	#endif /* _lib_poll */
135	#ifdef _hdr_poll
136	#    include    <poll.h>
137	#else
138	#   ifdef _sys_poll
139	#	include    <sys/poll.h>
140	#   endif /* _sys_poll */
141	#endif /* _hdr_poll */
142	#ifdef _lib_poll
143	#   undef poll
144	    extern int poll(struct pollfd*,unsigned long,int);
145	#endif /* _lib_poll */
146	#ifdef _lib_select
147	#   ifndef FD_ZERO
148	#	define FD_ZERO(x)	(*(x)=0)
149	#   endif /* FD_ZERO */
150	#   ifndef FD_SET
151	#	define FD_SET(n,x)	(*(x)|=(1L<<(n)))
152	#   endif /* FD_SET */
153	#   ifndef _typ_fd_set
154		typedef long fd_set;
155	#   endif /*_typ_fd_set */
156	#endif /* _lib_select */
157}end
158