#include <netdb.h>
#include <poll.h>
#include <pthread.h>
+//#include <signal.h>
#include <stdlib.h>
#include <sys/mman.h>
+//#include <sys/wait.h>
#include <unistd.h>
#include "sd-bus.h"
if (!a)
return -ENOMEM;
- free_and_replace(bus->address, a);
+ free(bus->address);
+ bus->address = a;
return 0;
}
return -ENOMEM;
}
- free_and_replace(bus->exec_path, p);
-
+ free(bus->exec_path);
strv_free(bus->exec_argv);
+
+ bus->exec_path = p;
bus->exec_argv = a;
return 0;
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- bus->is_monitor = !!b;
+ bus->is_monitor = b;
return 0;
}
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- bus->accept_fd = !!b;
+ bus->accept_fd = b;
return 0;
}
/* This is not actually supported by any of our transports these days, but we do honour it for synthetic
* replies, and maybe one day classic D-Bus learns this too */
- bus->attach_timestamp = !!b;
+ bus->attach_timestamp = b;
return 0;
}
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- bus->watch_bind = !!b;
+ bus->watch_bind = b;
return 0;
}
assert_return(bus->state == BUS_UNSET, -EPERM);
assert_return(!bus_pid_changed(bus), -ECHILD);
- bus->connected_signal = !!b;
+ bus->connected_signal = b;
return 0;
}
static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
const char *s;
- char *t;
sd_bus *bus;
int r;
if (!service_name_is_valid(s) || s[0] != ':')
return -EBADMSG;
- t = strdup(s);
- if (!t)
+ bus->unique_name = strdup(s);
+ if (!bus->unique_name)
return -ENOMEM;
- free_and_replace(bus->unique_name, t);
-
if (bus->state == BUS_HELLO) {
bus_set_state(bus, BUS_RUNNING);
static int parse_address_key(const char **p, const char *key, char **value) {
size_t l, n = 0, allocated = 0;
- _cleanup_free_ char *r = NULL;
const char *a;
+ char *r = NULL;
assert(p);
assert(*p);
int x, y;
x = unhexchar(a[1]);
- if (x < 0)
+ if (x < 0) {
+ free(r);
return x;
+ }
y = unhexchar(a[2]);
- if (y < 0)
+ if (y < 0) {
+ free(r);
return y;
+ }
c = (char) ((x << 4) | y);
a += 3;
*p = a;
- free_and_replace(*value, r);
+ free(*value);
+ *value = r;
return 1;
}
return 1;
}
+static void bus_kill_exec(sd_bus *bus) {
+ if (pid_is_valid(bus->busexec_pid) > 0) {
+ sigterm_wait(bus->busexec_pid);
+ bus->busexec_pid = 0;
+ }
+}
+
static int bus_start_address(sd_bus *b) {
int r;
bus_close_io_fds(b);
bus_close_inotify_fd(b);
+ bus_kill_exec(b);
+
/* If you provide multiple different bus-addresses, we
* try all of them in order and use the first one that
* succeeds. */
int bus_set_address_system_remote(sd_bus *b, const char *host) {
_cleanup_free_ char *e = NULL;
- char *m = NULL, *c = NULL, *a;
+ char *m = NULL, *c = NULL;
assert(b);
assert(host);
return -ENOMEM;
}
- a = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=elogind-stdio-bridge", c);
- if (!a)
+ b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=elogind-stdio-bridge", c);
+ if (!b->address)
return -ENOMEM;
- free_and_replace(b->address, a);
-
return 0;
}
int bus_set_address_system_machine(sd_bus *b, const char *machine) {
_cleanup_free_ char *e = NULL;
- char *a;
assert(b);
assert(machine);
if (!e)
return -ENOMEM;
- a = strjoin("x-machine-unix:machine=", e);
- if (!a)
+ b->address = strjoin("x-machine-unix:machine=", e);
+ if (!b->address)
return -ENOMEM;
- free_and_replace(b->address, a);
-
return 0;
}
if (bus_pid_changed(bus))
return;
+ /* Don't leave ssh hanging around */
+ bus_kill_exec(bus);
+
bus_set_state(bus, BUS_CLOSED);
sd_bus_detach_event(bus);
if (!bus)
return NULL;
+ /* Have to do this before flush() to prevent hang */
+ bus_kill_exec(bus);
+
sd_bus_flush(bus);
sd_bus_close(bus);