1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/***************************************************************************
* *
* Powersave Daemon *
* *
* Copyright (C) 2004,2005 SUSE Linux Products GmbH *
* *
* Author(s): Holger Macht <hmacht@suse.de> *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at you *
* option) any later version. *
* *
* This program 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 *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include "config.h"
#include "dbus_server.h"
#include "pm_interface.h"
#include "globals.h"
#include "main_loop.h"
#include "clientConnection.h"
#include "powersave_hal.h"
DBusConnection* DBus_Server::_connection = NULL;
using namespace Powersave::Globals;
DBus_Server::DBus_Server()
{
}
DBus_Server::~DBus_Server()
{
dbus_connection_close(_connection);
dbus_connection_unref(_connection);
}
gboolean DBus_Server::dbus_reconnect() {
DBusConnection *dbus_connection = openSystemConnection();
if (dbus_connection == NULL) {
return true;
} else {
dbus_connection_setup_with_g_main(dbus_connection, NULL);
pDebug(DBG_ERR,"Reconnected to dbus daemon");
return false;
}
}
bool DBus_Server::serviceHasOwner(const string &service)
{
if (_connection == NULL) {
pDebug(DBG_INFO, "Connection to dbus not ready, skipping check for service %s",
service.c_str());
return false;
}
DBusError dbus_error;
dbus_error_init(&dbus_error);
bool has_owner = dbus_bus_name_has_owner(_connection, service.c_str(), &dbus_error);
if (dbus_error_is_set(&dbus_error)) {
pDebug(DBG_WARN, "dbus_bus_name_has_owner() failed: %s",
dbus_error.message);
dbus_error_free(&dbus_error);
return false;
}
return has_owner;
}
DBusHandlerResult DBus_Server::filter_function(DBusConnection * conn, DBusMessage * msg, void *data)
{
pDebug(DBG_DEBUG, "");
/* Be careful, handle error at once and go out, or other functions
may fail ungracefully ... */
if (msg == NULL) {
return DBUS_HANDLER_RESULT_HANDLED;
pDebug(DBG_ERR, "Invalid message received");
}
/** @todo This means DBus daemon itself threw us out,
Need to disconnect gracefully and free all resources
*/
if (dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected")) {
pDebug(DBG_ERR, "DBus daemon disconnected. Trying to reconnect...");
// only way I see for now to get this information
Powersave::Globals::main_loop->addCallback(3000, (GSourceFunc) DBus_Server::dbus_reconnect);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
int type = dbus_message_get_type(msg);
if (type == DBUS_MESSAGE_TYPE_ERROR) {
pDebug(DBG_ERR, "Received error message: %s", dbus_message_get_error_name(msg));
return DBUS_HANDLER_RESULT_HANDLED;
}
/* Have to use const char* instead of string here.
* Return value might be NULL
*/
const char *t = dbus_message_get_interface(msg);
if (t == NULL) {
pDebug(DBG_INFO, "Received message from invalid interface");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
const string interface = t;
/* We only handle messages which want a reply */
int reply_wanted = !dbus_message_get_no_reply(msg);
// We do not compare the object path, I see no need for ...
// && strcmp(dbus_message_get_path(msg), DBUS_MESSAGE_BUS_PATH) == 0) {
/* ToDo: what should be filtered here and what should be filtered
in path_message_func (for what is this vtable stuff good for) ?!?
-> Deleted the vtable path_message_func stuff for now, all
message processing is started here
*/
DBusHandlerResult ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
int reply_error_id;
DBusMessage *reply;
/** @brief checking of interfaces is done here in filter function
* checking for the right member, message contents, etc. must
* be done in pm->handleDBusRequest(..)
* Create the Message here, we must send a reply (sendReply(),
* there the memory for the reply will be freed again.
*/
if (interface == PS_DBUS_MANAGER_INTERFACE) {
if (type != DBUS_MESSAGE_TYPE_METHOD_CALL || !reply_wanted) {
pDebug(DBG_ERR, "Invalid msg type sent to manager interface");
} else {
reply = dbus_message_new_method_return(msg);
pDebug(DBG_DIAG, "Method sent to manager interface");
reply_error_id = pm->handleDBusRequest(msg, reply, MANAGER_MESSAGE);
sendReply(reply, reply_error_id);
}
ret = DBUS_HANDLER_RESULT_HANDLED;
} else if (interface == PS_DBUS_SCRIPTS_INTERFACE){
if (type != DBUS_MESSAGE_TYPE_METHOD_CALL || !reply_wanted){
pDebug (DBG_ERR, "Invalid msg type sent to Scripts interface");
}
else{
reply = dbus_message_new_method_return(msg);
pDebug (DBG_DIAG, "Method sent to Scripts interface");
reply_error_id = pm->handleDBusRequest(msg, reply, SCRIPTS_MESSAGE);
sendReply(reply, reply_error_id);
}
ret = DBUS_HANDLER_RESULT_HANDLED;
} else if (interface == PS_DBUS_REQUEST_INTERFACE) {
if (type != DBUS_MESSAGE_TYPE_METHOD_CALL || !reply_wanted) {
pDebug(DBG_ERR, "Invalid msg sent to request interface");
} else {
reply = dbus_message_new_method_return(msg);
pDebug(DBG_INFO, "Method sent to request interface");
reply_error_id = pm->handleDBusRequest(msg, reply, REQUEST_MESSAGE);
sendReply(reply, reply_error_id);
}
ret = DBUS_HANDLER_RESULT_HANDLED;
} else if (interface == PS_DBUS_ACTION_INTERFACE
|| interface == PS_DBUS_ADMIN_INTERFACE) {
if (type != DBUS_MESSAGE_TYPE_METHOD_CALL || !reply_wanted) {
pDebug(DBG_ERR, "Invalid msg sent to action interface");
} else {
reply = dbus_message_new_method_return(msg);
pDebug(DBG_INFO, "Method sent to action interface");
reply_error_id = pm->handleDBusRequest(msg, reply, ACTION_MESSAGE);
sendReply(reply, reply_error_id);
}
ret = DBUS_HANDLER_RESULT_HANDLED;
} else if (interface == DBUS_INTERFACE_DBUS) {
if (type != DBUS_MESSAGE_TYPE_SIGNAL) {
pDebug(DBG_ERR, "Invalid signal received");
/** @todo HANDLED or NOT_YET_HANDLED, does it matter ?!? */
ret = DBUS_HANDLER_RESULT_HANDLED;
} else {
pDebug(DBG_INFO, "Signal from %s with member %s received",
DBUS_INTERFACE_DBUS, dbus_message_get_member(msg));
string msg_member = dbus_message_get_member(msg);
if (msg_member == "NameOwnerChanged") {
char *old_owner;
char *new_owner;
/** @todo Error catching... */
/* get the second (the old owner) and the third (the new owner)
string of the message (first string: affected owner) */
if (!dbusGetMessageString(msg, &old_owner, 0)
&& !dbusGetMessageString(msg, &new_owner, 2)
&& old_owner != NULL && new_owner != NULL) {
/* if the new owner is "" the client broke away -> remove
from client list */
if (!strcmp(new_owner, "")) {
// test if hal broke away
if (!strcmp(old_owner, "org.freedesktop.Hal")) {
pDebug(DBG_WARN, "Hal service stopped. "
"Battery information no longer available");
ps_hal_free();
} else {
pm->_cC->checkClientRemoval(old_owner);
}
}
}
}
}
} else if (interface == DBUS_HAL_MANAGER_INTERFACE) {
string msg_member = dbus_message_get_member(msg);
char *udi;
if (!dbusGetMessageString(msg, &udi, 0)) {
if (msg_member == "DeviceRemoved") {
pm->checkDevice(udi, false);
}
else if (msg_member == "DeviceAdded") {
pm->checkDevice(udi, true);
}
}
else {
pDebug(DBG_WARN, "Got signal %s from hal, but no string argument",
msg_member.c_str());
}
} else if (interface == DBUS_HAL_DEVICE_INTERFACE) {
string msg_member = dbus_message_get_member(msg);
const char *path = dbus_message_get_path(msg);
if (msg_member == "PropertyModified") {
if (ps_hal_query_capability((char*)path, "battery")) {
pm->checkBatteryStateChanges();
}
else if (ps_hal_query_capability((char*)path, "ac_adapter")) {
pm->checkACStateChanges();
}
}
} else {
/* This message is noy for us -> we are the only
* server offering com.novell.powersave
* -> client probably made a mistake and specified a wrong
* interface -> at least warning
* We cannot answer to an interface we don't know (I expect).
*/
pDebug(DBG_WARN, "Received a message for a foreign interface");
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
pDebug(DBG_INFO, "Message handled");
return ret;
}
DBusConnection *DBus_Server::openSystemConnection()
{
DBusError error;
dbus_error_init(&error);
_connection = dbus_connection_open_private(DBUS_SYSTEM_BUS_SOCKET, &error);
if (dbus_error_is_set(&error)) {
// only throw diag message. Real error message is thrown in main loop
pDebug(DBG_DIAG, "dbus_connection_open_private(): Error. (%s)", error.message);
dbus_error_free(&error);
return NULL;
} else {
dbus_connection_set_exit_on_disconnect(_connection, 0);
pDebug(DBG_INFO, "dbus_connection_open_private(): Success.");
}
dbus_bus_register(_connection, &error);
dbus_bus_request_name(_connection, PS_DBUS_SERVICE, 0, &error);
if (dbus_error_is_set(&error)) {
pDebug(DBG_ERR, "dbus_bus_request_name(): Error. (%s)", error.message);
if (_connection != NULL) {
dbus_connection_close(_connection);
dbus_connection_unref(_connection);
}
dbus_error_free(&error);
return NULL;
}
dbus_connection_add_filter(_connection, filter_function, NULL, NULL);
dbus_bus_add_match(_connection,
"type='method_call'," "interface='com.novell.powersave.manager'," "member='connect'",
NULL);
/* keep track o f disconnecting clients */
dbus_bus_add_match(_connection,
"type='signal'," "interface='org.freedesktop.DBus'," "member='NameOwnerChanged'",
NULL);
dbus_bus_add_match(_connection,
"type='signal'," "interface=org.freedesktop.Hal.Manager," "member=DeviceRemoved",
NULL);
dbus_bus_add_match(_connection,
"type='signal'," "interface=org.freedesktop.Hal.Manager," "member=DeviceAdded",
NULL);
dbus_bus_add_match(_connection,
"type='signal'," "interface=org.freedesktop.Hal.Device," "member=PropertyModified",
NULL);
return _connection;
}
void DBus_Server::sendReply(DBusMessage * reply, int error_id)
{
/* We do not generate DBus error messages ourselves
If needed, this is the place to do.
Problem: we need to generate the message here
and do not know whether the
message should become an error message
Maybe we could convert the message to an error message
later, but we should not need that...
dbus_message_set_error_name (DBusMessage *message,
const char *error_name)
reply = dbus_message_new_method_return(msg);
*/
dbus_uint16_t err_id_u16 = error_id;
if (reply == NULL) {
pDebug(DBG_WARN, "Invalid reply");
return;
}
pDebug(DBG_INFO, "Send error_id %d, '%s'", error_id, DBus_Error_Array[error_id]);
/* attache contents collected from raw_reply_msg to DBusMessage reply */
/* We only use DBUS_TYPE_UINT16 once: to indicate the error code */
dbus_message_append_args(reply, DBUS_TYPE_UINT16, &err_id_u16,
// don't need to also send the error string ?
// or could it be useful dbus-monitor ?
// DBUS_TYPE_STRING, &DBus_Error_Array[error_id],
DBUS_TYPE_INVALID);
// check for memory failure? -> then FALSE will be returned
if (reply == NULL) {
pDebug(DBG_WARN, "Could not send reply/error messge");
return;
}
dbus_connection_send(_connection, reply, NULL);
dbus_connection_flush(_connection);
dbus_message_unref(reply);
}
int DBus_Server::emitSignal(const char *signal, const char *value)
{
DBusMessage *message;
if (_connection == NULL) {
pDebug(DBG_INFO, "Connection to dbus not ready, skipping signal %s", value);
return false;
}
message = dbus_message_new_signal( "/com/novell/powersave",
"com.novell.powersave.manager",
signal );
/* append the event to the signal */
dbus_message_append_args(message,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID );
/* and send the message */
if ( ! dbus_connection_send( _connection,
message,
NULL ) ) {
pDebug (DBG_ERR, "Could not send message to D-BUS system bus.\n");
dbus_message_unref( message );
return false;
}
dbus_message_unref( message );
return true;
}
bool DBus_Server::sendMethodCall(const char *interface, const char *path,
const char* object, const char *method,
int first_arg_type, ...)
{
if (_connection == NULL) {
pDebug(DBG_INFO, "Connection to dbus not ready, skipping method call %s",
method);
return false;
}
DBusMessage *message;
va_list var_args;
message = dbus_message_new_method_call( interface, path, object, method );
va_start(var_args, first_arg_type);
dbus_message_append_args_valist(message, first_arg_type, var_args);
va_end(var_args);
if (!dbus_connection_send(_connection, message, NULL)) {
pDebug (DBG_ERR, "Could not send method call\n");
dbus_message_unref( message );
return false;
}
dbus_message_unref( message );
dbus_connection_flush(_connection);
return true;
}
bool DBus_Server::sendMethodCallWithReturn(const char *interface, const char *path,
const char* object ,const char *method,
int arg_type, void *value)
{
if (_connection == NULL) {
pDebug(DBG_INFO, "Connection to dbus not ready, skipping method call %s",
method);
return false;
}
DBusMessage *message;
DBusError dbus_error;
DBusMessage *reply;
dbus_error_init(&dbus_error);
message = dbus_message_new_method_call( interface, path, object, method );
reply = dbus_connection_send_with_reply_and_block( _connection, message, -1, &dbus_error);
if (dbus_error_is_set(&dbus_error)) {
pDebug (DBG_WARN, "Could not send method call to D-BUS system bus: %s\n",
dbus_error.message);
goto Error;
}
if (!dbus_message_get_args(reply, &dbus_error, arg_type, value, DBUS_TYPE_INVALID)){
if (dbus_error_is_set(&dbus_error))
pDebug(DBG_WARN, "Could not get argument from reply: %s",
dbus_error.message);
goto Error;
}
dbus_connection_flush(_connection);
return true;
Error:
dbus_message_unref( message );
dbus_message_unref( reply );
dbus_error_free(&dbus_error);
return false;
}
|