1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2009 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Roland Mainz <roland.mainz@nrubsig.org> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A
1N/A#include <shell.h>
1N/A#include <stdio.h>
1N/A#include <stdbool.h>
1N/A#include <option.h>
1N/A#include <stk.h>
1N/A#include <tm.h>
1N/A#include "name.h"
1N/A#undef nv_isnull
1N/A#ifndef SH_DICT
1N/A# define SH_DICT "libshell"
1N/A#endif
1N/A#include <poll.h>
1N/A#ifdef __GNUC__
1N/A#include <alloca.h>
1N/A#endif /* __GNUC__ */
1N/A
1N/A#define sh_contexttoshb(context) ((Shbltin_t*)(context))
1N/A#define sh_contexttoshell(context) ((context)?(sh_contexttoshb(context)->shp):(NULL))
1N/A
1N/Astatic const char sh_optpoll[] =
1N/A"[-?\n@(#)$Id: poll (AT&T Labs Research) 2009-05-14 $\n]"
1N/A"[-author?Roland Mainz <roland.mainz@nrubsig.org]"
1N/A"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
1N/A"[+NAME? poll - input/output multiplexing]"
1N/A"[+DESCRIPTION?The poll command provides applications with a mechanism "
1N/A "for multiplexing input/output over a set of file descriptors. "
1N/A "For each member of the array variable \bvar\b, "
1N/A "poll examines the given file descriptor in the subscript \b.fd\b "
1N/A "for the event(s) specified in the subscript \b.events\b."
1N/A "The poll command identifies those file descriptors on which an "
1N/A "application can read or write data, or on which certain events have "
1N/A "occurred.]"
1N/A"[+?The \bvar\b argument specifies the file descriptors to be examined "
1N/A "and the events of interest for each file descriptor. "
1N/A "It is a array of structured variables with one member for each open "
1N/A "file descriptor of interest. The array's members contain the following "
1N/A "subscripts:]{"
1N/A "[+?\b.fd\b # file descriptor]"
1N/A "[+?\b.events\b # requested events]"
1N/A "[+?\b.revents\b # returned event]"
1N/A "}"
1N/A"[+?The \bfd\b variable specifies an open file descriptor and the "
1N/A "\bevents\b and \brevents\b members are strings constructed from "
1N/A "a concatenation of the following event flags, separated by '|':]"
1N/A "{ "
1N/A "[+POLLIN?Data other than high priority data may be "
1N/A "read without blocking. For STREAMS, this "
1N/A "flag is set in revents even if the message "
1N/A "is of zero length.]"
1N/A "[+POLLRDNORM?Normal data (priority band equals 0) may be "
1N/A "read without blocking. For STREAMS, this "
1N/A "flag is set in revents even if the message "
1N/A "is of zero length.]"
1N/A "[+POLLRDBAND?Data from a non-zero priority band may be "
1N/A "read without blocking. For STREAMS, this "
1N/A "flag is set in revents even if the message "
1N/A "is of zero length.]"
1N/A "[+POLLPRI?High priority data may be received without "
1N/A "blocking. For STREAMS, this flag is set in "
1N/A "revents even if the message is of zero "
1N/A "length.]"
1N/A "[+POLLOUT?Normal data (priority band equals 0) may be "
1N/A "written without blocking.]"
1N/A "[+POLLWRNORM?The same as POLLOUT.]"
1N/A "[+POLLWRBAND?Priority data (priority band > 0) may be "
1N/A "written. This event only examines bands "
1N/A "that have been written to at least once.]"
1N/A "[+POLLERR?An error has occurred on the device or "
1N/A "stream. This flag is only valid in the "
1N/A "revents bitmask; it is not used in the "
1N/A "events member.]"
1N/A "[+POLLHUP?A hangup has occurred on the stream. This "
1N/A "event and POLLOUT are mutually exclusive; a "
1N/A "stream can never be writable if a hangup has "
1N/A "occurred. However, this event and POLLIN, "
1N/A ", POLLRDBAND, or POLLPRI are not "
1N/A "mutually exclusive. This flag is only valid "
1N/A "in the revents bitmask; it is not used in "
1N/A "the events member.]"
1N/A "[+POLLNVAL?The specified fd value does not belong to an "
1N/A "open file. This flag is only valid in the "
1N/A "revents member; it is not used in the events "
1N/A "member.]"
1N/A "}"
1N/A"]"
1N/A
1N/A"[+?If the value fd is less than 0, events is ignored and "
1N/A "revents is set to 0 in that entry on return from poll.]"
1N/A
1N/A"[+?The results of the poll query are stored in the revents "
1N/A "member in the \bvar\b structure. POLL*-strings are set in the \brevents\b "
1N/A "variable to indicate which of the requested events are true. "
1N/A "If none are true, the \brevents\b will be an empty string when "
1N/A "the poll command returns. The event flags "
1N/A "POLLHUP, POLLERR, and POLLNVAL are always set in \brevents\b "
1N/A "if the conditions they indicate are true; this occurs even "
1N/A "though these flags were not present in events.]"
1N/A
1N/A"[+?If none of the defined events have occurred on any selected "
1N/A "file descriptor, poll waits at least timeout milliseconds "
1N/A "for an event to occur on any of the selected file descriptors. "
1N/A "On a computer where millisecond timing accuracy is not "
1N/A "available, timeout is rounded up to the nearest legal value "
1N/A "available on that system. If the value timeout is 0, poll "
1N/A "returns immediately. If the value of timeout is -1, poll "
1N/A "blocks until a requested event occurs or until the call is "
1N/A "interrupted.]"
1N/A
1N/A"[+?The poll function supports regular files, terminal and "
1N/A "pseudo-terminal devices, STREAMS-based files, FIFOs and "
1N/A "pipes. The behavior of poll on elements of fds that refer "
1N/A "to other types of file is unspecified.]"
1N/A
1N/A"[+?The poll function supports sockets.]"
1N/A
1N/A"[+?A file descriptor for a socket that is listening for connections "
1N/A "will indicate that it is ready for reading, once connections "
1N/A "are available. A file descriptor for a socket that "
1N/A "is connecting asynchronously will indicate that it is ready "
1N/A "for writing, once a connection has been established.]"
1N/A
1N/A"[+?Regular files always poll TRUE for reading and writing.]"
1N/A
1N/A"[e:eventarray]:[fdcount?Upon successful completion, an indexed array "
1N/A "of strings is returned which contains a list of array subscripts "
1N/A "in the poll array which received events.]"
1N/A"[t:timeout]:[seconds?Timeout in seconds. If the value timeout is 0, "
1N/A "poll returns immediately. If the value of timeout is -1, poll "
1N/A "blocks until a requested event occurs or until the call is "
1N/A "interrupted.]"
1N/A"[T:mtimeout]:[milliseconds?Timeout in milliseconds. If the value timeout is 0, "
1N/A "poll returns immediately. If the value of timeout is -1, poll "
1N/A "blocks until a requested event occurs or until the call is "
1N/A "interrupted.]"
1N/A"\n"
1N/A"\nvar\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+0?Success.]"
1N/A "[+>0?An error occurred.]"
1N/A"}"
1N/A"[+SEE ALSO?\bopen\b(1),\btmpfile\b(1),\bdup\b(1),\bclose\b(1),\bpoll\b(2)]"
1N/A;
1N/A
1N/A/*
1N/A * |mystpcpy| - like |strcpy()| but returns the end of the buffer
1N/A *
1N/A * Copy string s2 to s1. s1 must be large enough.
1N/A * return s1-1 (position of string terminator ('\0') in destnation buffer).
1N/A */
1N/Astatic
1N/Achar *mystpcpy(char *s1, const char *s2)
1N/A{
1N/A while (*s1++ = *s2++)
1N/A ;
1N/A return (s1-1);
1N/A}
1N/A
1N/Astatic
1N/ANamval_t *nv_open_fmt(Dt_t *dict, int flags, const char *namefmt, ...)
1N/A{
1N/A char varnamebuff[PATH_MAX];
1N/A va_list ap;
1N/A
1N/A va_start(ap, namefmt);
1N/A vsnprintf(varnamebuff, sizeof(varnamebuff), namefmt, ap);
1N/A va_end(ap);
1N/A
1N/A return nv_open(varnamebuff, dict, flags);
1N/A}
1N/A
1N/Astatic
1N/Aint poll_strtoevents(const char *str)
1N/A{
1N/A int events = 0;
1N/A
1N/A if (strstr(str, "POLLIN")) events |= POLLIN;
1N/A if (strstr(str, "POLLRDNORM")) events |= POLLRDNORM;
1N/A if (strstr(str, "POLLRDBAND")) events |= POLLRDBAND;
1N/A if (strstr(str, "POLLPRI")) events |= POLLPRI;
1N/A if (strstr(str, "POLLOUT")) events |= POLLOUT;
1N/A if (strstr(str, "POLLWRNORM")) events |= POLLWRNORM;
1N/A if (strstr(str, "POLLWRBAND")) events |= POLLWRBAND;
1N/A if (strstr(str, "POLLERR")) events |= POLLERR;
1N/A if (strstr(str, "POLLHUP")) events |= POLLHUP;
1N/A if (strstr(str, "POLLNVAL")) events |= POLLNVAL;
1N/A
1N/A return events;
1N/A}
1N/A
1N/A
1N/Astatic
1N/Avoid poll_eventstostr(char *s, int events)
1N/A{
1N/A *s='\0';
1N/A if (!events)
1N/A return;
1N/A
1N/A if (events & POLLIN) s=mystpcpy(s, "POLLIN|");
1N/A if (events & POLLRDNORM) s=mystpcpy(s, "POLLRDNORM|");
1N/A if (events & POLLRDBAND) s=mystpcpy(s, "POLLRDBAND|");
1N/A if (events & POLLPRI) s=mystpcpy(s, "POLLPRI|");
1N/A if (events & POLLOUT) s=mystpcpy(s, "POLLOUT|");
1N/A if (events & POLLWRNORM) s=mystpcpy(s, "POLLWRNORM|");
1N/A if (events & POLLWRBAND) s=mystpcpy(s, "POLLWRBAND|");
1N/A if (events & POLLERR) s=mystpcpy(s, "POLLERR|");
1N/A if (events & POLLHUP) s=mystpcpy(s, "POLLHUP|");
1N/A if (events & POLLNVAL) s=mystpcpy(s, "POLLNVAL|");
1N/A
1N/A /* Remove trailling '|' */
1N/A s--;
1N/A if(*s=='|')
1N/A *s='\0';
1N/A}
1N/A
1N/A#undef getconf
1N/A#define getconf(x) strtol(astconf(x,NiL,NiL),NiL,0)
1N/A
1N/Aextern int b_poll(int argc, char *argv[], void *extra)
1N/A{
1N/A Namval_t *np;
1N/A Shell_t *shp = sh_contexttoshell(extra);
1N/A char *varname;
1N/A int n;
1N/A int fd;
1N/A nfds_t numpollfd = 0;
1N/A int i;
1N/A char *s;
1N/A double timeout = -1.;
1N/A char buff[PATH_MAX*2+1]; /* enough to hold two variable names */
1N/A char *eventarrayname = NULL;
1N/A
1N/A while (n = optget(argv, sh_optpoll)) switch (n)
1N/A {
1N/A case 't':
1N/A case 'T':
1N/A errno = 0;
1N/A timeout = strtod(opt_info.arg, (char **)NULL);
1N/A if (errno != 0)
1N/A errormsg(SH_DICT, ERROR_system(1), "%s: invalid timeout", opt_info.arg);
1N/A
1N/A /* -t uses seconds, -T milliseconds */
1N/A if (n == 't')
1N/A timeout *= 1000.;
1N/A break;
1N/A case 'e':
1N/A eventarrayname = opt_info.arg;
1N/A break;
1N/A case ':':
1N/A errormsg(SH_DICT, 2, "%s", opt_info.arg);
1N/A break;
1N/A case '?':
1N/A errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A argc -= opt_info.index;
1N/A argv += opt_info.index;
1N/A if(argc!=1)
1N/A errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
1N/A
1N/A varname = argv[0];
1N/A
1N/A Namval_t *array_np, *array_np_sub;
1N/A const char *subname;
1N/A
1N/A array_np = nv_open(varname, shp->var_tree, NV_NOFAIL|NV_NOADD);
1N/A if (!array_np)
1N/A errormsg(SH_DICT, ERROR_system(1), "cannot find array variable %s", varname);
1N/A if(!nv_isattr(array_np, NV_ARRAY))
1N/A errormsg(SH_DICT, ERROR_system(1), "variable %s is not an array", varname);
1N/A
1N/A /* Count number of array elememts. We need to do it "manually" to
1N/A * handle sparse indexed and associative arrays */
1N/A nv_putsub(array_np, NULL, ARRAY_SCAN);
1N/A array_np_sub = array_np;
1N/A do
1N/A {
1N/A if (!(subname=nv_getsub(array_np_sub)))
1N/A break;
1N/A numpollfd++;
1N/A } while( array_np_sub && nv_nextsub(array_np_sub) );
1N/A
1N/A#ifdef __GNUC__
1N/A /*
1N/A * Allocate stack space via |alloca()| for gcc builds since ctfconvert
1N/A * is unable to handle VLAs from gcc. We need this until CR #6379193
1N/A * is fixed.
1N/A */
1N/A struct pollfd *pollfd = alloca(sizeof(struct pollfd)*(numpollfd+1));
1N/A#else
1N/A /* We must allocate one more entry with VLA with zero elements do not work with all compilers */
1N/A struct pollfd pollfd[numpollfd+1];
1N/A#endif /* __GNUC__ */
1N/A
1N/A nv_putsub(array_np, NULL, ARRAY_SCAN);
1N/A array_np_sub = array_np;
1N/A i = 0;
1N/A do
1N/A {
1N/A if (!(subname=nv_getsub(array_np_sub)))
1N/A break;
1N/A
1N/A np = nv_open_fmt(shp->var_tree, NV_NOFAIL|NV_NOADD, "%s[%s].fd", varname, subname);
1N/A if (!np)
1N/A errormsg(SH_DICT, ERROR_system(1), "missing pollfd %s[%s].fd", varname, subname);
1N/A fd = (int)nv_getnum(np);
1N/A if (fd < 0 || fd > OPEN_MAX)
1N/A errormsg(SH_DICT, ERROR_system(1), "invalid pollfd fd %d", fd);
1N/A nv_close(np);
1N/A pollfd[i].fd = fd;
1N/A
1N/A np = nv_open_fmt(shp->var_tree, NV_NOFAIL|NV_NOADD, "%s[%s].events", varname, subname);
1N/A if (!np)
1N/A errormsg(SH_DICT, ERROR_system(1), "missing pollfd %s[%s].events", varname, subname);
1N/A
1N/A s = nv_getval(np);
1N/A if (!s)
1N/A errormsg(SH_DICT, ERROR_system(1), "missing pollfd events value");
1N/A pollfd[i].events = poll_strtoevents(s);
1N/A nv_close(np);
1N/A
1N/A pollfd[i].revents = 0;
1N/A
1N/A i++;
1N/A } while( array_np_sub && nv_nextsub(array_np_sub) );
1N/A
1N/A n = poll(pollfd, numpollfd, timeout);
1N/A /* FixMe: EGAIN and EINTR may require extra handling */
1N/A if (n < 0)
1N/A errormsg(SH_DICT, ERROR_system(1), "failure");
1N/A
1N/A if (eventarrayname)
1N/A {
1N/A np = nv_open_fmt(shp->var_tree, NV_VARNAME|NV_ARRAY|NV_NOFAIL, "%s", eventarrayname);
1N/A if (!np)
1N/A errormsg(SH_DICT, ERROR_system(1), "couldn't create poll count variable %s", eventarrayname);
1N/A nv_close(np);
1N/A }
1N/A
1N/A nv_putsub(array_np, NULL, ARRAY_SCAN);
1N/A array_np_sub = array_np;
1N/A i = 0;
1N/A do
1N/A {
1N/A if (!(subname=nv_getsub(array_np_sub)))
1N/A break;
1N/A
1N/A np = nv_open_fmt(shp->var_tree, NV_NOFAIL, "%s[%s].revents", varname, subname);
1N/A if (!np)
1N/A errormsg(SH_DICT, ERROR_system(1), "couldn't create pollfd %s[%s].revents", varname, subname);
1N/A
1N/A poll_eventstostr(buff, pollfd[i].revents);
1N/A
1N/A nv_putval(np, buff, 0);
1N/A nv_close(np);
1N/A
1N/A if (eventarrayname && pollfd[i].revents)
1N/A {
1N/A sprintf(buff, "%s+=( '%s' )", eventarrayname, subname);
1N/A sh_trap(buff, 0);
1N/A }
1N/A
1N/A i++;
1N/A } while( array_np_sub && nv_nextsub(array_np_sub) );
1N/A
1N/A nv_close(array_np);
1N/A
1N/A return(0);
1N/A}