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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2004-2005 Nokia Corporation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-dbus-utils.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "gnome-vfs-volume-monitor-daemon.h"
#include "gnome-vfs-volume-monitor-private.h"
#include "gnome-vfs-private.h"
#include "dbus-utils.h"
#include "daemon-connection.h"
#define d(x)
static DBusConnection * daemon_get_connection (gboolean create);
static void daemon_shutdown (void);
static gboolean daemon_init (void);
static void daemon_unregistered_func (DBusConnection *conn,
gpointer data);
static DBusHandlerResult daemon_message_func (DBusConnection *conn,
DBusMessage *message,
gpointer data);
static DBusHandlerResult dbus_filter_func (DBusConnection *connection,
DBusMessage *message,
void *data);
static gboolean replace_daemon = FALSE;
static DBusObjectPathVTable daemon_vtable = {
daemon_unregistered_func,
daemon_message_func,
NULL
};
typedef struct {
gint32 conn_id;
char *socket_dir;
} NewConnectionData;
typedef struct {
char *sender;
gint32 id;
GnomeVFSMonitorHandle *vfs_handle;
} MonitorHandle;
typedef struct {
char *sender;
GList *active_monitors;
} MonitorOwner;
static GHashTable *monitors;
static GHashTable *monitor_owners;
static DBusConnection *
daemon_get_connection (gboolean create)
{
static DBusConnection *conn = NULL;
DBusError error;
gint ret;
int flags;
if (conn) {
return conn;
}
if (!create) {
return NULL;
}
dbus_error_init (&error);
conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (!conn) {
g_printerr ("Failed to connect to the D-BUS daemon: %s\n",
error.message);
dbus_error_free (&error);
return NULL;
}
flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
if (replace_daemon) {
flags |= DBUS_NAME_FLAG_REPLACE_EXISTING;
}
ret = dbus_bus_request_name (conn, DVD_DAEMON_SERVICE, flags, &error);
if (dbus_error_is_set (&error)) {
g_warning ("Failed to acquire vfs-daemon service: %s", error.message);
dbus_error_free (&error);
dbus_connection_close (conn);
/* Remove this, it asserts:
dbus_connection_unref (conn); */
conn = NULL;
return NULL;
}
if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS) {
g_printerr ("VFS daemon already running, exiting.\n");
dbus_connection_close (conn);
/* Remove this, it asserts:
dbus_connection_unref (conn); */
conn = NULL;
return NULL;
}
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
g_printerr ("Not primary owner of the service, exiting.\n");
dbus_connection_close (conn);
/* Remove this, it asserts:
dbus_connection_unref (conn); */
conn = NULL;
return NULL;
}
dbus_connection_add_filter (conn,
dbus_filter_func,
NULL,
NULL);
if (!dbus_connection_register_object_path (conn,
DVD_DAEMON_OBJECT,
&daemon_vtable,
NULL)) {
g_printerr ("Failed to register object with D-BUS.\n");
dbus_connection_close (conn);
dbus_connection_unref (conn);
conn = NULL;
return NULL;
}
dbus_connection_setup_with_g_main (conn, NULL);
return conn;
}
static gboolean
daemon_init (void)
{
return daemon_get_connection (TRUE) != NULL;
}
static void
daemon_shutdown (void)
{
DBusConnection *conn;
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
dbus_connection_close (conn);
dbus_connection_unref (conn);
}
static void
daemon_unregistered_func (DBusConnection *conn,
gpointer data)
{
}
static void
new_connection_data_free (void *memory)
{
NewConnectionData *data;
data = memory;
g_free (data->socket_dir);
g_free (data);
}
static void
daemon_new_connection_func (DBusServer *server,
DBusConnection *conn,
gpointer user_data)
{
gint32 conn_id;
NewConnectionData *data;
data = user_data;
conn_id = data->conn_id;
/* Kill the server, no more need for it */
dbus_server_disconnect (server);
dbus_server_unref (server);
/* Remove the socket and dir after connected */
if (data->socket_dir) {
rmdir (data->socket_dir);
}
d(g_print ("Got a new connection, id %d\n", conn_id));
daemon_connection_setup (conn, conn_id);
}
#ifdef __linux__
#define USE_ABSTRACT_SOCKETS
#endif
#ifndef USE_ABSTRACT_SOCKETS
static gboolean
test_safe_socket_dir (const char *dirname)
{
struct stat statbuf;
if (g_stat (dirname, &statbuf) != 0) {
return FALSE;
}
#ifndef G_PLATFORM_WIN32
if (statbuf.st_uid != getuid ()) {
return FALSE;
}
if ((statbuf.st_mode & (S_IRWXG|S_IRWXO)) ||
!S_ISDIR (statbuf.st_mode)) {
return FALSE;
}
#endif
return TRUE;
}
static char *
create_socket_dir (void)
{
char *dirname;
long iteration = 0;
char *safe_dir;
gchar tmp[9];
int i;
safe_dir = NULL;
do {
g_free (safe_dir);
for (i = 0; i < 8; i++) {
if (g_random_int_range (0, 2) == 0) {
tmp[i] = g_random_int_range ('a', 'z' + 1);
} else {
tmp[i] = g_random_int_range ('A', 'Z' + 1);
}
}
tmp[8] = '\0';
dirname = g_strdup_printf ("gnomevfs-%s-%s",
g_get_user_name (), tmp);
safe_dir = g_build_filename (g_get_tmp_dir (), dirname, NULL);
g_free (dirname);
if (g_mkdir (safe_dir, 0700) < 0) {
switch (errno) {
case EACCES:
g_error ("I can't write to '%s', daemon init failed",
safe_dir);
break;
case ENAMETOOLONG:
g_error ("Name '%s' too long your system is broken",
safe_dir);
break;
case ENOMEM:
#ifdef ELOOP
case ELOOP:
#endif
case ENOSPC:
case ENOTDIR:
case ENOENT:
g_error ("Resource problem creating '%s'", safe_dir);
break;
default: /* carry on going */
break;
}
}
/* Possible race - so we re-scan. */
if (iteration++ == 1000) {
g_error ("Cannot find a safe socket path in '%s'", g_get_tmp_dir ());
}
} while (!test_safe_socket_dir (safe_dir));
return safe_dir;
}
#endif
static gchar *
generate_address (char **folder)
{
gint i;
gchar tmp[9];
gchar *path;
*folder = NULL;
for (i = 0; i < 8; i++) {
if (g_random_int_range (0, 2) == 0) {
tmp[i] = g_random_int_range ('a', 'z' + 1);
} else {
tmp[i] = g_random_int_range ('A', 'Z' + 1);
}
}
tmp[8] = '\0';
#ifdef USE_ABSTRACT_SOCKETS
path = g_strdup_printf ("unix:abstract=/dbus-vfs-daemon/socket-%s", tmp);
#else
{
char *dir;
dir = create_socket_dir ();
path = g_strdup_printf ("unix:path=%s/socket", dir);
*folder = dir;
}
#endif
return path;
}
static void
daemon_handle_get_connection (DBusConnection *conn, DBusMessage *message)
{
DBusServer *server;
DBusError error;
DBusMessage *reply;
gchar *address;
static gint32 conn_id = 0;
NewConnectionData *data;
char *socket_dir;
address = generate_address (&socket_dir);
dbus_error_init (&error);
server = dbus_server_listen (address, &error);
if (!server) {
reply = dbus_message_new_error (message,
DVD_ERROR_SOCKET_FAILED,
"Failed to create new socket");
if (!reply) {
g_error ("Out of memory");
}
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
g_free (address);
return;
}
data = g_new (NewConnectionData, 1);
data->conn_id = ++conn_id;
data->socket_dir = socket_dir;
dbus_server_set_new_connection_function (server,
daemon_new_connection_func,
data, new_connection_data_free);
dbus_server_setup_with_g_main (server, NULL);
reply = dbus_message_new_method_return (message);
dbus_message_append_args (reply,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_INT32, &conn_id,
DBUS_TYPE_INVALID);
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
g_free (address);
}
static void
daemon_handle_cancel (DBusConnection *conn, DBusMessage *message)
{
gint32 cancellation_id;
gint32 conn_id;
if (!dbus_message_get_args (message, NULL,
DBUS_TYPE_INT32, &cancellation_id,
DBUS_TYPE_INT32, &conn_id,
DBUS_TYPE_INVALID)) {
return;
}
d(g_print ("daemon got Cancel (conn id %d, cancel id %d)\n",
conn_id, cancellation_id));
daemon_connection_cancel (conn_id, cancellation_id);
}
static void
monitor_handle_free (MonitorHandle *handle)
{
g_free (handle->sender);
g_free (handle);
}
static void
monitor_owner_free (MonitorOwner *owner)
{
g_free (owner->sender);
g_free (owner);
}
/* Called on main loop always */
static void
monitor_callback_func (GnomeVFSMonitorHandle *vfs_handle,
const gchar *monitor_uri,
const gchar *info_uri,
GnomeVFSMonitorEventType event_type,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
MonitorHandle *handle = user_data;
gint32 event_type32;
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_MONITOR_SIGNAL);
dbus_message_set_destination (signal, handle->sender);
event_type32 = event_type;
if (dbus_message_append_args (signal,
DBUS_TYPE_INT32, &handle->id,
DBUS_TYPE_STRING, &info_uri,
DBUS_TYPE_INT32, &event_type32,
DBUS_TYPE_INVALID)) {
/* In case this gets fired off after we've disconnected. */
if (dbus_connection_get_is_connected (conn)) {
dbus_connection_send (conn, signal, NULL);
}
}
dbus_message_unref (signal);
}
static void
daemon_handle_monitor_add (DBusConnection *conn, DBusMessage *message)
{
char *uri_str;
gint32 monitor_type;
MonitorHandle *handle;
MonitorOwner *owner;
const char *sender;
static gint32 next_handle = 0;
GnomeVFSResult result;
if (!dbus_message_get_args (message, NULL,
DBUS_TYPE_STRING, &uri_str,
DBUS_TYPE_INT32, &monitor_type,
DBUS_TYPE_INVALID)) {
dbus_util_reply_result (conn, message,
GNOME_VFS_ERROR_INTERNAL);
return;
}
sender = dbus_message_get_sender (message);
handle = g_new (MonitorHandle, 1);
handle->sender = g_strdup (sender);
handle->id = ++next_handle;
result = gnome_vfs_monitor_add (&handle->vfs_handle,
uri_str,
monitor_type,
monitor_callback_func,
handle);
if (result == GNOME_VFS_OK) {
if (monitors == NULL) {
monitors = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify)monitor_handle_free);
monitor_owners = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify)monitor_owner_free);
}
g_hash_table_insert (monitors, GINT_TO_POINTER (handle->id), handle);
owner = g_hash_table_lookup (monitor_owners, sender);
if (owner == NULL) {
owner = g_new (MonitorOwner, 1);
owner->sender = g_strdup (sender);
owner->active_monitors = NULL;
g_hash_table_insert (monitor_owners, owner->sender, owner);
dbus_util_start_track_name (conn, owner->sender);
}
owner->active_monitors = g_list_prepend (owner->active_monitors, handle);
dbus_util_reply_id (conn, message, handle->id);
} else {
monitor_handle_free (handle);
dbus_util_reply_result (conn, message, result);
}
}
static void
daemon_handle_monitor_cancel (DBusConnection *conn, DBusMessage *message)
{
gint32 id;
MonitorHandle *handle;
MonitorOwner *owner;
GnomeVFSResult result;
if (!dbus_message_get_args (message, NULL,
DBUS_TYPE_INT32, &id,
DBUS_TYPE_INVALID)) {
dbus_util_reply_result (conn, message,
GNOME_VFS_ERROR_INTERNAL);
return;
}
handle = g_hash_table_lookup (monitors, GINT_TO_POINTER (id));
if (handle) {
result = gnome_vfs_monitor_cancel (handle->vfs_handle);
owner = g_hash_table_lookup (monitor_owners, handle->sender);
if (owner) {
owner->active_monitors = g_list_remove (owner->active_monitors, handle);
if (owner->active_monitors == NULL) {
dbus_util_stop_track_name (conn, handle->sender);
g_hash_table_remove (monitor_owners, handle->sender);
}
}
g_hash_table_remove (monitors, GINT_TO_POINTER (handle->id));
dbus_util_reply_result (conn, message, result);
} else {
dbus_util_reply_result (conn, message, GNOME_VFS_ERROR_INTERNAL);
}
}
static void
daemon_handle_get_volumes (DBusConnection *conn, DBusMessage *message)
{
GnomeVFSVolumeMonitor *monitor;
GList *volumes;
DBusMessage *reply;
d(g_print ("daemon got GetVolumes\n"));
monitor = gnome_vfs_get_volume_monitor ();
volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
reply = dbus_message_new_method_return (message);
dbus_utils_message_append_volume_list (reply, volumes);
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
g_list_foreach (volumes, (GFunc) gnome_vfs_volume_unref, NULL);
g_list_free (volumes);
}
static void
daemon_handle_get_drives (DBusConnection *conn, DBusMessage *message)
{
GnomeVFSVolumeMonitor *monitor;
GList *drives;
DBusMessage *reply;
d(g_print ("daemon got GetDrives\n"));
monitor = gnome_vfs_get_volume_monitor ();
drives = gnome_vfs_volume_monitor_get_connected_drives (monitor);
reply = dbus_message_new_method_return (message);
dbus_utils_message_append_drive_list (reply, drives);
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
g_list_foreach (drives, (GFunc) gnome_vfs_drive_unref, NULL);
g_list_free (drives);
}
static void
daemon_handle_force_probe (DBusConnection *conn, DBusMessage *message)
{
GnomeVFSVolumeMonitor *monitor;
DBusMessage *reply;
d(g_print ("daemon got ForceProbe\n"));
monitor = gnome_vfs_get_volume_monitor ();
gnome_vfs_volume_monitor_daemon_force_probe (monitor);
reply = dbus_message_new_method_return (message);
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
}
static void
daemon_handle_emit_pre_unmount_volume (DBusConnection *conn, DBusMessage *message)
{
GnomeVFSVolumeMonitor *monitor;
dbus_int32_t id;
GnomeVFSVolume *volume;
DBusMessage *reply;
d(g_print ("daemon got EmitPreUnmountVolume\n"));
monitor = gnome_vfs_get_volume_monitor ();
if (dbus_message_get_args (message, NULL,
DBUS_TYPE_INT32, &id,
DBUS_TYPE_INVALID)) {
volume = gnome_vfs_volume_monitor_get_volume_by_id (monitor, id);
if (volume != NULL) {
gnome_vfs_volume_monitor_emit_pre_unmount (monitor,
volume);
gnome_vfs_volume_unref (volume);
}
}
reply = dbus_message_new_method_return (message);
dbus_connection_send (conn, reply, NULL);
dbus_message_unref (reply);
}
static DBusHandlerResult
daemon_message_func (DBusConnection *conn,
DBusMessage *message,
gpointer data)
{
if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_GET_CONNECTION)) {
daemon_handle_get_connection (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_CANCEL)) {
daemon_handle_cancel (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_MONITOR_ADD)) {
daemon_handle_monitor_add (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_MONITOR_CANCEL)) {
daemon_handle_monitor_cancel (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_GET_VOLUMES)) {
daemon_handle_get_volumes (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_GET_DRIVES)) {
daemon_handle_get_drives (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_FORCE_PROBE)) {
daemon_handle_force_probe (conn, message);
}
else if (dbus_message_is_method_call (message,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_METHOD_EMIT_PRE_UNMOUNT_VOLUME)) {
daemon_handle_emit_pre_unmount_volume (conn, message);
} else {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
dbus_filter_func (DBusConnection *connection,
DBusMessage *message,
void *data)
{
if (dbus_message_is_signal (message,
DBUS_INTERFACE_DBUS,
"NameOwnerChanged")) {
gchar *service, *old_owner, *new_owner;
MonitorOwner *owner;
GList *l;
dbus_message_get_args (message,
NULL,
DBUS_TYPE_STRING, &service,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID);
/* Handle monitor owner going away */
if (*new_owner == 0) {
owner = g_hash_table_lookup (monitor_owners, service);
if (owner) {
for (l = owner->active_monitors; l != NULL; l = l->next) {
MonitorHandle *handle = l->data;
gnome_vfs_monitor_cancel (handle->vfs_handle);
g_hash_table_remove (monitors, GINT_TO_POINTER (handle->id));
}
owner->active_monitors = NULL;
dbus_util_stop_track_name (connection, service);
g_hash_table_remove (monitor_owners, service);
}
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static void
monitor_volume_mounted_cb (GnomeVFSVolumeMonitor *monitor,
GnomeVFSVolume *volume,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
d(g_print ("daemon got volume_mounted\n"));
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_VOLUME_MOUNTED_SIGNAL);
dbus_utils_message_append_volume (signal, volume);
dbus_connection_send (conn, signal, NULL);
dbus_message_unref (signal);
}
static void
monitor_volume_unmounted_cb (GnomeVFSVolumeMonitor *monitor,
GnomeVFSVolume *volume,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
gint32 id;
d(g_print ("daemon got volume_unmounted\n"));
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_VOLUME_UNMOUNTED_SIGNAL);
id = gnome_vfs_volume_get_id (volume);
dbus_message_append_args (signal,
DBUS_TYPE_INT32, &id,
DBUS_TYPE_INVALID);
dbus_connection_send (conn, signal, NULL);
dbus_message_unref (signal);
}
static void
monitor_volume_pre_unmount_cb (GnomeVFSVolumeMonitor *monitor,
GnomeVFSVolume *volume,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
gint32 id;
d(g_print ("daemon got volume_pre_unmount\n"));
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_VOLUME_PRE_UNMOUNT_SIGNAL);
id = gnome_vfs_volume_get_id (volume);
dbus_message_append_args (signal,
DBUS_TYPE_INT32, &id,
DBUS_TYPE_INVALID);
dbus_connection_send (conn, signal, NULL);
dbus_message_unref (signal);
}
static void
monitor_drive_connected_cb (GnomeVFSVolumeMonitor *monitor,
GnomeVFSDrive *drive,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
d(g_print ("daemon got drive_connected\n"));
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_DRIVE_CONNECTED_SIGNAL);
dbus_utils_message_append_drive (signal, drive);
dbus_connection_send (conn, signal, NULL);
dbus_message_unref (signal);
}
static void
monitor_drive_disconnected_cb (GnomeVFSVolumeMonitor *monitor,
GnomeVFSDrive *drive,
gpointer user_data)
{
DBusConnection *conn;
DBusMessage *signal;
gint32 id;
d(g_print ("daemon got drive_disconnected\n"));
conn = daemon_get_connection (FALSE);
if (!conn) {
return;
}
signal = dbus_message_new_signal (DVD_DAEMON_OBJECT,
DVD_DAEMON_INTERFACE,
DVD_DAEMON_DRIVE_DISCONNECTED_SIGNAL);
id = gnome_vfs_drive_get_id (drive);
dbus_message_append_args (signal,
DBUS_TYPE_INT32, &id,
DBUS_TYPE_INVALID);
dbus_connection_send (conn, signal, NULL);
dbus_message_unref (signal);
}
int
main (int argc, char *argv[])
{
GMainLoop *loop;
GnomeVFSVolumeMonitor *monitor;
int i;
d(g_print ("Starting daemon.\n"));
if (argc > 1) {
for (i = 1; i < argc; i++) {
if (strcmp (argv[i], "--replace") == 0) {
replace_daemon = TRUE;
}
}
}
setlocale(LC_ALL, "");
g_type_init ();
gnome_vfs_set_is_daemon (GNOME_VFS_TYPE_VOLUME_MONITOR_DAEMON,
gnome_vfs_volume_monitor_daemon_force_probe);
if (!gnome_vfs_init ()) {
g_printerr ("Could not initialize gnome vfs");
return 1;
}
loop = g_main_loop_new (NULL, FALSE);
if (!daemon_init ()) {
d(g_print ("Couldn't init daemon, exiting.\n"));
return 1;
}
/* Init the volume monitor. */
monitor = gnome_vfs_get_volume_monitor ();
g_signal_connect (monitor,
"volume_mounted",
G_CALLBACK (monitor_volume_mounted_cb),
NULL);
g_signal_connect (monitor,
"volume_unmounted",
G_CALLBACK (monitor_volume_unmounted_cb),
NULL);
g_signal_connect (monitor,
"volume_pre_unmount",
G_CALLBACK (monitor_volume_pre_unmount_cb),
NULL);
g_signal_connect (monitor,
"drive_connected",
G_CALLBACK (monitor_drive_connected_cb),
NULL);
g_signal_connect (monitor,
"drive_disconnected",
G_CALLBACK (monitor_drive_disconnected_cb),
NULL);
g_main_loop_run (loop);
d(g_print ("Shutting down.\n"));
daemon_shutdown ();
return 0;
}
|