xref: /onnv-gate/usr/src/lib/libshell/common/features/poll (revision 8462:6e341f5569ba)
14887Schinhdr,sys	poll,socket,netinet/in
24887Schinlib	select,poll,socket
34887Schinlib	htons,htonl sys/types.h sys/socket.h netinet/in.h
44887Schinlib	getaddrinfo sys/types.h sys/socket.h netdb.h
54887Schintyp	fd_set sys/socket.h sys/select.h
64887Schintst	pipe_socketpair note{ use socketpair() for peekable pipe() }end execute{
74887Schin	#include <ast.h>
84887Schin	#include <signal.h>
94887Schin	#include <sys/types.h>
104887Schin	#include <sys/socket.h>
11*8462SApril.Chin@Sun.COM	#ifndef SHUT_RD
12*8462SApril.Chin@Sun.COM	#define SHUT_RD		0
13*8462SApril.Chin@Sun.COM	#endif
14*8462SApril.Chin@Sun.COM	#ifndef SHUT_WR
15*8462SApril.Chin@Sun.COM	#define SHUT_WR		1
16*8462SApril.Chin@Sun.COM	#endif
174887Schin	static void handler(sig)
184887Schin	int	sig;
194887Schin	{
204887Schin		_exit(0);
214887Schin	}
224887Schin	int main()
234887Schin	{
244887Schin		int		n;
254887Schin		int		pfd[2];
264887Schin		int		sfd[2];
274887Schin		char		buf[256];
284887Schin		pid_t		pid;
294887Schin		static char	msg[] = "hello world\n";
304887Schin		close(0);
314887Schin		if (pipe(pfd) < 0 ||
324887Schin		    socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
33*8462SApril.Chin@Sun.COM		    shutdown(sfd[1], SHUT_RD) < 0 ||
34*8462SApril.Chin@Sun.COM		    shutdown(sfd[0], SHUT_WR) < 0)
354887Schin			return(1);
364887Schin		if ((pid = fork()) < 0)
374887Schin			return(1);
384887Schin		if (pid)
394887Schin		{
404887Schin			close(pfd[1]);
414887Schin			close(sfd[1]);
424887Schin			wait(&n);
434887Schin			if (sfpkrd(pfd[0], buf, sizeof(buf), '\n', -1, 1) >= 0 ||
444887Schin			    sfpkrd(sfd[0], buf, sizeof(buf), '\n', -1, 1) < 0)
454887Schin				return(1);
464887Schin		}
474887Schin		else
484887Schin		{
494887Schin			close(pfd[0]);
504887Schin			close(sfd[0]);
514887Schin			write(pfd[1], msg, sizeof(msg) - 1);
524887Schin			write(sfd[1], msg, sizeof(msg) - 1);
534887Schin			return(0);
544887Schin		}
554887Schin		close(pfd[0]);
564887Schin		close(sfd[0]);
574887Schin		signal(SIGPIPE, handler);
584887Schin		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
59*8462SApril.Chin@Sun.COM		    shutdown(sfd[1], SHUT_RD) < 0 ||
60*8462SApril.Chin@Sun.COM		    shutdown(sfd[0], SHUT_WR) < 0)
614887Schin			return(1);
624887Schin		close(sfd[0]);
634887Schin		write(sfd[1], msg, sizeof(msg) - 1);
644887Schin		return(1);
654887Schin	}
664887Schin}end
674887Schintst	socketpair_devfd note{ /dev/fd/N handles socketpair() }end execute{
684887Schin	#include <ast.h>
694887Schin	#include <fs3d.h>
704887Schin	#include <sys/types.h>
714887Schin	#include <sys/socket.h>
724887Schin	int main()
734887Schin	{
744887Schin		int		devfd;
754887Schin		int		n;
764887Schin		int		sfd[2];
774887Schin		fs3d(FS3D_OFF);
784887Schin		close(0);
794887Schin		open("/dev/null", O_RDONLY);
804887Schin		if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
814887Schin			return(1);
824887Schin		close(n);
834887Schin		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
844887Schin		    shutdown(sfd[0], 1) < 0 ||
854887Schin		    shutdown(sfd[1], 0) < 0)
864887Schin			return(1);
874887Schin		close(0);
884887Schin		dup(sfd[0]);
894887Schin		close(sfd[0]);
904887Schin		if ((n = open("/dev/fd/0", O_RDONLY)) < 0)
914887Schin			return(1);
924887Schin		return(0);
934887Schin	}
944887Schin}end
954887Schintst	socketpair_shutdown_mode note{ fchmod() after socketpair() shutdown() }end execute{
964887Schin	#include <ast.h>
974887Schin	#include <sys/types.h>
984887Schin	#include <sys/stat.h>
994887Schin	#include <sys/socket.h>
1004887Schin	int main()
1014887Schin	{
1024887Schin		int		sfd[2];
1034887Schin		struct stat	st0;
1044887Schin		struct stat	st1;
1054887Schin		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0 ||
1064887Schin		    shutdown(sfd[0], 1) < 0 ||
1074887Schin		    shutdown(sfd[1], 0) < 0)
1084887Schin			return(1);
1094887Schin		if (fstat(sfd[0], &st0) < 0 || fstat(sfd[1], &st1) < 0)
1104887Schin			return(1);
1114887Schin		if ((st0.st_mode & (S_IRUSR|S_IWUSR)) == S_IRUSR &&
1124887Schin		    (st1.st_mode & (S_IRUSR|S_IWUSR)) == S_IWUSR)
1134887Schin			return(1);
1144887Schin		if (fchmod(sfd[0], S_IRUSR) < 0 ||
1154887Schin		    fstat(sfd[0], &st0) < 0 ||
1164887Schin		    (st0.st_mode & (S_IRUSR|S_IWUSR)) != S_IRUSR)
1174887Schin			return(1);
1184887Schin		if (fchmod(sfd[1], S_IWUSR) < 0 ||
1194887Schin		    fstat(sfd[1], &st1) < 0 ||
1204887Schin		    (st1.st_mode & (S_IRUSR|S_IWUSR)) != S_IWUSR)
1214887Schin			return(1);
1224887Schin		return(0);
1234887Schin	}
1244887Schin}end
1254887Schincat{
1264887Schin	#pragma prototyped
1274887Schin	#ifdef _lib_poll
1284887Schin	#   define poll _SYS_poll
1294887Schin	#else
1304887Schin	#   undef _hdr_poll
1314887Schin	#   undef _sys_poll
1324887Schin	#endif /* _lib_poll */
1334887Schin	#ifdef _hdr_poll
1344887Schin	#    include    <poll.h>
1354887Schin	#else
1364887Schin	#   ifdef _sys_poll
1374887Schin	#	include    <sys/poll.h>
1384887Schin	#   endif /* _sys_poll */
1394887Schin	#endif /* _hdr_poll */
1404887Schin	#ifdef _lib_poll
1414887Schin	#   undef poll
1424887Schin	    extern int poll(struct pollfd*,unsigned long,int);
1434887Schin	#endif /* _lib_poll */
1444887Schin	#ifdef _lib_select
1454887Schin	#   ifndef FD_ZERO
1464887Schin	#	define FD_ZERO(x)	(*(x)=0)
1474887Schin	#   endif /* FD_ZERO */
1484887Schin	#   ifndef FD_SET
1494887Schin	#	define FD_SET(n,x)	(*(x)|=(1L<<(n)))
1504887Schin	#   endif /* FD_SET */
1514887Schin	#   ifndef _typ_fd_set
1524887Schin		typedef long fd_set;
1534887Schin	#   endif /*_typ_fd_set */
1544887Schin	#endif /* _lib_select */
1554887Schin}end
156