LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-27-2006, 11:54 PM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 283

Rep: Reputation: 32
DBUS problem


Hi friends,
i m new to DBUS so i downloaded this sample program from freedesktop.org. But when i compiled, it gave following error :....

Error :--------------

example.c: In function `query':
example.c:163: warning: assignment makes pointer from integer without a cast
/tmp/ccw0BHcv.o(.text+0xa0): In function `sendsignal':
/home/barun/DBUS/example.c:47: undefined reference to `dbus_bus_request_name'
/tmp/ccw0BHcv.o(.text+0x143):/home/barun/DBUS/example.c:67: undefined reference to `dbus_message_iter_init_append'
/tmp/ccw0BHcv.o(.text+0x158):/home/barun/DBUS/example.c:68: undefined reference to `dbus_message_iter_append_basic'
/tmp/ccw0BHcv.o(.text+0x1ef):/home/barun/DBUS/example.c:84: undefined reference to `dbus_connection_close'
/tmp/ccw0BHcv.o(.text+0x291): In function `query':
/home/barun/DBUS/example.c:117: undefined reference to `dbus_bus_request_name'
/tmp/ccw0BHcv.o(.text+0x336):/home/barun/DBUS/example.c:137: undefined reference to `dbus_message_iter_init_append'
/tmp/ccw0BHcv.o(.text+0x34b):/home/barun/DBUS/example.c:138: undefined reference to `dbus_message_iter_append_basic'
/tmp/ccw0BHcv.o(.text+0x415):/home/barun/DBUS/example.c:163: undefined reference to `dbus_pending_call_steal_reply'
/tmp/ccw0BHcv.o(.text+0x4bc):/home/barun/DBUS/example.c:177: undefined reference to `dbus_message_iter_get_basic'
/tmp/ccw0BHcv.o(.text+0x529):/home/barun/DBUS/example.c:184: undefined reference to `dbus_message_iter_get_basic'
/tmp/ccw0BHcv.o(.text+0x563):/home/barun/DBUS/example.c:190: undefined reference to `dbus_connection_close'
/tmp/ccw0BHcv.o(.text+0x5f1): In function `reply_to_method_call':
/home/barun/DBUS/example.c:208: undefined reference to `dbus_message_iter_get_basic'
/tmp/ccw0BHcv.o(.text+0x627):/home/barun/DBUS/example.c:216: undefined reference to `dbus_message_iter_init_append'
/tmp/ccw0BHcv.o(.text+0x63c):/home/barun/DBUS/example.c:217: undefined reference to `dbus_message_iter_append_basic'
/tmp/ccw0BHcv.o(.text+0x675):/home/barun/DBUS/example.c:221: undefined reference to `dbus_message_iter_append_basic'
/tmp/ccw0BHcv.o(.text+0x7a3): In function `listen':
/home/barun/DBUS/example.c:267: undefined reference to `dbus_bus_request_name'
/tmp/ccw0BHcv.o(.text+0x81a):/home/barun/DBUS/example.c:280: undefined reference to `dbus_connection_read_write'
/tmp/ccw0BHcv.o(.text+0x91a): In function `receive':
/home/barun/DBUS/example.c:329: undefined reference to `dbus_bus_request_name'
/tmp/ccw0BHcv.o(.text+0x9e3):/home/barun/DBUS/example.c:351: undefined reference to `dbus_connection_read_write'
/tmp/ccw0BHcv.o(.text+0xa96):/home/barun/DBUS/example.c:369: undefined reference to `dbus_message_iter_get_basic'
collect2: ld returned 1 exit status



SOURCE CODE :------------------------------

/*
* Example low-level D-Bus code.
* Written by Matthew Johnson <dbus@matthew.ath.cx>
*
* This code has been released into the Public Domain.
* You may do whatever you like with it.
*/
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

/**
* Connect to the DBUS bus and send a broadcast signal
*/
void sendsignal(char* sigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
dbus_uint32_t serial = 0;

printf("Sending signal with value %s\n", sigvalue);

// initialise the error value
dbus_error_init(&err);

// connect to the DBUS system bus, and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}

// register our name on the bus, and check for errors
ret = dbus_bus_request_name(conn, "test.signal.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
exit(1);
}

// create a signal & check for errors
msg = dbus_message_new_signal("/test/signal/Object", // object name of the signal
"test.signal.Type", // interface name of the signal
"Test"); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
exit(1);
}

