X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/22b9fa74de8e80471a5033ea067d3b360930b91d..8ab2aa9fd51a89e06d92a4f7c3792aaa4a08cc71:/lib/speaker-protocol.c
diff --git a/lib/speaker-protocol.c b/lib/speaker-protocol.c
index 1c5199a..4928091 100644
--- a/lib/speaker-protocol.c
+++ b/lib/speaker-protocol.c
@@ -2,32 +2,29 @@
* This file is part of DisOrder.
* Copyright (C) 2005, 2007 Richard Kettlewell
*
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program. If not, see .
*/
/** @file lib/speaker-protocol.c
* @brief Speaker/server protocol support
*/
-#include
-#include "types.h"
+#include "common.h"
#include
-#include
#include
-#include
+#include
+#include
#include "speaker-protocol.h"
#include "log.h"
@@ -35,85 +32,33 @@
/** @brief Send a speaker message
* @param fd File descriptor to send to
* @param sm Pointer to message
- * @param datafd File descriptoxr to pass with message or -1
- *
- * @p datafd will be the output from some decoder.
*/
-void speaker_send(int fd, const struct speaker_message *sm, int datafd) {
- struct msghdr m;
- struct iovec iov;
- union {
- struct cmsghdr cmsg;
- char size[CMSG_SPACE(sizeof (int))];
- } u;
+void speaker_send(int fd, const struct speaker_message *sm) {
int ret;
- memset(&m, 0, sizeof m);
- m.msg_iov = &iov;
- m.msg_iovlen = 1;
- iov.iov_base = (void *)sm;
- iov.iov_len = sizeof *sm;
- if(datafd != -1) {
- m.msg_control = (void *)&u.cmsg;
- m.msg_controllen = sizeof u;
- memset(&u, 0, sizeof u);
- u.cmsg.cmsg_len = CMSG_LEN(sizeof (int));
- u.cmsg.cmsg_level = SOL_SOCKET;
- u.cmsg.cmsg_type = SCM_RIGHTS;
- *(int *)CMSG_DATA(&u.cmsg) = datafd;
- }
do {
- ret = sendmsg(fd, &m, 0);
+ ret = write(fd, sm, sizeof *sm);
} while(ret < 0 && errno == EINTR);
if(ret < 0)
- fatal(errno, "sendmsg");
+ disorder_fatal(errno, "write");
}
/** @brief Receive a speaker message
* @param fd File descriptor to read from
* @param sm Where to store received message
- * @param datafd Where to store received file descriptor or NULL
* @return -ve on @c EAGAIN, 0 at EOF, +ve on success
- *
- * If @p datafd is NULL but a file descriptor is nonetheless received,
- * the process is terminated with an error.
*/
-int speaker_recv(int fd, struct speaker_message *sm, int *datafd) {
- struct msghdr m;
- struct iovec iov;
- union {
- struct cmsghdr cmsg;
- char size[CMSG_SPACE(sizeof (int))];
- } u;
+int speaker_recv(int fd, struct speaker_message *sm) {
int ret;
- memset(&m, 0, sizeof m);
- m.msg_iov = &iov;
- m.msg_iovlen = 1;
- iov.iov_base = (void *)sm;
- iov.iov_len = sizeof *sm;
- if(datafd) {
- m.msg_control = (void *)&u.cmsg;
- m.msg_controllen = sizeof u;
- memset(&u, 0, sizeof u);
- u.cmsg.cmsg_len = CMSG_LEN(sizeof (int));
- u.cmsg.cmsg_level = SOL_SOCKET;
- u.cmsg.cmsg_type = SCM_RIGHTS;
- *datafd = -1;
- }
do {
- ret = recvmsg(fd, &m, MSG_DONTWAIT);
+ ret = read(fd, sm, sizeof *sm);
} while(ret < 0 && errno == EINTR);
if(ret < 0) {
- if(errno != EAGAIN) fatal(errno, "recvmsg");
+ if(errno != EAGAIN)
+ disorder_fatal(errno, "recvmsg");
return -1;
}
- if((size_t)m.msg_controllen >= CMSG_LEN(sizeof (int))) {
- if(!datafd)
- fatal(0, "got an unexpected file descriptor from recvmsg");
- else
- *datafd = *(int *)CMSG_DATA(&u.cmsg);
- }
return ret;
}