NIO CommKit Host Interface Installation and System Administration Manual

6-30
Porting CommKit Applications To Release 4.x
Examples
The code in Figure 6-9 replaces the old DIOCSIG ioctl directive for data
switch circuits. Note that the STREAMS SIGPOLL signal is generated
when the requested event happens, if it is the event at the head of the queue.
If there are events already on the queue, you can use the poll system call
with a zero timeout to look at the queue, as in the example.
Figure 6-9 assumes an open file descriptor fd is assigned to a data switch
circuit and shows only the framework that would be placed around some
local processing. Line 5 declares the signal catching function, and lines 14
and 15 define a structure and a pointer used by the poll system call. Line 18
requests that the SIGPOLL signal be caught, and line 27 is the ioctl call that
requests that the stream head send a SIGPOLL when ordinary data arrives.
Once the request for service is made, it is in effect until it is explicitly turned
off.
Line 28 uses the poll system call with three arguments to look at one file
descriptor. The first argument is the address of the pollfd structure initialized
on lines 20 and 21. The second is the number of pollfd structures found at
this address; in this example, it is one. The last argument is the timeout
value, which is set to zero so that the system call is not be blocked.
Since we have only requested that poll look at the events on one circuit, poll
returns a 1 if data has arrived on that circuit and a 0 if not. If poll returns 1
(true), lines 34 to 41 perform local processing. Remember that if the poll is
true, there will not be any SIGPOLL signals unless the queue is emptied. If
the poll test on line 28 returns 0 (false), processing moves to line 44.
At the end of this local processing, the example notifies the stream head to
turn off the SIGPOLL notification on line 92. The example concludes with a
simple signal catching function, starting at line 95. Remember to reset the
SIGPOLL catching before reading all of the data from the queue, since the
default action of SIGPOLL is to kill the process. Since this fragment uses a
very simple signal example and UNIX System V Release 4 offers many new
signal options, review all of the new options before you port your
application.