00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <dbus/dbus.h>
00034 #include <dbus/dbus-glib.h>
00035
00036 #include <libmnt/libmnt.h>
00037
00038 #include "mntd_dbus_error.h"
00039 #include "mntd_dbus_manager.h"
00040 #include "mntd_volume_manager.h"
00041 #include "mntd_volume.h"
00042 #include "errmanager.h"
00043 #include "main.h"
00044
00045
00046 static void *user_data = NULL;
00047
00048
00049
00050 int cb_append_udi(void *data, void *userdata);
00051
00052
00053
00054
00055
00056
00066 DBusConnection* dbus_connection = NULL;
00067
00068
00073 int
00074 mntd_dbus_init(void *data)
00075 {
00076 DBusError dbus_error;
00077
00078 dbus_connection_set_change_sigpipe(TRUE);
00079
00080 dbus_error_init(&dbus_error);
00081 dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
00082 if (dbus_connection==NULL) {
00083 MSG_ERR("dbus_bus_get(): '%s'", dbus_error.message);
00084 return -1;
00085 }
00086
00087 dbus_connection_setup_with_g_main(dbus_connection, NULL);
00088
00089 dbus_bus_acquire_service(dbus_connection, DBUS_SERVICE_MNT, 0, &dbus_error);
00090 if (dbus_error_is_set(&dbus_error)) {
00091 MSG_ERR("dbus_bus_acquire_service(): '%s'", dbus_error.message);
00092 return -1;
00093 }
00094
00095 user_data = data;
00096
00097 dbus_connection_add_filter(dbus_connection, mntd_dbus_manager_filter_function,
00098 user_data, NULL);
00099
00100 return 0;
00101 }
00102
00103
00108 int
00109 mntd_dbus_quit(void)
00110 {
00111 return 0;
00112 }
00113
00114
00121 void mntd_dbus_raise_no_such_device(DBusConnection* connection,
00122 DBusMessage* in_reply_to,
00123 const char* udi)
00124 {
00125 char buf[512];
00126 DBusMessage *reply;
00127
00128 snprintf(buf, 511, "No device with id %s", udi);
00129
00130
00131 reply = dbus_message_new_error(in_reply_to,
00132 DBUS_ERROR_NO_SUCH_DEVICE,
00133 buf);
00134 if (reply==NULL) {
00135 MSG_EMERG("Out of memory");
00136 return;
00137 }
00138 if (!dbus_connection_send(connection, reply, NULL)) {
00139 MSG_EMERG("Out of memory");
00140 return;
00141 }
00142 dbus_message_unref(reply);
00143 }
00144
00145
00153 void mntd_dbus_raise_syntax(DBusConnection* connection,
00154 DBusMessage* in_reply_to,
00155 const char* method_name)
00156 {
00157 char buf[512];
00158 DBusMessage *reply;
00159
00160 snprintf(buf, 511,
00161 "There is a syntax error in the invocation of "
00162 "the method %s", method_name);
00163
00164
00165 reply = dbus_message_new_error(in_reply_to,
00166 DBUS_ERROR_SYNTAX,
00167 buf);
00168 if (reply==NULL) {
00169 MSG_EMERG("Out of memory");
00170 return;
00171 }
00172 if (!dbus_connection_send(connection, reply, NULL)) {
00173 MSG_EMERG("Out of memory");
00174 return;
00175 }
00176 dbus_message_unref(reply);
00177 }
00178
00179
00186 void mntd_dbus_raise_no_mntpnt(DBusConnection* connection,
00187 DBusMessage* in_reply_to,
00188 const char* udi)
00189 {
00190 char buf[512];
00191 DBusMessage *reply;
00192
00193 snprintf(buf, 511,
00194 "No mount point for '%s' found.", udi);
00195
00196
00197 reply = dbus_message_new_error(in_reply_to,
00198 DBUS_ERROR_NO_MNTPNT,
00199 buf);
00200 if (reply==NULL) {
00201 MSG_EMERG("Out of memory");
00202 return;
00203 }
00204 if (!dbus_connection_send(connection, reply, NULL)) {
00205 MSG_EMERG("Out of memory");
00206 return;
00207 }
00208 dbus_message_unref(reply);
00209 }
00210
00211
00221 DBusHandlerResult mntd_dbus_manager_filter_function(DBusConnection* connection,
00222 DBusMessage* message,
00223 void* user_data)
00224 {
00225 if (dbus_message_is_method_call(message,
00226 DBUS_INTERFACE_MNT_MANAGER,
00227 "GetAllVolumes")) {
00228 MSG_DEBUG("biz.bambach.Mnt.Manager::GetAllVolumes() called");
00229 return mntd_dbus_manager_get_all_volumes(vols, connection, message);
00230
00231 } else if (dbus_message_is_method_call(message,
00232 DBUS_INTERFACE_MNT_MANAGER,
00233 "GetMntPnt")) {
00234 MSG_DEBUG("biz.bambach.Mnt.Manager::GetMntPnt() called");
00235 return mntd_dbus_manager_get_mntpnt(vols, connection, message);
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 }
00269
00270 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00271 }
00272
00273
00279 void
00280 mntd_dbus_manager_send_signal_volume_mounted(PVOLUME pv)
00281 {
00282 DBusMessage* message;
00283 DBusMessageIter it;
00284
00285
00286
00287 message = dbus_message_new_signal(
00288 DBUS_PATH_MNT_MANAGER,
00289 DBUS_INTERFACE_MNT_MANAGER,
00290 "VolumeMounted");
00291 if (message==NULL) {
00292 MSG_EMERG("Out of memory");
00293 return;
00294 }
00295
00296 dbus_message_iter_init(message, &it);
00297 dbus_message_iter_append_string(&it, pv->udi);
00298 dbus_message_iter_append_string(&it, pv->mntpnt);
00299
00300 if (!dbus_connection_send(dbus_connection, message, NULL)) {
00301 MSG_EMERG("error broadcasting message");
00302 }
00303
00304 dbus_message_unref(message);
00305 }
00306
00307
00313 void
00314 mntd_dbus_manager_send_signal_volume_unmounted(PVOLUME pv)
00315 {
00316 DBusMessage* message;
00317 DBusMessageIter it;
00318
00319
00320
00321 message = dbus_message_new_signal(
00322 DBUS_PATH_MNT_MANAGER,
00323 DBUS_INTERFACE_MNT_MANAGER,
00324 "VolumeUnmounted");
00325 if (message==NULL) {
00326 MSG_EMERG("Out of memory");
00327 return;
00328 }
00329
00330 dbus_message_iter_init(message, &it);
00331 dbus_message_iter_append_string(&it, pv->udi);
00332 dbus_message_iter_append_string(&it, pv->mntpnt);
00333
00334 if (!dbus_connection_send(dbus_connection, message, NULL)) {
00335 MSG_EMERG("error broadcasting message");
00336 }
00337
00338 dbus_message_unref(message);
00339 }
00340
00341
00348 int cb_append_udi(void *data, void *userdata)
00349 {
00350 DBusMessageIter *itarr = NULL;
00351 PHASHMAPELEMENT phme = NULL;
00352 PHASHMAP phm = NULL;
00353 PVOLUME pv = NULL;
00354 char *udi = NULL;
00355 void *hashdata = NULL;
00356
00357 if ((data==NULL) || (userdata==NULL)) {
00358 return -1;
00359 }
00360
00361
00362 itarr = (DBusMessageIter *) userdata;
00363
00364 phme = (PHASHMAPELEMENT) data;
00365
00366 phm = phme->phm;
00367
00368 udi = phme->key;
00369
00370 if (phm->get(phm, udi, &hashdata) == 0) {
00371 pv = (PVOLUME) hashdata;
00372 if (pv != NULL) {
00373
00374 if (pv->is_in_mntpath(pv)) {
00375
00376 dbus_message_iter_append_string(itarr, udi);
00377 return 0;
00378 }
00379 }
00380 }
00381
00382 return -1;
00383 }
00384
00385
00397 DBusHandlerResult
00398 mntd_dbus_manager_get_all_volumes( PVOLUMEMANAGER pvm,
00399 DBusConnection* connection,
00400 DBusMessage* message)
00401 {
00402 DBusMessage* reply;
00403 DBusMessageIter it;
00404 DBusMessageIter itarr;
00405 DBusHandlerResult res = DBUS_HANDLER_RESULT_HANDLED;
00406
00407
00408 reply = dbus_message_new_method_return(message);
00409 if (reply==NULL) {
00410 MSG_EMERG("Out of memory");
00411 goto error;
00412 }
00413
00414
00415 dbus_message_iter_init(reply, &it);
00416 dbus_message_iter_append_array(&it, &itarr, DBUS_TYPE_STRING);
00417 pvm->foreach_mounted(pvm, cb_append_udi, &itarr);
00418
00419
00420 if(!dbus_connection_send(connection, reply, NULL)) {
00421 MSG_EMERG("Out of memory");
00422 goto error;
00423 }
00424
00425 goto cleanup;
00426
00427 error:
00428 res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00429
00430 cleanup:
00431 if (reply!=NULL) {
00432 dbus_message_unref(reply);
00433 }
00434
00435 return res;
00436 }
00437
00438
00450 DBusHandlerResult
00451 mntd_dbus_manager_get_mntpnt( PVOLUMEMANAGER pvm,
00452 DBusConnection* connection,
00453 DBusMessage* message)
00454 {
00455 DBusMessage *reply = NULL;
00456 DBusMessageIter it;
00457 DBusError error;
00458 DBusHandlerResult res = DBUS_HANDLER_RESULT_HANDLED;
00459 char *udi = NULL;
00460 char *mntpnt = NULL;
00461
00462
00463 dbus_error_init(&error);
00464 if (!dbus_message_get_args(message, &error,
00465 DBUS_TYPE_STRING, &udi,
00466 DBUS_TYPE_INVALID) )
00467 {
00468 mntd_dbus_raise_syntax(connection, message, "Manager.GetMntPnt");
00469 goto error;
00470 }
00471
00472
00473 reply = dbus_message_new_method_return(message);
00474 if (reply==NULL) {
00475 MSG_EMERG("Out of memory");
00476 goto error;
00477 }
00478
00479
00480 dbus_message_iter_init(reply, &it);
00481 if (pvm->contains(pvm, udi)) {
00482 mntpnt = pvm->get_mntpnt(pvm, udi);
00483 if (mntpnt==NULL) {
00484 mntd_dbus_raise_no_mntpnt(connection, message, udi);
00485 goto error;
00486 }
00487 dbus_message_iter_append_string(&it, mntpnt);
00488 } else {
00489 mntd_dbus_raise_no_such_device(connection, message, udi);
00490 goto error;
00491 }
00492
00493
00494 if(!dbus_connection_send(connection, reply, NULL)) {
00495 MSG_EMERG("Out of memory");
00496 goto error;
00497 }
00498
00499 goto cleanup;
00500
00501 error:
00502 res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00503
00504 cleanup:
00505 if (reply!=NULL) {
00506 dbus_message_unref(reply);
00507 }
00508 if (udi!=NULL) {
00509 dbus_free(udi);
00510 }
00511 if (mntpnt!=NULL) {
00512 free(mntpnt);
00513 }
00514
00515 return res;
00516 }
00517
00518
00522
00523
00524
00525