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