shm: Block if required

This commit is contained in:
Olivier Crête 2009-11-03 16:09:35 -05:00
parent 67785f331a
commit c5797b06ca

View file

@ -530,11 +530,11 @@ sp_writer_send_buf (ShmPipe * self, gchar * buf, size_t size)
} }
static int static int
read_command (int fd, struct CommandBuffer *cb) recv_command (int fd, struct CommandBuffer *cb)
{ {
int retval; int retval;
retval = recv (fd, cb, sizeof (struct CommandBuffer), 0); retval = recv (fd, cb, sizeof (struct CommandBuffer), MSG_DONTWAIT);
if (retval == sizeof (struct CommandBuffer)) { if (retval == sizeof (struct CommandBuffer)) {
return 1; return 1;
} else { } else {
@ -549,8 +549,9 @@ sp_client_recv (ShmPipe * self, char **buf)
ShmArea *newarea, *oldarea; ShmArea *newarea, *oldarea;
ShmArea *area; ShmArea *area;
struct CommandBuffer cb; struct CommandBuffer cb;
int retval;
if (!read_command (self->main_socket, &cb)) if (!recv_command (self->main_socket, &cb))
return -1; return -1;
switch (cb.type) { switch (cb.type) {
@ -559,9 +560,11 @@ sp_client_recv (ShmPipe * self, char **buf)
assert (cb.payload.new_shm_area.size > 0); assert (cb.payload.new_shm_area.size > 0);
area_name = malloc (cb.payload.new_shm_area.path_size); area_name = malloc (cb.payload.new_shm_area.path_size);
if (read (self->main_socket, area_name, cb.payload.new_shm_area.path_size) retval = recv (self->main_socket, area_name,
!= cb.payload.new_shm_area.path_size) { cb.payload.new_shm_area.path_size, 0);
if (retval != cb.payload.new_shm_area.path_size) {
free (area_name); free (area_name);
g_debug ("retval: %d, errno: %d", retval, errno);
return -3; return -3;
} }
@ -610,7 +613,7 @@ sp_writer_recv (ShmPipe * self, ShmClient * client)
ShmBuffer *buf = NULL, *prev_buf = NULL; ShmBuffer *buf = NULL, *prev_buf = NULL;
struct CommandBuffer cb; struct CommandBuffer cb;
if (!read_command (client->fd, &cb)) if (!recv_command (client->fd, &cb))
return -1; return -1;
switch (cb.type) { switch (cb.type) {
@ -662,7 +665,6 @@ ShmPipe *
sp_client_open (const char *path) sp_client_open (const char *path)
{ {
ShmPipe *self = spalloc_new (ShmPipe); ShmPipe *self = spalloc_new (ShmPipe);
int flags;
struct sockaddr_un sun; struct sockaddr_un sun;
memset (self, 0, sizeof (ShmPipe)); memset (self, 0, sizeof (ShmPipe));
@ -680,14 +682,6 @@ sp_client_open (const char *path)
sizeof (struct sockaddr_un)) < 0) sizeof (struct sockaddr_un)) < 0)
goto error; goto error;
flags = fcntl (self->main_socket, F_GETFL, 0);
if (flags < 0)
goto error;
flags |= O_NONBLOCK | FD_CLOEXEC;
if (fcntl (self->main_socket, F_SETFL, flags) < 0)
goto error;
return self; return self;
error: error:
@ -701,20 +695,12 @@ sp_writer_accept_client (ShmPipe * self)
{ {
ShmClient *client = NULL; ShmClient *client = NULL;
int fd; int fd;
int flags;
struct CommandBuffer cb; struct CommandBuffer cb;
int pathlen = strlen (self->shm_area->shm_area_name) + 1; int pathlen = strlen (self->shm_area->shm_area_name) + 1;
fd = accept (self->main_socket, NULL, NULL); fd = accept (self->main_socket, NULL, NULL);
flags = fcntl (fd, F_GETFL, 0);
if (flags < 0)
goto error;
flags |= O_NONBLOCK | FD_CLOEXEC;
if (fcntl (fd, F_SETFL, flags) < 0)
goto error;
cb.payload.new_shm_area.size = self->shm_area->shm_area_len; cb.payload.new_shm_area.size = self->shm_area->shm_area_len;
cb.payload.new_shm_area.path_size = pathlen; cb.payload.new_shm_area.path_size = pathlen;
if (!send_command (fd, &cb, COMMAND_NEW_SHM_AREA, self->shm_area->id)) if (!send_command (fd, &cb, COMMAND_NEW_SHM_AREA, self->shm_area->id))