1 .\" $OpenBSD: imsg_init.3,v 1.13 2015/07/11 16:23:59 deraadt Exp $
3 .\" Copyright (c) 2010 Nicholas Marriott <nicm@openbsd.org>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
14 .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
15 .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 .Dd $Mdocdate: July 11 2015 $
48 .Nd IPC messaging functions
55 .Fn imsg_init "struct imsgbuf *ibuf" "int fd"
57 .Fn imsg_read "struct imsgbuf *ibuf"
59 .Fn imsg_get "struct imsgbuf *ibuf" "struct imsg *imsg"
61 .Fn imsg_compose "struct imsgbuf *ibuf" "u_int32_t type" "uint32_t peerid" \
62 "pid_t pid" "int fd" "const void *data" "u_int16_t datalen"
64 .Fn imsg_composev "struct imsgbuf *ibuf" "u_int32_t type" "u_int32_t peerid" \
65 "pid_t pid" "int fd" "const struct iovec *iov" "int iovcnt"
67 .Fn imsg_create "struct imsgbuf *ibuf" "u_int32_t type" "u_int32_t peerid" \
68 "pid_t pid" "u_int16_t datalen"
70 .Fn imsg_add "struct ibuf *buf" "const void *data" "u_int16_t datalen"
72 .Fn imsg_close "struct imsgbuf *ibuf" "struct ibuf *msg"
74 .Fn imsg_free "struct imsg *imsg"
76 .Fn imsg_flush "struct imsgbuf *ibuf"
78 .Fn imsg_clear "struct imsgbuf *ibuf"
80 .Fn ibuf_open "size_t len"
82 .Fn ibuf_dynamic "size_t len" "size_t max"
84 .Fn ibuf_add "struct ibuf *buf" "const void *data" "size_t len"
86 .Fn ibuf_reserve "struct ibuf *buf" "size_t len"
88 .Fn ibuf_seek "struct ibuf *buf" "size_t pos" "size_t len"
90 .Fn ibuf_size "struct ibuf *buf"
92 .Fn ibuf_left "struct ibuf *buf"
94 .Fn ibuf_close "struct msgbuf *msgbuf" "struct ibuf *buf"
96 .Fn ibuf_write "struct msgbuf *msgbuf"
98 .Fn ibuf_free "struct ibuf *buf"
100 .Fn msgbuf_init "struct msgbuf *msgbuf"
102 .Fn msgbuf_clear "struct msgbuf *msgbuf"
104 .Fn msgbuf_write "struct msgbuf *msgbuf"
106 .Fn msgbuf_drain "struct msgbuf *msgbuf" "size_t n"
110 functions provide a simple mechanism for communication between processes
112 Each transmitted message is guaranteed to be presented to the receiving program
114 They are commonly used in privilege separated processes, where processes with
115 different rights are required to cooperate.
117 A program using these functions should be linked with
124 which wraps a file descriptor and represents one side of a channel on which
125 messages are sent and received:
126 .Bd -literal -offset indent
128 TAILQ_HEAD(, imsg_fd) fds;
137 is a routine which initializes
139 as one side of a channel associated with
141 The file descriptor is used to send and receive messages,
142 but is not closed by any of the imsg functions.
143 An imsgbuf is initialized with the
145 member as the output buffer queue,
147 with the file descriptor passed to
149 and the other members for internal use only.
153 function frees any data allocated as part of an imsgbuf.
159 are generic construction routines for messages that are to be sent using an
163 creates a new message with header specified by
170 of zero uses the process ID returned by
175 In addition to this common imsg header,
177 bytes of space may be reserved for attaching to this imsg.
178 This space is populated using
180 Additionally, the file descriptor
182 may be passed over the socket to the other process.
185 is given, it is closed in the sending program after the message is sent.
186 A value of \-1 indicates no file descriptor should be passed.
188 returns a pointer to a new message if it succeeds, NULL otherwise.
194 bytes of ancillary data pointed to by
198 if it succeeds, \-1 otherwise.
201 completes creation of
208 is a routine which is used to quickly create and queue an imsg.
209 It takes the same parameters as the
215 except that only one ancillary data buffer can be provided.
216 This routine returns 1 if it succeeds, \-1 otherwise.
221 It takes the same parameters, except that the ancillary data buffer is specified
226 is a function which calls
228 in a loop until all imsgs in the output buffer are sent.
229 It returns 0 if it succeeds, \-1 otherwise.
233 routine reads pending data with
235 and queues it as individual messages on
237 It returns the number of bytes read on success, or \-1 on error.
238 A return value of \-1 from
242 and renders it suitable only for passing to
246 fills in an individual imsg pending on
248 into the structure pointed to by
250 It returns the total size of the message, 0 if no messages are ready, or \-1
252 Received messages are returned as a
254 which must be freed by
256 when no longer required.
259 .Bd -literal -offset indent
275 The header members are:
276 .Bl -tag -width Ds -offset indent
278 A integer identifier, typically used to express the meaning of the message.
280 The total length of the imsg, including the header and any ancillary data
281 transmitted with the message (pointed to by the
283 member of the message itself).
285 Flags used internally by the imsg functions: should not be used by application
288 32-bit values specified on message creation and free for any use by the
289 caller, normally used to identify the message sender.
295 .Bl -tag -width Ds -offset indent
297 The file descriptor specified when the message was created and passed using the
298 socket control message API, or \-1 if no file descriptor was sent.
300 A pointer to the ancillary data transmitted with the imsg.
303 The IMSG_HEADER_SIZE define is the size of the imsg message header, which
304 may be subtracted from the
308 to obtain the length of any additional data passed with the message.
310 MAX_IMSGSIZE is defined as the maximum size of a single imsg, currently
313 The imsg API defines functions to manipulate buffers, used internally and during
314 construction of imsgs with
318 is a single buffer and a
320 a queue of output buffers for transmission:
321 .Bd -literal -offset indent
323 TAILQ_ENTRY(ibuf) entry;
333 TAILQ_HEAD(, ibuf) bufs;
341 function allocates a fixed-length buffer.
342 The buffer may not be resized and may contain a maximum of
347 returns a pointer to the buffer; on failure it returns NULL.
350 allocates a resizeable buffer of initial length
354 Buffers allocated with
356 are automatically grown if necessary when data is added.
359 is a routine which appends a block of data to
361 0 is returned on success and \-1 on failure.
368 A pointer to the start of the reserved space is returned, or NULL on error.
371 is a function which returns a pointer to the part of the buffer at offset
375 NULL is returned if the requested range is outside the part of the buffer
381 are functions which return the total bytes used and available in
394 routine transmits as many pending buffers as possible from
398 It returns 1 if it succeeds, \-1 on error and 0 when no buffers were
399 pending or an EOF condition on the socket is detected.
400 Temporary resource shortages are returned with errno
402 and require the application to retry again in the future.
407 and any associated storage.
413 so that buffers may be appended to it.
416 member should also be set directly before
421 empties a msgbuf, removing and discarding any queued buffers.
427 to transmit buffers queued in
429 It returns 1 if it succeeds, \-1 on error, and 0 when the queue was empty
430 or an EOF condition on the socket is detected.
431 Temporary resource shortages are returned with errno
433 and require the application to retry again in the future.
436 discards data from buffers queued in
440 bytes have been removed or
444 In a typical program, a channel between two processes is created with
448 created around one file descriptor in each process:
449 .Bd -literal -offset indent
450 struct imsgbuf parent_ibuf, child_ibuf;
453 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
454 err(1, "socketpair");
462 imsg_init(&child_ibuf, imsg_fds[1]);
463 exit(child_main(&child_ibuf));
468 imsg_init(&parent_ibuf, imsg_fds[0]);
469 exit(parent_main(&parent_ibuf));
472 Messages may then be composed and queued on the
474 for example using the
477 .Bd -literal -offset indent
484 child_main(struct imsgbuf *ibuf)
489 imsg_compose(ibuf, IMSG_A_MESSAGE,
490 0, 0, -1, &idata, sizeof idata);
499 library is used to monitor the socket file descriptor.
500 When the socket is ready for writing, queued messages are transmitted with
502 .Bd -literal -offset indent
503 if (msgbuf_write(&ibuf-\*(Gtw) \*(Lt= 0 && errno != EAGAIN) {
504 /* handle write failure */
508 And when ready for reading, messages are first received using
510 and then extracted with
512 .Bd -literal -offset indent
514 dispatch_imsg(struct imsgbuf *ibuf)
520 if ((n = imsg_read(ibuf)) == -1 || n == 0) {
521 /* handle socket error */
525 if ((n = imsg_get(ibuf, &imsg)) == -1) {
526 /* handle read error */
528 if (n == 0) /* no more messages */
530 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
532 switch (imsg.hdr.type) {
534 if (datalen \*(Lt sizeof idata) {
535 /* handle corrupt message */
537 memcpy(&idata, imsg.data, sizeof idata);
538 /* handle message received */