xref: /openbsd/usr.sbin/ldapd/imsgev.c (revision 3d8817e4)
1 /*
2  * Copyright (c) 2009 Eric Faurot <eric@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/param.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/uio.h>
21 
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "imsgev.h"
29 
30 void imsgev_add(struct imsgev *);
31 void imsgev_dispatch(int, short, void *);
32 void imsgev_disconnect(struct imsgev *, int);
33 
34 void
35 imsgev_init(struct imsgev *iev, int fd, void *data,
36     void (*callback)(struct imsgev *, int, struct imsg *))
37 {
38 	imsg_init(&iev->ibuf, fd);
39 	iev->terminate = 0;
40 
41 	iev->data = data;
42 	iev->handler = imsgev_dispatch;
43 	iev->callback = callback;
44 
45 	iev->events = EV_READ;
46 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
47 	event_add(&iev->ev, NULL);
48 }
49 
50 int
51 imsgev_compose(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
52     uint32_t pid, int fd, void *data, u_int16_t datalen)
53 {
54 	int	r;
55 
56 	r = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
57 	if (r != -1)
58 		imsgev_add(iev);
59 
60 	return (r);
61 }
62 
63 void
64 imsgev_close(struct imsgev *iev)
65 {
66 	iev->terminate = 1;
67 	imsgev_add(iev);
68 }
69 
70 void
71 imsgev_clear(struct imsgev *iev)
72 {
73 	event_del(&iev->ev);
74 	msgbuf_clear(&iev->ibuf.w);
75 	close(iev->ibuf.fd);
76 }
77 
78 void
79 imsgev_add(struct imsgev *iev)
80 {
81 	short	events = 0;
82 
83 	if (!iev->terminate)
84 		events = EV_READ;
85 	if (iev->ibuf.w.queued || iev->terminate)
86 		events |= EV_WRITE;
87 
88 	/* optimization: skip event_{del/set/add} if already set */
89 	if (events == iev->events)
90 		return;
91 
92 	iev->events = events;
93 	event_del(&iev->ev);
94 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
95 	event_add(&iev->ev, NULL);
96 }
97 
98 void
99 imsgev_dispatch(int fd, short ev, void *humppa)
100 {
101 	struct imsgev	*iev = humppa;
102 	struct imsgbuf	*ibuf = &iev->ibuf;
103 	struct imsg	 imsg;
104 	ssize_t		 n;
105 
106 	iev->events = 0;
107 
108 	if (ev & EV_READ) {
109 		if ((n = imsg_read(ibuf)) == -1) {
110 			imsgev_disconnect(iev, IMSGEV_EREAD);
111 			return;
112 		}
113 		if (n == 0) {
114 			/*
115 			 * Connection is closed for reading, and we assume
116 			 * it is also closed for writing, so we error out
117 			 * if write data is pending.
118 			 */
119 			imsgev_disconnect(iev,
120 			    (iev->ibuf.w.queued) ? IMSGEV_EWRITE : IMSGEV_DONE);
121 			return;
122 		}
123 	}
124 
125 	if (ev & EV_WRITE) {
126 		/*
127 		 * We wanted to write data out but the connection is either
128 		 * closed, or some error occured. Both case are not recoverable
129 		 * from the imsg perspective, so we treat it as a WRITE error.
130 		 */
131 		if ((n = msgbuf_write(&ibuf->w)) != 0) {
132 			imsgev_disconnect(iev, IMSGEV_EWRITE);
133 			return;
134 		}
135 	}
136 
137 	while (iev->terminate == 0) {
138 		if ((n = imsg_get(ibuf, &imsg)) == -1) {
139 			imsgev_disconnect(iev, IMSGEV_EIMSG);
140 			return;
141 		}
142 		if (n == 0)
143 			break;
144 		iev->callback(iev, IMSGEV_IMSG, &imsg);
145 		imsg_free(&imsg);
146 	}
147 
148 	if (iev->terminate && iev->ibuf.w.queued == 0) {
149 		imsgev_disconnect(iev, IMSGEV_DONE);
150 		return;
151 	}
152 
153 	imsgev_add(iev);
154 }
155 
156 void
157 imsgev_disconnect(struct imsgev *iev, int code)
158 {
159 	iev->callback(iev, code, NULL);
160 }
161