From: Arvin Schnell Date: Fri, 20 Jul 2012 11:17:46 +0000 (+0200) Subject: - work on dbus interface X-Git-Tag: v0.1.3~203 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=afbe1c4c05698170bf623fde85fc9b910f0876b1;p=thirdparty%2Fsnapper.git - work on dbus interface --- diff --git a/client/commands.cc b/client/commands.cc index 0583a5e1..3b15c763 100644 --- a/client/commands.cc +++ b/client/commands.cc @@ -33,7 +33,7 @@ command_list_xconfigs(DBus::Connection& conn) { DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "ListConfigs"); - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); list ret; @@ -52,7 +52,7 @@ command_get_xconfig(DBus::Connection& conn, const string& config_name) DBus::Hoho hoho(call); hoho << config_name; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); XConfigInfo ret; @@ -72,7 +72,7 @@ command_create_xconfig(DBus::Connection& conn, const string& config_name, const DBus::Hoho hoho(call); hoho << config_name << subvolume << fstype << template_name; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -84,7 +84,7 @@ command_delete_xconfig(DBus::Connection& conn, const string& config_name) DBus::Hoho hoho(call); hoho << config_name; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -96,7 +96,7 @@ command_list_xsnapshots(DBus::Connection& conn, const string& config_name) DBus::Hoho hoho(call); hoho << config_name; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); XSnapshots ret; @@ -115,7 +115,7 @@ command_get_xsnapshot(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << num; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); XSnapshot ret; @@ -135,7 +135,7 @@ command_set_xsnapshot(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << num << data.description << data.cleanup << data.userdata; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -149,7 +149,7 @@ command_create_single_xsnapshot(DBus::Connection& conn, const string& config_nam DBus::Hoho hoho(call); hoho << config_name << description << cleanup << userdata; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); unsigned int number; @@ -170,7 +170,7 @@ command_create_pre_xsnapshot(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << description << cleanup << userdata; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); unsigned int number; @@ -191,7 +191,7 @@ command_create_post_xsnapshot(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << prenum << description << cleanup << userdata; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); unsigned int number; @@ -211,7 +211,7 @@ command_delete_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << nums; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -224,7 +224,7 @@ command_mount_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << num; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -237,7 +237,7 @@ command_umount_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << num; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -250,7 +250,7 @@ command_create_xcomparison(DBus::Connection& conn, const string& config_name, un DBus::Hoho hoho(call); hoho << config_name << number1 << number2; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -263,7 +263,7 @@ command_get_xfiles(DBus::Connection& conn, const string& config_name, unsigned i DBus::Hoho hoho(call); hoho << config_name << number1 << number2; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); list files; @@ -283,7 +283,7 @@ command_get_xdiff(DBus::Connection& conn, const string& config_name, unsigned in DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << name << options; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); vector files; @@ -303,7 +303,7 @@ command_set_xundo(DBus::Connection& conn, const string& config_name, unsigned in DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << undos; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -316,7 +316,7 @@ command_set_xundo_all(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << undo; - conn.send_and_reply_and_block(call); + conn.send_with_reply_and_block(call); } @@ -329,7 +329,7 @@ command_get_xundo_steps(DBus::Connection& conn, const string& config_name, unsig DBus::Hoho hoho(call); hoho << config_name << number1 << number2; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); vector undo_steps; @@ -349,7 +349,7 @@ command_do_xundo_step(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << undo_step; - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); bool ret; @@ -365,7 +365,7 @@ command_xdebug(DBus::Connection& conn) { DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "Debug"); - DBus::Message reply = conn.send_and_reply_and_block(call); + DBus::Message reply = conn.send_with_reply_and_block(call); vector lines; diff --git a/dbus/DBusConnection.cc b/dbus/DBusConnection.cc index ef492eca..05430c29 100644 --- a/dbus/DBusConnection.cc +++ b/dbus/DBusConnection.cc @@ -58,6 +58,8 @@ namespace DBus void Connection::request_name(const char* name, unsigned int flags) { + boost::lock_guard lock(mutex); + DBusError err; dbus_error_init(&err); @@ -78,6 +80,8 @@ namespace DBus void Connection::read_write(int timeout) { + // TODO + dbus_connection_read_write(conn, timeout); } @@ -85,6 +89,8 @@ namespace DBus void Connection::send(Message& m) { + boost::lock_guard lock(mutex); + if (!dbus_connection_send(conn, m.get_message(), NULL)) { throw FatalException(); @@ -93,8 +99,10 @@ namespace DBus Message - Connection::send_and_reply_and_block(Message& m) + Connection::send_with_reply_and_block(Message& m) { + boost::lock_guard lock(mutex); + DBusError err; dbus_error_init(&err); @@ -113,6 +121,8 @@ namespace DBus void Connection::add_match(const char* rule) { + boost::lock_guard lock(mutex); + DBusError err; dbus_error_init(&err); @@ -128,6 +138,8 @@ namespace DBus unsigned long Connection::get_unix_userid(const Message& m) { + boost::lock_guard lock(mutex); + string sender = m.get_sender(); if (sender.empty()) { @@ -149,10 +161,8 @@ namespace DBus string - Connection::get_unix_username(const Message& m) + Connection::get_unix_username(unsigned long userid) { - unsigned long userid = get_unix_userid(m); - long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); char* buf = (char*) malloc(bufsize); if (!buf) diff --git a/dbus/DBusConnection.h b/dbus/DBusConnection.h index d4a3c531..0f77a303 100644 --- a/dbus/DBusConnection.h +++ b/dbus/DBusConnection.h @@ -27,6 +27,7 @@ #include #include +#include #include "DBusMessage.h" @@ -49,12 +50,14 @@ namespace DBus void send(Message& m); - Message send_and_reply_and_block(Message& m); + Message send_with_reply_and_block(Message& m); void add_match(const char* rule); unsigned long get_unix_userid(const Message& m); - string get_unix_username(const Message& m); + static string get_unix_username(unsigned long userid); // TODO + + boost::mutex mutex; private: diff --git a/server/Client.cc b/server/Client.cc index 723833de..9f29c4d3 100644 --- a/server/Client.cc +++ b/server/Client.cc @@ -96,8 +96,6 @@ Client::has_lock(const string& config_name) const void Client::add_task(DBus::Connection& conn, DBus::Message& msg) { -#if 1 - if (!t) t = new boost::thread(boost::bind(&Client::worker, this)); @@ -106,12 +104,6 @@ Client::add_task(DBus::Connection& conn, DBus::Message& msg) l.unlock(); c->notify_one(); - -#else - - dispatch(conn, msg); - -#endif } @@ -149,6 +141,8 @@ Clients::find(const string& name) Clients::iterator Clients::add(const string& name) { + assert(find(name) == entries.end()); + entries.push_back(Client(name)); return --entries.end(); } diff --git a/server/Client.h b/server/Client.h index e3f7c3c4..b85cdf4f 100644 --- a/server/Client.h +++ b/server/Client.h @@ -53,6 +53,13 @@ class Commands { public: + void signal_config_created(DBus::Connection& conn, const string& config_name); + void signal_config_deleted(DBus::Connection& conn, const string& config_name); + void signal_snapshot_created(DBus::Connection& conn, const string& config_name, + unsigned int num); + void signal_snapshots_deleted(DBus::Connection& conn, const string& config_name, + list nums); + void list_configs(DBus::Connection& conn, DBus::Message& msg); void get_config(DBus::Connection& conn, DBus::Message& msg); void create_config(DBus::Connection& conn, DBus::Message& msg); diff --git a/server/snapperd.cc b/server/snapperd.cc index bc43cbc9..3af7bac0 100644 --- a/server/snapperd.cc +++ b/server/snapperd.cc @@ -57,9 +57,6 @@ using namespace snapper; Clients clients; -boost::mutex dbus_mutex; - - void reply_to_introspect(DBus::Connection& conn, DBus::Message& msg) { @@ -269,7 +266,7 @@ check_permission(DBus::Connection& conn, DBus::Message& msg, const string& confi if (uid == 0) return; - string username = conn.get_unix_username(msg); + string username = DBus::Connection::get_unix_username(uid); map::const_iterator pos = it->raw.find("USERS"); if (pos == it->raw.end()) @@ -315,7 +312,7 @@ check_lock(DBus::Connection& conn, DBus::Message& msg, const string& config_name void -send_signal_config_created(DBus::Connection& conn, const string& config_name) +Commands::signal_config_created(DBus::Connection& conn, const string& config_name) { DBus::MessageSignal msg(PATH, INTERFACE, "ConfigCreated"); @@ -327,7 +324,7 @@ send_signal_config_created(DBus::Connection& conn, const string& config_name) void -send_signal_config_deleted(DBus::Connection& conn, const string& config_name) +Commands::signal_config_deleted(DBus::Connection& conn, const string& config_name) { DBus::MessageSignal msg(PATH, INTERFACE, "ConfigDeleted"); @@ -339,8 +336,8 @@ send_signal_config_deleted(DBus::Connection& conn, const string& config_name) void -send_signal_snapshot_created(DBus::Connection& conn, const string& config_name, - unsigned int num) +Commands::signal_snapshot_created(DBus::Connection& conn, const string& config_name, + unsigned int num) { DBus::MessageSignal msg(PATH, INTERFACE, "SnapshotCreated"); @@ -352,8 +349,8 @@ send_signal_snapshot_created(DBus::Connection& conn, const string& config_name, void -send_signal_snapshots_deleted(DBus::Connection& conn, const string& config_name, - list nums) +Commands::signal_snapshots_deleted(DBus::Connection& conn, const string& config_name, + list nums) { DBus::MessageSignal msg(PATH, INTERFACE, "SnapshotsDeleted"); @@ -369,8 +366,6 @@ Commands::list_configs(DBus::Connection& conn, DBus::Message& msg) { y2mil("ListConfigs"); - boost::unique_lock dbus_lock(dbus_mutex); - list config_infos = Snapper::getConfigs(); DBus::MessageMethodReturn reply(msg); @@ -392,8 +387,6 @@ Commands::get_config(DBus::Connection& conn, DBus::Message& msg) y2mil("GetConfig config_name:" << config_name); - boost::unique_lock dbus_lock(dbus_mutex); - check_permission(conn, msg, config_name); Snapper* snapper = getSnapper(config_name); @@ -432,7 +425,7 @@ Commands::create_config(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_config_created(conn, config_name); + signal_config_created(conn, config_name); } @@ -454,7 +447,7 @@ Commands::delete_config(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_config_deleted(conn, config_name); + signal_config_deleted(conn, config_name); } @@ -514,8 +507,6 @@ Commands::list_snapshots(DBus::Connection& conn, DBus::Message& msg) y2mil("ListSnapshots config_name:" << config_name); - boost::unique_lock dbus_lock(dbus_mutex); - check_permission(conn, msg, config_name); Snapper* snapper = getSnapper(config_name); @@ -540,8 +531,6 @@ Commands::get_snapshot(DBus::Connection& conn, DBus::Message& msg) y2mil("GetSnapshot config_name:" << config_name << " num:" << num); - boost::unique_lock dbus_lock(dbus_mutex); - check_permission(conn, msg, config_name); Snapper* snapper = getSnapper(config_name); @@ -622,7 +611,7 @@ Commands::create_single_snapshot(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_snapshot_created(conn, config_name, snap1->getNum()); + signal_snapshot_created(conn, config_name, snap1->getNum()); } @@ -656,7 +645,7 @@ Commands::create_pre_snapshot(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_snapshot_created(conn, config_name, snap1->getNum()); + signal_snapshot_created(conn, config_name, snap1->getNum()); } @@ -695,7 +684,7 @@ Commands::create_post_snapshot(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_snapshot_created(conn, config_name, snap2->getNum()); + signal_snapshot_created(conn, config_name, snap2->getNum()); } @@ -728,7 +717,7 @@ Commands::delete_snapshots(DBus::Connection& conn, DBus::Message& msg) conn.send(reply); - send_signal_snapshots_deleted(conn, config_name, nums); + signal_snapshots_deleted(conn, config_name, nums); } @@ -1201,7 +1190,7 @@ listen(DBus::Connection& conn) { boost::this_thread::sleep(boost::posix_time::milliseconds(100)); // TODO - boost::unique_lock dbus_lock(dbus_mutex); + boost::unique_lock dbus_lock(conn.mutex); conn.read_write(10); // TODO