test-bus-chat.c revision 6482f6269c87d2249e52e889a63adbdd50f2d691
/*-*- 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 <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "formats-util.h"
#include "sd-bus.h"
#include "bus-error.h"
#include "bus-match.h"
#include "bus-internal.h"
#include "bus-util.h"
static int match_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m)));
return 0;
}
static int object_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
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 log_error_errno(r, "Failed to send reply: %m");
return 1;
}
return 0;
}
int r;
const char *unique;
r = sd_bus_open_user(&bus);
if (r < 0) {
log_error_errno(r, "Failed to connect to user bus: %m");
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to get server ID: %m");
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to get unique name: %m");
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to acquire name: %m");
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to add object: %m");
goto fail;
}
r = sd_bus_add_match(bus, NULL, "type='signal',interface='foo.bar',member='Notify'", match_callback, NULL);
if (r < 0) {
log_error_errno(r, "Failed to add match: %m");
goto fail;
}
r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL);
if (r < 0) {
log_error_errno(r, "Failed to add match: %m");
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) {
log_error_errno(r, "Failed to process requests: %m");
goto fail;
}
if (r == 0) {
if (r < 0) {
log_error_errno(r, "Failed to wait: %m");
goto fail;
}
continue;
}
if (!m)
continue;
pid,
/* bus_message_dump(m); */
/* sd_bus_message_rewind(m, true); */
const char *hello;
if (r < 0) {
log_error_errno(r, "Failed to get parameter: %m");
goto fail;
}
if (!lowercase) {
r = log_oom();
goto fail;
}
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
goto fail;
}
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
goto fail;
}
client1_gone = true;
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
goto fail;
}
client2_gone = true;
sleep(1);
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
goto fail;
}
int fd;
static const char x = 'X';
if (r < 0) {
log_error_errno(r, "Failed to get parameter: %m");
goto fail;
}
safe_close(fd);
goto fail;
}
r = sd_bus_reply_method_return(m, NULL);
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
goto fail;
}
m,
if (r < 0) {
log_error_errno(r, "Failed to send reply: %m");
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) {
log_error_errno(r, "Failed to connect to user bus: %m");
goto finish;
}
r = sd_bus_call_method(
bus,
"/",
"LowerCase",
&error,
&reply,
"s",
"HELLO");
if (r < 0) {
log_error_errno(r, "Failed to issue method call: %m");
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to get string: %m");
goto finish;
}
r = -errno;
goto finish;
}
r = sd_bus_call_method(
bus,
"/",
"FileDescriptor",
&error,
NULL,
"h",
pp[1]);
if (r < 0) {
log_error_errno(r, "Failed to issue method call: %m");
goto finish;
}
errno = 0;
goto finish;
}
r = 0;
if (bus) {
bus,
&q,
"/",
"ExitClient1");
if (r < 0)
log_error_errno(r, "Failed to allocate method call: %m");
else
}
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) {
log_error_errno(r, "Failed to connect to user bus: %m");
goto finish;
}
bus,
&m,
"org.object.test",
"Foobar");
if (r < 0) {
log_error_errno(r, "Failed to allocate method call: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
m = NULL;
bus,
&m,
"/foobar",
"foo.bar",
"Notify");
if (r < 0) {
log_error_errno(r, "Failed to allocate signal: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
m = NULL;
bus,
&m,
"/",
"org.freedesktop.DBus.Peer",
"GetMachineId");
if (r < 0) {
log_error_errno(r, "Failed to allocate method call: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to parse machine ID: %m");
goto finish;
}
m = NULL;
bus,
&m,
"/",
"Slow");
if (r < 0) {
log_error_errno(r, "Failed to allocate method call: %m");
goto finish;
}
if (r < 0)
else
log_info("Slow call succeed.");
m = NULL;
bus,
&m,
"/",
"Slow");
if (r < 0) {
log_error_errno(r, "Failed to allocate method call: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
while (!quit) {
if (r < 0) {
log_error_errno(r, "Failed to process requests: %m");
goto finish;
}
if (r == 0) {
if (r < 0) {
log_error_errno(r, "Failed to wait: %m");
goto finish;
}
}
}
r = 0;
if (bus) {
bus,
&q,
"/",
"ExitClient2");
if (r < 0) {
log_error_errno(r, "Failed to allocate method call: %m");
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;
}