STREAMS Programming Guide

Part I Application Programming Interface

Chapter 1 Overview of STREAMS







Figure 1-1 Simple Stream


STREAMS Definitions




Stream Head










Figure 1-2 Messages Passing Using Queues










Flow Control


When to Use STREAMS



How STREAMS Works--Application Interface


Opening a Stream






Closing a Stream


Controlling Data Flow


Simple Stream Example


Example 1-1 Simple Stream

#include <sys/fcntl.h>
#include <stdio.h>

		char buf[1024];
		int fd, count;
			perror("open failed");
				perror("write failed");


Figure 1-3 Stream to Communications Driver




Note -



How STREAMS Works--Kernel-level


Stream Head








Figure 1-4 Stream in More Detail





Processing procedures


Private Data


Open and Close






There are some significant differences between modules and drivers.

A driver:





Message Types


Message Queueing Priority



Figure 1-5 Message Priorities

















STREAMS in Operation

Service Interfaces




Manipulating Modules



Protocol Portability


Figure 1-6 Protocol Module Portability


Protocol Substitution


Protocol Migration



Figure 1-7 Protocol Migration


Module Reusability


Figure 1-8 Module Reusability


Chapter 2 STREAMS Application-Level Components

This chapter shows how to construct, use, and dismantle a Stream using STREAMS-related system calls. It provides a general discussion of the relationship between STREAMS components in a simple Streams example.

STREAMS Interfaces

The Stream head provides the interface between the Stream and an application program. After a Stream has been opened, STREAMS-related system calls let a user process insert and delete (push and pop) modules. That process can then communicate with and control the operation of the Stream head, modules, and drivers. The Stream head handles most system calls so that the related processing does not have to be incorporated in a module or driver.

STREAMS System Calls

Table 2-1 offers an overview of some basic STREAMS-related system calls.

Table 2-1 Summary of Basic STREAMS-related System Calls




Opens a Stream 


Closes a Stream 


Reads data from a Stream 


Writes data to a Stream 


Controls a Stream  


Receives a message at the Stream head 


Receives a priority message at the Stream head 


Sends a message downstream 


Sends a priority message downstream 


Identifies files on which a user can send or receive messages,  

or on which certain events have occurred (not restricted to 

STREAMS, although historically it was) 


Creates a bidirectional channel that provides a communication 

path between multiple processes  

Note -

Sections 1, 2, 3, 7, and 9 of the online manual pages (man pages) contain all the STREAMS information.

Action Summary

The open(2) system call recognizes a STREAMS special file and creates a Stream to the specified driver. A user process can receive and send data on STREAMS files using read(2) and write(2) in the same way as with traditional character files. ioctl(2) lets users perform functions specific to a particular device. STREAMS ioctl(2) commands (see the streamio(7I) man page) support a variety of functions to access and control Streams. The final close(2) on a Stream dismantles it.

The poll(2) system call provides a mechanism for multiplexing input/output over a set of file descriptors that reference open files. putmsg(2) and getmsg(2) and the putpmsg(2) and getpmsg(2) send and receive STREAMS messages, and can act on STREAMS modules and drivers through a service interface.

Opening a STREAMS Device File

One way to construct a Stream is to open(2) a STREAMS special file. If the open call is the initial file open, a Stream is created. (There is one Stream for each major or minor device pair.) If this open is not the initial open of this Stream, the open procedures of the driver and all pushable modules on the Stream are called.

Sometimes a user process needs to connect a new Stream to a driver regardless of which minor device is used to access the driver. Instead of the user process polling for an available minor device node, STREAMS provides a facility called clone open. If a STREAMS driver is implemented as a clone device, a single node in the file system may be opened to access any unused device that the driver controls. This special node guarantees that the user is allocated a separate Stream to the driver for every open call. Each Stream is associated with an unused major or minor device, so the total number of Streams that can connect to a particular clone driver is limited to the number of minor devices configured for the driver.

Clone devices are used, for example, in a networking environment where a protocol pseudo-device driver requires each user to open a separate Stream to establish communication.



When a Stream is already open, further opens of the same device result in the open routines of all modules and the driver on the Stream being called. In this case, a driver is opened and a module is pushed on a Stream. When a push occurs the module open routine is called. If another open of the same device is made, the open routine of the module is called, followed by the open routine of the driver. This is opposite to the initial order of opens when the Stream is created.


Initializing Details

There is one Stream head per Stream. The Stream head, which is initiated by the first open call, is created from a data structure and a pair of queue structures. The content of the Stream head and queues is initialized with predetermined values, including the Stream head processing procedures.



Adding and Removing Modules

As part of constructing a Stream, a module can be added (pushed) with an I_PUSH ioctl(2) (see streamio(7I)) call. The push inserts a module beneath the Stream head. Because of the similarity of STREAMS components, the push operation is similar to the driver open.

Each push of a module is independent, even in the same Stream. If the same module is pushed more than once on a Stream, there are multiple occurrences of the module in the Stream. The total number of pushable modules that may be contained on any one Stream is limited by the kernel parameter nstrpush.

An I_POP ioctl(2) (see streamio(7I)) system call removes (pops) the module immediately below the Stream head. The pop calls the module close procedure. On return from the module close, any messages left on the module's message queues are freed (deallocated). Then, the Stream head connects to the component previously below the popped module and releases the module's queue pair. I_PUSH and I_POP enable a user process to dynamically alter the configuration of a Stream by pushing and popping modules as required. For example, a module may be removed and a new one inserted below the Stream head. Then the original module can be pushed back after the new module has been pushed.

Closing the Stream

The last close to a STREAMS device dismantles the Stream. Dismantling consists of popping any modules on the Stream and closing the driver. Before a module is popped, the close(2) may delay to allow any messages on the write message queue of the module to be drained by module processing. Similarly, before the driver is closed, the close(2) may delay to allow any messages on the write message queue of the driver to be drained by driver processing. If O_NDELAY (or O_NONBLOCK, see open(2) and fcntl(2)) is clear, close(2) waits up to 15 seconds for each module to drain and up to 15 seconds for the driver to drain. The default close delay is 15 seconds, but this can be changed on a per-Stream basis with the I_SETCLTIME ioctl(2).

Closing Delay Details

The delay is independent of any delay that the module or driver's close routine itself chooses to impose. If O_NDELAY (or O_NONBLOCK) is set, the pop is performed immediately and the driver is closed without delay.

Messages can remain queued, for example, if flow control is inhibiting execution of the write queue service procedure. When all modules are popped and any wait for the driver to drain is completed, the driver close routine is called. On return from the driver close, any messages left on the driver's queues are freed, and the queue and Stream head structures are released.

Stream Construction Example

This example extends the communications device-echoing example shown in " Simple Stream Example". The module in this example converts (change case, delete, duplicate) selected alphabetic characters.

Note -

The complete listing of the module is on the CD.

Inserting Modules


Example 2-1 Header Definition

#include <string.h>
#include <fcntl.h>
#include <stropts.h>
#define				BUFLEN				1024
 * These definitions would typically be
 * found in a header file for the module
#define			DELETE				 2 /* delete char */
#define			DUPLICATE				 3 /* duplicate char */
 	char buf[BUFLEN];
 	int fd, count;
 	struct strioctl strioctl;


Example 2-2 Pushing a Module

 	perror("open failed");
 	perror("ioctl I_PUSH failed");


Figure 2-1 Pushing the Character Conversion Module



Module and Driver Control

The next step in this example is to pass the commands and corresponding strings to the character conversion module. This can be accomplished by calling ioctl(2) to invoke the character conversion module as shown in the next example.

Example 2-3 uses the conventional I_STR ioctl(2), an indirect way of passing commands and data pointers. Example 2-4 shows the data structure for I_STR.

Instead of I_STR, some systems support transparent ioctls in which calls can be made directly. For example, a module calls I_PUSH. Both modules and drivers can process ioctls without requiring user programs to first encapsulate them with I_STR (that is, the ioctls in the examples would look like ioctl(fd,DELETE,"AEIOU");). This style of call works only for modules and drivers that have been converted to use the new facilities that also accept the I_STR form.

Example 2-3 Processing ioctl(2)

