| 1 |
/* |
| 2 |
xfermem: unidirectional fast pipe |
| 3 |
|
| 4 |
copyright ?-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Oliver Fromme |
| 7 |
old timestamp: Sun Apr 6 02:26:26 MET DST 1997 |
| 8 |
|
| 9 |
See xfermem.h for documentation/description. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "config.h" |
| 13 |
#include "../compat/compat.h" |
| 14 |
#include "xfermem.h" |
| 15 |
#include <string.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <sys/uio.h> |
| 18 |
#include <sys/mman.h> |
| 19 |
#include <sys/socket.h> |
| 20 |
#include <fcntl.h> |
| 21 |
|
| 22 |
#ifndef HAVE_MMAP |
| 23 |
#include <sys/ipc.h> |
| 24 |
#include <sys/shm.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "../common/debug.h" |
| 28 |
|
| 29 |
#if defined (HAVE_MMAP) && defined(MAP_ANONYMOUS) && !defined(MAP_ANON) |
| 30 |
#define MAP_ANON MAP_ANONYMOUS |
| 31 |
#endif |
| 32 |
|
| 33 |
void INT123_xfermem_init (txfermem **xf, size_t bufsize, size_t msize, size_t skipbuf) |
| 34 |
{ |
| 35 |
size_t regsize = bufsize + msize + skipbuf + sizeof(txfermem); |
| 36 |
|
| 37 |
#ifdef HAVE_MMAP |
| 38 |
# ifdef MAP_ANON |
| 39 |
if ((*xf = (txfermem *) mmap(0, regsize, PROT_READ | PROT_WRITE, |
| 40 |
MAP_ANON | MAP_SHARED, -1, 0)) == (txfermem *) -1) { |
| 41 |
perror ("mmap()"); |
| 42 |
exit (1); |
| 43 |
} |
| 44 |
# else |
| 45 |
int devzero; |
| 46 |
if ((devzero = open("/dev/zero", O_RDWR, 0)) == -1) { |
| 47 |
perror ("open(/dev/zero)"); |
| 48 |
exit (1); |
| 49 |
} |
| 50 |
if ((*xf = (txfermem *) mmap(0, regsize, PROT_READ | PROT_WRITE, |
| 51 |
MAP_SHARED, devzero, 0)) == (txfermem *) -1) { |
| 52 |
perror ("mmap()"); |
| 53 |
exit (1); |
| 54 |
} |
| 55 |
close (devzero); |
| 56 |
# endif |
| 57 |
#else |
| 58 |
struct shmid_ds shmemds; |
| 59 |
int shmemid; |
| 60 |
if ((shmemid = shmget(IPC_PRIVATE, regsize, IPC_CREAT | 0600)) == -1) { |
| 61 |
perror ("shmget()"); |
| 62 |
exit (1); |
| 63 |
} |
| 64 |
if ((*xf = (txfermem *) shmat(shmemid, 0, 0)) == (txfermem *) -1) { |
| 65 |
perror ("shmat()"); |
| 66 |
shmctl (shmemid, IPC_RMID, &shmemds); |
| 67 |
exit (1); |
| 68 |
} |
| 69 |
if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) { |
| 70 |
perror ("shmctl()"); |
| 71 |
INT123_xfermem_done (*xf); |
| 72 |
exit (1); |
| 73 |
} |
| 74 |
#endif |
| 75 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, (*xf)->fd) < 0) { |
| 76 |
perror ("socketpair()"); |
| 77 |
INT123_xfermem_done (*xf); |
| 78 |
exit (1); |
| 79 |
} |
| 80 |
(*xf)->freeindex = (*xf)->readindex = 0; |
| 81 |
(*xf)->data = ((char *) *xf) + sizeof(txfermem) + msize; |
| 82 |
(*xf)->metadata = ((char *) *xf) + sizeof(txfermem); |
| 83 |
(*xf)->size = bufsize; |
| 84 |
(*xf)->metasize = msize + skipbuf; |
| 85 |
} |
| 86 |
|
| 87 |
void INT123_xfermem_done (txfermem *xf) |
| 88 |
{ |
| 89 |
if(!xf) |
| 90 |
return; |
| 91 |
#ifdef HAVE_MMAP |
| 92 |
/* Here was a cast to (caddr_t) ... why? Was this needed for SunOS? |
| 93 |
Casting to (void*) should silence compilers in case of funny |
| 94 |
prototype for munmap(). */ |
| 95 |
munmap ( (void*)xf, xf->size + xf->metasize + sizeof(txfermem)); |
| 96 |
#else |
| 97 |
if (shmdt((void *) xf) == -1) { |
| 98 |
perror ("shmdt()"); |
| 99 |
exit (1); |
| 100 |
} |
| 101 |
#endif |
| 102 |
} |
| 103 |
|
| 104 |
void INT123_xfermem_init_writer (txfermem *xf) |
| 105 |
{ |
| 106 |
if(xf) |
| 107 |
close (xf->fd[XF_READER]); |
| 108 |
debug1("xfermem writer fd=%i", xf->fd[XF_WRITER]); |
| 109 |
} |
| 110 |
|
| 111 |
void INT123_xfermem_init_reader (txfermem *xf) |
| 112 |
{ |
| 113 |
if(xf) |
| 114 |
close (xf->fd[XF_WRITER]); |
| 115 |
debug1("xfermem reader fd=%i", xf->fd[XF_READER]); |
| 116 |
} |
| 117 |
|
| 118 |
size_t INT123_xfermem_get_freespace (txfermem *xf) |
| 119 |
{ |
| 120 |
size_t freeindex, readindex; |
| 121 |
|
| 122 |
if(!xf) |
| 123 |
return 0; |
| 124 |
|
| 125 |
if ((freeindex = xf->freeindex) < 0 |
| 126 |
|| (readindex = xf->readindex) < 0) |
| 127 |
return (0); |
| 128 |
if (readindex > freeindex) |
| 129 |
return ((readindex - freeindex) - 1); |
| 130 |
else |
| 131 |
return ((xf->size - (freeindex - readindex)) - 1); |
| 132 |
} |
| 133 |
|
| 134 |
size_t INT123_xfermem_get_usedspace (txfermem *xf) |
| 135 |
{ |
| 136 |
size_t freeindex, readindex; |
| 137 |
|
| 138 |
if(!xf) |
| 139 |
return 0; |
| 140 |
|
| 141 |
if ((freeindex = xf->freeindex) < 0 |
| 142 |
|| (readindex = xf->readindex) < 0) |
| 143 |
return (0); |
| 144 |
if (freeindex >= readindex) |
| 145 |
return (freeindex - readindex); |
| 146 |
else |
| 147 |
return (xf->size - (readindex - freeindex)); |
| 148 |
} |
| 149 |
|
| 150 |
static int xfermem_getcmd_raw (int fd, int block, byte *cmds, int count) |
| 151 |
{ |
| 152 |
fd_set selfds; |
| 153 |
int ret; |
| 154 |
|
| 155 |
for (;;) { |
| 156 |
struct timeval selto = {0, 0}; |
| 157 |
|
| 158 |
FD_ZERO (&selfds); |
| 159 |
FD_SET (fd, &selfds); |
| 160 |
#ifdef HPUX |
| 161 |
switch (select(FD_SETSIZE, (int *) &selfds, NULL, NULL, block ? NULL : &selto)) |
| 162 |
#else |
| 163 |
switch (select(FD_SETSIZE, &selfds, NULL, NULL, block ? NULL : &selto)) |
| 164 |
#endif |
| 165 |
{ |
| 166 |
case 0: |
| 167 |
if (!block) |
| 168 |
return (0); |
| 169 |
continue; |
| 170 |
case -1: |
| 171 |
if (errno == EINTR) |
| 172 |
continue; |
| 173 |
return (-2); |
| 174 |
case 1: |
| 175 |
if (FD_ISSET(fd, &selfds)) |
| 176 |
switch((ret=read(fd, cmds, count))) |
| 177 |
{ |
| 178 |
case 0: /* EOF */ |
| 179 |
return (-1); |
| 180 |
case -1: |
| 181 |
if (errno == EINTR) |
| 182 |
continue; |
| 183 |
return (-3); |
| 184 |
default: |
| 185 |
return ret; |
| 186 |
} |
| 187 |
else /* ?!? */ |
| 188 |
return (-5); |
| 189 |
default: /* ?!? */ |
| 190 |
return (-6); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/* Verbose variant for debugging communication. */ |
| 196 |
int INT123_xfermem_getcmd(int fd, int block) |
| 197 |
{ |
| 198 |
byte cmd; |
| 199 |
int res = xfermem_getcmd_raw(fd, block, &cmd, 1); |
| 200 |
debug3("INT123_xfermem_getcmd(%i, %i) = %i", fd, block, res == 1 ? cmd : res); |
| 201 |
return res == 1 ? cmd : res; |
| 202 |
} |
| 203 |
|
| 204 |
int INT123_xfermem_getcmds(int fd, int block, byte *cmds, int count) |
| 205 |
{ |
| 206 |
int res = xfermem_getcmd_raw(fd, block, cmds, count); |
| 207 |
debug5("INT123_xfermem_getcmds(%i, %i, %p, %i) = %i" |
| 208 |
, fd, block, (void*)cmds, count |
| 209 |
, res); |
| 210 |
return res; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
int INT123_xfermem_putcmd (int fd, byte cmd) |
| 215 |
{ |
| 216 |
for (;;) { |
| 217 |
switch (write(fd, &cmd, 1)) { |
| 218 |
case 1: |
| 219 |
debug2("INT123_xfermem_putcmd(%i, %i) = 1", fd, cmd); |
| 220 |
return (1); |
| 221 |
case -1: |
| 222 |
if (errno != EINTR) |
| 223 |
{ |
| 224 |
debug3("INT123_xfermem_putcmd(%i, %i) = -1 (%s)" |
| 225 |
, fd, cmd, INT123_strerror(errno)); |
| 226 |
return (-1); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
There is a basic assumetry between reader and writer: |
| 234 |
The reader does work in periodic pieces and can be relied upon to |
| 235 |
eventually answer a call. It is important that it does not block |
| 236 |
for a significant duration unless it has really nothing to do. |
| 237 |
|
| 238 |
The writer is more undefined in its behaviour, it is controlled by |
| 239 |
external agents. You cannot rely on it answering synchronization |
| 240 |
requests in a timely manner. But on the other hand, it can be left |
| 241 |
hanging for a while. The critical side is that of the reader. |
| 242 |
|
| 243 |
Because of that, it is only sensible to provide a voluntary |
| 244 |
INT123_xfermem_writer_block() here. The reader does not need such a function. |
| 245 |
Only if it has nothing else to do, it will simply block on |
| 246 |
INT123_xfermem_getcmd(), and the writer promises to INT123_xfermem_putcmd() when |
| 247 |
something happens. |
| 248 |
|
| 249 |
The writer always sends a wakeup command to the reader since the latter |
| 250 |
could be in the process of putting itself to sleep right now, without |
| 251 |
a flag indicating so being set yet. |
| 252 |
|
| 253 |
The reader periodically reads from its file descriptor so that it does |
| 254 |
not get clogged up with pending messages. It will only (and always) send |
| 255 |
a wakeup call in response to a received command. |
| 256 |
*/ |
| 257 |
|
| 258 |
/* Wait a bit to get a sign of life from the reader. |
| 259 |
Returns -1 if even that did not work. */ |
| 260 |
int INT123_xfermem_writer_block(txfermem *xf) |
| 261 |
{ |
| 262 |
int myfd = xf->fd[XF_WRITER]; |
| 263 |
int result; |
| 264 |
|
| 265 |
INT123_xfermem_putcmd(myfd, XF_CMD_PING); |
| 266 |
result = INT123_xfermem_getcmd(myfd, TRUE); |
| 267 |
/* Only a pong to my ping is the expected good answer. |
| 268 |
Everything else is a problem to be communicated. */ |
| 269 |
return (result == XF_CMD_PONG) ? 0 : result; |
| 270 |
} |
| 271 |
|
| 272 |
/* Return: 0 on success, -1 on communication error, > 0 for |
| 273 |
error on buffer side, some special return code from buffer to be |
| 274 |
evaluated. */ |
| 275 |
int INT123_xfermem_write(txfermem *xf, void *buffer, size_t bytes) |
| 276 |
{ |
| 277 |
if(buffer == NULL || bytes < 1) return 0; |
| 278 |
|
| 279 |
/* You weren't so braindead not allocating enough space at all, right? */ |
| 280 |
while (INT123_xfermem_get_freespace(xf) < bytes) |
| 281 |
{ |
| 282 |
int cmd = INT123_xfermem_writer_block(xf); |
| 283 |
if(cmd) /* Non-successful wait. */ |
| 284 |
return cmd; |
| 285 |
} |
| 286 |
/* Now we have enough space. copy the memory, possibly with the wrap. */ |
| 287 |
if(xf->size - xf->freeindex >= bytes) |
| 288 |
{ /* one block of free memory */ |
| 289 |
memcpy(xf->data+xf->freeindex, buffer, bytes); |
| 290 |
} |
| 291 |
else |
| 292 |
{ /* two blocks */ |
| 293 |
size_t endblock = xf->size - xf->freeindex; |
| 294 |
memcpy(xf->data+xf->freeindex, buffer, endblock); |
| 295 |
memcpy(xf->data, (char*)buffer + endblock, bytes-endblock); |
| 296 |
} |
| 297 |
/* Advance the free space pointer, including the wrap. */ |
| 298 |
xf->freeindex = (xf->freeindex + bytes) % xf->size; |
| 299 |
/* Always notify the buffer process. */ |
| 300 |
debug("write waking"); |
| 301 |
return INT123_xfermem_putcmd(xf->fd[XF_WRITER], XF_CMD_DATA) < 0 |
| 302 |
? -1 |
| 303 |
: 0; |
| 304 |
} |