From 8a4429ff863d0b869b3f924e78c23a9922ad92b0 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Fri, 18 Dec 2009 16:54:44 +0100 Subject: [PATCH] added pipe related files real --- dbus/dbus-pipe-unix.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-pipe-win.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-pipe.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-pipe.h | 52 +++++++++++++++++++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 dbus/dbus-pipe-unix.c create mode 100644 dbus/dbus-pipe-win.c create mode 100644 dbus/dbus-pipe.c create mode 100644 dbus/dbus-pipe.h diff --git a/dbus/dbus-pipe-unix.c b/dbus/dbus-pipe-unix.c new file mode 100644 index 0000000..f3f5e5e --- /dev/null +++ b/dbus/dbus-pipe-unix.c @@ -0,0 +1,81 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-pipe-unix.c unix related pipe implementation + * + * Copyright (C) 2002, 2003, 2006 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "dbus-protocol.h" +#include "dbus-string.h" +#include "dbus-internals.h" +#include "dbus-pipe.h" + +#include + +/** + * write data to a pipe. + * + * @param pipe the pipe instance + * @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, + const DBusString *buffer, + int start, + int len, + DBusError *error) +{ + 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; +} + +/** + * close a pipe. + * + * @param pipe the pipe instance + * @param error return location for an error + * @returns #FALSE if error is set + */ +int +_dbus_pipe_close (DBusPipe *pipe, + DBusError *error) +{ + if (_dbus_close (pipe->fd_or_handle, error) < 0) + { + return -1; + } + else + { + _dbus_pipe_invalidate (pipe); + return 0; + } +} diff --git a/dbus/dbus-pipe-win.c b/dbus/dbus-pipe-win.c new file mode 100644 index 0000000..c2917c6 --- /dev/null +++ b/dbus/dbus-pipe-win.c @@ -0,0 +1,86 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-pipe-win.c windows related pipe implementation + * + * Copyright (C) 2002, 2003, 2006 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "dbus-protocol.h" +#include "dbus-string.h" +#include "dbus-internals.h" +#include "dbus-pipe.h" + +#include + +/** + * write data to a pipe. + * + * @param pipe the pipe instance + * @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, + const DBusString *buffer, + int start, + int len, + DBusError *error) +{ + int written; + const char *buffer_c = _dbus_string_get_const_data (buffer); + + written = _write (pipe->fd_or_handle, buffer_c + start, len); + if (written < 0) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Writing to pipe: %s\n", + strerror (errno)); + } + return written; +} + +/** + * close a pipe. + * + * @param pipe the pipe instance + * @param error return location for an error + * @returns #FALSE if error is set + */ +int +_dbus_pipe_close (DBusPipe *pipe, + DBusError *error) +{ + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (_close (pipe->fd_or_handle) < 0) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno)); + return -1; + } + else + { + _dbus_pipe_invalidate (pipe); + return 0; + } +} diff --git a/dbus/dbus-pipe.c b/dbus/dbus-pipe.c new file mode 100644 index 0000000..59cf934 --- /dev/null +++ b/dbus/dbus-pipe.c @@ -0,0 +1,84 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-pipe.c pipe implementation (internal to D-Bus implementation) + * + * Copyright (C) 2002, 2003, 2006 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "dbus-pipe.h" + +/* + * init a pipe instance. + * + * @param pipe the pipe + * @param fd the file descriptor to init from + */ +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) +{ + _dbus_pipe_init (pipe, 1); +} + +/** + * 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) +{ + return pipe->fd_or_handle >= 0; +} + +/** + * Check if a pipe is stdout or stderr. + * + * @param pipe the pipe instance + * @returns #TRUE if pipe is one of the standard out/err channels + */ +dbus_bool_t +_dbus_pipe_is_stdout_or_stderr (DBusPipe *pipe) +{ + 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; +} diff --git a/dbus/dbus-pipe.h b/dbus/dbus-pipe.h new file mode 100644 index 0000000..75b7d2f --- /dev/null +++ b/dbus/dbus-pipe.h @@ -0,0 +1,52 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-sysdeps.h Wrappers around system/libc features (internal to D-Bus implementation) + * + * Copyright (C) 2002, 2003 Red Hat, Inc. + * Copyright (C) 2003 CodeFactory AB + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_PIPE_H +#define DBUS_PIPE_H + +#include + +#include +#include +#include + +typedef struct { + int fd_or_handle; +} DBusPipe; + +void _dbus_pipe_init (DBusPipe *pipe, + int fd); +void _dbus_pipe_init_stdout (DBusPipe *pipe); +int _dbus_pipe_write (DBusPipe *pipe, + const DBusString *buffer, + int start, + int len, + DBusError *error); +int _dbus_pipe_close (DBusPipe *pipe, + DBusError *error); +dbus_bool_t _dbus_pipe_is_valid (DBusPipe *pipe); +void _dbus_pipe_invalidate (DBusPipe *pipe); +dbus_bool_t _dbus_pipe_is_stdout_or_stderr (DBusPipe *pipe); + +#endif -- 2.7.4