2008-11-11 23:54:25 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2009-01-31 10:33:39 -08:00
|
|
|
#include <stdio.h>
|
2008-11-11 23:54:25 -08:00
|
|
|
#include <errno.h>
|
2011-06-09 07:54:24 -07:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_ISFINITE
|
2011-10-20 22:40:39 -07:00
|
|
|
# define ECORE_FINITE(t) isfinite(t)
|
2011-06-09 07:54:24 -07:00
|
|
|
#else
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define ECORE_FINITE(t) _finite(t)
|
|
|
|
# else
|
|
|
|
# define ECORE_FINITE(t) finite(t)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FIX_HZ 1
|
|
|
|
|
|
|
|
#ifdef FIX_HZ
|
|
|
|
# ifndef _MSC_VER
|
|
|
|
# include <sys/param.h>
|
|
|
|
# endif
|
|
|
|
# ifndef HZ
|
|
|
|
# define HZ 100
|
|
|
|
# endif
|
|
|
|
#endif
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
#ifdef HAVE_EVIL
|
|
|
|
# include <Evil.h>
|
|
|
|
#endif
|
|
|
|
|
2011-11-05 10:49:49 -07:00
|
|
|
#ifdef HAVE_ESCAPE
|
|
|
|
# include <Escape.h>
|
|
|
|
#endif
|
|
|
|
|
2008-11-11 23:54:25 -08:00
|
|
|
#include "Ecore.h"
|
2009-12-22 13:15:12 -08:00
|
|
|
#include "ecore_private.h"
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
/* How of then we should retry to write to the pipe */
|
|
|
|
#define ECORE_PIPE_WRITE_RETRY 6
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On Windows, pipe() is implemented with sockets.
|
|
|
|
* Contrary to Linux, Windows uses different functions
|
|
|
|
* for sockets and fd's: write() is for fd's and send
|
|
|
|
* is for sockets. So I need to put some win32 code
|
|
|
|
* here. I can't think of a solution where the win32
|
|
|
|
* code is in Evil and not here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
# include <winsock2.h>
|
|
|
|
|
|
|
|
# define pipe_write(fd, buffer, size) send((fd), (char *)(buffer), size, 0)
|
|
|
|
# define pipe_read(fd, buffer, size) recv((fd), (char *)(buffer), size, 0)
|
2009-05-29 07:32:57 -07:00
|
|
|
# define pipe_close(fd) closesocket(fd)
|
2011-10-20 22:40:39 -07:00
|
|
|
# define PIPE_FD_INVALID INVALID_SOCKET
|
|
|
|
# define PIPE_FD_ERROR SOCKET_ERROR
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
|
|
|
|
# define pipe_write(fd, buffer, size) write((fd), buffer, size)
|
|
|
|
# define pipe_read(fd, buffer, size) read((fd), buffer, size)
|
2009-05-29 07:32:57 -07:00
|
|
|
# define pipe_close(fd) close(fd)
|
2011-10-20 22:40:39 -07:00
|
|
|
# define PIPE_FD_INVALID -1
|
|
|
|
# define PIPE_FD_ERROR -1
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
#endif /* ! _WIN32 */
|
|
|
|
|
2011-12-11 00:29:35 -08:00
|
|
|
struct _Ecore_Pipe
|
|
|
|
{
|
|
|
|
ECORE_MAGIC;
|
|
|
|
int fd_read;
|
|
|
|
int fd_write;
|
|
|
|
Ecore_Fd_Handler *fd_handler;
|
|
|
|
const void *data;
|
|
|
|
Ecore_Pipe_Cb handler;
|
|
|
|
unsigned int len;
|
|
|
|
int handling;
|
|
|
|
size_t already_read;
|
|
|
|
void *passed_data;
|
|
|
|
int message;
|
|
|
|
Eina_Bool delete_me : 1;
|
|
|
|
};
|
|
|
|
GENERIC_ALLOC_SIZE_DECLARE(Ecore_Pipe);
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
static Eina_Bool _ecore_pipe_read(void *data,
|
|
|
|
Ecore_Fd_Handler *fd_handler);
|
2010-10-17 00:03:28 -07:00
|
|
|
|
|
|
|
/**
|
2011-08-10 12:14:48 -07:00
|
|
|
* @addtogroup Ecore_Pipe_Group
|
2008-11-11 23:54:25 -08:00
|
|
|
*
|
2011-08-10 12:14:48 -07:00
|
|
|
* @{
|
2008-11-11 23:54:25 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create two file descriptors (sockets on Windows). Add
|
|
|
|
* a callback that will be called when the file descriptor that
|
|
|
|
* is listened receives data. An event is also put in the event
|
|
|
|
* queue when data is received.
|
|
|
|
*
|
|
|
|
* @param handler The handler called when data is received.
|
|
|
|
* @param data Data to pass to @p handler when it is called.
|
|
|
|
* @return A newly created Ecore_Pipe object if successful.
|
|
|
|
* @c NULL otherwise.
|
|
|
|
*/
|
|
|
|
EAPI Ecore_Pipe *
|
2011-10-20 22:40:39 -07:00
|
|
|
ecore_pipe_add(Ecore_Pipe_Cb handler,
|
|
|
|
const void *data)
|
2008-11-11 23:54:25 -08:00
|
|
|
{
|
|
|
|
Ecore_Pipe *p;
|
2011-10-20 22:40:39 -07:00
|
|
|
int fds[2];
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2009-08-12 17:27:53 -07:00
|
|
|
if (!handler) return NULL;
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2011-12-02 19:39:07 -08:00
|
|
|
p = ecore_pipe_calloc(1);
|
2009-08-12 17:27:53 -07:00
|
|
|
if (!p) return NULL;
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
if (pipe(fds))
|
|
|
|
{
|
2011-12-02 19:39:07 -08:00
|
|
|
ecore_pipe_mp_free(p);
|
2008-11-11 23:54:25 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ECORE_MAGIC_SET(p, ECORE_MAGIC_PIPE);
|
|
|
|
p->fd_read = fds[0];
|
|
|
|
p->fd_write = fds[1];
|
|
|
|
p->handler = handler;
|
|
|
|
p->data = data;
|
|
|
|
|
|
|
|
fcntl(p->fd_read, F_SETFL, O_NONBLOCK);
|
|
|
|
p->fd_handler = ecore_main_fd_handler_add(p->fd_read,
|
2011-10-20 22:40:39 -07:00
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_pipe_read,
|
|
|
|
p,
|
|
|
|
NULL, NULL);
|
2008-11-11 23:54:25 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free an Ecore_Pipe object created with ecore_pipe_add().
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object to be freed.
|
|
|
|
* @return The pointer to the private data
|
|
|
|
*/
|
|
|
|
EAPI void *
|
|
|
|
ecore_pipe_del(Ecore_Pipe *p)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_del");
|
|
|
|
return NULL;
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
2010-10-12 17:35:09 -07:00
|
|
|
p->delete_me = EINA_TRUE;
|
|
|
|
if (p->handling > 0) return (void *)p->data;
|
2011-08-11 22:22:07 -07:00
|
|
|
if (p->fd_handler) _ecore_main_fd_handler_del(p->fd_handler);
|
2009-08-12 17:27:53 -07:00
|
|
|
if (p->fd_read != PIPE_FD_INVALID) pipe_close(p->fd_read);
|
|
|
|
if (p->fd_write != PIPE_FD_INVALID) pipe_close(p->fd_write);
|
2008-11-11 23:54:25 -08:00
|
|
|
data = (void *)p->data;
|
2011-12-02 19:39:07 -08:00
|
|
|
ecore_pipe_mp_free(p);
|
2008-11-11 23:54:25 -08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2009-04-22 18:38:55 -07:00
|
|
|
/**
|
|
|
|
* Close the read end of an Ecore_Pipe object created with ecore_pipe_add().
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object.
|
|
|
|
*/
|
|
|
|
EAPI void
|
2009-04-23 00:08:50 -07:00
|
|
|
ecore_pipe_read_close(Ecore_Pipe *p)
|
2009-04-22 18:38:55 -07:00
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_close");
|
|
|
|
return;
|
2009-04-22 18:38:55 -07:00
|
|
|
}
|
2010-10-12 17:37:12 -07:00
|
|
|
if (p->fd_handler)
|
|
|
|
{
|
2011-08-11 22:22:07 -07:00
|
|
|
_ecore_main_fd_handler_del(p->fd_handler);
|
2010-10-12 17:37:12 -07:00
|
|
|
p->fd_handler = NULL;
|
|
|
|
}
|
|
|
|
if (p->fd_read != PIPE_FD_INVALID)
|
|
|
|
{
|
|
|
|
pipe_close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
}
|
2009-04-22 18:38:55 -07:00
|
|
|
}
|
|
|
|
|
2011-05-30 09:52:19 -07:00
|
|
|
/**
|
2011-05-31 01:05:16 -07:00
|
|
|
* Stop monitoring if necessary the pipe for reading. See ecore_pipe_thaw()
|
2011-05-30 09:52:19 -07:00
|
|
|
* for monitoring it again.
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object.
|
2011-06-09 08:32:08 -07:00
|
|
|
* @since 1.1
|
2011-05-30 09:52:19 -07:00
|
|
|
*/
|
|
|
|
EAPI void
|
|
|
|
ecore_pipe_freeze(Ecore_Pipe *p)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2011-06-06 23:37:25 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_freeze");
|
2011-05-30 09:52:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->fd_handler)
|
|
|
|
{
|
2011-08-11 22:22:07 -07:00
|
|
|
_ecore_main_fd_handler_del(p->fd_handler);
|
2011-05-30 09:52:19 -07:00
|
|
|
p->fd_handler = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start monitoring again the pipe for reading. See ecore_pipe_freeze() for
|
|
|
|
* stopping the monitoring activity. This will not work if
|
|
|
|
* ecore_pipe_read_close() was previously called on the same pipe.
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object.
|
2011-06-09 08:32:08 -07:00
|
|
|
* @since 1.1
|
2011-05-30 09:52:19 -07:00
|
|
|
*/
|
|
|
|
EAPI void
|
2011-05-31 01:05:16 -07:00
|
|
|
ecore_pipe_thaw(Ecore_Pipe *p)
|
2011-05-30 09:52:19 -07:00
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2011-06-06 23:37:25 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_thaw");
|
2011-05-30 09:52:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!p->fd_handler && p->fd_read != PIPE_FD_INVALID)
|
|
|
|
{
|
|
|
|
p->fd_handler = ecore_main_fd_handler_add(p->fd_read,
|
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_pipe_read,
|
|
|
|
p,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 07:54:24 -07:00
|
|
|
/**
|
|
|
|
* @brief Wait from another thread on the read side of a pipe.
|
|
|
|
*
|
|
|
|
* @param p The pipe to watch on.
|
|
|
|
* @param message_count The minimal number of message to wait before exiting.
|
|
|
|
* @param wait The amount of time in second to wait before exiting.
|
|
|
|
* @return the number of message catched during that wait call.
|
2011-06-09 08:32:08 -07:00
|
|
|
* @since 1.1
|
2011-06-09 07:54:24 -07:00
|
|
|
*
|
|
|
|
* Negative value for @p wait means infite wait.
|
|
|
|
*/
|
|
|
|
EAPI int
|
2011-10-20 22:40:39 -07:00
|
|
|
ecore_pipe_wait(Ecore_Pipe *p,
|
|
|
|
int message_count,
|
|
|
|
double wait)
|
2011-06-09 07:54:24 -07:00
|
|
|
{
|
|
|
|
struct timeval tv, *t;
|
|
|
|
fd_set rset;
|
|
|
|
double end = 0.0;
|
|
|
|
double timeout;
|
|
|
|
int ret;
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
if (p->fd_read == PIPE_FD_INVALID)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(p->fd_read, &rset);
|
|
|
|
|
|
|
|
if (wait >= 0.0)
|
|
|
|
end = ecore_time_get() + wait;
|
|
|
|
timeout = wait;
|
|
|
|
|
|
|
|
while (message_count > 0 && (timeout > 0.0 || wait <= 0.0))
|
|
|
|
{
|
|
|
|
if (wait >= 0.0)
|
|
|
|
{
|
|
|
|
/* finite() tests for NaN, too big, too small, and infinity. */
|
2011-10-20 22:40:39 -07:00
|
|
|
if ((!ECORE_FINITE(timeout)) || (timeout == 0.0))
|
|
|
|
{
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
else if (timeout > 0.0)
|
|
|
|
{
|
|
|
|
int sec, usec;
|
2011-06-09 07:54:24 -07:00
|
|
|
#ifdef FIX_HZ
|
2011-10-20 22:40:39 -07:00
|
|
|
timeout += (0.5 / HZ);
|
|
|
|
sec = (int)timeout;
|
|
|
|
usec = (int)((timeout - (double)sec) * 1000000);
|
2011-06-09 07:54:24 -07:00
|
|
|
#else
|
2011-10-20 22:40:39 -07:00
|
|
|
sec = (int)timeout;
|
|
|
|
usec = (int)((timeout - (double)sec) * 1000000);
|
2011-06-09 07:54:24 -07:00
|
|
|
#endif
|
2011-10-20 22:40:39 -07:00
|
|
|
tv.tv_sec = sec;
|
|
|
|
tv.tv_usec = usec;
|
|
|
|
}
|
|
|
|
t = &tv;
|
2011-06-09 07:54:24 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = main_loop_select(p->fd_read + 1, &rset, NULL, NULL, t);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
_ecore_pipe_read(p, NULL);
|
|
|
|
message_count -= p->message;
|
|
|
|
total += p->message;
|
|
|
|
p->message = 0;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (errno != EINTR)
|
|
|
|
{
|
|
|
|
close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait >= 0.0)
|
|
|
|
timeout = end - ecore_time_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2009-04-22 18:38:55 -07:00
|
|
|
/**
|
|
|
|
* Close the write end of an Ecore_Pipe object created with ecore_pipe_add().
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object.
|
|
|
|
*/
|
|
|
|
EAPI void
|
2009-04-23 00:08:50 -07:00
|
|
|
ecore_pipe_write_close(Ecore_Pipe *p)
|
2009-04-22 18:38:55 -07:00
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write_close");
|
|
|
|
return;
|
2009-04-22 18:38:55 -07:00
|
|
|
}
|
2010-10-12 17:37:12 -07:00
|
|
|
if (p->fd_write != PIPE_FD_INVALID)
|
|
|
|
{
|
|
|
|
pipe_close(p->fd_write);
|
|
|
|
p->fd_write = PIPE_FD_INVALID;
|
|
|
|
}
|
2009-04-22 18:38:55 -07:00
|
|
|
}
|
|
|
|
|
2008-11-11 23:54:25 -08:00
|
|
|
/**
|
|
|
|
* Write on the file descriptor the data passed as parameter.
|
|
|
|
*
|
|
|
|
* @param p The Ecore_Pipe object.
|
|
|
|
* @param buffer The data to write into the pipe.
|
|
|
|
* @param nbytes The size of the @p buffer in bytes
|
2010-02-25 21:56:49 -08:00
|
|
|
* @return Returns EINA_TRUE on a successful write, EINA_FALSE on an error
|
2008-11-11 23:54:25 -08:00
|
|
|
*/
|
2010-06-10 04:57:12 -07:00
|
|
|
EAPI Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
ecore_pipe_write(Ecore_Pipe *p,
|
|
|
|
const void *buffer,
|
|
|
|
unsigned int nbytes)
|
2008-11-11 23:54:25 -08:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
2011-10-20 22:40:39 -07:00
|
|
|
size_t already_written = 0;
|
|
|
|
int retry = ECORE_PIPE_WRITE_RETRY;
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write");
|
|
|
|
return EINA_FALSE;
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
2009-04-22 18:38:55 -07:00
|
|
|
|
2010-10-12 17:37:12 -07:00
|
|
|
if (p->delete_me) return EINA_FALSE;
|
2011-04-20 07:15:33 -07:00
|
|
|
|
2010-02-25 21:56:49 -08:00
|
|
|
if (p->fd_write == PIPE_FD_INVALID) return EINA_FALSE;
|
2009-04-22 18:38:55 -07:00
|
|
|
|
|
|
|
/* First write the len into the pipe */
|
2008-11-11 23:54:25 -08:00
|
|
|
do
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ret = pipe_write(p->fd_write, &nbytes, sizeof(nbytes));
|
|
|
|
if (ret == sizeof(nbytes))
|
|
|
|
{
|
|
|
|
retry = ECORE_PIPE_WRITE_RETRY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret > 0)
|
|
|
|
{
|
|
|
|
/* XXX What should we do here? */
|
2011-10-20 22:40:39 -07:00
|
|
|
ERR("The length of the data was not written complete"
|
|
|
|
" to the pipe");
|
|
|
|
return EINA_FALSE;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
|
|
|
else if (ret == PIPE_FD_ERROR && errno == EPIPE)
|
|
|
|
{
|
|
|
|
pipe_close(p->fd_write);
|
|
|
|
p->fd_write = PIPE_FD_INVALID;
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
else if (ret == PIPE_FD_ERROR && errno == EINTR)
|
|
|
|
/* try it again */
|
|
|
|
;
|
|
|
|
else
|
|
|
|
{
|
2012-02-18 04:55:26 -08:00
|
|
|
ERR("An unhandled error (ret: %zd errno: %d)"
|
2010-09-29 23:09:20 -07:00
|
|
|
"occurred while writing to the pipe the length",
|
|
|
|
ret, errno);
|
|
|
|
}
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
|
|
|
while (retry--);
|
|
|
|
|
2010-02-25 21:56:49 -08:00
|
|
|
if (retry != ECORE_PIPE_WRITE_RETRY) return EINA_FALSE;
|
2008-11-11 23:54:25 -08:00
|
|
|
|
|
|
|
/* and now pass the data to the pipe */
|
|
|
|
do
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ret = pipe_write(p->fd_write,
|
2009-08-12 17:27:53 -07:00
|
|
|
((unsigned char *)buffer) + already_written,
|
|
|
|
nbytes - already_written);
|
2011-04-20 07:15:33 -07:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
if (ret == (ssize_t)(nbytes - already_written))
|
|
|
|
return EINA_TRUE;
|
|
|
|
else if (ret >= 0)
|
|
|
|
{
|
|
|
|
already_written -= ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ret == PIPE_FD_ERROR && errno == EPIPE)
|
|
|
|
{
|
|
|
|
pipe_close(p->fd_write);
|
|
|
|
p->fd_write = PIPE_FD_INVALID;
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
else if (ret == PIPE_FD_ERROR && errno == EINTR)
|
|
|
|
/* try it again */
|
|
|
|
;
|
|
|
|
else
|
|
|
|
{
|
2012-02-18 04:55:26 -08:00
|
|
|
ERR("An unhandled error (ret: %zd errno: %d)"
|
2010-09-29 23:09:20 -07:00
|
|
|
"occurred while writing to the pipe the length",
|
|
|
|
ret, errno);
|
|
|
|
}
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
|
|
|
while (retry--);
|
|
|
|
|
2010-02-25 21:56:49 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
|
|
|
|
2010-10-17 00:03:28 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2008-11-11 23:54:25 -08:00
|
|
|
/* Private function */
|
2010-10-12 17:35:09 -07:00
|
|
|
static void
|
|
|
|
_ecore_pipe_unhandle(Ecore_Pipe *p)
|
|
|
|
{
|
|
|
|
p->handling--;
|
|
|
|
if (p->delete_me)
|
|
|
|
{
|
|
|
|
ecore_pipe_del(p);
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2010-06-24 09:15:56 -07:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_pipe_read(void *data,
|
|
|
|
Ecore_Fd_Handler *fd_handler __UNUSED__)
|
2008-11-11 23:54:25 -08:00
|
|
|
{
|
2011-10-20 22:40:39 -07:00
|
|
|
Ecore_Pipe *p = (Ecore_Pipe *)data;
|
|
|
|
int i;
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2010-10-12 17:35:09 -07:00
|
|
|
p->handling++;
|
|
|
|
for (i = 0; i < 16; i++)
|
2008-11-11 23:54:25 -08:00
|
|
|
{
|
2011-10-20 22:40:39 -07:00
|
|
|
ssize_t ret;
|
2010-09-29 23:09:20 -07:00
|
|
|
|
|
|
|
/* if we already have read some data we don't need to read the len
|
|
|
|
* but to finish the already started job
|
|
|
|
*/
|
|
|
|
if (p->len == 0)
|
|
|
|
{
|
|
|
|
/* read the len of the passed data */
|
2011-10-20 22:40:39 -07:00
|
|
|
ret = pipe_read(p->fd_read, &p->len, sizeof(p->len));
|
|
|
|
|
|
|
|
/* catch the non error case first */
|
|
|
|
/* read amount ok - nothing more to do */
|
|
|
|
if (ret == sizeof(p->len))
|
|
|
|
;
|
|
|
|
else if (ret > 0)
|
|
|
|
{
|
|
|
|
/* we got more data than we asked for - definite error */
|
|
|
|
ERR("Only read %i bytes from the pipe, although"
|
|
|
|
" we need to read %i bytes.",
|
|
|
|
(int)ret, (int)sizeof(p->len));
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
/* we got no data */
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
/* no data on first try through means an error */
|
|
|
|
if (!p->delete_me)
|
|
|
|
p->handler((void *)p->data, NULL, 0);
|
|
|
|
if (p->passed_data) free(p->passed_data);
|
|
|
|
p->passed_data = NULL;
|
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
|
|
|
p->message++;
|
|
|
|
pipe_close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
p->fd_handler = NULL;
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no data after first loop try is ok */
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
}
|
2009-10-25 00:02:03 -07:00
|
|
|
#ifndef _WIN32
|
2011-10-20 22:40:39 -07:00
|
|
|
else if ((ret == PIPE_FD_ERROR) &&
|
|
|
|
((errno == EINTR) || (errno == EAGAIN)))
|
|
|
|
{
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("An unhandled error (ret: %i errno: %i [%s])"
|
|
|
|
"occurred while reading from the pipe the length",
|
|
|
|
(int)ret, errno, strerror(errno));
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
2009-10-25 00:02:03 -07:00
|
|
|
#else
|
2011-10-20 22:40:39 -07:00
|
|
|
else /* ret == PIPE_FD_ERROR is the only other case on Windows */
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
{
|
|
|
|
if (!p->delete_me)
|
2011-01-20 22:25:36 -08:00
|
|
|
p->handler((void *)p->data, NULL, 0);
|
2011-10-20 22:40:39 -07:00
|
|
|
if (p->passed_data) free(p->passed_data);
|
|
|
|
p->passed_data = NULL;
|
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
|
|
|
p->message++;
|
|
|
|
pipe_close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
p->fd_handler = NULL;
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
}
|
2009-10-25 00:02:03 -07:00
|
|
|
#endif
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2011-07-06 23:23:46 -07:00
|
|
|
/* if somehow we got less than or equal to 0 we got an errnoneous
|
2011-07-18 03:34:25 -07:00
|
|
|
* messages so call callback with null and len we got. this case should
|
|
|
|
* never happen */
|
|
|
|
if (p->len == 0)
|
2011-07-06 23:23:46 -07:00
|
|
|
{
|
|
|
|
if (!p->delete_me)
|
2011-10-20 22:40:39 -07:00
|
|
|
p->handler((void *)p->data, NULL, 0);
|
2011-07-06 23:23:46 -07:00
|
|
|
/* reset all values to 0 */
|
|
|
|
if (p->passed_data) free(p->passed_data);
|
|
|
|
p->passed_data = NULL;
|
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
|
|
|
p->message++;
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we dont have a buffer to hold the data, so alloc it */
|
2010-09-29 23:09:20 -07:00
|
|
|
if (!p->passed_data)
|
2011-07-06 23:23:46 -07:00
|
|
|
{
|
|
|
|
p->passed_data = malloc(p->len);
|
|
|
|
/* alloc failed - error case */
|
|
|
|
if (!p->passed_data)
|
|
|
|
{
|
|
|
|
if (!p->delete_me)
|
2011-10-20 22:40:39 -07:00
|
|
|
p->handler((void *)p->data, NULL, 0);
|
|
|
|
/* close the pipe */
|
2011-07-06 23:23:46 -07:00
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
|
|
|
p->message++;
|
|
|
|
pipe_close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
p->fd_handler = NULL;
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 23:54:25 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
/* and read the passed data */
|
|
|
|
ret = pipe_read(p->fd_read,
|
2009-08-12 17:27:53 -07:00
|
|
|
((unsigned char *)p->passed_data) + p->already_read,
|
|
|
|
p->len - p->already_read);
|
2011-04-20 07:15:33 -07:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
/* catch the non error case first */
|
2011-07-06 23:23:46 -07:00
|
|
|
/* if we read enough data to finish the message/buffer */
|
2010-09-29 23:09:20 -07:00
|
|
|
if (ret == (ssize_t)(p->len - p->already_read))
|
|
|
|
{
|
2011-01-20 22:25:36 -08:00
|
|
|
if (!p->delete_me)
|
2011-10-20 22:40:39 -07:00
|
|
|
p->handler((void *)p->data, p->passed_data, p->len);
|
2010-09-29 23:09:20 -07:00
|
|
|
free(p->passed_data);
|
|
|
|
/* reset all values to 0 */
|
|
|
|
p->passed_data = NULL;
|
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
2011-06-09 07:54:24 -07:00
|
|
|
p->message++;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
2011-07-06 23:23:46 -07:00
|
|
|
else if (ret > 0)
|
2010-09-29 23:09:20 -07:00
|
|
|
{
|
2011-07-06 23:23:46 -07:00
|
|
|
/* more data left to read */
|
2011-10-20 22:40:39 -07:00
|
|
|
p->already_read += ret;
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
2011-07-18 03:34:25 -07:00
|
|
|
/* 0 bytes to read - could be more to read next select wake up */
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
2009-10-25 00:02:03 -07:00
|
|
|
#ifndef _WIN32
|
2011-04-20 07:15:33 -07:00
|
|
|
else if ((ret == PIPE_FD_ERROR) &&
|
2011-01-20 22:25:36 -08:00
|
|
|
((errno == EINTR) || (errno == EAGAIN)))
|
2011-07-06 23:23:46 -07:00
|
|
|
{
|
|
|
|
_ecore_pipe_unhandle(p);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
2010-09-29 23:09:20 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("An unhandled error (ret: %zd errno: %d)"
|
|
|
|
"occurred while reading from the pipe the data",
|
|
|
|
ret, errno);
|
2010-10-12 17:35:09 -07:00
|
|
|
_ecore_pipe_unhandle(p);
|
2010-09-29 23:09:20 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
2009-10-25 00:02:03 -07:00
|
|
|
#else
|
|
|
|
else /* ret == PIPE_FD_ERROR is the only other case on Windows */
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
{
|
2011-01-20 22:25:36 -08:00
|
|
|
if (!p->delete_me)
|
2011-10-20 22:40:39 -07:00
|
|
|
p->handler((void *)p->data, NULL, 0);
|
2011-07-06 23:23:46 -07:00
|
|
|
if (p->passed_data) free(p->passed_data);
|
|
|
|
p->passed_data = NULL;
|
|
|
|
p->already_read = 0;
|
|
|
|
p->len = 0;
|
|
|
|
p->message++;
|
2009-10-25 00:02:03 -07:00
|
|
|
pipe_close(p->fd_read);
|
|
|
|
p->fd_read = PIPE_FD_INVALID;
|
|
|
|
p->fd_handler = NULL;
|
2010-10-12 17:35:09 -07:00
|
|
|
_ecore_pipe_unhandle(p);
|
2009-10-25 00:02:03 -07:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-11 23:54:25 -08:00
|
|
|
}
|
2011-04-20 07:15:33 -07:00
|
|
|
|
2010-10-12 17:35:09 -07:00
|
|
|
_ecore_pipe_unhandle(p);
|
2008-11-11 23:54:25 -08:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
2011-10-20 22:40:39 -07:00
|
|
|
|