free(b->wqueue);
b->rqueue = b->wqueue = NULL;
+ b->rqueue_allocated = b->wqueue_allocated = 0;
b->rqueue_size = b->wqueue_size = 0;
}
/* We guarantee that wqueue always has space for at least one
* entry */
- r->wqueue = new(sd_bus_message*, 1);
- if (!r->wqueue) {
+ if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
free(r);
return -ENOMEM;
}
}
static int parse_address_key(const char **p, const char *key, char **value) {
- size_t l, n = 0;
+ size_t l, n = 0, allocated = 0;
const char *a;
char *r = NULL;
a = *p;
while (*a != ';' && *a != ',' && *a != 0) {
- char c, *t;
+ char c;
if (*a == '%') {
int x, y;
a++;
}
- t = realloc(r, n + 2);
- if (!t) {
- free(r);
+ if (!GREEDY_REALLOC(r, allocated, n + 2))
return -ENOMEM;
- }
- r = t;
r[n++] = c;
}
char *path = NULL;
unsigned n_argv = 0, j;
char **argv = NULL;
+ size_t allocated = 0;
int r;
assert(b);
(*p) ++;
if (ul >= n_argv) {
- char **x;
-
- x = realloc(argv, sizeof(char*) * (ul + 2));
- if (!x) {
+ if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
r = -ENOMEM;
goto fail;
}
- memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
-
- argv = x;
n_argv = ul + 1;
}
return 0;
}
-static int parse_container_address(sd_bus *b, const char **p, char **guid) {
+static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
_cleanup_free_ char *machine = NULL;
int r;
return 0;
}
+static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) {
+ _cleanup_free_ char *machine = NULL;
+ int r;
+
+ assert(b);
+ assert(p);
+ assert(*p);
+ assert(guid);
+
+ while (**p != 0 && **p != ';') {
+ r = parse_address_key(p, "guid", guid);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(p, "machine", &machine);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ skip_address_key(p);
+ }
+
+ if (!machine)
+ return -EINVAL;
+
+ if (!filename_is_safe(machine))
+ return -EINVAL;
+
+ free(b->machine);
+ b->machine = machine;
+ machine = NULL;
+
+ free(b->kernel);
+ b->kernel = strdup("/dev/kdbus/0-system/bus");
+ if (!b->kernel)
+ return -ENOMEM;
+
+ return 0;
+}
+
static void bus_reset_parsed_address(sd_bus *b) {
assert(b);
return r;
break;
- } else if (startswith(a, "x-container:")) {
+ } else if (startswith(a, "x-container-unix:")) {
+
+ a += 17;
+ r = parse_container_unix_address(b, &a, &guid);
+ if (r < 0)
+ return r;
+
+ break;
+ } else if (startswith(a, "x-container-kernel:")) {
- a += 12;
- r = parse_container_address(b, &a, &guid);
+ a += 19;
+ r = parse_container_kernel_address(b, &a, &guid);
if (r < 0)
return r;
if (b->exec_path)
r = bus_socket_exec(b);
+ else if (b->machine && b->kernel)
+ r = bus_container_connect_kernel(b);
+ else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
+ r = bus_container_connect_socket(b);
else if (b->kernel)
r = bus_kernel_connect(b);
- else if (b->machine)
- r = bus_container_connect(b);
else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
r = bus_socket_connect(b);
else
return -ENOMEM;
#ifdef ENABLE_KDBUS
- p = strjoin("kernel:path=/dev/kdbus/ns/machine-", e, "/0-system/bus;x-container:machine=", e, NULL);
+ p = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
#else
- p = strjoin("x-container:machine=", e, NULL);
+ p = strjoin("x-container-unix:machine=", e, NULL);
#endif
if (!p)
return -ENOMEM;
}
int bus_rqueue_make_room(sd_bus *bus) {
- sd_bus_message **q;
- unsigned x;
-
- x = bus->rqueue_size + 1;
-
- if (bus->rqueue_allocated >= x)
- return 0;
+ assert(bus);
- if (x > BUS_RQUEUE_MAX)
+ if (bus->rqueue_size >= BUS_RQUEUE_MAX)
return -ENOBUFS;
- q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
- if (!q)
+ if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
return -ENOMEM;
- bus->rqueue = q;
- bus->rqueue_allocated = x;
-
return 0;
}
bus->windex = idx;
}
} else {
- sd_bus_message **q;
-
/* Just append it to the queue. */
if (bus->wqueue_size >= BUS_WQUEUE_MAX)
return -ENOBUFS;
- q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
- if (!q)
+ if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
return -ENOMEM;
- bus->wqueue = q;
- q[bus->wqueue_size ++] = sd_bus_message_ref(m);
+ bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m);
}
if (serial)