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