int ret;
struct GNUNET_SIGNAL_Context *shc_chld;
- sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
+ sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
GNUNET_assert (sigpipe != NULL);
shc_chld =
GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
if (filename_expanded == NULL)\r
return NULL;\r
\r
- progress_pipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);\r
+ progress_pipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);\r
if (progress_pipe == NULL)\r
{\r
GNUNET_free (filename_expanded);\r
* @return handle to the new pipe, NULL on error
*/
struct GNUNET_DISK_PipeHandle *
-GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write);
+GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write);
/**
* @return handle to the new pipe, NULL on error
*/
struct GNUNET_DISK_PipeHandle *
-GNUNET_DISK_pipe_from_fd (int blocking, int fd[2]);
+GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]);
/**
* Closes an interprocess channel
GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
size_t len);
+/**
+ * Read the contents of a binary file into a buffer.
+ * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
+ * when no data can be read).
+ *
+ * @param h handle to an open file
+ * @param result the buffer to write the result to
+ * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
+ */
+ssize_t
+GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
+ void *result, size_t len);
/**
* Read the contents of a binary file into a buffer.
const void *buffer, size_t n);
+/**
+ * Write a buffer to a file, blocking, if necessary.
+ * @param h handle to open file
+ * @param buffer the data to write
+ * @param n number of bytes to write
+ * @return number of bytes written on success, GNUNET_SYSERR on error
+ */
+ssize_t
+GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
+ const void *buffer, size_t n);
+
/**
* Write a buffer to a file. If the file is longer than
* the given buffer size, it will be truncated.
(h->internal_address != NULL) &&
(NULL !=
(h->server_stdout =
- GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES))))
+ GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES))))
{
#if DEBUG_NAT
LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting `%s' at `%s'\n",
eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle));
eh->cb = cb;
eh->cb_cls = cb_cls;
- eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (NULL == eh->opipe)
{
GNUNET_free (eh);
/* Start create hostkey process if we don't already know the peer identity! */
if (GNUNET_NO == d->have_hostkey)
{
- d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
+ d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
if (d->pipe_stdout == NULL)
{
cb = d->cb;
return GNUNET_YES;
}
- plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (plugin->server_stdout == NULL)
return GNUNET_SYSERR;
- plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
+ plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
if (plugin->server_stdin == NULL)
return GNUNET_SYSERR;
#endif
}
+/**
+ * Read the contents of a binary file into a buffer.
+ * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
+ * when no data can be read).
+ *
+ * @param h handle to an open file
+ * @param result the buffer to write the result to
+ * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
+ */
+ssize_t
+GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
+ void *result, size_t len)
+{
+ if (h == NULL)
+ {
+ errno = EINVAL;
+ return GNUNET_SYSERR;
+ }
+
+#ifdef MINGW
+ DWORD bytesRead;
+
+ if (h->type != GNUNET_PIPE)
+ {
+ if (!ReadFile (h->h, result, len, &bytesRead, NULL))
+ {
+ SetErrnoFromWinError (GetLastError ());
+ return GNUNET_SYSERR;
+ }
+ }
+ else
+ {
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe, trying to read\n");
+#endif
+ if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead))
+ {
+ if (GetLastError () != ERROR_IO_PENDING)
+ {
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ());
+#endif
+ SetErrnoFromWinError (GetLastError ());
+ return GNUNET_SYSERR;
+ }
+ else
+ {
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "ReadFile() queued a read, cancelling\n");
+#endif
+ CancelIo (h->h);
+ errno = EAGAIN;
+ return GNUNET_SYSERR;
+ }
+ }
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead);
+#endif
+ }
+ return bytesRead;
+#else
+ /* FIXME: set to non-blocking (fcntl?), read, then set back? */
+ return read (h->fd, result, len);
+#endif
+}
+
/**
* Read the contents of a binary file into a buffer.
#endif
}
+/**
+ * Write a buffer to a file, blocking, if necessary.
+ * @param h handle to open file
+ * @param buffer the data to write
+ * @param n number of bytes to write
+ * @return number of bytes written on success, GNUNET_SYSERR on error
+ */
+ssize_t
+GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
+ const void *buffer, size_t n)
+{
+ if (h == NULL)
+ {
+ errno = EINVAL;
+ return GNUNET_SYSERR;
+ }
+
+#ifdef MINGW
+ DWORD bytesWritten;
+ /* We do a non-overlapped write, which is as blocking as it gets */
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n);
+#endif
+ if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
+ {
+ SetErrnoFromWinError (GetLastError ());
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
+ GetLastError ());
+#endif
+ return GNUNET_SYSERR;
+ }
+ if (bytesWritten == 0 && n > 0)
+ {
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n");
+#endif
+ WaitForSingleObject (h->h, INFINITE);
+ if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
+ {
+ SetErrnoFromWinError (GetLastError ());
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n",
+ GetLastError ());
+#endif
+ return GNUNET_SYSERR;
+ }
+ }
+#if DEBUG_PIPE
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten);
+#endif
+ return bytesWritten;
+#else
+ /* FIXME: switch to blocking mode (fcntl?), write, then switch back? */
+ return write (h->fd, buffer, n);
+#endif
+}
+
/**
* Write a buffer to a file. If the file is longer than the
* number of bytes that will be written, it will be truncated.
* @return handle to the new pipe, NULL on error
*/
struct GNUNET_DISK_PipeHandle *
-GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
+GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write)
{
struct GNUNET_DISK_PipeHandle *p;
struct GNUNET_DISK_FileHandle *fds;
p->fd[1]->fd = fd[1];
ret = 0;
flags = fcntl (fd[0], F_GETFL);
- if (!blocking)
+ if (!blocking_read)
flags |= O_NONBLOCK;
if (0 > fcntl (fd[0], F_SETFL, flags))
ret = -1;
ret = -1;
flags = fcntl (fd[1], F_GETFL);
- if (!blocking)
+ if (!blocking_write)
flags |= O_NONBLOCK;
if (0 > fcntl (fd[1], F_SETFL, flags))
ret = -1;
ret =
create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0,
- FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED);
+ blocking_read ? 0 : FILE_FLAG_OVERLAPPED,
+ blocking_write ? 0 : FILE_FLAG_OVERLAPPED);
if (!ret)
{
GNUNET_free (p);
}
CloseHandle (p->fd[1]->h);
p->fd[1]->h = tmp_handle;
- if (!blocking)
- {
- DWORD mode;
- mode = PIPE_NOWAIT;
- SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
- SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
- /* this always fails on Windows 95, so we don't care about error handling */
- }
p->fd[0]->type = GNUNET_PIPE;
p->fd[1]->type = GNUNET_PIPE;
* @return handle to the new pipe, NULL on error
*/
struct GNUNET_DISK_PipeHandle *
-GNUNET_DISK_pipe_from_fd (int blocking, int fd[2])
+GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
{
struct GNUNET_DISK_PipeHandle *p;
struct GNUNET_DISK_FileHandle *fds;
if (fd[0] >= 0)
{
flags = fcntl (fd[0], F_GETFL);
- if (!blocking)
+ if (!blocking_read)
flags |= O_NONBLOCK;
if (0 > fcntl (fd[0], F_SETFL, flags))
ret = -1;
if (fd[1] >= 0)
{
flags = fcntl (fd[1], F_GETFL);
- if (!blocking)
+ if (!blocking_write)
flags |= O_NONBLOCK;
if (0 > fcntl (fd[1], F_SETFL, flags))
ret = -1;
else
p->fd[1]->h = INVALID_HANDLE_VALUE;
- if (!blocking)
- {
- DWORD mode;
-
- mode = PIPE_NOWAIT;
- if (p->fd[0]->h != INVALID_HANDLE_VALUE)
- SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
- if (p->fd[1]->h != INVALID_HANDLE_VALUE)
- SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
- /* this always fails on Windows 95, so we don't care about error handling */
- }
-
if (p->fd[0]->h != INVALID_HANDLE_VALUE)
{
p->fd[0]->type = GNUNET_PIPE;
static void
start_helper (struct GNUNET_HELPER_Handle *h)
{
- h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
- h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
+ h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if ( (h->helper_in == NULL) || (h->helper_out == NULL))
{
/* out of file descriptors? try again later... */
}
if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
{
- lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
+ lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
if (lsocks_pipe == NULL)
{
struct GNUNET_DISK_PipeHandle *opipe;
va_list ap;
- opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (NULL == opipe)
return NULL;
va_start (ap, binary);
rs = GNUNET_NETWORK_fdset_create ();
ws = GNUNET_NETWORK_fdset_create ();
GNUNET_assert (shutdown_pipe_handle == NULL);
- shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
+ shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
GNUNET_assert (shutdown_pipe_handle != NULL);
pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
GNUNET_DISK_PIPE_END_READ);
{
const struct GNUNET_DISK_FileHandle *stdout_read_handle;
- pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (pipe_stdout == NULL)
{
GNUNET_asprintf (&fn, "cat");
- hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
- hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+ hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
+ hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
{
GNUNET_assert (5 == *ok);
(*ok) = 6;
- p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
+ p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
GNUNET_assert (NULL != p);
fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);