1 /* $OpenBSD: mio_rmidi.c,v 1.21 2015/11/24 11:03:18 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/stat.h> 20 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <limits.h> 24 #include <poll.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "debug.h" 31 #include "mio_priv.h" 32 33 #define DEVPATH_PREFIX "/dev/rmidi" 34 #define DEVPATH_MAX (1 + \ 35 sizeof(DEVPATH_PREFIX) - 1 + \ 36 sizeof(int) * 3) 37 38 struct mio_rmidi_hdl { 39 struct mio_hdl mio; 40 int fd; 41 }; 42 43 static void mio_rmidi_close(struct mio_hdl *); 44 static size_t mio_rmidi_read(struct mio_hdl *, void *, size_t); 45 static size_t mio_rmidi_write(struct mio_hdl *, const void *, size_t); 46 static int mio_rmidi_nfds(struct mio_hdl *); 47 static int mio_rmidi_pollfd(struct mio_hdl *, struct pollfd *, int); 48 static int mio_rmidi_revents(struct mio_hdl *, struct pollfd *); 49 50 static struct mio_ops mio_rmidi_ops = { 51 mio_rmidi_close, 52 mio_rmidi_write, 53 mio_rmidi_read, 54 mio_rmidi_nfds, 55 mio_rmidi_pollfd, 56 mio_rmidi_revents 57 }; 58 59 static int 60 mio_rmidi_getfd(const char *str, unsigned int mode, int nbio) 61 { 62 const char *p; 63 char path[DEVPATH_MAX]; 64 unsigned int devnum; 65 int fd, flags; 66 67 p = _sndio_parsetype(str, "rmidi"); 68 if (p == NULL) { 69 DPRINTF("mio_rmidi_getfd: %s: \"rsnd\" expected\n", str); 70 return -1; 71 } 72 switch (*p) { 73 case '/': 74 p++; 75 break; 76 default: 77 DPRINTF("mio_rmidi_getfd: %s: '/' expected\n", str); 78 return -1; 79 } 80 p = _sndio_parsenum(p, &devnum, 255); 81 if (p == NULL || *p != '\0') { 82 DPRINTF("mio_rmidi_getfd: %s: number expected after '/'\n", str); 83 return -1; 84 } 85 snprintf(path, sizeof(path), DEVPATH_PREFIX "%u", devnum); 86 if (mode == (MIO_IN | MIO_OUT)) 87 flags = O_RDWR; 88 else 89 flags = (mode & MIO_IN) ? O_WRONLY : O_RDONLY; 90 while ((fd = open(path, flags | O_NONBLOCK | O_CLOEXEC)) < 0) { 91 if (errno == EINTR) 92 continue; 93 DPERROR(path); 94 return -1; 95 } 96 return fd; 97 } 98 99 static struct mio_hdl * 100 mio_rmidi_fdopen(int fd, unsigned int mode, int nbio) 101 { 102 struct mio_rmidi_hdl *hdl; 103 104 hdl = malloc(sizeof(struct mio_rmidi_hdl)); 105 if (hdl == NULL) 106 return NULL; 107 _mio_create(&hdl->mio, &mio_rmidi_ops, mode, nbio); 108 hdl->fd = fd; 109 return (struct mio_hdl *)hdl; 110 } 111 112 struct mio_hdl * 113 _mio_rmidi_open(const char *str, unsigned int mode, int nbio) 114 { 115 struct mio_hdl *hdl; 116 int fd; 117 118 fd = mio_rmidi_getfd(str, mode, nbio); 119 if (fd < 0) 120 return NULL; 121 hdl = mio_rmidi_fdopen(fd, mode, nbio); 122 if (hdl != NULL) 123 return hdl; 124 while (close(fd) < 0 && errno == EINTR) 125 ; /* retry */ 126 return NULL; 127 } 128 129 static void 130 mio_rmidi_close(struct mio_hdl *sh) 131 { 132 struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; 133 int rc; 134 135 do { 136 rc = close(hdl->fd); 137 } while (rc < 0 && errno == EINTR); 138 free(hdl); 139 } 140 141 static size_t 142 mio_rmidi_read(struct mio_hdl *sh, void *buf, size_t len) 143 { 144 struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; 145 ssize_t n; 146 147 while ((n = read(hdl->fd, buf, len)) < 0) { 148 if (errno == EINTR) 149 continue; 150 if (errno != EAGAIN) { 151 DPERROR("mio_rmidi_read: read"); 152 hdl->mio.eof = 1; 153 } 154 return 0; 155 } 156 if (n == 0) { 157 DPRINTF("mio_rmidi_read: eof\n"); 158 hdl->mio.eof = 1; 159 return 0; 160 } 161 return n; 162 } 163 164 static size_t 165 mio_rmidi_write(struct mio_hdl *sh, const void *buf, size_t len) 166 { 167 struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; 168 ssize_t n; 169 170 while ((n = write(hdl->fd, buf, len)) < 0) { 171 if (errno == EINTR) 172 continue; 173 if (errno != EAGAIN) { 174 DPERROR("mio_rmidi_write: write"); 175 hdl->mio.eof = 1; 176 } 177 return 0; 178 } 179 return n; 180 } 181 182 static int 183 mio_rmidi_nfds(struct mio_hdl *sh) 184 { 185 return 1; 186 } 187 188 static int 189 mio_rmidi_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events) 190 { 191 struct mio_rmidi_hdl *hdl = (struct mio_rmidi_hdl *)sh; 192 193 pfd->fd = hdl->fd; 194 pfd->events = events; 195 return 1; 196 } 197 198 static int 199 mio_rmidi_revents(struct mio_hdl *sh, struct pollfd *pfd) 200 { 201 return pfd->revents; 202 } 203