test-bus-chat.c revision df2d202e6ed4001a21c6512c244acad5d4706c87
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "sd-bus.h"
#include "bus-message.h"
#include "bus-error.h"
#include "bus-match.h"
#include "bus-internal.h"
#include "bus-util.h"
log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m)));
return 0;
}
int r;
if (sd_bus_message_is_method_error(m, NULL))
return 0;
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
return r;
}
return 1;
}
return 0;
}
int r;
const char *unique;
r = sd_bus_open_user(&bus);
if (r < 0) {
goto fail;
}
if (r < 0) {
goto fail;
}
if (r < 0) {
goto fail;
}
if (r < 0) {
goto fail;
}
if (r < 0) {
goto fail;
}
r = sd_bus_add_match(bus, "type='signal',interface='foo.bar',member='Notify'", match_callback, NULL);
if (r < 0) {
goto fail;
}
r = sd_bus_add_match(bus, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL);
if (r < 0) {
goto fail;
}
return 0;
fail:
if (bus)
return r;
}
int r;
bool client1_gone = false, client2_gone = false;
while (!client1_gone || !client2_gone) {
r = sd_bus_process(bus, &m);
if (r < 0) {
goto fail;
}
if (r == 0) {
if (r < 0) {
goto fail;
}
continue;
}
if (!m)
continue;
sd_bus_message_get_pid(m, &pid);
log_info("Got message! member=%s pid=%lu label=%s",
(unsigned long) pid,
/* bus_message_dump(m); */
/* sd_bus_message_rewind(m, true); */
const char *hello;
if (r < 0) {
goto fail;
}
if (!lowercase) {
r = log_oom();
goto fail;
}
if (r < 0) {
goto fail;
}
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
goto fail;
}
client1_gone = true;
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
goto fail;
}
client2_gone = true;
sleep(1);
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
goto fail;
}
int fd;
static const char x = 'X';
if (r < 0) {
goto fail;
}
log_error("Failed to write to fd: %m");
goto fail;
}
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
goto fail;
}
m,
if (r < 0) {
goto fail;
}
}
}
r = 0;
fail:
if (bus) {
}
return r;
}
static void* client1(void*p) {
const char *hello;
int r;
char x;
r = sd_bus_open_user(&bus);
if (r < 0) {
goto finish;
}
r = sd_bus_call_method(
bus,
"/",
"LowerCase",
&error,
&reply,
"s",
"HELLO");
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
log_error("Failed to allocate pipe: %m");
r = -errno;
goto finish;
}
r = sd_bus_call_method(
bus,
"/",
"FileDescriptor",
&error,
NULL,
"h",
pp[1]);
if (r < 0) {
goto finish;
}
errno = 0;
goto finish;
}
r = 0;
if (bus) {
bus,
"/",
"ExitClient1",
&q);
if (r < 0)
else
}
close_pipe(pp);
return INT_TO_PTR(r);
}
bool *x = userdata;
*x = 1;
return 1;
}
static void* client2(void*p) {
bool quit = false;
const char *mid;
int r;
r = sd_bus_open_user(&bus);
if (r < 0) {
goto finish;
}
bus,
"org.object.test",
"Foobar",
&m);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
m = NULL;
bus,
"/foobar",
"foo.bar",
"Notify",
&m);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
m = NULL;
bus,
"/",
"org.freedesktop.DBus.Peer",
"GetMachineId",
&m);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
m = NULL;
bus,
"/",
"Slow",
&m);
if (r < 0) {
goto finish;
}
if (r < 0)
else
log_info("Slow call succeed.");
m = NULL;
bus,
"/",
"Slow",
&m);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
while (!quit) {
if (r < 0) {
goto finish;
}
if (r == 0) {
if (r < 0) {
goto finish;
}
}
}
r = 0;
if (bus) {
bus,
"/",
"ExitClient2",
&q);
if (r < 0) {
goto finish;
}
}
return INT_TO_PTR(r);
}
void *p;
int q, r;
r = server_init(&bus);
if (r < 0) {
log_info("Failed to connect to bus, skipping tests.");
return EXIT_TEST_SKIP;
}
log_info("Initialized...");
if (r != 0)
return EXIT_FAILURE;
if (r != 0)
return EXIT_FAILURE;
q = pthread_join(c1, &p);
if (q != 0)
return EXIT_FAILURE;
if (PTR_TO_INT(p) < 0)
return EXIT_FAILURE;
q = pthread_join(c2, &p);
if (q != 0)
return EXIT_FAILURE;
if (PTR_TO_INT(p) < 0)
return EXIT_FAILURE;
if (r < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}