gstreamer/sys/shm/shmpipe.c

932 lines
19 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) <2009> Collabora Ltd
* @author: Olivier Crete <olivier.crete@collabora.co.uk
* Copyright (C) <2009> Nokia Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2009-10-29 19:18:25 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "shmpipe.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/mman.h>
#include <assert.h>
2010-10-20 19:41:15 +00:00
#include "shmalloc.h"
2009-10-29 19:18:25 +00:00
/*
* The protocol over the pipe is in packets
*
* The defined types are:
* type 1: new shm area
* Area length
* Size of path (followed by path)
*
* type 2: Close shm area:
* No payload
*
* type 3: shm buffer
* offset
* bufsize
*
* type 4: ack buffer
* offset
*
* Type 4 goes from the client to the server
* The rest are from the server to the client
* The client should never write in the SHM
*/
2010-10-20 19:41:15 +00:00
#define LISTEN_BACKLOG 10
2009-10-29 19:18:25 +00:00
enum
{
COMMAND_NEW_SHM_AREA = 1,
COMMAND_CLOSE_SHM_AREA = 2,
COMMAND_NEW_BUFFER = 3,
COMMAND_ACK_BUFFER = 4
};
typedef struct _ShmArea ShmArea;
struct _ShmArea
{
int id;
int use_count;
int shm_fd;
2010-10-20 20:17:30 +00:00
char *shm_area_buf;
2009-10-29 19:18:25 +00:00
size_t shm_area_len;
char *shm_area_name;
ShmAllocSpace *allocspace;
ShmArea *next;
};
struct _ShmBuffer
{
int use_count;
ShmArea *shm_area;
unsigned long offset;
size_t size;
ShmAllocBlock *ablock;
2009-10-29 19:18:25 +00:00
ShmBuffer *next;
int num_clients;
int clients[0];
uint64_t tag;
2009-10-29 19:18:25 +00:00
};
struct _ShmPipe
{
int main_socket;
char *socket_path;
int use_count;
void *data;
2009-10-29 19:18:25 +00:00
ShmArea *shm_area;
int next_area_id;
ShmBuffer *buffers;
int num_clients;
ShmClient *clients;
mode_t perms;
};
struct _ShmClient
{
int fd;
ShmClient *next;
};
struct _ShmBlock
{
ShmPipe *pipe;
ShmArea *area;
ShmAllocBlock *ablock;
};
struct CommandBuffer
{
unsigned int type;
int area_id;
union
{
struct
{
size_t size;
unsigned int path_size;
/* Followed by path */
} new_shm_area;
struct
{
unsigned long offset;
unsigned long size;
} buffer;
struct
{
unsigned long offset;
} ack_buffer;
} payload;
};
2010-10-20 20:49:59 +00:00
static ShmArea *sp_open_shm (char *path, int id, mode_t perms, size_t size);
static void sp_close_shm (ShmArea * area);
2009-10-29 19:18:25 +00:00
static int sp_shmbuf_dec (ShmPipe * self, ShmBuffer * buf,
ShmBuffer * prev_buf, ShmClient * client);
2009-10-29 19:18:25 +00:00
static void sp_shm_area_dec (ShmPipe * self, ShmArea * area);
#define RETURN_ERROR(format, ...) do { \
fprintf (stderr, format, __VA_ARGS__); \
sp_close (self); \
return NULL; \
} while (0)
2009-10-29 19:18:25 +00:00
ShmPipe *
sp_writer_create (const char *path, size_t size, mode_t perms)
{
ShmPipe *self = spalloc_new (ShmPipe);
int flags;
struct sockaddr_un sock_un;
int i = 0;
2009-10-29 19:18:25 +00:00
memset (self, 0, sizeof (ShmPipe));
self->main_socket = socket (PF_UNIX, SOCK_STREAM, 0);
self->use_count = 1;
2009-10-29 19:18:25 +00:00
2010-10-20 19:41:15 +00:00
if (self->main_socket < 0)
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("Could not create socket (%d): %s\n", errno,
strerror (errno));
flags = fcntl (self->main_socket, F_GETFL, 0);
2010-10-20 19:41:15 +00:00
if (flags < 0)
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("fcntl(F_GETFL) failed (%d): %s\n", errno, strerror (errno));
2010-10-20 19:41:15 +00:00
if (fcntl (self->main_socket, F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) < 0)
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("fcntl(F_SETFL) failed (%d): %s\n", errno, strerror (errno));
sock_un.sun_family = AF_UNIX;
strncpy (sock_un.sun_path, path, sizeof (sock_un.sun_path) - 1);
2009-10-29 19:18:25 +00:00
while (bind (self->main_socket, (struct sockaddr *) &sock_un,
2009-10-29 19:18:25 +00:00
sizeof (struct sockaddr_un)) < 0) {
if (errno != EADDRINUSE)
RETURN_ERROR ("bind() failed (%d): %s\n", errno, strerror (errno));
if (i > 256)
RETURN_ERROR ("Could not find a free socket name for %s", path);
snprintf (sock_un.sun_path, sizeof (sock_un.sun_path), "%s.%d", path, i);
i++;
2009-10-29 19:18:25 +00:00
}
self->socket_path = strdup (sock_un.sun_path);
2009-10-29 19:18:25 +00:00
2010-10-20 19:41:15 +00:00
if (listen (self->main_socket, LISTEN_BACKLOG) < 0)
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("listen() failed (%d): %s\n", errno, strerror (errno));
2010-10-20 20:49:59 +00:00
self->shm_area = sp_open_shm (NULL, ++self->next_area_id, perms, size);
2009-10-29 19:18:25 +00:00
self->perms = perms;
if (!self->shm_area)
RETURN_ERROR ("Could not open shm area (%d): %s", errno, strerror (errno));
2009-10-29 19:18:25 +00:00
return self;
}
#undef RETURN_ERROR
#define RETURN_ERROR(format, ...) do { \
fprintf (stderr, format, __VA_ARGS__); \
area->use_count--; \
sp_close_shm (area); \
return NULL; \
} while (0)
2009-10-29 19:18:25 +00:00
2010-10-20 19:41:15 +00:00
/**
* sp_open_shm:
2010-10-20 20:49:59 +00:00
* @path: Path of the shm area for a reader,
* NULL if this is a writer (then it will allocate its own path)
2010-10-20 19:41:15 +00:00
*
* Opens a ShmArea
*/
2009-10-29 19:18:25 +00:00
static ShmArea *
2010-10-20 20:49:59 +00:00
sp_open_shm (char *path, int id, mode_t perms, size_t size)
2009-10-29 19:18:25 +00:00
{
ShmArea *area = spalloc_new (ShmArea);
char tmppath[32];
2009-10-29 19:18:25 +00:00
int flags;
int prot;
int i = 0;
2009-10-29 19:18:25 +00:00
memset (area, 0, sizeof (ShmArea));
area->use_count = 1;
area->shm_area_len = size;
2010-10-20 20:49:59 +00:00
if (path)
2009-10-29 19:18:25 +00:00
flags = O_RDONLY;
2010-10-20 20:49:59 +00:00
else
flags = O_RDWR | O_CREAT | O_TRUNC | O_EXCL;
2009-10-29 19:18:25 +00:00
area->shm_fd = -1;
if (path) {
area->shm_fd = shm_open (path, flags, perms);
} else {
do {
snprintf (tmppath, sizeof (tmppath), "/shmpipe.%5d.%5d", getpid (), i++);
area->shm_fd = shm_open (tmppath, flags, perms);
} while (area->shm_fd < 0 && errno == EEXIST);
}
2009-10-29 19:18:25 +00:00
2010-10-20 19:41:15 +00:00
if (area->shm_fd < 0)
RETURN_ERROR ("shm_open failed on %s (%d): %s\n",
path ? path : tmppath, errno, strerror (errno));
2009-10-29 19:18:25 +00:00
2010-10-20 20:49:59 +00:00
if (!path) {
area->shm_area_name = strdup (tmppath);
2009-10-29 19:18:25 +00:00
2010-10-20 19:41:15 +00:00
if (ftruncate (area->shm_fd, size))
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("Could not resize memory area to header size,"
" ftruncate failed (%d): %s\n", errno, strerror (errno));
prot = PROT_READ | PROT_WRITE;
2010-10-20 20:49:59 +00:00
} else {
2009-10-29 19:18:25 +00:00
prot = PROT_READ;
2010-10-20 20:49:59 +00:00
}
2009-10-29 19:18:25 +00:00
2010-10-20 20:17:30 +00:00
area->shm_area_buf = mmap (NULL, size, prot, MAP_SHARED, area->shm_fd, 0);
2009-10-29 19:18:25 +00:00
2010-10-20 20:17:30 +00:00
if (area->shm_area_buf == MAP_FAILED)
2009-10-29 19:18:25 +00:00
RETURN_ERROR ("mmap failed (%d): %s\n", errno, strerror (errno));
area->id = id;
2010-10-20 20:49:59 +00:00
if (!path)
2009-10-29 19:18:25 +00:00
area->allocspace = shm_alloc_space_new (area->shm_area_len);
return area;
}
#undef RETURN_ERROR
static void
sp_close_shm (ShmArea * area)
2009-10-29 19:18:25 +00:00
{
assert (area->use_count == 0);
if (area->allocspace)
shm_alloc_space_free (area->allocspace);
2010-10-20 20:17:30 +00:00
if (area->shm_area_buf != MAP_FAILED)
munmap (area->shm_area_buf, area->shm_area_len);
2009-10-29 19:18:25 +00:00
if (area->shm_fd >= 0)
close (area->shm_fd);
if (area->shm_area_name) {
shm_unlink (area->shm_area_name);
free (area->shm_area_name);
}
spalloc_free (ShmArea, area);
}
static void
sp_shm_area_inc (ShmArea * area)
{
area->use_count++;
}
static void
sp_shm_area_dec (ShmPipe * self, ShmArea * area)
{
assert (area->use_count > 0);
area->use_count--;
if (area->use_count == 0) {
ShmArea *item = NULL;
ShmArea *prev_item = NULL;
for (item = self->shm_area; item; item = item->next) {
if (item == area) {
if (prev_item)
prev_item->next = item->next;
else
self->shm_area = item->next;
break;
}
prev_item = item;
}
assert (item);
sp_close_shm (area);
2009-10-29 19:18:25 +00:00
}
}
void *
sp_get_data (ShmPipe * self)
{
return self->data;
}
void
sp_set_data (ShmPipe * self, void *data)
{
self->data = data;
}
static void
sp_inc (ShmPipe * self)
{
self->use_count++;
}
static void
sp_dec (ShmPipe * self)
{
self->use_count--;
if (self->use_count > 0)
return;
while (self->shm_area)
sp_shm_area_dec (self, self->shm_area);
spalloc_free (ShmPipe, self);
}
2009-10-29 19:18:25 +00:00
void
sp_close (ShmPipe * self)
{
if (self->main_socket >= 0)
close (self->main_socket);
if (self->socket_path) {
unlink (self->socket_path);
free (self->socket_path);
}
while (self->clients)
sp_writer_close_client (self, self->clients);
sp_dec (self);
2009-10-29 19:18:25 +00:00
}
2010-06-03 18:22:36 +00:00
int
2009-10-29 19:18:25 +00:00
sp_writer_setperms_shm (ShmPipe * self, mode_t perms)
{
int ret = 0;
ShmArea *area;
2009-10-29 19:18:25 +00:00
self->perms = perms;
for (area = self->shm_area; area; area = area->next)
ret |= fchmod (area->shm_fd, perms);
return ret;
2009-10-29 19:18:25 +00:00
}
static int
send_command (int fd, struct CommandBuffer *cb, unsigned short int type,
int area_id)
{
cb->type = type;
cb->area_id = area_id;
if (send (fd, cb, sizeof (struct CommandBuffer), MSG_NOSIGNAL) !=
sizeof (struct CommandBuffer))
return 0;
return 1;
}
int
sp_writer_resize (ShmPipe * self, size_t size)
{
ShmArea *newarea;
ShmArea *old_current;
ShmClient *client;
int c = 0;
int pathlen;
if (self->shm_area->shm_area_len == size)
return 0;
2010-10-20 20:49:59 +00:00
newarea = sp_open_shm (NULL, ++self->next_area_id, self->perms, size);
2009-10-29 19:18:25 +00:00
if (!newarea)
return -1;
old_current = self->shm_area;
newarea->next = self->shm_area;
self->shm_area = newarea;
pathlen = strlen (newarea->shm_area_name) + 1;
for (client = self->clients; client; client = client->next) {
2009-11-03 23:33:35 +00:00
struct CommandBuffer cb = { 0 };
2009-10-29 19:18:25 +00:00
if (!send_command (client->fd, &cb, COMMAND_CLOSE_SHM_AREA,
old_current->id))
continue;
cb.payload.new_shm_area.size = newarea->shm_area_len;
cb.payload.new_shm_area.path_size = pathlen;
if (!send_command (client->fd, &cb, COMMAND_NEW_SHM_AREA, newarea->id))
continue;
if (send (client->fd, newarea->shm_area_name, pathlen, MSG_NOSIGNAL) !=
pathlen)
continue;
c++;
}
sp_shm_area_dec (self, old_current);
return c;
}
ShmBlock *
sp_writer_alloc_block (ShmPipe * self, size_t size)
{
ShmBlock *block;
ShmAllocBlock *ablock =
shm_alloc_space_alloc_block (self->shm_area->allocspace, size);
if (!ablock)
return NULL;
block = spalloc_new (ShmBlock);
sp_shm_area_inc (self->shm_area);
block->pipe = self;
block->area = self->shm_area;
block->ablock = ablock;
sp_inc (self);
2009-10-29 19:18:25 +00:00
return block;
}
char *
sp_writer_block_get_buf (ShmBlock * block)
{
2010-10-20 20:17:30 +00:00
return block->area->shm_area_buf +
2009-10-29 19:18:25 +00:00
shm_alloc_space_alloc_block_get_offset (block->ablock);
}
ShmPipe *
sp_writer_block_get_pipe (ShmBlock * block)
{
return block->pipe;
}
2009-10-29 19:18:25 +00:00
void
sp_writer_free_block (ShmBlock * block)
{
shm_alloc_space_block_dec (block->ablock);
sp_shm_area_dec (block->pipe, block->area);
sp_dec (block->pipe);
2009-10-29 19:18:25 +00:00
spalloc_free (ShmBlock, block);
}
/* Returns the number of client this has successfully been sent to */
int
sp_writer_send_buf (ShmPipe * self, char *buf, size_t size, uint64_t tag)
2009-10-29 19:18:25 +00:00
{
ShmArea *area = NULL;
unsigned long offset = 0;
unsigned long bsize = size;
ShmBuffer *sb;
ShmClient *client = NULL;
ShmAllocBlock *ablock = NULL;
2009-10-29 19:18:25 +00:00
int i = 0;
int c = 0;
if (self->num_clients == 0)
return 0;
for (area = self->shm_area; area; area = area->next) {
2010-10-20 20:17:30 +00:00
if (buf >= area->shm_area_buf &&
buf < (area->shm_area_buf + area->shm_area_len)) {
offset = buf - area->shm_area_buf;
ablock = shm_alloc_space_block_get (area->allocspace, offset);
assert (ablock);
2009-10-29 19:18:25 +00:00
break;
}
}
if (!ablock)
2009-10-29 19:18:25 +00:00
return -1;
sb = spalloc_alloc (sizeof (ShmBuffer) + sizeof (int) * self->num_clients);
memset (sb, 0, sizeof (ShmBuffer));
memset (sb->clients, -1, sizeof (int) * self->num_clients);
sb->shm_area = area;
sb->offset = offset;
sb->size = size;
sb->num_clients = self->num_clients;
sb->ablock = ablock;
sb->tag = tag;
2009-10-29 19:18:25 +00:00
for (client = self->clients; client; client = client->next) {
2009-11-03 23:33:35 +00:00
struct CommandBuffer cb = { 0 };
2009-10-29 19:18:25 +00:00
cb.payload.buffer.offset = offset;
cb.payload.buffer.size = bsize;
if (!send_command (client->fd, &cb, COMMAND_NEW_BUFFER, self->shm_area->id))
continue;
sb->clients[i++] = client->fd;
c++;
}
if (c == 0) {
spalloc_free1 (sizeof (ShmBuffer) + sizeof (int) * sb->num_clients, sb);
2009-10-29 19:18:25 +00:00
return 0;
}
sp_shm_area_inc (area);
shm_alloc_space_block_inc (ablock);
2009-10-29 19:18:25 +00:00
sb->use_count = c;
sb->next = self->buffers;
self->buffers = sb;
return c;
}
static int
2009-11-03 21:09:35 +00:00
recv_command (int fd, struct CommandBuffer *cb)
2009-10-29 19:18:25 +00:00
{
int retval;
2009-11-03 21:09:35 +00:00
retval = recv (fd, cb, sizeof (struct CommandBuffer), MSG_DONTWAIT);
2009-10-29 19:18:25 +00:00
if (retval == sizeof (struct CommandBuffer)) {
return 1;
} else {
return 0;
}
}
long int
2009-10-29 19:18:25 +00:00
sp_client_recv (ShmPipe * self, char **buf)
{
char *area_name = NULL;
ShmArea *newarea;
2009-10-29 19:18:25 +00:00
ShmArea *area;
struct CommandBuffer cb;
2009-11-03 21:09:35 +00:00
int retval;
2009-10-29 19:18:25 +00:00
2009-11-03 21:09:35 +00:00
if (!recv_command (self->main_socket, &cb))
2009-10-29 19:18:25 +00:00
return -1;
switch (cb.type) {
case COMMAND_NEW_SHM_AREA:
assert (cb.payload.new_shm_area.path_size > 0);
assert (cb.payload.new_shm_area.size > 0);
area_name = malloc (cb.payload.new_shm_area.path_size);
2009-11-03 21:09:35 +00:00
retval = recv (self->main_socket, area_name,
cb.payload.new_shm_area.path_size, 0);
if (retval != cb.payload.new_shm_area.path_size) {
2009-10-29 19:18:25 +00:00
free (area_name);
return -3;
}
2010-10-20 20:49:59 +00:00
newarea = sp_open_shm (area_name, cb.area_id, 0,
2009-10-29 19:18:25 +00:00
cb.payload.new_shm_area.size);
free (area_name);
if (!newarea)
return -4;
newarea->next = self->shm_area;
self->shm_area = newarea;
break;
case COMMAND_CLOSE_SHM_AREA:
for (area = self->shm_area; area; area = area->next) {
if (area->id == cb.area_id) {
sp_shm_area_dec (self, area);
break;
}
}
break;
case COMMAND_NEW_BUFFER:
assert (buf);
for (area = self->shm_area; area; area = area->next) {
if (area->id == cb.area_id) {
2010-10-20 20:17:30 +00:00
*buf = area->shm_area_buf + cb.payload.buffer.offset;
sp_shm_area_inc (area);
2009-10-29 19:18:25 +00:00
return cb.payload.buffer.size;
}
}
return -23;
default:
return -99;
}
return 0;
}
int
sp_writer_recv (ShmPipe * self, ShmClient * client)
{
ShmBuffer *buf = NULL, *prev_buf = NULL;
struct CommandBuffer cb;
2009-11-03 21:09:35 +00:00
if (!recv_command (client->fd, &cb))
2009-10-29 19:18:25 +00:00
return -1;
switch (cb.type) {
case COMMAND_ACK_BUFFER:
for (buf = self->buffers; buf; buf = buf->next) {
if (buf->shm_area->id == cb.area_id &&
buf->offset == cb.payload.ack_buffer.offset) {
sp_shmbuf_dec (self, buf, prev_buf, client);
2009-10-29 19:18:25 +00:00
break;
}
prev_buf = buf;
}
if (!buf)
return -2;
break;
default:
return -99;
}
return 0;
}
int
sp_client_recv_finish (ShmPipe * self, char *buf)
{
ShmArea *shm_area = NULL;
unsigned long offset;
2009-11-03 23:33:35 +00:00
struct CommandBuffer cb = { 0 };
2009-10-29 19:18:25 +00:00
for (shm_area = self->shm_area; shm_area; shm_area = shm_area->next) {
2010-10-20 20:17:30 +00:00
if (buf >= shm_area->shm_area_buf &&
buf < shm_area->shm_area_buf + shm_area->shm_area_len)
2009-10-29 19:18:25 +00:00
break;
}
assert (shm_area);
2010-10-20 20:17:30 +00:00
offset = buf - shm_area->shm_area_buf;
2009-10-29 19:18:25 +00:00
sp_shm_area_dec (self, shm_area);
2009-10-29 19:18:25 +00:00
cb.payload.ack_buffer.offset = offset;
return send_command (self->main_socket, &cb, COMMAND_ACK_BUFFER,
self->shm_area->id);
}
ShmPipe *
sp_client_open (const char *path)
{
ShmPipe *self = spalloc_new (ShmPipe);
struct sockaddr_un sock_un;
2009-10-29 19:18:25 +00:00
memset (self, 0, sizeof (ShmPipe));
self->main_socket = socket (PF_UNIX, SOCK_STREAM, 0);
self->use_count = 1;
if (self->main_socket < 0)
goto error;
2009-10-29 19:18:25 +00:00
sock_un.sun_family = AF_UNIX;
strncpy (sock_un.sun_path, path, sizeof (sock_un.sun_path) - 1);
2009-10-29 19:18:25 +00:00
if (connect (self->main_socket, (struct sockaddr *) &sock_un,
2009-10-29 19:18:25 +00:00
sizeof (struct sockaddr_un)) < 0)
goto error;
return self;
error:
sp_close (self);
2009-10-29 19:18:25 +00:00
return NULL;
}
ShmClient *
sp_writer_accept_client (ShmPipe * self)
{
ShmClient *client = NULL;
int fd;
2009-11-03 23:33:35 +00:00
struct CommandBuffer cb = { 0 };
2009-10-29 19:18:25 +00:00
int pathlen = strlen (self->shm_area->shm_area_name) + 1;
fd = accept (self->main_socket, NULL, NULL);
2010-02-02 15:35:26 +00:00
if (fd < 0) {
fprintf (stderr, "Could not client connection");
return NULL;
}
2009-10-29 19:18:25 +00:00
cb.payload.new_shm_area.size = self->shm_area->shm_area_len;
cb.payload.new_shm_area.path_size = pathlen;
2009-11-04 01:03:24 +00:00
if (!send_command (fd, &cb, COMMAND_NEW_SHM_AREA, self->shm_area->id)) {
fprintf (stderr, "Sending new shm area failed: %s", strerror (errno));
2009-10-29 19:18:25 +00:00
goto error;
2009-11-04 01:03:24 +00:00
}
2009-10-29 19:18:25 +00:00
if (send (fd, self->shm_area->shm_area_name, pathlen, MSG_NOSIGNAL) !=
2009-11-04 01:03:24 +00:00
pathlen) {
fprintf (stderr, "Sending new shm area path failed: %s", strerror (errno));
2009-10-29 19:18:25 +00:00
goto error;
2009-11-04 01:03:24 +00:00
}
2009-10-29 19:18:25 +00:00
client = spalloc_new (ShmClient);
client->fd = fd;
/* Prepend ot linked list */
client->next = self->clients;
self->clients = client;
self->num_clients++;
return client;
error:
close (fd);
return NULL;
}
static int
sp_shmbuf_dec (ShmPipe * self, ShmBuffer * buf, ShmBuffer * prev_buf,
ShmClient * client)
{
int i;
int had_client = 0;
/**
* Remove client from the list of buffer users. Here we make sure that
* if a client closes connection but already decremented the use count
* for this buffer, but other clients didn't have time to decrement
* buffer will not be freed too early in sp_writer_close_client.
*/
for (i = 0; i < buf->num_clients; i++) {
if (buf->clients[i] == client->fd) {
buf->clients[i] = -1;
had_client = 1;
break;
}
}
assert (had_client);
2009-10-29 19:18:25 +00:00
buf->use_count--;
if (buf->use_count == 0) {
/* Remove from linked list */
if (prev_buf)
prev_buf->next = buf->next;
else
self->buffers = buf->next;
shm_alloc_space_block_dec (buf->ablock);
2009-10-29 19:18:25 +00:00
sp_shm_area_dec (self, buf->shm_area);
spalloc_free1 (sizeof (ShmBuffer) + sizeof (int) * buf->num_clients, buf);
2009-10-29 19:18:25 +00:00
return 0;
}
return 1;
}
void
sp_writer_close_client (ShmPipe * self, ShmClient * client)
{
ShmBuffer *buffer = NULL, *prev_buf = NULL;
ShmClient *item = NULL, *prev_item = NULL;
close (client->fd);
again:
for (buffer = self->buffers; buffer; buffer = buffer->next) {
int i;
for (i = 0; i < buffer->num_clients; i++) {
if (buffer->clients[i] == client->fd) {
if (!sp_shmbuf_dec (self, buffer, prev_buf, client))
2009-10-29 19:18:25 +00:00
goto again;
break;
}
prev_buf = buffer;
}
}
for (item = self->clients; item; item = item->next) {
if (item == client)
break;
prev_item = item;
}
assert (item);
if (prev_item)
prev_item->next = client->next;
else
self->clients = client->next;
self->num_clients--;
spalloc_free (ShmClient, client);
}
int
sp_get_fd (ShmPipe * self)
{
return self->main_socket;
}
int
sp_writer_get_client_fd (ShmClient * client)
{
return client->fd;
}
int
sp_writer_pending_writes (ShmPipe * self)
{
return (self->buffers != NULL);
}
const char *
2010-04-07 23:05:15 +00:00
sp_writer_get_path (ShmPipe * pipe)
{
return pipe->socket_path;
}
ShmBuffer *
sp_writer_get_pending_buffers (ShmPipe * self)
{
return self->buffers;
}
ShmBuffer *
sp_writer_get_next_buffer (ShmBuffer * buffer)
{
return buffer->next;
}
uint64_t
sp_writer_buf_get_tag (ShmBuffer * buffer)
{
return buffer->tag;
}