// append arguments onto signal
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}

// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);

printf("Signal Sent\n");

// free the message and close the connection
dbus_message_unref(msg);
dbus_connection_close(conn);
}

/**
* Call a method on a remote object
*/
void query(char* param)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
DBusPendingCall* pending;
int ret;
bool stat;
dbus_uint32_t level;

printf("Calling remote method with %s\n", param);

// initialiset the errors
dbus_error_init(&err);

// connect to the system bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}

// request our name on the bus
ret = dbus_bus_request_name(conn, "test.method.caller", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
exit(1);
}

// create a new method call and check for errors
msg = dbus_message_new_method_call("test.method.server", // target for the method call
"/test/method/Object", // object to call on
"test.method.Type", // interface to call on
"Method"); // method name
if (NULL == msg) {
fprintf(stderr, "Message Null\n");
exit(1);
}

// append arguments
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &param)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}

// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
if (NULL == pending) {
fprintf(stderr, "Pending Call Null\n");
exit(1);
}
dbus_connection_flush(conn);

printf("Request Sent\n");

// free message
dbus_message_unref(msg);

// block until we recieve a reply
dbus_pending_call_block(pending);

// get the reply message
msg = dbus_pending_call_steal_reply(pending);
if (NULL == msg) {
fprintf(stderr, "Reply Null\n");
exit(1);
}
// free the pending message handle
dbus_pending_call_unref(pending);

// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message has no arguments!\n");
else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not boolean!\n");
else
dbus_message_iter_get_basic(&args, &stat);

if (!dbus_message_iter_next(&args))
fprintf(stderr, "Message has too few arguments!\n");
else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not int!\n");
else
dbus_message_iter_get_basic(&args, &level);

printf("Got Reply: %d, %d\n", stat, level);

// free reply and close connection
dbus_message_unref(msg);
dbus_connection_close(conn);
}

void reply_to_method_call(DBusMessage* msg, DBusConnection* conn)
{
DBusMessage* reply;
DBusMessageIter args;
bool stat = true;
dbus_uint32_t level = 21614;
dbus_uint32_t serial = 0;
char* param = "";

// read the arguments
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message has no arguments!\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!\n");
else
dbus_message_iter_get_basic(&args, &param);

printf("Method called with %s\n", param);

// create a reply from the message
reply = dbus_message_new_method_return(msg);

// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &stat)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &level)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}

// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);

// free the reply
dbus_message_unref(reply);
}

/**
* Server that exposes a method call and waits for it to be called
*/
void listen()
{
DBusMessage* msg;
DBusMessage* reply;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
char* param;

printf("Listening for method calls\n");

// initialise the error
dbus_error_init(&err);

// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
fprintf(stderr, "Connection Null\n");
exit(1);
}

// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "test.method.server", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
fprintf(stderr, "Not Primary Owner (%d)\n", ret);
exit(1);
}

// loop, testing for new messages
while (true) {
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);

// loop again if we haven't got a message
if (NULL == msg) {
sleep(1);
continue;
}

// check this is a method call for the right interface & method
if (dbus_message_is_method_call(msg, "test.method.Type", "Method"))
reply_to_method_call(msg, conn);

// free the message
dbus_message_unref(msg);
}

// close the connection
dbus_connection_close(conn);
}

/**
* Listens for signals on the bus
*/
void receive()
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
char* sigvalue;

printf("Listening for signals\n");

// initialise the errors
dbus_error_init(&err);

// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}

// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "test.signal.sink", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
exit(1);
}

// add a rule for which messages we want to see
dbus_bus_add_match(conn, "type='signal',interface='test.signal.Type'", &err); // see signals from the given interface
dbus_connection_flush(conn);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Match Error (%s)\n", err.message);
exit(1);
}
printf("Match rule sent\n");

