xref: /openbsd-src/lib/libsndio/mio_aucat.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: mio_aucat.c,v 1.7 2012/04/11 06:05:43 ratchov Exp $	*/
2 /*
3  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4  *
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.
8  *
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 USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <poll.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "aucat.h"
31 #include "debug.h"
32 #include "mio_priv.h"
33 
34 struct mio_aucat_hdl {
35 	struct mio_hdl mio;
36 	struct aucat aucat;
37 	int events;
38 };
39 
40 static void mio_aucat_close(struct mio_hdl *);
41 static size_t mio_aucat_read(struct mio_hdl *, void *, size_t);
42 static size_t mio_aucat_write(struct mio_hdl *, const void *, size_t);
43 static int mio_aucat_pollfd(struct mio_hdl *, struct pollfd *, int);
44 static int mio_aucat_revents(struct mio_hdl *, struct pollfd *);
45 
46 static struct mio_ops mio_aucat_ops = {
47 	mio_aucat_close,
48 	mio_aucat_write,
49 	mio_aucat_read,
50 	mio_aucat_pollfd,
51 	mio_aucat_revents,
52 };
53 
54 struct mio_hdl *
55 mio_aucat_open(const char *str, unsigned int mode,
56     int nbio, unsigned int type)
57 {
58 	struct mio_aucat_hdl *hdl;
59 
60 	hdl = malloc(sizeof(struct mio_aucat_hdl));
61 	if (hdl == NULL)
62 		return NULL;
63 	if (!aucat_open(&hdl->aucat, str, mode, type))
64 		goto bad;
65 	mio_create(&hdl->mio, &mio_aucat_ops, mode, nbio);
66 	if (!aucat_setfl(&hdl->aucat, nbio, &hdl->mio.eof))
67 		goto bad;
68 	return (struct mio_hdl *)hdl;
69 bad:
70 	free(hdl);
71 	return NULL;
72 }
73 
74 static void
75 mio_aucat_close(struct mio_hdl *sh)
76 {
77 	struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
78 
79 	if (!hdl->mio.eof)
80 		aucat_setfl(&hdl->aucat, 0, &hdl->mio.eof);
81 	aucat_close(&hdl->aucat, hdl->mio.eof);
82 	free(hdl);
83 }
84 
85 static size_t
86 mio_aucat_read(struct mio_hdl *sh, void *buf, size_t len)
87 {
88 	struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
89 
90 	while (hdl->aucat.rstate == RSTATE_MSG) {
91 		if (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof))
92 			return 0;
93 	}
94 	return aucat_rdata(&hdl->aucat, buf, len, &hdl->mio.eof);
95 }
96 
97 static size_t
98 mio_aucat_write(struct mio_hdl *sh, const void *buf, size_t len)
99 {
100 	struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
101 
102 	return aucat_wdata(&hdl->aucat, buf, len, 1, &hdl->mio.eof);
103 }
104 
105 static int
106 mio_aucat_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events)
107 {
108 	struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
109 
110 	hdl->events = events;
111 	return aucat_pollfd(&hdl->aucat, pfd, events);
112 }
113 
114 static int
115 mio_aucat_revents(struct mio_hdl *sh, struct pollfd *pfd)
116 {
117 	struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
118 	int revents = pfd->revents;
119 
120 	if (revents & POLLIN) {
121 		while (hdl->aucat.rstate == RSTATE_MSG) {
122 			if (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof))
123 				break;
124 		}
125 		if (hdl->aucat.rstate != RSTATE_DATA)
126 			revents &= ~POLLIN;
127 	}
128 	if (hdl->mio.eof)
129 		return POLLHUP;
130 	return revents & (hdl->events | POLLHUP);
131 }
132