summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-sysdeps-unix.c
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2007-03-12 22:52:40 +0000
committerHavoc Pennington <hp@redhat.com>2007-03-12 22:52:40 +0000
commit9362aac398e3f2ec680e30c61ebfcb1e407eff72 (patch)
treefaeb94c89d680598d1a809f6a3d952910dd3c741 /dbus/dbus-sysdeps-unix.c
parentcc0aea750cb03ffa6a9e94e493455920ab3e612b (diff)
2007-03-11 Havoc Pennington <hp@redhat.com>
* tools/dbus-launch.c (do_close_stderr): fix C89 problem and formatting problem * Mostly fix the DBusPipe mess. - put line break after function return types - put space before parens - do not pass structs around by value - don't use dbus_strerror after calling supposedly cross-platform api - don't name pipe variables "fd" - abstract special fd numbers like -1 and 1
Diffstat (limited to 'dbus/dbus-sysdeps-unix.c')
-rw-r--r--dbus/dbus-sysdeps-unix.c78
1 files changed, 59 insertions, 19 deletions
diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c
index edd4025d..3b265d78 100644
--- a/dbus/dbus-sysdeps-unix.c
+++ b/dbus/dbus-sysdeps-unix.c
@@ -172,14 +172,25 @@ _dbus_write_socket (int fd,
/**
* init a pipe instance.
*
+ * @param pipe the pipe
* @param fd the file descriptor to init from
- * @returns a DBusPipe instance
*/
-DBusPipe _dbus_pipe_init(int fd)
+void
+_dbus_pipe_init (DBusPipe *pipe,
+ int fd)
+{
+ pipe->fd_or_handle = fd;
+}
+
+/**
+ * init a pipe with stdout
+ *
+ * @param pipe the pipe
+ */
+void
+_dbus_pipe_init_stdout (DBusPipe *pipe)
{
- DBusPipe pipe;
- pipe.fd = fd;
- return pipe;
+ _dbus_pipe_init (pipe, 1);
}
/**
@@ -189,15 +200,26 @@ DBusPipe _dbus_pipe_init(int fd)
* @param buffer the buffer to write data from
* @param start the first byte in the buffer to write
* @param len the number of bytes to try to write
+ * @param error error return
* @returns the number of bytes written or -1 on error
*/
int
-_dbus_pipe_write (DBusPipe pipe,
+_dbus_pipe_write (DBusPipe *pipe,
const DBusString *buffer,
int start,
- int len)
+ int len,
+ DBusError *error)
{
- return _dbus_write (pipe.fd, buffer, start, len);
+ int written;
+
+ written = _dbus_write (pipe->fd_or_handle, buffer, start, len);
+ if (written < 0)
+ {
+ dbus_set_error (error, DBUS_ERROR_FAILED,
+ "Writing to pipe: %s\n",
+ _dbus_strerror (errno));
+ }
+ return written;
}
/**
@@ -208,36 +230,54 @@ _dbus_pipe_write (DBusPipe pipe,
* @returns #FALSE if error is set
*/
int
-_dbus_pipe_close (DBusPipe pipe,
+_dbus_pipe_close (DBusPipe *pipe,
DBusError *error)
{
- return _dbus_close (pipe.fd, error);
+ if (_dbus_close (pipe->fd_or_handle, error) < 0)
+ {
+ return -1;
+ }
+ else
+ {
+ _dbus_pipe_invalidate (pipe);
+ return 0;
+ }
}
/**
- * check if a pipe is valid, which means is constructed
- * by a valid file descriptor
+ * check if a pipe is valid; pipes can be set invalid, similar to
+ * a -1 file descriptor.
*
* @param pipe the pipe instance
* @returns #FALSE if pipe is not valid
*/
-dbus_bool_t _dbus_pipe_is_valid(DBusPipe pipe)
+dbus_bool_t
+_dbus_pipe_is_valid(DBusPipe *pipe)
{
- return pipe.fd >= 0;
+ return pipe->fd_or_handle >= 0;
}
/**
- * check if a pipe is a special pipe, which means using
- * a non default file descriptor (>2)
+ * Check if a pipe is stdout or stderr.
*
* @param pipe the pipe instance
- * @returns #FALSE if pipe is not a special pipe
+ * @returns #TRUE if pipe is one of the standard out/err channels
*/
-dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe)
+dbus_bool_t
+_dbus_pipe_is_stdout_or_stderr (DBusPipe *pipe)
{
- return pipe.fd > 2;
+ return pipe->fd_or_handle == 1 || pipe->fd_or_handle == 2;
}
+/**
+ * Initializes a pipe to an invalid value.
+ * @param pipe the pipe
+ */
+void
+_dbus_pipe_invalidate (DBusPipe *pipe)
+{
+ pipe->fd_or_handle = -1;
+}
/**
* Like _dbus_write_two() but only works on sockets and is thus