// loop listening for signals being emmitted
while (true) {

// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);

// loop again if we haven't read a message
if (NULL == msg) {
sleep(1);
continue;
}

// check if the message is a signal from the correct interface and with the correct name
if (dbus_message_is_signal(msg, "test.signal.Type", "Test")) {

// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parameters\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!\n");
else
dbus_message_iter_get_basic(&args, &sigvalue);

printf("Got Signal with value %s\n", sigvalue);
}

// free the message
dbus_message_unref(msg);
}
// close the connection
dbus_connection_close(conn);
}

int main(int argc, char** argv)
{
if (2 > argc) {
printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]\n");
return 1;
}
char* param = "no param";
if (3 >= argc && NULL != argv[2]) param = argv[2];
if (0 == strcmp(argv[1], "send"))
sendsignal(param);
else if (0 == strcmp(argv[1], "receive"))
receive();
else if (0 == strcmp(argv[1], "listen"))
listen();
else if (0 == strcmp(argv[1], "query"))
query(param);
else {
printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]\n");
return 1;
}
return 0;
}


comilation :---------------

I have compiled by
gcc -g -ldbus-1 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -o example example.c

I guess it's not able to link but not able to figure out the problem.
With thanks in advance,
barun
 
Old 11-28-2006, 04:10 AM   #2
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
Moved: This thread is more suitable in Linux Software and has been moved accordingly to help your thread/question get the exposure it deserves.

All new Linux distros include dbus as standard, so you don't need to compile it yourself. Is there any particular reason why you want to compile it on your own?
 
Old 11-28-2006, 05:58 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Try to compile it with -Wall:
Code:
gcc -Wall -g -ldbus-1 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -o example example.c
Otherwise this line seems correct. You probably don't have libdbus-1-dev
 
Old 09-17-2008, 06:32 AM   #4
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Rep: Reputation: 15
hello all,

i have also used the same DBUS example downloaded from

http://www.matthew.ath.cx/projects.git/dbus-example.c

i could compile this successfully and while running...

[thinesh@thinesh test]$ ./dbus-example listen

i got below prints

Quote:
Listening for method calls
Name Error (Connection ":1.48" is not allowed to own the service "test.method.server" due to security policies in the configuration file)
Not Primary Owner (-1)
i have tried adding the current user to the "dbus" group and still i face the same problem. (is there significance on doing so )

is there anything i need to do with the configuration file to resolve this.

help me to rectify this issue.

thanks then
 
Old 09-17-2008, 07:54 AM   #5
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Rep: Reputation: 15
Solved!

i have gone thru the "/etc/dbus-1/system.conf" and found these lines
Quote:
<!-- Config files are placed here that among other things, punch
holes in the above policy for specific services. -->
<includedir>system.d</includedir>

<!-- This is included last so local configuration can override what's
in this standard file -->
<include ignore_missing="yes">system-local.conf</include>
and hence i created the system-local.conf and modified as below
Quote:
<policy context="default">
<!-- Deny everything then punch holes -->
<allow send_interface="*"/>
<allow receive_interface="*"/>
<allow own="*"/>
<!-- But allow all users to connect -->
<allow user="*"/>
<!-- Allow anyone to talk to the message bus -->
<!-- FIXME I think currently these allow rules are always implicit
even if they aren't in here -->
<allow send_destination="org.freedesktop.DBus"/>
<allow receive_sender="org.freedesktop.DBus"/>
<!-- valid replies are always allowed -->
<allow send_requested_reply="true"/>
<allow receive_requested_reply="true"/>
</policy>
to rectify this issue

Note: donot forget to remove the

Quote:
<include ignore_missing="yes">system-local.conf</include>
in the newly created system-local.conf file


anyhow experts... please comment on making this allow in the conf file., will it breaches any security issues..! then

thanks in advance..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HAL and DBUS? Archer36 Slackware 17 10-26-2006 09:50 PM
dbus problem paulmedic555 Linux - Desktop 0 09-13-2006 10:55 AM
Power Manager / dbus Problem JWilliamCupp Linux - Software 0 05-15-2006 08:46 PM
KDE & hal/dbus/udev problem with USB card... mlaverdiere Debian 7 02-23-2006 08:00 PM
dbus and bluez chopp Slackware 3 02-20-2006 12:49 AM


All times are GMT -5. The time now is 06:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration