-2007-03-09 Ralf Habacker <ralf.habacker@freenet.de>
+2007-03-10 Ralf Habacker <ralf.habacker@freenet.de>
+
+ * bus/bus.c, bus/bus.h, bus/main.c, bus/test.c,
+ dbus/dbus-sysdeps-unix.c, dbus/dbus-sysdeps-util-unix.c,
+ dbus/dbus-sysdeps-util-win.c, dbus/dbus-sysdeps-win.c,
+ dbus/dbus-sysdeps.h: renamed _dbus_xxx_pipe to _dbus_pipe_xxx,
+ completed _dbus_pipe support.
+
+2007-03-10 Ralf Habacker <ralf.habacker@freenet.de>
* dbus/dbus-sysdeps.h (_dbus_listen_tcp_socket):
changed type or port to pointer, because the port is given back.
BusContext*
bus_context_new (const DBusString *config_file,
ForceForkSetting force_fork,
- int print_addr_fd,
- int print_pid_fd,
+ DBusPipe print_addr_fd,
+ DBusPipe print_pid_fd,
DBusError *error)
{
BusContext *context;
* other random thing. But I think the answer is "don't do
* that then"
*/
- if (print_addr_fd >= 0)
+ if (_dbus_pipe_is_valid(print_addr_fd))
{
DBusString addr;
const char *a = bus_context_get_address (context);
}
bytes = _dbus_string_get_length (&addr);
- if (_dbus_write_pipe (print_addr_fd, &addr, 0, bytes) != bytes)
+ if (_dbus_pipe_write(print_addr_fd, &addr, 0, bytes) != bytes)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Printing message bus address: %s\n",
goto failed;
}
- if (print_addr_fd > 2)
- _dbus_close_socket (print_addr_fd, NULL);
+ if (_dbus_pipe_is_special(print_addr_fd))
+ _dbus_pipe_close(print_addr_fd, NULL);
_dbus_string_free (&addr);
}
}
/* Write PID if requested */
- if (print_pid_fd >= 0)
+ if (_dbus_pipe_is_valid(print_pid_fd))
{
DBusString pid;
int bytes;
}
bytes = _dbus_string_get_length (&pid);
- if (_dbus_write_pipe (print_pid_fd, &pid, 0, bytes) != bytes)
+ if (_dbus_pipe_write (print_pid_fd, &pid, 0, bytes) != bytes)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Printing message bus PID: %s\n",
goto failed;
}
- if (print_pid_fd > 2)
- _dbus_close_socket (print_pid_fd, NULL);
+ if (_dbus_pipe_is_special (print_pid_fd))
+ _dbus_pipe_close (print_pid_fd, NULL);
_dbus_string_free (&pid);
}
BusContext* bus_context_new (const DBusString *config_file,
ForceForkSetting force_fork,
- int print_addr_fd,
- int print_pid_fd,
+ DBusPipe print_addr_fd,
+ DBusPipe print_pid_fd,
DBusError *error);
dbus_bool_t bus_context_reload_config (BusContext *context,
DBusError *error);
DBusString addr_fd;
DBusString pid_fd;
const char *prev_arg;
- int print_addr_fd;
- int print_pid_fd;
+ DBusPipe print_addr_fd;
+ DBusPipe print_pid_fd;
int i;
dbus_bool_t print_address;
dbus_bool_t print_pid;
usage ();
}
- print_addr_fd = -1;
+ print_addr_fd = _dbus_pipe_init(-1);
if (print_address)
{
- print_addr_fd = 1; /* stdout */
+ print_addr_fd = _dbus_pipe_init(1); /* stdout */
if (_dbus_string_get_length (&addr_fd) > 0)
{
long val;
exit (1);
}
- print_addr_fd = val;
+ print_addr_fd = _dbus_pipe_init(val);
}
}
_dbus_string_free (&addr_fd);
- print_pid_fd = -1;
+ print_pid_fd = _dbus_pipe_init(-1);
if (print_pid)
{
- print_pid_fd = 1; /* stdout */
+ print_pid_fd = _dbus_pipe_init(1); /* stdout */
if (_dbus_string_get_length (&pid_fd) > 0)
{
long val;
exit (1);
}
- print_pid_fd = val;
+ print_pid_fd = _dbus_pipe_init(val);
}
}
_dbus_string_free (&pid_fd);
#include "test.h"
#include <dbus/dbus-internals.h>
#include <dbus/dbus-list.h>
+#include <dbus/dbus-sysdeps.h>
/* The "debug client" watch/timeout handlers don't dispatch messages,
* as we manually pull them in order to verify them. This is why they
DBusString config_file;
DBusString relative;
BusContext *context;
+ DBusPipe pipe;
if (!_dbus_string_init (&config_file))
{
}
dbus_error_init (&error);
- context = bus_context_new (&config_file, FALSE, -1, -1, &error);
+ pipe = _dbus_pipe_init(-1);
+ context = bus_context_new (&config_file, FALSE, pipe, pipe, &error);
if (context == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (&error);
return _dbus_write (fd, buffer, start, len);
}
+/**
+ * init a pipe instance.
+ *
+ * @param fd the file descriptor to init from
+ * @returns a DBusPipe instance
+ */
+DBusPipe _dbus_pipe_init(int fd)
+{
+ DBusPipe pipe;
+ pipe.fd = fd;
+ return pipe;
+}
+
+/**
+ * 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
+ * @returns the number of bytes written or -1 on error
+ */
int
-_dbus_write_pipe (DBusPipe pipe,
+_dbus_pipe_write (DBusPipe pipe,
const DBusString *buffer,
int start,
int len)
{
- return _dbus_write (pipe, buffer, start, len);
+ return _dbus_write (pipe.fd, buffer, start, len);
+}
+
+/**
+ * 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)
+{
+ return _dbus_close (pipe.fd, error);
+}
+
+/**
+ * check if a pipe is valid, which means is constructed
+ * by a valid 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 >= 0;
}
/**
+ * check if a pipe is a special pipe, which means using
+ * a non default file descriptor (>2)
+ *
+ * @param pipe the pipe instance
+ * @returns #FALSE if pipe is not a special pipe
+ */
+dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe)
+{
+ return pipe.fd > 2;
+}
+
+
+/**
* Like _dbus_write_two() but only works on sockets and is thus
* available on Windows.
*
*/
dbus_bool_t
_dbus_become_daemon (const DBusString *pidfile,
- int print_pid_fd,
+ DBusPipe print_pid_fd,
DBusError *error)
{
const char *s;
}
bytes = _dbus_string_get_length (&pid);
- if (_dbus_write_socket (print_pid_fd, &pid, 0, bytes) != bytes)
+ if (_dbus_pipe_write (print_pid_fd, &pid, 0, bytes) != bytes)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Printing message bus PID: %s\n",
*/
dbus_bool_t
_dbus_become_daemon (const DBusString *pidfile,
- int print_pid_fd,
+ DBusPipe print_pid_fd,
DBusError *error)
{
return TRUE;
return fstat(file->FDATA, sb) >= 0;
}
+/**
+ * init a pipe instance.
+ *
+ * @param fd the file descriptor to init from
+ * @returns a DBusPipe instance
+ */
+DBusPipe _dbus_pipe_init(int fd)
+{
+ DBusPipe pipe;
+ pipe.fd = fd;
+ return pipe;
+}
+
+/**
+ * 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
+ * @returns the number of bytes written or -1 on error
+ */
int
-_dbus_write_pipe (DBusPipe pipe,
+_dbus_pipe_write (DBusPipe pipe,
const DBusString *buffer,
int start,
int len)
{
DBusFile file;
- file.FDATA = pipe;
+ file.FDATA = pipe.fd;
return _dbus_write_file(&file, buffer, start, len);
}
+/**
+ * read data from a pipe.
+ *
+ * @param pipe the pipe instance
+ * @param buffer the buffer to read data in
+ * @param count the number of bytes to try to read
+ * @returns the number of bytes read or -1 on error
+ */
int
-_dbus_read_pipe(DBusPipe pipe,
+_dbus_pipe_read(DBusPipe pipe,
DBusString *buffer,
int count)
{
DBusFile file;
- file.FDATA = pipe;
+ file.FDATA = pipe.fd;
return _dbus_read_file(&file, buffer, count);
}
+/**
+ * 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)
+{
+ DBusFile file;
+ file.FDATA = pipe.fd;
+ return _dbus_close_file(&file, error);
+}
+
+/**
+ * check if a pipe is valid, which means is constructed
+ * by a valid 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 >= 0;
+}
+
+/**
+ * check if a pipe is a special pipe, which means using
+ * a non default file descriptor (>2)
+ *
+ * @param pipe the pipe instance
+ * @returns #FALSE if pipe is not a special pipe
+ */
+dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe)
+{
+ return pipe.fd > 2;
+}
+
#undef FDATA
/**
dbus_bool_t _dbus_get_standard_session_servicedirs (DBusList **dirs);
-typedef int DBusPipe;
-int _dbus_write_pipe (DBusPipe pipe,
+typedef struct {
+ int fd;
+} DBusPipe;
+
+DBusPipe _dbus_pipe_init(int fd);
+
+int _dbus_pipe_write (DBusPipe pipe,
const DBusString *buffer,
int start,
int len);
+int _dbus_pipe_close (DBusPipe pipe,
+ DBusError *error);
+
+dbus_bool_t _dbus_pipe_is_valid(DBusPipe pipe);
+dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe);
+
/** Opaque type for reading a directory listing */
typedef struct DBusDirIter DBusDirIter;
void _dbus_print_backtrace (void);
dbus_bool_t _dbus_become_daemon (const DBusString *pidfile,
- int print_pid_fd,
+ DBusPipe print_pid_fd,
DBusError *error);
dbus_bool_t _dbus_write_pid_file (const DBusString *filename,
unsigned long pid,