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 <glib.h>
00034 #include <dbus/dbus.h>
00035 #include <dbus/dbus-glib.h>
00036
00037 #include "libmnt.h"
00038
00039
00040 static DBusConnection* connection = NULL;
00041 static int is_initialized = FALSE;
00042 static PLIBMNTFUNCS functions = NULL;
00043 static void *user_data = NULL;
00044
00063 static DBusHandlerResult filter_func(DBusConnection* connection,
00064 DBusMessage* message,
00065 void* user_data)
00066 {
00067 DBusError error;
00068
00069 g_assert(connection!=NULL);
00070 g_assert(message!=NULL);
00071
00072 dbus_error_init(&error);
00073
00074 if (dbus_message_is_signal(message,
00075 DBUS_INTERFACE_MNT_MANAGER,
00076 "VolumeMounted")) {
00077 char *udi = NULL;
00078 char *mntpnt = NULL;
00079 if (dbus_message_get_args(message, &error,
00080 DBUS_TYPE_STRING, &udi,
00081 DBUS_TYPE_STRING, &mntpnt,
00082 DBUS_TYPE_INVALID)) {
00083 if (functions->volume_mounted!=NULL) {
00084 functions->volume_mounted(udi, mntpnt, user_data);
00085 }
00086 dbus_free(udi);
00087 }
00088
00089 } else if (dbus_message_is_signal(message,
00090 DBUS_INTERFACE_MNT_MANAGER,
00091 "VolumeUnmounted")) {
00092 char *udi = NULL;
00093 char *mntpnt = NULL;
00094 if (dbus_message_get_args(message, &error,
00095 DBUS_TYPE_STRING, &udi,
00096 DBUS_TYPE_STRING, &mntpnt,
00097 DBUS_TYPE_INVALID)) {
00098 if (functions->volume_unmounted!=NULL) {
00099 functions->volume_unmounted(udi, mntpnt, user_data);
00100 }
00101 dbus_free(udi);
00102 }
00103
00104 }
00105
00106 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00107 }
00108
00109
00111 static LIBMNTFUNCS dummy_functions = {
00112 NULL,
00113 NULL,
00114 NULL
00115 };
00116
00117
00126 int mnt_init(PLIBMNTFUNCS cb_functions, void *data)
00127 {
00128 DBusError error;
00129 int res = 0;
00130
00131
00132 if (is_initialized) {
00133 fprintf(stderr, "libmnt is already initialized!\n");
00134 goto cleanup;
00135 }
00136
00137
00138 functions = cb_functions;
00139 if (cb_functions==NULL) {
00140 functions = &dummy_functions;
00141 }
00142
00143
00144 user_data = data;
00145
00146
00147 dbus_error_init(&error);
00148 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
00149 if (connection==NULL) {
00150 fprintf(stderr, "Couldn't connect to DBUS, error %s: %s",
00151 error.name, error.message);
00152 goto error;
00153 }
00154
00155
00156
00157
00158
00159 if (!dbus_bus_service_exists(connection, DBUS_SERVICE_MNT, &error)) {
00160 fprintf(stderr, "Service '%s' doesn't exist !\n", DBUS_SERVICE_MNT);
00161 goto error;
00162 } else {
00163
00164 }
00165
00166 if (dbus_bus_acquire_service(connection, DBUS_SERVICE_MNT, 0, &error) == -1) {
00167 fprintf(stderr, "Error Acquiring service '" DBUS_SERVICE_MNT "'\n");
00168 if (dbus_error_is_set(&error)) {
00169 fprintf(stderr, "Can't acquire '" DBUS_SERVICE_MNT "' service, error %s: %s'",
00170 error.name, error.message);
00171 }
00172 goto error;
00173 } else {
00174
00175 }
00176
00177
00178 if (functions->main_loop!=NULL) {
00179 functions->main_loop(connection, user_data);
00180 }
00181
00182
00183 if (!dbus_connection_add_filter(connection, filter_func, user_data, NULL)) {
00184 fprintf(stderr, "Couldn't add filters to dbus connection handler");
00185 goto error;
00186 } else {
00187
00188 }
00189
00190 dbus_bus_add_match(connection,
00191 "type='signal',"
00192 "interface='" DBUS_INTERFACE_MNT_MANAGER "',"
00193 "sender='" DBUS_SERVICE_MNT "',"
00194 "path='" DBUS_PATH_MNT_MANAGER "'",
00195 &error);
00196 if (dbus_error_is_set(&error)) {
00197 fprintf(stderr, "Couldn't register Manager signal handler: %s: %s", error.name, error.message);
00198 goto error;
00199 } else {
00200
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 is_initialized = TRUE;
00217
00218 goto cleanup;
00219
00220 error:
00221 dbus_connection_disconnect(connection);
00222 is_initialized = FALSE;
00223 res = -1;
00224
00225 cleanup:
00226 if (dbus_error_is_set(&error)) {
00227 dbus_error_free(&error);
00228 }
00229
00230 return res;
00231 }
00232
00237 int mnt_quit(void)
00238 {
00239 if (!is_initialized) {
00240 return -1;
00241 }
00242
00243 dbus_connection_disconnect(connection);
00244 connection = NULL;
00245
00246 is_initialized = FALSE;
00247 return 0;
00248 }
00249
00250
00258 char **mnt_get_all_volumes(int *num_volumes)
00259 {
00260 DBusError error;
00261 DBusMessage* message = NULL;
00262 DBusMessage* reply = NULL;
00263 DBusMessageIter iter;
00264 char **res = NULL;
00265 char **data_array;
00266 int data_length;
00267 int i=0;
00268
00269 message = dbus_message_new_method_call(
00270 DBUS_SERVICE_MNT,
00271 DBUS_PATH_MNT_MANAGER,
00272 DBUS_INTERFACE_MNT_MANAGER,
00273 "GetAllVolumes");
00274
00275 if (message==NULL) {
00276 goto error;
00277 }
00278
00279 dbus_error_init(&error);
00280
00281 reply = dbus_connection_send_with_reply_and_block(connection, message, -1, &error);
00282 if (dbus_error_is_set(&error)) {
00283 goto error;
00284 }
00285 if (reply==NULL) {
00286 goto error;
00287 }
00288
00289
00290 dbus_message_iter_init(reply, &iter);
00291 if (!dbus_message_iter_get_string_array(&iter, &data_array, &data_length)) {
00292 fprintf(stderr, "wrong reply from mntd\n");
00293 goto error;
00294 }
00295
00296
00297 res = (char **) malloc(sizeof(char*)*data_length);
00298 if (res == NULL) {
00299 goto error;
00300 }
00301
00302
00303 *num_volumes = 0;
00304 for(i=0; i<data_length; i++) {
00305 if ((data_array[i] != NULL) && (strlen(data_array[i])>0)) {
00306 res[i] = strdup(data_array[i]);
00307 (*num_volumes)++;
00308 }
00309 }
00310
00311
00312 dbus_free_string_array(data_array);
00313
00314 goto cleanup;
00315
00316 error:
00317 res = NULL;
00318
00319 cleanup:
00320 if (message!=NULL) dbus_message_unref(message);
00321 if (reply!=NULL) dbus_message_unref(reply);
00322
00323 return res;
00324 }
00325
00326
00333 char *mnt_get_mntpnt(char *udi)
00334 {
00335 DBusError error;
00336 DBusMessage* message = NULL;
00337 DBusMessage* reply = NULL;
00338 DBusMessageIter iter;
00339 char *res = NULL;
00340 char *mntpnt = NULL;
00341
00342 message = dbus_message_new_method_call(
00343 DBUS_SERVICE_MNT,
00344 DBUS_PATH_MNT_MANAGER,
00345 DBUS_INTERFACE_MNT_MANAGER,
00346 "GetMntPnt");
00347
00348 if (message==NULL) {
00349 goto error;
00350 }
00351
00352
00353 dbus_message_iter_init(message, &iter);
00354 dbus_message_iter_append_string(&iter, udi);
00355
00356
00357 dbus_error_init(&error);
00358 reply = dbus_connection_send_with_reply_and_block(connection, message, -1, &error);
00359 if (dbus_error_is_set(&error)) {
00360 fprintf(stderr, "Error calling GetMntPnt('%s'): %s : %s\n", udi, error.name, error.message);
00361 goto error;
00362 }
00363 if (reply==NULL) {
00364 goto error;
00365 }
00366
00367
00368 if (!dbus_message_get_args(reply, &error,
00369 DBUS_TYPE_STRING, &mntpnt,
00370 DBUS_TYPE_INVALID)) {
00371 fprintf(stderr, "Error getting mntpnt from GetMntPnt() call (%s : %s)\n", error.name, error.message);
00372 goto error;
00373 }
00374 res = strdup(mntpnt);
00375 if (res == NULL) {
00376 goto error;
00377 }
00378
00379 goto cleanup;
00380
00381 error:
00382 res = NULL;
00383
00384 cleanup:
00385 if (mntpnt!=NULL) dbus_free(mntpnt);
00386 if (message!=NULL) dbus_message_unref(message);
00387 if (reply!=NULL) dbus_message_unref(reply);
00388
00389 return res;
00390 }
00391
00392