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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* This library 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; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <errno.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <gvfsdaemon.h>
#include <gvfsdaemonprotocol.h>
#include <gvfsdaemonutils.h>
#include <gvfsutils.h>
#include <gvfsjobmount.h>
#include <gvfsjobopenforread.h>
#include <gvfsjobopenforwrite.h>
#include <gvfsjobunmount.h>
#include <gvfsmonitorimpl.h>
enum {
PROP_0
};
enum {
SHUTDOWN,
LAST_SIGNAL
};
typedef struct {
char *obj_path;
GVfsRegisterPathCallback callback;
gpointer data;
GDBusInterfaceSkeleton *session_skeleton;
GHashTable *client_skeletons;
} RegisteredPath;
struct _GVfsDaemon
{
GObject parent_instance;
GMutex lock;
gboolean main_daemon;
GThreadPool *thread_pool;
GHashTable *registered_paths;
GHashTable *client_connections;
GList *jobs;
GList *job_sources;
guint exit_tag;
gint mount_counter;
GDBusAuthObserver *auth_observer;
GDBusConnection *conn;
GVfsDBusDaemon *daemon_skeleton;
GVfsDBusMountable *mountable_skeleton;
guint name_watcher;
gboolean lost_main_daemon;
};
static guint signals[LAST_SIGNAL] = { 0 };
static void g_vfs_daemon_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void g_vfs_daemon_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static gboolean handle_get_connection (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
gpointer user_data);
static gboolean handle_cancel (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
guint arg_serial,
gpointer user_data);
static gboolean handle_list_monitor_implementations (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
gpointer user_data);
static gboolean daemon_handle_mount (GVfsDBusMountable *object,
GDBusMethodInvocation *invocation,
GVariant *arg_mount_spec,
gboolean arg_automount,
GVariant *arg_mount_source,
gpointer user_data);
static void g_vfs_daemon_re_register_job_sources (GVfsDaemon *daemon);
G_DEFINE_TYPE (GVfsDaemon, g_vfs_daemon, G_TYPE_OBJECT)
static void
registered_path_free (RegisteredPath *data)
{
g_free (data->obj_path);
if (data->session_skeleton)
{
/* Unexport the interface skeleton on session bus */
g_dbus_interface_skeleton_unexport (data->session_skeleton);
g_object_unref (data->session_skeleton);
}
g_hash_table_destroy (data->client_skeletons);
g_free (data);
}
static void
g_vfs_daemon_finalize (GObject *object)
{
GVfsDaemon *daemon;
daemon = G_VFS_DAEMON (object);
if (daemon->thread_pool != NULL)
g_thread_pool_free (daemon->thread_pool, TRUE, FALSE);
/* There may be some jobs outstanding if we've been force unmounted. */
if (daemon->jobs)
g_warning ("daemon->jobs != NULL when finalizing daemon!");
if (daemon->name_watcher)
g_bus_unwatch_name (daemon->name_watcher);
if (daemon->daemon_skeleton != NULL)
{
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (daemon->daemon_skeleton));
g_object_unref (daemon->daemon_skeleton);
}
if (daemon->mountable_skeleton != NULL)
{
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (daemon->mountable_skeleton));
g_object_unref (daemon->mountable_skeleton);
}
if (daemon->conn != NULL)
g_object_unref (daemon->conn);
if (daemon->auth_observer != NULL)
g_object_unref (daemon->auth_observer);
g_hash_table_destroy (daemon->registered_paths);
g_hash_table_destroy (daemon->client_connections);
g_mutex_clear (&daemon->lock);
if (G_OBJECT_CLASS (g_vfs_daemon_parent_class)->finalize)
(*G_OBJECT_CLASS (g_vfs_daemon_parent_class)->finalize) (object);
}
static void
g_vfs_daemon_class_init (GVfsDaemonClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = g_vfs_daemon_finalize;
gobject_class->set_property = g_vfs_daemon_set_property;
gobject_class->get_property = g_vfs_daemon_get_property;
signals[SHUTDOWN] =
g_signal_new ("shutdown",
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GVfsDaemonClass, shutdown),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
job_handler_callback (gpointer data,
gpointer user_data)
{
GVfsJob *job = G_VFS_JOB (data);
g_vfs_job_run (job);
g_object_unref (job);
}
static void
name_appeared_handler (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
if (strcmp (name, G_VFS_DBUS_DAEMON_NAME) == 0 &&
*name_owner != 0 &&
daemon->lost_main_daemon)
{
/* There is a new owner. Register mounts with it */
g_vfs_daemon_re_register_job_sources (daemon);
}
}
static void
name_vanished_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
/* Ensure we react only to really lost daemon */
daemon->lost_main_daemon = TRUE;
}
/*
* Authentication observer signal handler that rejects all authentication
* mechanisms except for EXTERNAL (credentials-passing), which is the
* recommended authentication mechanism for AF_UNIX sockets.
*/
static gboolean
allow_mechanism_cb (GDBusAuthObserver *observer,
const gchar *mechanism,
G_GNUC_UNUSED gpointer user_data)
{
if (g_strcmp0 (mechanism, "EXTERNAL") == 0)
return TRUE;
return FALSE;
}
/*
* Authentication observer signal handler that authorizes connections
* from the same uid as this process. This matches the behaviour of a
* libdbus DBusServer/DBusConnection when no DBusAllowUnixUserFunction
* has been set, but is not the default in GDBus.
*/
static gboolean
authorize_authenticated_peer_cb (GDBusAuthObserver *observer,
G_GNUC_UNUSED GIOStream *stream,
GCredentials *credentials,
G_GNUC_UNUSED gpointer user_data)
{
gboolean authorized = FALSE;
if (credentials != NULL)
{
GCredentials *own_credentials;
own_credentials = g_credentials_new ();
if (g_credentials_is_same_user (credentials, own_credentials, NULL))
authorized = TRUE;
g_object_unref (own_credentials);
}
return authorized;
}
static void
g_vfs_daemon_init (GVfsDaemon *daemon)
{
GError *error;
gint max_threads = 1; /* TODO: handle max threads */
daemon->thread_pool = g_thread_pool_new (job_handler_callback,
daemon,
max_threads,
FALSE, NULL);
/* TODO: verify thread_pool != NULL in a nicer way */
g_assert (daemon->thread_pool != NULL);
g_mutex_init (&daemon->lock);
daemon->mount_counter = 0;
daemon->jobs = NULL;
daemon->registered_paths =
g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify)registered_path_free);
/* This is where we store active client connections so when a new filter is registered,
* we re-register them on all active connections */
daemon->client_connections =
g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, NULL);
daemon->conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert (daemon->conn != NULL);
daemon->auth_observer = g_dbus_auth_observer_new ();
g_signal_connect (daemon->auth_observer, "allow-mechanism", G_CALLBACK (allow_mechanism_cb), NULL);
g_signal_connect (daemon->auth_observer, "authorize-authenticated-peer", G_CALLBACK (authorize_authenticated_peer_cb), NULL);
daemon->daemon_skeleton = gvfs_dbus_daemon_skeleton_new ();
g_signal_connect (daemon->daemon_skeleton, "handle-get-connection", G_CALLBACK (handle_get_connection), daemon);
g_signal_connect (daemon->daemon_skeleton, "handle-cancel", G_CALLBACK (handle_cancel), daemon);
g_signal_connect (daemon->daemon_skeleton, "handle-list-monitor-implementations", G_CALLBACK (handle_list_monitor_implementations), daemon);
error = NULL;
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (daemon->daemon_skeleton),
daemon->conn,
G_VFS_DBUS_DAEMON_PATH,
&error))
{
g_warning ("Error exporting daemon interface: %s (%s, %d)\n",
error->message, g_quark_to_string (error->domain), error->code);
g_error_free (error);
}
daemon->mountable_skeleton = gvfs_dbus_mountable_skeleton_new ();
g_signal_connect (daemon->mountable_skeleton, "handle-mount", G_CALLBACK (daemon_handle_mount), daemon);
error = NULL;
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (daemon->mountable_skeleton),
daemon->conn,
G_VFS_DBUS_MOUNTABLE_PATH,
&error))
{
g_warning ("Error exporting mountable interface: %s (%s, %d)\n",
error->message, g_quark_to_string (error->domain), error->code);
g_error_free (error);
}
}
static void
g_vfs_daemon_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_vfs_daemon_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
GVfsDaemon *
g_vfs_daemon_new (gboolean main_daemon, gboolean replace)
{
GVfsDaemon *daemon;
GDBusConnection *conn;
GError *error;
error = NULL;
conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (!conn)
{
g_printerr ("Failed to connect to the D-BUS daemon: %s (%s, %d)\n",
error->message, g_quark_to_string (error->domain), error->code);
g_error_free (error);
return NULL;
}
daemon = g_object_new (G_VFS_TYPE_DAEMON, NULL);
daemon->main_daemon = main_daemon;
if (! main_daemon)
{
daemon->name_watcher = g_bus_watch_name_on_connection (conn,
G_VFS_DBUS_DAEMON_NAME,
G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
name_appeared_handler,
name_vanished_handler,
daemon,
NULL);
}
g_object_unref (conn);
return daemon;
}
void
g_vfs_daemon_set_max_threads (GVfsDaemon *daemon,
gint max_threads)
{
g_thread_pool_set_max_threads (daemon->thread_pool, max_threads, NULL);
}
static gboolean
exit_at_idle (GVfsDaemon *daemon)
{
g_signal_emit (daemon, signals[SHUTDOWN], 0);
return FALSE;
}
static void
daemon_unschedule_exit (GVfsDaemon *daemon)
{
if (daemon->exit_tag != 0)
{
g_source_remove (daemon->exit_tag);
daemon->exit_tag = 0;
}
}
static void
daemon_schedule_exit (GVfsDaemon *daemon)
{
if (daemon->exit_tag == 0)
daemon->exit_tag = g_timeout_add_seconds (1, (GSourceFunc)exit_at_idle, daemon);
}
static void
job_source_new_job_callback (GVfsJobSource *job_source,
GVfsJob *job,
GVfsDaemon *daemon)
{
g_vfs_daemon_queue_job (daemon, job);
}
static void
job_source_closed_callback (GVfsJobSource *job_source,
GVfsDaemon *daemon)
{
g_mutex_lock (&daemon->lock);
daemon->job_sources = g_list_remove (daemon->job_sources,
job_source);
g_signal_handlers_disconnect_by_func (job_source,
(GCallback)job_source_new_job_callback,
daemon);
g_signal_handlers_disconnect_by_func (job_source,
(GCallback)job_source_closed_callback,
daemon);
g_object_unref (job_source);
if (daemon->job_sources == NULL)
daemon_schedule_exit (daemon);
g_mutex_unlock (&daemon->lock);
}
static void
re_register_jobs_cb (GVfsBackend *backend,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
g_vfs_backend_register_mount_finish (backend, res, &error);
g_debug ("re_register_jobs_cb, error: %p\n", error);
g_clear_error (&error);
}
static void
g_vfs_daemon_re_register_job_sources (GVfsDaemon *daemon)
{
GList *l;
g_mutex_lock (&daemon->lock);
for (l = daemon->job_sources; l != NULL; l = l->next)
{
if (G_VFS_IS_BACKEND (l->data))
{
GVfsBackend *backend = G_VFS_BACKEND (l->data);
/* Only re-register if we registered before, not e.g
if we're currently mounting. */
if (g_vfs_backend_is_mounted (backend))
g_vfs_backend_register_mount (backend, (GAsyncReadyCallback) re_register_jobs_cb, NULL);
}
}
g_mutex_unlock (&daemon->lock);
}
void
g_vfs_daemon_add_job_source (GVfsDaemon *daemon,
GVfsJobSource *job_source)
{
g_debug ("Added new job source %p (%s)\n", job_source, g_type_name_from_instance ((gpointer)job_source));
g_mutex_lock (&daemon->lock);
daemon_unschedule_exit (daemon);
g_object_ref (job_source);
daemon->job_sources = g_list_append (daemon->job_sources,
job_source);
g_signal_connect (job_source, "new_job",
(GCallback)job_source_new_job_callback, daemon);
g_signal_connect (job_source, "closed",
(GCallback)job_source_closed_callback, daemon);
g_mutex_unlock (&daemon->lock);
}
static void
unref_skeleton (gpointer object)
{
GDBusInterfaceSkeleton *skeleton = object;
g_dbus_interface_skeleton_unexport (skeleton);
g_object_unref (skeleton);
}
static void
peer_register_skeleton (const gchar *obj_path,
RegisteredPath *reg_path,
GDBusConnection *dbus_conn)
{
GDBusInterfaceSkeleton *skeleton;
if (! g_hash_table_contains (reg_path->client_skeletons, dbus_conn))
{
skeleton = reg_path->callback (dbus_conn, obj_path, reg_path->data);
g_hash_table_insert (reg_path->client_skeletons, dbus_conn, skeleton);
}
else
{
/* Interface skeleton has been already registered on the connection, skipping */
}
}
static void
client_conn_register_skeleton (GDBusConnection *dbus_conn,
gpointer value,
RegisteredPath *reg_path)
{
peer_register_skeleton (reg_path->obj_path, reg_path, dbus_conn);
}
/* This registers a dbus interface skeleton on *all* connections, client and session bus */
/* The object path needs to be unique globally. */
void
g_vfs_daemon_register_path (GVfsDaemon *daemon,
const char *obj_path,
GVfsRegisterPathCallback callback,
gpointer user_data)
{
RegisteredPath *data;
data = g_new0 (RegisteredPath, 1);
data->obj_path = g_strdup (obj_path);
data->callback = callback;
data->data = user_data;
data->client_skeletons = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)unref_skeleton);
g_hash_table_insert (daemon->registered_paths, g_strdup (obj_path), data);
/* Export the newly registered interface skeleton on session bus */
/* TODO: change the way we export skeletons on connections once
* https://bugzilla.gnome.org/show_bug.cgi?id=662718 is in place.
*/
data->session_skeleton = callback (daemon->conn, obj_path, user_data);
/* Export this newly registered path to all active client connections */
g_hash_table_foreach (daemon->client_connections, (GHFunc) client_conn_register_skeleton, data);
}
void
g_vfs_daemon_unregister_path (GVfsDaemon *daemon,
const char *obj_path)
{
g_hash_table_remove (daemon->registered_paths, obj_path);
}
/* NOTE: Might be emitted on a thread */
static void
job_new_source_callback (GVfsJob *job,
GVfsJobSource *job_source,
GVfsDaemon *daemon)
{
g_vfs_daemon_add_job_source (daemon, job_source);
}
/* NOTE: Might be emitted on a thread */
static void
job_finished_callback (GVfsJob *job,
GVfsDaemon *daemon)
{
g_signal_handlers_disconnect_by_func (job,
(GCallback)job_new_source_callback,
daemon);
g_signal_handlers_disconnect_by_func (job,
(GCallback)job_finished_callback,
daemon);
g_mutex_lock (&daemon->lock);
daemon->jobs = g_list_remove (daemon->jobs, job);
g_mutex_unlock (&daemon->lock);
g_object_unref (job);
}
void
g_vfs_daemon_queue_job (GVfsDaemon *daemon,
GVfsJob *job)
{
g_debug ("Queued new job %p (%s)\n", job, g_type_name_from_instance ((gpointer)job));
g_object_ref (job);
g_signal_connect (job, "finished", (GCallback)job_finished_callback, daemon);
g_signal_connect (job, "new_source", (GCallback)job_new_source_callback, daemon);
g_mutex_lock (&daemon->lock);
daemon->jobs = g_list_prepend (daemon->jobs, job);
g_mutex_unlock (&daemon->lock);
/* Can we start the job immediately / async */
if (!g_vfs_job_try (job))
{
/* Couldn't finish / run async, queue worker thread */
if (!g_thread_pool_push (daemon->thread_pool, g_object_ref (job), NULL)) /* TODO: Check error */
g_object_unref (job);
}
}
static void
peer_unregister_skeleton (const gchar *obj_path,
RegisteredPath *reg_path,
GDBusConnection *dbus_conn)
{
g_hash_table_remove (reg_path->client_skeletons, dbus_conn);
}
static void
peer_connection_closed (GDBusConnection *connection,
gboolean remote_peer_vanished,
GError *error,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
GList *l;
GVfsDBusDaemon *daemon_skeleton;
GVfsJob *job_to_cancel;
do
{
job_to_cancel = NULL;
g_mutex_lock (&daemon->lock);
for (l = daemon->jobs; l != NULL; l = l->next)
{
GVfsJob *job = G_VFS_JOB (l->data);
if (G_VFS_IS_JOB_DBUS (job) &&
!g_vfs_job_is_cancelled (job) &&
G_VFS_JOB_DBUS (job)->invocation &&
g_dbus_method_invocation_get_connection (G_VFS_JOB_DBUS (job)->invocation) == connection)
{
job_to_cancel = g_object_ref (job);
break;
}
}
g_mutex_unlock (&daemon->lock);
if (job_to_cancel)
{
g_vfs_job_cancel (job_to_cancel);
g_object_unref (job_to_cancel);
}
}
while (job_to_cancel != NULL);
daemon_skeleton = g_object_get_data (G_OBJECT (connection), "daemon_skeleton");
/* daemon_skeleton should be always valid in this case */
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (daemon_skeleton));
/* Unexport the registered interface skeletons */
g_hash_table_foreach (daemon->registered_paths, (GHFunc) peer_unregister_skeleton, connection);
/* The peer-to-peer connection was disconnected */
g_signal_handlers_disconnect_by_data (connection, user_data);
g_hash_table_remove (daemon->client_connections, connection);
}
static void
daemon_peer_connection_setup (GVfsDaemon *daemon,
GDBusConnection *dbus_conn)
{
GVfsDBusDaemon *daemon_skeleton;
GError *error;
daemon_skeleton = gvfs_dbus_daemon_skeleton_new ();
g_signal_connect (daemon_skeleton, "handle-cancel", G_CALLBACK (handle_cancel), daemon);
error = NULL;
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (daemon_skeleton),
dbus_conn,
G_VFS_DBUS_DAEMON_PATH,
&error))
{
g_warning ("Failed to accept client: %s, %s (%s, %d)", "object registration failed",
error->message, g_quark_to_string (error->domain), error->code);
g_error_free (error);
return;
}
g_object_set_data_full (G_OBJECT (dbus_conn), "daemon_skeleton",
daemon_skeleton, (GDestroyNotify) g_object_unref);
/* Export registered interface skeletons on this new connection */
g_hash_table_foreach (daemon->registered_paths, (GHFunc) peer_register_skeleton, dbus_conn);
g_hash_table_insert (daemon->client_connections, g_object_ref (dbus_conn), NULL);
g_signal_connect (dbus_conn, "closed", G_CALLBACK (peer_connection_closed), daemon);
}
static void
generate_address (gchar **address, gchar **socket_path)
{
gchar tmp[16] = "socket-";
gchar *socket_dir;
gvfs_randomize_string (tmp + 7, 8);
tmp[15] = '\0';
socket_dir = gvfs_get_socket_dir ();
*socket_path = g_build_filename (socket_dir, tmp, NULL);
*address = g_strdup_printf ("unix:path=%s", *socket_path);
g_free (socket_dir);
}
static gboolean
daemon_new_connection_func (GDBusServer *server,
GDBusConnection *connection,
gpointer user_data)
{
GVfsDaemon *daemon = user_data;
daemon_peer_connection_setup (daemon, connection);
/* Kill the server, no more need for it */
g_dbus_server_stop (server);
g_object_unref (server);
return TRUE;
}
static gboolean
handle_get_connection (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
GDBusServer *server;
GError *error;
gchar *address1;
gchar *socket_path;
gchar *guid;
const char *pkexec_uid;
generate_address (&address1, &socket_path);
guid = g_dbus_generate_guid ();
error = NULL;
server = g_dbus_server_new_sync (address1,
G_DBUS_SERVER_FLAGS_NONE,
guid,
daemon->auth_observer,
NULL, /* GCancellable */
&error);
g_free (guid);
if (server == NULL)
{
g_dbus_method_invocation_return_gerror (invocation, error);
g_printerr ("daemon: Error creating server at address %s: %s\n", address1, error->message);
g_error_free (error);
goto error_out;
}
g_dbus_server_start (server);
/* This is needed for gvfsd-admin to ensure correct ownership. */
pkexec_uid = g_getenv ("PKEXEC_UID");
if (pkexec_uid != NULL)
{
uid_t uid;
uid = strtol (pkexec_uid, NULL, 10);
if (uid != 0)
if (chown (socket_path, uid, (gid_t)-1) < 0)
g_warning ("Failed to change socket ownership: %s", g_strerror (errno));
}
g_signal_connect (server, "new-connection", G_CALLBACK (daemon_new_connection_func), daemon);
gvfs_dbus_daemon_complete_get_connection (object,
invocation,
address1,
"");
g_free (address1);
g_free (socket_path);
return TRUE;
error_out:
g_free (address1);
g_unlink (socket_path);
g_free (socket_path);
return TRUE;
}
static gboolean
handle_cancel (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
guint arg_serial,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
GList *l;
GVfsJob *job_to_cancel = NULL;
g_mutex_lock (&daemon->lock);
for (l = daemon->jobs; l != NULL; l = l->next)
{
GVfsJob *job = G_VFS_JOB (l->data);
if (G_VFS_IS_JOB_DBUS (job) &&
g_vfs_job_dbus_is_serial (G_VFS_JOB_DBUS (job),
g_dbus_method_invocation_get_connection (invocation),
arg_serial))
{
job_to_cancel = g_object_ref (job);
break;
}
}
g_mutex_unlock (&daemon->lock);
if (job_to_cancel)
{
g_vfs_job_cancel (job_to_cancel);
g_object_unref (job_to_cancel);
}
gvfs_dbus_daemon_complete_cancel (object, invocation);
return TRUE;
}
static gboolean
handle_list_monitor_implementations (GVfsDBusDaemon *object,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
GList *impls, *l;
GVariantBuilder builder;
impls = g_vfs_list_monitor_implementations ();
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ssbia{sv})"));
for (l = impls; l != NULL; l = l->next)
{
GVfsMonitorImplementation *impl = l->data;
g_variant_builder_add_value (&builder, g_vfs_monitor_implementation_to_dbus (impl));
}
g_list_free_full (impls, (GDestroyNotify)g_vfs_monitor_implementation_free);
gvfs_dbus_daemon_complete_list_monitor_implementations (object,
invocation,
g_variant_builder_end (&builder));
return TRUE;
}
static gboolean
daemon_handle_mount (GVfsDBusMountable *object,
GDBusMethodInvocation *invocation,
GVariant *arg_mount_spec,
gboolean arg_automount,
GVariant *arg_mount_source,
gpointer user_data)
{
GVfsDaemon *daemon = G_VFS_DAEMON (user_data);
GMountSpec *mount_spec;
GMountSource *mount_source;
mount_spec = g_mount_spec_from_dbus (arg_mount_spec);
if (mount_spec == NULL)
g_dbus_method_invocation_return_error_literal (invocation,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Error in mount spec");
else
{
mount_source = g_mount_source_from_dbus (arg_mount_source);
g_vfs_daemon_initiate_mount (daemon, mount_spec, mount_source, arg_automount,
object, invocation);
g_object_unref (mount_source);
g_mount_spec_unref (mount_spec);
}
return TRUE;
}
void
g_vfs_daemon_initiate_mount (GVfsDaemon *daemon,
GMountSpec *mount_spec,
GMountSource *mount_source,
gboolean is_automount,
GVfsDBusMountable *object,
GDBusMethodInvocation *invocation)
{
const char *type;
GType backend_type;
char *obj_path;
GVfsJob *job;
GVfsBackend *backend;
type = g_mount_spec_get_type (mount_spec);
backend_type = G_TYPE_INVALID;
if (type)
backend_type = g_vfs_lookup_backend (type);
if (backend_type == G_TYPE_INVALID)
{
if (invocation)
g_dbus_method_invocation_return_error_literal (invocation,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"Invalid backend type");
else
g_warning ("Error mounting: invalid backend type\n");
return;
}
obj_path = g_strdup_printf ("/org/gtk/vfs/mount/%d", ++daemon->mount_counter);
backend = g_object_new (backend_type,
"daemon", daemon,
"object-path", obj_path,
NULL);
g_free (obj_path);
g_vfs_daemon_add_job_source (daemon, G_VFS_JOB_SOURCE (backend));
g_object_unref (backend);
job = g_vfs_job_mount_new (mount_spec, mount_source, is_automount, object, invocation, backend);
g_vfs_daemon_queue_job (daemon, job);
g_object_unref (job);
}
/**
* g_vfs_daemon_get_blocking_processes:
* @daemon: A #GVfsDaemon.
*
* Gets all processes that blocks unmounting, e.g. processes with open
* file handles. Returned array could be empty in spite of
* g_vfs_daemon_has_blocking_processes returns TRUE, because for jobs without
* channel we can't get #GPid.
*
* Returns: An array of #GPid. Free with g_array_unref().
*/
GArray *
g_vfs_daemon_get_blocking_processes (GVfsDaemon *daemon)
{
GArray *processes;
GList *l;
g_mutex_lock (&daemon->lock);
processes = g_array_new (FALSE, FALSE, sizeof (GPid));
for (l = daemon->job_sources; l != NULL; l = l->next)
{
if (G_VFS_IS_CHANNEL (l->data))
{
GPid pid;
pid = g_vfs_channel_get_actual_consumer (G_VFS_CHANNEL (l->data));
g_array_append_val (processes, pid);
}
}
g_mutex_unlock (&daemon->lock);
return processes;
}
/**
* g_vfs_daemon_has_blocking_processes:
* @daemon: A #GVfsDaemon.
*
* Determines if there are any jobs blocking unmounting, all jobs excepting
* unmount job.
*
* Returns: TRUE if there are any blocking processes, or FALSE otherwise.
*/
gboolean
g_vfs_daemon_has_blocking_processes (GVfsDaemon *daemon)
{
GList *l;
g_mutex_lock (&daemon->lock);
for (l = daemon->jobs; l != NULL; l = l->next)
{
if (!G_VFS_IS_JOB_UNMOUNT (l->data))
{
g_debug ("blocking job: %p\n", l->data);
g_mutex_unlock (&daemon->lock);
return TRUE;
}
}
g_mutex_unlock (&daemon->lock);
return FALSE;
}
void
g_vfs_daemon_run_job_in_thread (GVfsDaemon *daemon,
GVfsJob *job)
{
if (!g_thread_pool_push (daemon->thread_pool, g_object_ref (job), NULL)) /* TODO: Check error */
g_object_unref (job);
}
void
g_vfs_daemon_close_active_channels (GVfsDaemon *daemon,
GVfsBackend *backend)
{
GList *l;
GVfsChannel *channel_to_close;
do
{
channel_to_close = NULL;
g_mutex_lock (&daemon->lock);
for (l = daemon->job_sources; l != NULL; l = l->next)
{
if (G_VFS_IS_CHANNEL (l->data) &&
g_vfs_channel_get_backend (G_VFS_CHANNEL (l->data)) == backend)
{
channel_to_close = g_object_ref (G_VFS_CHANNEL (l->data));
break;
}
}
g_mutex_unlock (&daemon->lock);
if (channel_to_close)
{
g_vfs_channel_force_close (channel_to_close);
g_object_unref (channel_to_close);
}
}
while (channel_to_close != NULL);
}
|