/* change all uppercase vowels to lowercase */
strioctl.ic_cmd = XCASE;
strioctl.ic_timout = 0; /* default timeout (15 sec) */
strioctl.ic_dp = "AEIOU";
strioctl.ic_len = strlen(strioctl.ic_dp);
if (ioctl(fd, I_STR, &strioctl) < 0) {
   perror("ioctl I_STR failed");
/* delete all instances of the chars 'x' and 'X' */
strioctl.ic_cmd = DELETE;
strioctl.ic_dp = "xX";
strioctl.ic_len = strlen(strioctl.ic_dp);
   perror("ioctl I_STR failed");

In Example 2-3 the module changes all uppercase vowels to lowercase, and deletes all instances of either uppercase or lowercase "x". ioctl(2) requests are issued indirectly, using I_STR ioctl(2) (see streamio(7I)). The argument to I_STR must be a pointer to a strioctl structure, which specifies the request to be made to a module or driver. This structure is described in streamio(7I) and has the format shown in Example 2-4.

Example 2-4 Structure of strioctl

struct strioctl {
	int         ic_cmd;             /* ioctl request */
	int         ic_timout;          /* ACK/NAK timeout */
	int         ic_len;             /* length of data argument */
	char        *ic_dp;             /* ptr to data argument */



Identifies the command intended for a module or driver.


Specifies the number of seconds an I_STR request should wait for an acknowledgment before timing out.


The number of bytes of data to accompany the request.


Points to the data. In the example, two separate commands are sent to the character-conversion module:

  • The first command sets ic_cmd to the command XCASE and sends as data the string "AEIOU"; it converts all uppercase vowels in data passing through the module to lowercase.

  • The second command sets ic_cmd to the command DELETE and sends as data the string "xX"; it deletes all occurrences of the characters "x" and "X" from data passing through the module.

For each command, the value of ic_timout is set to zero, which specifies the system default timeout value of 15 seconds. ic_dp points to the beginning of the data for each command; ic_len is set to the length of the data.

I_STR is intercepted by the Stream head, which packages it into a message, using information contained in the strioctl structure, then sends the message downstream. Any module that cannot process the command in ic_cmd passes the message further downstream. The request is processed by the module or driver closest to the Stream head that understands the command specified by ic_cmd. ioctl(2) blocks up to ic_timout seconds, waiting for the target module or driver to respond with either a positive or negative acknowledgment message. If an acknowledgment is not received in ic_timout seconds, ioctl(2) fails.

Note -

Only one ioctl(2) can be active on a Stream at one time, whether or not it is issued with I_STR. Further requests will block until the active ioctl(2) is acknowledged and the system call concludes.

The strioctl structure is also used to retrieve the results, if any, of an I_STR request. If data is returned by the target module or driver, ic_dp must point to a buffer large enough to hold that data, and ic_len is set on return to indicate the amount of data returned.

The remainder of this example is identical to Example 1-1 in Chapter 1, Overview of STREAMS.

Example 2-5 Process Input

while ((count = read(fd, buf, BUFLEN)) > 0) {
				perror("write failed");

Notice that the character-conversion processing was realized with no change to the communications driver.

exit(2) dismantles the Stream before terminating the process. The character conversion module is removed from the Stream automatically when it is closed. Alternatively, remove modules from a Stream using I_POP ioctl(2) described in streamio(7I). This call removes the topmost module on the Stream, and enables a user process to alter the configuration of a Stream dynamically by popping modules as needed.

Several other ioctl(2) requests support STREAMS operations, such as determining if a given module is on a Stream, or flushing the data on a Stream. streamio(7I) describes these requests.

Chapter 3 STREAMS Application-Level Mechanisms

The previous chapters described the components of a Stream from an application level. This chapter explains how those components work together. It shows how the kernel interprets system calls being passed from an application, so that driver and module developers can know what structures are being passed.

Message Handling

Since messages are the communication medium between the user application process and the various components of the Stream, this chapter describes the path they travel and the changes that occur to them. Chapter 8, Messages - Kernel Level covers the underlying mechanics of the kernel.

Modifying Messages

The put(9E) and srv(9E) interfaces process messages as they pass through the queue. Messages are generally processed by type, resulting in a modified message, one or more new messages, or no message at all. The message usually continues in the same direction it was passing through the queue, but can be sent in either direction. A put(9E) procedure can place messages on its queue as they arrive, for later processing by the service procedure. For a more detailed explanation of put and service, see Chapter 8, Messages - Kernel Level.

Some kernel operations are explained here to show you how to manipulate the driver or module appropriately.

Message Types

STREAMS messages differ according to their intended purpose and their queueing priority. The contents of certain message types can be transferred between a process and a Stream using system calls. Appendix A, Message Types describes message types in detail.

Control of Stream-Head Processing

The Stream-head responds to a message by altering the processing associated with certain system calls. Six Stream-head characteristics can be modified. Four characteristics correspond to fields contained in queue (packet sizes -- minimum and maximum, and watermarks -- high and low. Packet sizes are discussed in this chapter. Watermarks are discussed in "Flush Handling" in Chapter 4, STREAMS Driver and Module Interfaces.

Read Options

The value of read options (so_readopt) specifies two sets of three modes that can be set by the I_SRDOPT ioctl(2) (see streamio(7I)).

Byte-stream mode approximately models pipe data transfer. Message nondiscard mode is similar to a TTY in canonical mode.

Table 3-1 Read Option Modes













The first set of bits, RMODEMASK, deals with data and message boundaries, as shown in Table 3-1.

byte-stream (RNORM)

The read(2) call finishes when the byte count is satisfied, the Stream-head read queue becomes empty, or a zero length message is encountered. A zero-length message is put back in the queue. A subsequent read returns 0 bytes.

message non-discard (RMSGN)

The read(2) call finishes when the byte count is satisfied or a message boundary is found, whichever comes first. Any data remaining in the message is put back on the Stream-head read queue.

message discard (RMSGD)

The read(2) call finishes when the byte count is satisfied or a message boundary is found. Any data remaining in the message is discarded up to the message boundary.

The second set of bits, RPROTMASK, specifies the treatment of protocol messages by the read(2) system call:

normal protocol (RPROTNORM)

The read(2) call fails with EBADMSG if an M_PROTO or M_PCPROTO message is at the front of the Stream-head read queue. This is the default operation protocol.

protocol discard (RPROTDIS)

The read(2) call discards any M_PROTO or M_PCPROTO blocks in a message, delivering the M_DATA blocks to the user.

protocol data (RPROTDAT)

The read(2) call converts the M_PROTO and M_PCPROTO message blocks to M_DATA blocks, treating the entire message as data.

Write Options

send zero (I_SWROPT)

The write(2) mode is set using the value of the argument arg. Legal bit settings for arg are: SNDZERO--Send a zero-length message downstream when the write of 0 bytes occurs. To avoid sending a zero-length message when a write of 0 bytes occurs, this bit must not be set in arg. On failure, errno can be set to EINVAL--arg is above the legal value.

Message Queueing and Priorities

Any delay in processing messages causes message queues to grow. Normally, queued messages are handled in a first-in, first-out (FIFO) manner. However, certain conditions can require that associated messages (for instance, an error message) reach their Stream destination as rapidly as possible. For this reason messages are assigned priorities using a priority band associated with each message. Ordinary messages have a priority of zero. High-priority messages are high priority by nature of their message type. Their priority band is ignored. By convention, they are not affected by flow control. Figure 3-1 shows how messages are ordered in a queue according to priority.

Figure 3-1 Message Ordering in a Queue


When a message is queued, it is placed after the messages of the same priority already in the queue (in other words, FIFO within their order of queueing). This affects the flow-control parameters associated with the band of the same priority. Message priorities range from 0 (normal) to 255 (highest). This provides up to 256 bands of message flow within a Stream. Expedited data can be implemented with one extra band of flow (priority band 1) of data. This is shown in Figure 3-2.

Figure 3-2 Message Ordering With One Priority Band


Controlling Flow and Priorities

The I_FLUSHBAND, I_CKBAND, I_GETBAND, I_CANPUT, and I_ATMARK ioctl(2)s support multiple bands of data flow. The I_FLUSHBAND ioctl(2) allows a user to flush a particular band of messages. "Flush Handling" discusses it in more detail.

The I_CKBAND ioctl(2) checks if a message of a given priority exists on the Stream-head read queue. Its interface is:

ioctl(fd, I_CKBAND, pri); 

The call returns 1 if a message of priority pri exists on the Stream-head read queue and 0 if no message of priority pri exists. If an error occurs, -1 is returned. Note that pri should be of type int.

The I_GETBAND ioctl(2) checks the priority of the first message on the Stream-head read queue. The interface is:

ioctl (fd, I_GETBAND, prip); 

The call results in the integer referenced by prip being set to the priority band of the message on the front of the Stream-head read queue.

The I_CANPUT ioctl(2) checks if a certain band is writable. Its interface is:

ioctl (fd, I_CANPUT, pri); 

The return value is 0 if the priority band pri is flow controlled, 1 if the band is writable, and -1 on error.

A module or driver can mark a message. This supports Transmission Control Protocol's (TCP) ability to indicate to the user the last byte of out-of-band data. Once marked, a message sent to the Stream-head causes the Stream-head to remember the message. A user can check if the message on the front of its Stream-head read queue is marked or not with the I_ATMARK ioctl(2). If a user is reading data from the Stream-head, there are multiple messages on the read queue, and one of those messages is marked, the read(2) terminates when it reaches the marked message and returns the data only up to the marked message. Get the rest of the data with successive reads. Chapter 4, STREAMS Driver and Module Interfaces discusses this in more detail.

The I_ATMARK ioctl(2) has the format:

ioctl (fd, I_ATMARK, flag); 

where flag can be either ANYMARK or LASTMARK. ANYMARK indicates that the user wants to check if any message is marked. LASTMARK indicates that the user wants to see if the message is the one and only one marked in the queue. If the test succeeds, 1 is returned. On failure, 0 is returned. If an error occurs, -1 is returned.

Accessing the Service Provider

The first routine presented, inter_open, opens the protocol driver device file specified by path and binds the protocol address contained in addr so that it can receive data. On success, the routine returns the file descriptor associated with the open Stream; on failure, it returns -1 and sets errno to indicate the appropriate UNIX system error value. Example 3-1 shows the inter_open routine.

Example 3-1 inter_open Routine

inter_open (char *path, oflags, addr)
 	intt fd;
 	struct bind_req bind_req;
 	struct strbuf ctlbuf;
 	union  primitives rcvbuf;
 	struct error_ack *error_ack;
 	int flags;

		if ((fd = open(path, oflags)) < 0)

	/* send bind request msg down stream */

		bind_req.PRIM_type = BIND_REQ;
 	bind_req.BIND_addr = addr;
 	ctlbuf.len = sizeof(struct bind_req);
 	ctlbuf.buf = (char *)&bind_req;

		if (putmsg(fd, &ctlbuf, NULL, 0) < 0) {

After opening the protocol driver, inter_open packages a bind request message to send downstream. putmsg is called to send the request to the service provider. The bind request message contains a control part that holds a bind_req structure, but it has no data part. ctlbuf is a structure of type strbuf, and it is initialized with the primitive type and address. Notice that the maxlen field of ctlbuf is not set before calling putmsg. That is because putmsg ignores this field. The dataptr argument to putmsg is set to NULL to indicate that the message contains no data part. The flags argument is 0, which specifies that the message is not a high-priority message.

After inter_open sends the bind request, it must wait for an acknowledgment from the service provider, as Example 3-2 shows:

Example 3-2 Service Provider

/* wait for ack of request */

 ctlbuf.maxlen = sizeof(union primitives);
 ctlbuf.len = 0;
 ctlbuf.buf = (char *)&rcvbuf;
 flags = RS_HIPRI;

 if (getmsg(fd, &ctlbuf, NULL, &flags) < 0) {

 /* did we get enough to determine type? */
 if (ctlbuf.len < sizeof(long)) {
 	errno = EPROTO;

 /* switch on type (first long in rcvbuf) */
 	switch(rcvbuf.type) {
 			errno = EPROTO;

	case OK_ACK:

	case ERROR_ACK:
 			if (ctlbuf.len < sizeof(struct error_ack)) {
 				errno = EPROTO;
 			error_ack = (struct error_ack *)&rcvbuf;
 			errno = error_ack->UNIX_error;

getmsg is called to retrieve the acknowledgment of the bind request. The acknowledgment message consists of a control part that contains either an OK_ACK or an error_ack structure, and no data part.

The acknowledgment primitives are defined as high-priority messages. Messages are queued in a first-in, first-out (FIFO) manner within their priority at the Stream-head; high-priority messages are placed at the front of the Stream-head queue, followed by priority band messages and ordinary messages. The STREAMS mechanism allows only one high-priority message per Stream at the Stream-head at one time. Any additional high-priority messages are discarded on reaching the Stream-head. (There can be only one high-priority message present on the Stream-head read queue at any time.) High-priority messages are particularly suitable for acknowledging service requests when the acknowledgment should be placed ahead of any other messages at the Stream-head.

Before calling getmsg, this routine must initialize the strbuf structure for the control part. buf should point to a buffer large enough to hold the expected control part, and maxlen must be set to indicate the maximum number of bytes this buffer can hold.

Because neither acknowledgment primitive contains a data part, the dataptr argument to getmsg is set to NULL. The flagsp argument points to an integer containing the value RS_HIPRI. This flag indicates that getmsg should wait for a STREAMS high-priority message before returning. It is set because you want to catch the acknowledgment primitives that are priority messages. Otherwise, if the flag is zero, the first message is taken. With RS_HIPRI set, even if a normal message is available, getmsg blocks until a high-priority message arrives.

On return from getmsg, check the len field to ensure that the control part of the retrieved message is an appropriate size. The example then checks the primitive type and takes appropriate actions. An OK_ACK indicates a successful bind operation, and inter_open returns the file descriptor of the open Stream. An error_ack indicates a bind failure, and errno is set to identify the problem with the request.

Closing the Service Provider

The next routine in the service interface library example is inter_close, which closes the Stream to the service provider.


The routine closes the given file descriptor. This causes the protocol driver to free any resources associated with that Stream. For example, the driver can unbind the protocol address that had previously been bound to that Stream, thereby freeing that address for use by another service user.

Sending Data to the Service Provider

The third routine, inter_snd, passes data to the service provider for transmission to the user at the address specified in addr. The data to be transmitted is contained in the buffer pointed to by buf and contains len bytes. On successful completion, this routine returns the number of bytes of data passed to the service provider; on failure, it returns -1.

Example 3-3 inter_snd

inter_snd(int fd, char *buf, int len, long *addr)
 	struct strbuf ctlbuf;
 	struct strbuf databuf;
 	struct unitdata_req unitdata_req;

 	unitdata_req.PRIM_type = UNITDATA_REQ;
 	unitdata_req.DEST_addr = addr;

 	ctlbuf.len = sizeof(struct unitdata_req);
 	ctlbuf.buf = (char *)&unitdata_req;
 	databuf.len = len;
 	databuf.buf = buf;

 	if (putmsg(fd, &ctlbuf, &databuf, 0) < 0)

In this example, the data request primitive is packaged with both a control part and a data part. The control part contains a unitdata_req structure that identifies the primitive type and the destination address of the data. The data to be transmitted is placed in the data part of the request message.

Unlike the bind request, the data request primitive requires no acknowledgment from the service provider. In the example, this choice was made to minimize the overhead during data transfer. If the putmsg call succeeds, this routine returns the number of bytes passed to the service provider.

Receiving Data

The final routine in Example 3-4, inter_rcv, retrieves the next available data. buf points to a buffer where the data should be stored, len indicates the size of that buffer, and addr points to a long integer where the source address of the data is placed. On successful completion, inter_rcv returns the number of bytes of retrieved data; on failure, it returns -1 and an appropriate UNIX system error value.

Example 3-4 Receiving Data

int inter_rcv (int fd, char *buf, int len, long *addr, int *errorp)
 	struct strbuf ctlbuf;
 	struct strbuf databuf;
 	struct unitdata_ind unitdata_ind;
 	int retval;
 	int flagsp;

	ctlbuf.maxlen = sizeof(struct unitdata_ind);
	ctlbuf.len = 0;
	ctlbuf.buf = (char *)&unitdata_ind;
	databuf.maxlen = len;
	databuf.len = 0;
	databuf.buf = buf;
	flagsp = 0;

	if((retval=getmsg(fd,&ctlbuf,&databuf,&flagsp))<0) {
			*errorp = EIO;
	if (retval) {
			*errorp = EIO;
	if (unitdata_ind.PRIM_type != UNITDATA_IND) {
			*errorp = EPROTO;
	*addr = unitdata_ind.SRC_addr;

getmsg is called to retrieve the data indication primitive, where that primitive contains both a control and data part. The control part consists of a unitdata_ind structure that identifies the primitive type and the source address of the data sender. The data part contains the data itself. In ctlbuf, buf points to a buffer containing the control information, and maxlen indicates the maximum size of the buffer. Similar initialization is done for databuf.

The integer pointed to by flagsp in the getmsg call is set to zero, indicating that the next message should be retrieved from the Stream-head regardless of its priority. Data arrives in normal priority messages. If there is no message at the Stream-head, getmsg blocks until a message arrives.

The user's control and data buffers should be large enough to hold any incoming data. If both buffers are large enough, getmsg processes the data indication and returns 0, indicating that a full message was retrieved successfully. However, if neither buffer is large enough, getmsg only returns the part of the message that fits into each user buffer. The remainder of the message is saved for subsequent retrieval (in message nondiscard mode), and a positive, nonzero value is returned to the user. A return value of MORECTL indicates that more control information is waiting for retrieval. A return value of MOREDATA indicates that more data is waiting for retrieval. A return value of (MORECTL | MOREDATA) indicates that data from both parts of the message remain. In the example, if the user buffers are not large enough (that is, getmsg returns a positive, nonzero value), the function sets errno to EIO and fails.

The type of the primitive returned by getmsg is checked to make sure it is a data indication (UNITDATA_IND in the example). The source address is then set and the number of bytes of data is returned.

The example presented is a simplified service interface. It shows typical uses of putmsg(2) and getmsg(2). The state transition rules for the interface are not presented and this example does not handle expedited data.

Input and Output Polling

This section describes the synchronous polling mechanism and asynchronous event notification in STREAMS.

User processes can efficiently monitor and control multiple Streams with two system calls: poll(2) and the I_SETSIG ioctl(2) command. These calls allow a user process to detect events that occur at the Stream-head on one or more Streams, including receipt of data or messages on the read queue and cessation of flow control on the write queue. Note that poll(2) is usable on any character device file descriptor, not just STREAMS.

To monitor Streams with poll(2), a user process issues that system call and specifies the Streams and other files to be monitored, the events to check, and the amount of time to wait for an event. poll(2) blocks the process until the time expires or until an event occurs. If an event occurs, it returns the type of event and the descriptor on which the event occurred.

Instead of waiting for an event to occur, a user process can monitor one or more Streams while processing other data. To do so, issue the I_SETSIG ioctl(2), specifying a Stream and events (as with poll(2)). This ioctl(2) does not block the process and force the user process to wait for the event, but returns immediately and issues a signal when an event occurs. The process calls one of sigaction(2), signal(3C), or sigset(3C) to catch the resulting SIGPOLL signal.

If any selected event occurs on any of the selected Streams, STREAMS sends SIGPOLL to all associated requesting processes. The processes have no information on what event occurred on what Stream. A signaled process can get more information by calling poll(2).

Synchronous Input and Output

poll(2) provides a mechanism to identify the Streams over which a user can send or receive data. For each Stream of interest, users can specify one or more events about which they should be notified. The types of events that can be polled are POLLIN, POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND, detailed in Table 3-2.

Table 3-2 Events That Can Be Polled




A message other than high-priority data can be read without blocking. This event is maintained for compatibility with the previous releases of the Solaris operating environment.  


A normal (nonpriority) message is at the front of the Stream-head read queue.  


A priority message (band > 0) is at the front of the Stream-head queue.  


A high-priority message is at the front of the Stream-head read queue.  


The normal priority band of the queue is writable (not flow controlled).  


The same as POLLOUT.


A priority band greater than 0 of a queue downstream.  

Some of the events may not be applicable to all file types. For example, it is not expected that the POLLPRI event is generated when polling a non-STREAMS character device. POLLIN, POLLRDNORM, POLLRDBAND, and POLLPRI are set even if the message is of zero length.

poll(2) checks each file descriptor for the requested events and, on return, indicates which events have occurred for each file descriptor. If no event has occurred on any polled file descriptor, poll(2) blocks until a requested event or timeout occurs. poll(2) takes the following arguments:

Example 3-5 shows the use of poll(2). Two separate minor devices of the communications driver are opened, thereby establishing two separate Streams to the driver. The pollfd entry is initialized for each device. Each Stream is polled for incoming data. If data arrive on either Stream, data is read and then written back to the other Stream.

Example 3-5 Polling

#include <sys/stropts.h>
#include <fcntl.h>
#include <poll.h>

#define NPOLL 2						/* number of file descriptors to poll */
 	struct pollfd pollfds[NPOLL];
 	char buf[1024];
 	int count, i;

		if ((pollfds[0].fd = open("/dev/ttya", O_RDWR|O_NONBLOCK)) < 0) {
 			perror("open failed for /dev/ttya");
 	if ((pollfds[1].fd = open("/dev/ttyb", O_RDWR|O_NONBLOCK)) < 0) {
 			perror("open failed for /dev/ttyb");

The variable pollfds is declared as an array of the pollfd structure, defined in <poll.h>, and has the format:

struct pollfd {
		int fd;             /* file descriptor */
		short events;       /* requested events */
		short revents;      /* returned events */

For each entry in the array, fd specifies the file descriptor to be polled and events is a bitmask that contains the bitwise inclusive OR of events to be polled on that file descriptor. On return, the revents bitmask indicates which of the requested events has occurred.

The example continues to process incoming data, as shown below:

pollfds[0].events = POLLIN;	/* set events to poll */
	pollfds[1].events = POLLIN;	/* for incoming data */
	while (1) {
		/* poll and use -1 timeout (infinite) */
		if (poll(pollfds, NPOLL, -1) < 0) {
			perror("poll failed");
		for (i = 0; i < NPOLL; i++) {
			switch (pollfds[i].revents) {
				default:						/* default error case */
					fprintf(stderr,"error event\n");

				case 0:						/* no events */

				case POLLIN:
					/*echo incoming data on "other" Stream*/
					while ((count = read(pollfds[i].fd, buf, 1024)) > 0)
						 * write loses data if flow control
						 * prevents the transmit at this time
						if (write(pollfds[(i+1) % NPOLL].fd buf, count) != count)
							fprintf(stderr,"writer lost data");

The user specifies the polled events by setting the events field of the pollfd structure to POLLIN. This request tells poll(2) to notify the user of any incoming data on each Stream. The bulk of the example is an infinite loop, where each iteration polls both Streams for incoming data.

The second argument of poll(2) specifies the number of entries in the pollfds array (2 in this example). The third argument indicates the number of milliseconds poll(2) waits for an event if none has occurred. On a system where millisecond accuracy is not available, timeout is rounded up to the nearest value available on that system. If the value of timeout is 0, poll(2) returns immediately. Here, timeout is set to -1, specifying that poll(2) blocks until a requested event occurs or until the call is interrupted.

If poll(2) succeeds, the program checks each entry in the pollfds array. If revents is set to 0, no event has occurred on that file descriptor. If revents is set to POLLIN, incoming data is available, so all available data is read from the polled minor device and written to the other minor device.

If revents is set to a value other than 0 or POLLIN, an error event must have occurred on that Stream because POLLIN was the only requested event. Table 3-3 shows poll error events.

Table 3-3 poll Error Events




A fatal error has occurred in a module or driver on the Stream associated with the specified file descriptor. Further system calls fail.  


A hangup condition exists on the Stream associated with the specified file descriptor. This event and POLLOUT are mutually exclusive; a Stream is not writable if a hangup has occurred.


The specified file descriptor is not associated with an open Stream.  

These events cannot be polled for by the user but are reported in revents when they occur. They are only valid in the revents bitmask.

The example attempts to process incoming data as quickly as possible. However, when writing data to a Stream, write(2) can block if the Stream is exerting flow control. To prevent the process from blocking, the minor devices of the communications driver are opened with the O_NDELAY (or O_NONBLOCK; see note) flag set. write(2) cannot send all the data if flow control is on and O_NDELAY (O_NONBLOCK) is set. This can happen if the communications driver processes characters slower than the user transmits. If the Stream becomes full, the number of bytes write(2) sends is less than the requested count. For simplicity, the example ignores the data if the Stream becomes full, and a warning is printed to stderr.

Note -

To conform with the IEEE operating system interface standard, POSIX, new applications should use the O_NONBLOCK flag, whose behavior is the same as that of O_NDELAY unless otherwise noted.

This program continues until an error occurs on a Stream, or until the process is interrupted.

Asynchronous Input and Output

poll(2) lets a user monitor multiple Streams synchronously. poll(2) normally blocks until an event occurs on any of the polled file descriptors. In some applications, however, it is desirable to process incoming data asynchronously. For example, an application can attempt to do some local processing and be interrupted when a pending event occurs. Some time-critical applications must not block, and must have immediate success or failure indication.

The I_SETSIG ioctl(2) (see streamio(7I)) is used to request that a SIGPOLL signal be sent to a user process when a specific event occurs. Table 3-4 lists events for I_SETSIG. These are similar to those described for poll(2).

Table 3-4 I_SETSIG ioctl(2) Events




A message other than a high-priority message has arrived on a Stream-head read queue. This event is maintained for compatibility with the previous releases of Solaris operating environment.  


A normal (nonpriority) message has arrived on the Stream-head read queue.  


A priority message (band > 0) has arrived on the Stream-head read queue.  


A high-priority message has arrived on the Stream-head read queue.  


A write queue for normal data (priority band = 0) is no longer full (not flow controlled). This notifies a user that there is space on the queue for sending or writing normal data downstream.  


The same as S_OUTPUT.


A priority band greater than 0 of a queue downstream exists and is writable. This notifies a user that there is space on the queue for sending or writing priority data downstream.  


A signal message sent from a module or driver has arrived on the Stream-head read queue.  


An error message reaches the Stream-head.  


A hangup message sent from a module or driver has arrived at the Stream-head.  


When used with S_RDBAND, SIGURG is generated instead of SIGPOLL when a priority message reaches the front of the Stream-head read queue.

S_INPUT, S_RDNORM, S_RDBAND, and S_HIPRI are set even if the message is of zero length. A user process can handle only high-priority messages by setting the arg to S_HIPRI.


STREAMS allows modules and drivers to cause a signal to be sent to user processes through a special signal message. If the signal specified by the module or driver is not SIGPOLL (see signal(5)), the signal is sent to the process group associated with the Stream. If the signal is SIGPOLL, the signal is only sent to processes that have registered for the signal by using the I_SETSIG ioctl(2).

Extended Signals

To let a process obtain the band and event associated with SIGPOLL more readily, STREAMS supports extended signals. For the given events, a special code is defined in <sys/siginfo.h> that describes the reason SIGPOLL was generated. Table 3-5 describes the data available in the siginfo_t structure passed to the signal handler.

Table 3-5 Data in siginfo_t Structure










Band readable 





Band writable 





Band signaled 






Stream error 











Stream as a Controlling Terminal

The controlling terminal can receive signals and send signals. If a foreground process group has the Stream as a controlling terminal Stream, drivers and modules can use M_SIG messages to send signals to processes.

Job Control

An overview of Job Control is provided here because it interacts with the STREAMS-based terminal subsystem. You can obtain more information on Job Control from the following manual pages: exit(2), getpgid(2), getpgrp(2), getsid(2), kill(2), setpgid(2), setpgrp(2), setsid(2), sigaction(2), signal(5), sigsend(2), termios(3), waitid(2), waitpid(2), and termio(7I).

Job Control breaks a login session into smaller units called jobs. Each job consists of one or more related and cooperating processes. One job, the foreground job, is given complete access to the controlling terminal. The other jobs, background jobs, are denied read access to the controlling terminal and given conditional write and ioctl(2) access to it. The user can stop the executing job and resume the stopped job either in the foreground or in the background.

Under Job Control, background jobs do not receive events generated by the terminal and are not informed with a hangup indication when the controlling process exits. Background jobs that linger after the login session has been dissolved are prevented from further access to the controlling terminal, and do not interfere with the creation of new login sessions.

The following list defines terms associated with Job Control:

Background Process Group

A process group that is a member of a session that established a connection with a controlling terminal and is not the foreground process group.

Controlling Process

A session leader that established a connection to a controlling terminal.

Controlling Terminal

A terminal that is associated with a session. Each session can have at most one controlling terminal associated with it and a controlling terminal can be associated with at most one session. Certain input sequences from the controlling terminal cause signals to be sent to the process groups in the session associated with the controlling terminal.

Foreground Process Group

Each session that establishes a connection with a controlling terminal distinguishes one process group of the session as a foreground process group. The foreground process group has certain privileges that are denied to background process groups when accessing its controlling terminal.

Orphaned Process Group

A process group in which the parent of every member in the group is either a member of the group, or is not a member of the process group's session.

Process Group

Each process in the system is a member of a process group that is identified by a process group ID. Any process that is not a process group leader can create a new process group and become its leader. Any process that is not a process group leader can join an existing process group that shares the same session as the process. A newly created process joins the process group of its creator.

Process Group Leader

A process whose process ID is the same as its process group ID.

Process Group Lifetime

A time period that begins when a process group is created by its process group leader and ends when the last process that is a member in the group leaves the group.

Process ID

A positive integer that uniquely identifies each process in the system. A process ID cannot be reused by the system until the process lifetime, process group lifetime, and session lifetime end for any process ID, process group ID, and session ID sharing that value.

Process Lifetime

A period that begins when the process is forked and ends after the process exits, when its termination has been acknowledged by its parent process.


Each process group is a member of a session that is identified by a session ID.

Session ID

A positive integer that uniquely identifies each session in the system. It is the same as the process ID of its session leader (POSIX).

Session Leader

A process whose session ID is the same as its process and process group ID.

Session Lifetime

A period that begins when the session is created by its session leader and ends when the lifetime of the last process group that is a member of the session ends.

The following signals manage Job Control: (see also signal(5))


Sent to a stopped process to continue it.


Sent to a process to stop it. This signal cannot be caught or ignored.


Sent to a process to stop it. It is typically used when a user requests to stop the foreground process.


Sent to a background process to stop it when it attempts to read from the controlling terminal.


Sent to a background process to stop it when a user attempts to write to or modify the controlling terminal.

A session can be allocated a controlling terminal. For every allocated controlling terminal, Job Control elevates one process group in the controlling process's session to the status of foreground process group. The remaining process groups in the controlling process's session are background process groups. A controlling terminal gives a user the ability to control execution of jobs within the session. Controlling terminals are critical in Job Control. A user can cause the foreground job to stop by typing a predefined key on the controlling terminal. A user can inhibit access to the controlling terminal by background jobs. Background jobs that attempt to access a terminal that has been so restricted is sent a signal that typically causes the job to stop. (See "Accessing the Controlling Terminal".)

Job Control requires support from a line-discipline module on the controlling terminal's Stream. The TCSETA, TCSETAW, and TCSETAF commands of termio(7I) allow a process to set the following line discipline values relevant to Job Control:

SUSP character

A user-defined character that, when typed, causes the line discipline module to request that the Stream-head send a SIGTSTP signal to the foreground process, which by default stops the members of that group. If the value of SUSP is zero, the SIGTSTP signal is not sent, and the SUSP character is disabled.


If TOSTOP is set, background processes are inhibited from writing to their controlling terminal. A line discipline module must record the SUSP suspend character and notify the Stream-head when the user has typed it, and record the state of the TOSTOP bit and notify the Stream-head when the user has changed it.

Allocation and Deallocation

A Stream is allocated as a controlling terminal for a session if:

Hungup Streams

When a Stream-head receives a hangup message from a device or module, it is marked as hung up. A Stream that is marked as hung up is allowed to be reopened by its session leader if it is allocated as a controlling terminal, and by any process if it is not allocated as a controlling terminal. This way, the hangup error can be cleared without forcing all file descriptors to be closed first.

If the reopen is successful, the hangup condition is cleared.

Hangup Signals

When the SIGHUP signal is generated by a hangup message instead of a signal message, the signal is sent to the controlling process instead of the foreground process group, since the allocation and deallocation of controlling terminals to a session is the responsibility of that process group.

Accessing the Controlling Terminal

If a process attempts to access its controlling terminal after it has been deallocated, access is denied. If the process is not holding or ignoring SIGHUP, it is sent a SIGHUP signal. Otherwise, the access fails with an EIO error.

Members of background process groups have limited access to their controlling terminals:

Chapter 4 STREAMS Driver and Module Interfaces

This chapter describes getting messages into and out of the driver from an application level. It shows the relationship between messages overall and the specific ioctl(2) that pertain to application-level operations.

System Calls Used

Table 4-1 summarizes the system calls commonly used in controlling and transferring data and messages within a Stream.

Table 4-1 System Calls Used

Man Page 



Reads data from a Stream  


Writes data to a Stream  


Controls a Stream  


Receives a message at the Stream head  


Receives a priority message at the Stream head  


Sends a message downstream  


Sends a priority message downstream  


Identifies files on which a user can send or receive messages, or on which certain events have occurred (historically, it was restricted to streams but it does not need to be restricted to streams) 


Creates a bidirectional channel that provides a communication path between multiple processes 


Module and Driver ioctl(2)

STREAMS is a special type of character device driver that is different from the historical character input/output (I/O) mechanism. In this section, the phrases character I/O mechanism and I/O mechanism refer only to that part of the mechanism that existed before STREAMS.

The character I/O mechanism handles all ioctl(2) calls transparently. That is, the kernel expects all ioctl(2) to be handled by the device driver associated with the character special file on which the call is sent. All ioctl(2) calls are sent to the driver, which is expected to perform all validation and processing other than file descriptor validity checking. The operation of any specific ioctl(2) is dependent on the device driver. If the driver requires data to be transferred in from user space, it will use the kernel ddi_copyin function. It may also use ddi_copyout to transfer any data results to user space.

With STREAMS, there are a number of differences from the character I/O mechanism that impart ioctl(2) processing.

First, there is a set of generic STREAMS ioctl(2) command values recognized and processed by the Stream head. This is described in streamio(7I). The operation of the generic STREAMS ioctl(2) is generally independent of the presence of any specific module or driver on the Stream.

The second difference is the absence of user context in a module and driver when the information associated with the ioctl(2) is received. This prevents use of ddi_copyin(9F) or ddi_copyout(9F) by the module. This also prevents the module and driver from associating any kernel data with the currently running process. (It is likely that by the time the module or driver receives the ioctl(2), the process generating it may no longer be running.)

A third difference is that for the character I/O mechanism, all ioctl(2) are handled by the single driver associated with the file. In STREAMS, there can be multiple modules on a Stream and each one can have its own set of ioctl(2). That is, the ioctl(2) that can be used on a Stream can change as modules are pushed and popped.

STREAMS provides the capability for user processes to perform control functions on specific modules and drivers in a Stream with ioctl(2) calls. Most streamio(7I) ioctl(2) commands go no further than the Stream head. They are fully processed there and no related messages are sent downstream. However, certain commands and all unrecognized commands cause the Stream head to create an M_IOCTL </function> message, which includes the ioctl(2) arguments, and send the message downstream to be received and processed by a specific module or driver. The M_IOCTL </function> message is the initial message type that carries ioctl(2) information to modules. Other message types are used to complete the ioctl(2) processing in the Stream. In general, each module must uniquely recognize and act on specific M_IOCTL </function> messages.

STREAMS ioctl(2) handling is equivalent to the transparent processing of the character I/O mechanism. STREAMS modules and drivers can process ioctl(2) generated by applications that are implemented for a non-STREAMS environment.

General ioctl(2) Processing

STREAMS blocks a user process that issues an ioctl(2) and causes the Stream head to generate an M_IOCTL </function> message. The process remains blocked until one of the following occurs:

For an I_STR, the STREAMS module or driver that generates a positive acknowledgment message can also return data to the process in the message. An alternate means to return data is provided with transparent ioctl(2). If the Stream head does not receive a positive or negative acknowledgment message in the specified time, the ioctl(2) call fails.

A module that receives an unrecognized M_IOCTL </function> message must pass it on unchanged. A driver that receives an unrecognized M_IOCTL must produce a negative acknowledgment.

The two STREAMS ioctl(2) mechanisms, I_STR and transparent, are described next. (Here, I_STR means the streamio(7I) I_STR command and implies the related STREAMS processing unless noted otherwise.) I_STR has a restricted format and restricted addressing for transferring ioctl(2)-related data between user and kernel space. It requires only a single pair of messages to complete ioctl(2) processing. The transparent mechanism is more general and has almost no restrictions on ioctl(2) data format and addressing. The transparent mechanism generally requires that multiple pairs of messages be exchanged between the Stream head and module to complete the processing.

This is a rather simplistic view. There is nothing preventing a given ioctl(2) from being issued either directly (transparent) or by means of I_STR. Furthermore, ioctl(2) issued through I_STR potentially can require further processing of the form typically associated with transparent ioctl(2).

I_STR ioctl(2) Processing

The I_STR ioctl(2) provides a capability for user applications to perform module and driver control functions on STREAMS files. I_STR allows an application to specify the ioctl(2) timeout. It encourages that all user ioctl(2) data (to be received by the destination module) be placed in a single block that is pointed to from the user stioctl structure. The module can also return data to this block.

Transparent ioctl(2) Processing

The transparent STREAMS ioctl(2) mechanism allows application programs to perform module and driver control functions with ioctl(2) other than I_STR. It is intended to transparently support applications developed prior to the introduction of STREAMS. It alleviates the need to recode and recompile the user-level software to run over STREAMS files. More importantly, applications do not have to package their ioctl(2) requests into the form demanded by I_STR.

The mechanism extends the data transfer capability for STREAMS ioctl(2) calls beyond those provided in the I_STR form. Modules and drivers can transfer data between their kernel space and user space in any ioctl(2) that has a value of the command argument not defined in streamio(7I). These ioctl(2) are known as transparent ioctl(2) to differentiate them from the I_STR form. Existing applications that use non-STREAMS character devices require transparent processing for ioctl(2) if the device driver is converted to STREAMS. The ioctl(2) data can be in any format mutually understood by the user application and module.

The transparent mechanism also supports STREAMS applications that send ioctl(2) data to a driver or module in a single call, where the data may not be in a form readily embedded in a single user block. For example, the data may be contained in nested structures and different user space buffers.

I_LIST ioctl(2)

The I_LIST ioctl(2) supports the strconf(1) and strchg(1) commands that are used to query or change the configuration of a Stream. Only root or an owner of a STREAMS device can alter the configuration of that Stream.

strchg(1) does the following:

strconf(1) does the following:

The I_LIST ioctl(2) (illustrated in Example 4-1) performs two functions. When the third argument of the ioctl(2) call is NULL

if ((mods = ioctl(s, I_LIST, 0)) < 0) {
, the return value of the call indicates the number of modules, plus the driver, present on the Stream. For example, if there are two modules above the driver, 3 is returned. On failure, errno may be set to a value specified in streamio(7I). The second function of the I_LIST ioctl(2) is to copy the module names found on the Stream to the user-supplied buffer. The address of the buffer in user space and the size of the buffer are passed to the ioctl(2) through a structure str_list that is defined as:

struct str_mlist {
		char l_name[FMNAMESZ+1]; /*space for holding a module name*/
	struct str_list {
		int sl_nmods; 				 /*#of modules for which space is allocated*/
		struct str_mlist *sl_modlist;		/*addr of buf for names*/

Here sl_nmods is the number of modules in the sl_modlist array that the user has allocated. Each element in the array must be at least FMNAMESZ+1 bytes long. The array is FMNAMESZ+1 so the extra byte can hold the NULL character at the end of the string. FMNAMESZ is defined by <sys/conf.h>.

The amount of space to allocate for module names is indicated by the number of modules in the STREAM. This is not completely reliable because another module might be pushed onto the Stream after the application invokes the I_LIST ioctl(2) with the NULL argument and before it invokes the I_LIST ioctl(2) with the structure argument.

The I_LIST call with arg pointing to the str_list structure returns the number of entries that have been filled into the sl_modlist array (the number represents the number of modules including the driver). If there is not enough space in the sl_modlist array (see note) or sl_nmods is less than 1, the I_LIST call fails and errno is set to EINVAL. If arg or the sl_modlist array points outside the allocated address space, EFAULT is returned.

Example 4-1 I_LIST ioctl(2)

#include <stdio.h>
#include <string.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>

main(int argc, const char **argv)
		int							s, i;
		unsigned int				mods;
		struct str_list	 mod_list;struct str_mlist *mlist;
		/* Get a socket... */

		if ((s = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
			perror("socket: ");

		/* Determine the number of modules in the stream */	
			perror("I_LIST ioctl");
		if (mods == 0) {
			printf("No modules\n");
		} else {
			printf("%d modules\n", mods);

		/* Allocate memory for all of the module names */
		mlist = (struct str_mlist *)
		calloc(mods, sizeof (struct str_mlist));
		if (mlist == 0) {
			perror("malloc failure");
		mod_list.sl_modlist = mlist;
		mod_list.sl_nmods = mods;
		/* Do the ioctl and get the module names... */
		if (ioctl(s, I_LIST, &mod_list) < 0) {

		/* Print out the name of the modules... */
		for (i = 0; i < mods; i++) {
			printf("s: %s\n", mod_list.sl_modlist[i].l_name);

		/* Free the calloc'd structures... */

Other ioctl(2) Commands

streamio(7I) details the following ioctl(2) commands.

Table 4-2 Other ioctl(2) Commands




Retrieves the name of the module just below the Stream head 


Flushes all input or output queues 


Flushes a band of messages 


Compares the names of all modules present in the Stream to a specific name 


Lets the user look at information in the first message on the Stream head read queue without taking the message off the queue  


Sets the read mode using a series of flag options in the argument 


Indicates the read mode in an int


Counts the data bytes in data blocks in the first message on the Stream head read queue 


Creates a message from a user buffer, adds information about another Stream, and sends the message downstream 


Sets the write mode using the value of the argument 


Returns the current write mode setting 


Requests that the Stream send a message with file pointer to the Stream head at the other end of a Stream pipe 


Retrieves the file descriptor of the message sent by an I_SENDFD ioctl(2) over a Stream pipe


Lets the application see if the current message on the Stream-head read queue is marked by a module downstream 


Checks if the message of a given priority band exists on the Stream-head read queue 


Returns the priority band of the first message on the Stream-head read queue 


Checks if a certain band is writable 


Lets the user set the time the Stream head will delay when a Stream is closing if data exists on the write queues 


Returns the close time delay 


Connects two Streams 


Disconnects two Streams 


Connects two Streams with a persistent link below a multiplexing driver 


Disconnects two Streams that have been connected with a persistent link 

Message Direction

Various system calls let the user create messages and send them downstream and prioritize the messages.

Table 4-3 Send and Recieve Mesages


Creates a message from the caller supplied control and data buffers and sends the message downstream 


Does the same as putmsg(2) and lets the caller specify a priority band for the message


Retrieves M_DATA, M_PROTO, or M_PCPROTO or high priority messages from the Stream head, and places the contents into two user buffers


Does the same as getmsg(2) and lets the caller specify a priority band for the message

The Stream head guarantees that the control part of a message generated by putmsg(2) is at least 64 bytes long. This promotes reusability of the buffer. When the buffer is a reasonable size, modules and drivers may reuse the buffer for other headers.

stropts.h contains the specification of strbuf, which describes the control and data buffers.

Flush Handling

All modules and drivers are expected to handle the flushing of messages. The user may cause data to be flushed of queued messages from a Stream by the submission of an I_FLUSH ioctl(2). Data may be flushed from the read side, write side, or both sides of a Stream.

ioctl (fd, I_FLUSH, arg);

Table 4-4 describes the arguments that may be passed to M_FLUSH.

Table 4-4 M_FLUSH Arguments and bi_flag values




Flushes read side of Stream 


Flushes write queue 


Flushes both read and write queues 

Flushing Priority Bands

In addition to being able to flush all the data from a queue, a specific band may be flushed using the I_FLUSHBAND ioctl(2).

ioctl (fd, I_FLUSHBAND, bandp); 

The ioctl(2) is passed pointer to a bandinfo structure. The bi_pri field indicates the band priority to be flushed (values from 0 to 255). The bi_flag field indicates the type of flush to do. The legal values for bi_flag are defined in Table 4-4. bandinfo has the following format:

struct bandinfo {
		unsigned char    bi_pri;
		int              bi_flag;

See "Flushing Priority Band", which describes M_FLUSHBAND processing, for details on how modules and drivers should handle flush band requests.

Chapter 5 STREAMS Administration

This chapter describes the tools available to administer STREAMS. It shows how to keep track of names, where to find modules, and how to monitor statistics. Kernel debugging is covered in Chapter 15, Debugging.

Tools Available

Table 5-1 identifies some common tools available for monitoring, logging, and administering STREAMS.

Table 5-1 Tools Available for STREAMS Administration

Man Page 



Configures list of modules to be automatically pushed 


Examines system memory images 


Detaches a name from a file descriptor 

strchg(1), strconf(1)

Prints STREAMS trace messages 

strchg(1), strconf(1)

Changes or queries a Stream configuration 


Logs STREAMS errors 


Loads a kernel module 


Unloads a kernel module 


Displays information about loaded kernel modules 

Autopush Facility

The autopush facility (see autopush(1M)) lets administrators specify a list of modules to be automatically pushed onto the Stream whenever a STREAMS device is opened. A prespecified list (/etc/iu.ap) of modules can be pushed onto the Stream if the STREAMS device is not already open.

The STREAMS Administrative Driver (SAD) (see sad(7D)) provides an interface to the autopush mechanism. System administrators can open the SAD and set or get autopush information on other drivers. The SAD caches the list of modules to push for each driver. When the driver is opened, if not already open, the Stream head checks the SAD's cache to determine if the device is configured to have modules automatically pushed. If an entry is found, the modules are pushed. If the device is open, another open does not cause the list of the prespecified modules to be pushed again.

Three options configure the module list:

When the configuration list is cleared, a range of minor devices has to be cleared as a range and not in parts.

Application Interface

The SAD is accessed through /dev/sad/admin or /dev/sad/user. After the device is initialized, a program can be run to perform any needed autopush configuration. The program should open the SAD, read a configuration file to find out what modules must be configured for which devices, format the information into strapush structures, and perform the necessary SAD_SAP ioctl(2)s. See sad(7D) for more information.

All autopush operations are performed through an ioctl(2) command to set or get autopush information. Only root can set autopush information, but any user can get the autopush information for a device.

In the ioctl(2) call, the parameters are the file descriptor of the SAD, either SAD_SAP (set autopush information) or SAD_GAP (get autopush information), and a pointer to a strapush structure.

strapush is defined as:

 * maximum number of modules that can be pushed on a
 * Stream using the autopush feature should be no greater
 * than nstrpush
#define MAXAPUSH 8

/* autopush information common to user and kernel */

struct apcommon {
 	uint apc_cmd;          /* command - see below */
 	major_t apc_major;     /* major device number */
 	minor_t apc_minor;     /* minor device number */
 	minor_t apc_lastminor; /* last minor dev # for range */
 	uint apc_npush;        /* number of modules to push */

/* ap_cmd - various options of autopush */
#define SAP_CLEAR    0     /* remove configuration list */
#define SAP_ONE      1     /* configure one minor device*/
#define SAP_RANGE    2     /* config range of minor devices */
#define SAP_ALL      3     /* configure all minor devices */

/* format of autopush ioctls */
struct strapush {
 	struct apcommon sap_common;
 	char sap_list[MAXAPUSH] [FMNAMESZ + 1]; /* module list */

#define sap_cmd              sap_common.apc_cmd
#define sap_major            sap_common.apc_major
#define sap_minor            sap_common.apc_minor
#define sap_lastminor        sap_common.apc_lastminor
#define sap_npush            sap_common.apc_npush

A device is identified by its major device number, sap_major. A SAD_CMD ioctl(2) is one of the following commands:


Configures a single minor device, sap_minor, of a driver


Configures a range of minor devices from sap_minor to sap_lastminor, inclusive


Configures all minor devices of a device


Clears the previous settings by removing the entry with the matching sap_major and sap_minor fields

The list of modules is specified as a list of module names in sap_list. The maximum number of modules to push automatically is defined by MAXAPUSH.

A user can query the current configuration status of a given major or minor device by issuing the SAD_GAP ioctl(2) with sap_major and sap_minor values of the device set. On successful return from this system call, the strapush structure will be filled in with the corresponding information for that device. The maximum number of entries the SAD driver can cache is determined by the tunable parameter NAUTOPUSH found in the SAD driver's master file.

The following is an example of an autopush configuration file in /etc/iu.ap:

# /dev/console and /dev/contty autopush setup
#	major    minor   lastminor   modules
		wc       0       0           ldterm ttcompat
		zs       0       1           ldterm ttcompat
		ptsl     0       15          ldterm ttcompat

The first line is the configuration of a single minor device whose major name is wc and minor numbers start at 0 and end at 0, creating only one minor number. The modules automatically pushed are ldterm and ttcompat. The second line represents the configuration of the zs driver. The minor device numbers are 0 and 1. The last line allows minor device numbers from 0 to 15 for the ptsl driver.

Administration Tool Description

STREAMS error and trace loggers are provided for debugging and for administering STREAMS modules and drivers. This facility consists of log(7D), strace(1M), strclean(1M), strerr(1M), and the strlog(9F) function.


strace(1M) is a utility that displays the messages in a specified STREAMS log. The log to display is identified by STREAMS module ID number, a sub-ID number, and the priority level.


strlog(9F) sends formatted text to log(7D) driver. Required definitions are contained in <sys/strlog.h> and <sys/log.h>. The call specifies the STREAMS module ID number, a sub-ID number, and the priority level. A flag parameter can specify any combination of:


The message is for the error logger


The message is for the tracer


Advisory notification of a fatal error


Modifies the SL_ERROR flag to request that a copy of the message be mailed to the system administrator


Log the message to the console


Warning message


Notice the message

The flags are followed by a printf(3S)-style format string, but %s, %e, %E, %g, and %G conversion specifications are not recognized. Up to NLOGARGS of numeric or character arguments can be specified.


strqget(9F) gets information about a queue or band of a queue. The information is returned in a long. The stream must be frozen by the caller when calling strqget.


strqset(9F) changes information about a queue or band of the queue. The updated information is provided in an int. If the field is read-only, EPERM is returned and the field is left unchanged. See <sys/stream.h> for valid values. The stream must be frozen by the caller when calling strqset(9F).


strerr(1M) is the streams error logger daemon.

Chapter 6 Pipes and Queues

This chapter covers communication between processes using STREAMS-based pipes and named pipes. Discussion is limited to communications between applications.

Overview of Pipes and FIFOs

A pipe in the SunOS 5.6 system is a mechanism that provides a communication path between multiple processes. Prior to SunOS 5.0, SunOS had standard pipes and named pipes (also called FIFOs). With standard pipes, one end was opened for reading and the other end for writing, so data flow was unidirectional. FIFOs had only one end and typically one process opened the file for reading and another process opened the file for writing. Data written into the FIFO by the writer could then be read by the reader.

To provide greater support and development flexibility for networked applications, pipes and FIFOs are STREAMS-based in SunOS 5. The interface is unchanged but the underlying implementation is changed. When a pipe is created through the pipe(2) interface, two Streams are opened and connected. Data flow is serial.

The remainder of this chapter uses the terms pipe and STREAMS-based pipe interchangeably to mean a STREAMS-based pipe.

Note -

After both ends of a FIFO have been opened, there is no guarantee that further calls to open() O_RDONLY (O_WRONLY) will synchronize with later calls to open() O_WRONLY (O_RDONLY) until both ends of the FIFO have been closed by all readers and writers. Any data written into a FIFO will be lost if both ends of the FIFO are closed before the data is read.

Creating and Opening Pipes and FIFOs

A named pipe, also called a FIFO, is a pipe identified by an entry in a file system's name space. FIFOs are created using mknod(2), mkfifo(3C), or the mknod(1M) command. They are removed using unlink(2) or the rm(1) command.

FIFOs look like regular file system nodes, but are distinguished from them by a p in the first column when the ls -l command is run.

	/usr/sbin/mknod xxx pls -l xxx
prw-r--r-- 1 guest other 0 Aug 26 10:55 xxx
echoput> hello.world>xxx &put>
[1] 8733
cat xxx
hello world
[1]  + Done
rm xxx

FIFOs are unidirectional: that is, one end of the FIFO is used for writing data, the other for reading data. FIFOs allow simple one-way interprocess communication between unrelated processes. Modules may be pushed onto a FIFO. Data written to the FIFO is passed down the write side of the module and back up the read side as shown in Figure 6-1.

Figure 6-1 Pushing Modules on a STREAMS-based FIFO


FIFOs are opened in the same manner as other file system nodes with open(2). Any data written to the FIFO can be read from the same file descriptor in the first-in, first-out manner (serial, sequentially). Modules can also be pushed on the FIFO. See open(2) for the restrictions that apply when opening a FIFO. If O_NDELAY or O_NONBLOCK is not specified, an open on a FIFO blocks until both a reader and a writer are present.

Named or mounted Streams provide a more powerful interface for interprocess communications than does a FIFO. See "Named Streams" for details.

A STREAMS-based pipe, also referred to as an anonymous pipe, is created using pipe(2) which returns two file descriptors--fd[0] and fd[1], each with its own Stream head. The ends of the pipe are constructed so that data written to either end of a pipe may be read from the opposite end.

STREAMS modules can be added to a pipe with I_PUSH ioctl(2). A module can be pushed onto one or both ends of the pipe (see Figure 6-2). However, if a module is pushed onto one end of the pipe, that module cannot be popped from the other end.

Figure 6-2 Pushing Modules on a STREAMS-based Pipe


Using Pipes and FIFOs

Pipes and FIFOs can be accessed through the operating system routines read(2), write(2), ioctl(2), close(2), putmsg(2), putpmsg(2), getmsg(2), getpmsg(2), and poll(2). For FIFOs, open(2) is also used.

Reading From a Pipe or FIFO



Writing to a Pipe or FIFO

When a user process calls write(2), data is sent down the associated Stream. If the pipe or FIFO is empty (no modules pushed), data written is placed on the read queue of the other Stream for pipes, and on the read queue of the same Stream for FIFOs. Since the size of a pipe is the number of unread data bytes, the written data is reflected in the size of the other end of the pipe.

Zero-Length Writes

If a user process issues write(2) with 0 as the number of bytes to send a pipe or FIFO, 0 is returned, and, by default, no message is sent down the Stream. However, if a user must send a zero-length message downstream, SNDZERO ioctl(2) can be used to change this default behavior. If SNDZERO is set in the Stream head, write(2) requests of 0 bytes generate a zero-length message and sends the message down the Stream. If SNDZERO is not set, no message is generated and 0 is returned to the user.

The SNDZERO bit may be changed by theI_SWROPT ioctl(2). If the arg in the ioctl(2) has SNDZERO set, the bit is turned on. If the arg is set to 0, the SNDZERO bit is turned off.

The I_GWROPT ioctl(2) is used to get the current write settings.

Atomic Writes

If multiple processes simultaneously write to the same pipe, data from one process can be interleaved with data from another process, if modules are pushed on the pipe or the write is greater than PIPE_BUF. The order of data written is not necessarily the order of data read. To ensure that writes of less than PIPE_BUF bytes are not interleaved with data written by other processes, any modules pushed on the pipe should have a maximum packet size of at least PIPE_BUF.

Note -

PIPE_BUF is an implementation-specific constant that specifies the maximum number of bytes that are atomic when writing to a pipe. When writing to a pipe, write requests of PIPE_BUF or fewer bytes are not interleaved with data from other processes doing writes to the same pipe. However, write requests of more than PIPE_BUF bytes may have data interleaved on arbitrary byte boundaries with writes by other processes whether or not the O_NONBLOCK or O_NDELAY flag is set.

If the module packet size is at least the size of PIPE_BUF, the Stream-head packages the data in such a way that the first message is at least PIPE_BUF bytes. The remaining data may be packaged into smaller or equal-sized blocks depending on buffer availability. If the first module on the Stream cannot support a packet of PIPE_BUF, atomic writes on the pipe cannot be guaranteed.

Closing a Pipe or FIFO


Flushing Pipes and FIFOs

When the flush request is initiated from an ioctl(2) or from flushq(9F), the FLUSHR or the FLUSHW bits of an M_FLUSH message must be switched. Bits are switchedd at the point where the M_FLUSH message is passed from a write queue to a read queue. This point is also known as the midpoint of the pipe.

The midpoint of a pipe is not always easily detectable, especially if there are numerous modules pushed on either end of the pipe. In that case, some mechanism needs to intercept all messages passing through the Stream. If the message is an M_FLUSH message and it is at the Stream midpoint, the flush bits need to be switched.

This bit switching is handled by the pipemod module. pipemod should be pushed onto a pipe or FIFO where flushing of any kind will take place. The pipemod(7M) module can be pushed on either end of the pipe. The only requirement is that it is pushed onto an end that previously did not have modules on it. That is, pipemod(7M) must be the first module pushed onto a pipe so that it is at the midpoint of the pipe itself.

The pipemod(7M) module handles only M_FLUSH messages. All other messages are passed to the next module using the putnext(9F) utility routine. If an M_FLUSH message is passed to pipemod(7M) and the FLUSHR and FLUSHW bits are set, the message is not processed but is passed to the next module using putnext(9F). If only the FLUSHR bit is set, it is turned off and the FLUSHW bit is set. The message is then passed to the next module, using putnext(9F). Similarly, if the FLUSHW bit was the only bit set in the M_FLUSH message, it is turned off and the FLUSHR bit is turned on. The message is then passed to the next module on the Stream.

The pipemod(7M) module can be pushed on any Stream if it requires the bit switching.

Named Streams

It can be useful to name a Stream or STREAMS-based pipe by associating the Stream with an existing node in the file system name space. This allows unrelated processes to open the pipe and exchange data with the application. The following interfaces support naming a Stream or STREAMS-based pipe.


Attaches a Stream file descriptor to a node in the file system name space, thus naming the Stream.


Detaches a named Stream file descriptor from its node in the file system name space, thus unnaming the Stream.


Tests whether a file descriptor is associated with a Stream.

Passing File Descriptors

Named Streams are useful for passing file descriptors between unrelated processes on the same machine. A user process can send a file descriptor to another process by invoking the I_SENDFD ioctl(2) on one end of a named Stream. This sends a message containing a file pointer to the Stream head at the other end of the pipe. Another process can retrieve the message containing the file pointer by a I_RECVFD ioctl(2) call on the other end of the pipe.

Unique Connections

With named pipes, client processes may communicate with a server process using the module connld that lets a client process get a unique, non-multiplexed connection to a server. The connld(7M) module can be pushed onto the named end of the pipe. If the named end of the pipe is then opened by a client, a new pipe is created. One file descriptor for the new pipe is passed back to a client (named Stream) as the file descriptor from open(2) and the other file descriptor is passed to the server using I_RECUFD ioctl(2). The server and the client may then communicate through a new pipe.

Figure 6-3 shows a server process that has created a pipe and pushed the connld module on the other end. The server then invokes the fattach(3C) routine to name the other end /usr/toserv.

Figure 6-3 Server Sets Up a Pipe


Figure 6-4 Processes X and Y Open /usr/toserv


When process X (procx) opens /usr/toserv, it gains a unique connection to the server process that was at one end of the original STREAMS-based pipe. When process Y (procy) does the same, it also gains a unique connection to the server. As shown in Figure 6-4, the server process has access to three separate pipes through three file descriptors.

connld(7M) is a STREAMS-based module that has open, close, and put procedures.

When the named Stream is opened, the open routine of connld(7M) is called. The open fails if:

The open is not complete and will block until the server process has received the file descriptor using the ioctl I_RECVFD. The setting of the O_NDELAY or O_NONBLOCK flag has no impact on the open.

connld(7M) does not process messages. All messages are passed to the next object in the Stream. The read, write, put routines call putnext(9F) to send the message up or down the Stream.