gunixmounts.c 61.4 KB
Newer Older
1 2
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 20 21 22
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

23 24
/* Prologue {{{1 */

25
#include "config.h"
26 27 28 29 30 31 32 33 34

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifndef HAVE_SYSCTLBYNAME
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#endif
35
#ifdef HAVE_POLL
36 37 38 39 40 41 42 43
#include <poll.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
44
#include <gstdio.h>
45
#include <dirent.h>
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#if HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#if HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#if HAVE_SYS_VFS_H
#include <sys/vfs.h>
#elif HAVE_SYS_MOUNT_H
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/mount.h>
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif

66 67 68
#include "gunixmounts.h"
#include "gfile.h"
#include "gfilemonitor.h"
69 70
#include "glibintl.h"
#include "gthemedicon.h"
71
#include "gcontextspecificgroup.h"
72

73

Patrick Welche's avatar
Patrick Welche committed
74
#ifdef HAVE_MNTENT_H
75
static const char *_resolve_dev_root (void);
Patrick Welche's avatar
Patrick Welche committed
76
#endif
77

78 79
/**
 * SECTION:gunixmounts
80
 * @include: gio/gunixmounts.h
Matthias Clasen's avatar
Matthias Clasen committed
81 82
 * @short_description: UNIX mounts
 *
83 84
 * Routines for managing mounted UNIX mount points and paths.
 *
85
 * Note that `<gio/gunixmounts.h>` belongs to the UNIX-specific GIO
86 87
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
 * file when using it.
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
 * GUnixMountType:
 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
 * 
 * Types of UNIX mounts.
 **/
typedef enum {
  G_UNIX_MOUNT_TYPE_UNKNOWN,
  G_UNIX_MOUNT_TYPE_FLOPPY,
  G_UNIX_MOUNT_TYPE_CDROM,
  G_UNIX_MOUNT_TYPE_NFS,
  G_UNIX_MOUNT_TYPE_ZIP,
  G_UNIX_MOUNT_TYPE_JAZ,
  G_UNIX_MOUNT_TYPE_MEMSTICK,
  G_UNIX_MOUNT_TYPE_CF,
  G_UNIX_MOUNT_TYPE_SM,
  G_UNIX_MOUNT_TYPE_SDMMC,
  G_UNIX_MOUNT_TYPE_IPOD,
  G_UNIX_MOUNT_TYPE_CAMERA,
  G_UNIX_MOUNT_TYPE_HD
} GUnixMountType;

struct _GUnixMountEntry {
125 126 127 128 129 130 131 132 133 134 135
  char *mount_path;
  char *device_path;
  char *filesystem_type;
  gboolean is_read_only;
  gboolean is_system_internal;
};

struct _GUnixMountPoint {
  char *mount_path;
  char *device_path;
  char *filesystem_type;
136
  char *options;
137 138 139 140 141 142 143 144
  gboolean is_read_only;
  gboolean is_user_mountable;
  gboolean is_loopback;
};

static GList *_g_get_unix_mounts (void);
static GList *_g_get_unix_mount_points (void);

145 146
static guint64 mount_poller_time = 0;

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#ifdef HAVE_SYS_MNTTAB_H
#define MNTOPT_RO	"ro"
#endif

#ifdef HAVE_MNTENT_H
#include <mntent.h>
#elif defined (HAVE_SYS_MNTTAB_H)
#include <sys/mnttab.h>
#endif

#ifdef HAVE_SYS_VFSTAB_H
#include <sys/vfstab.h>
#endif

#if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
#include <sys/mntctl.h>
#include <sys/vfs.h>
#include <sys/vmount.h>
#include <fshelp.h>
#endif

Patrick Welche's avatar
Patrick Welche committed
168
#if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
169
#include <sys/param.h>
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
#include <sys/ucred.h>
#include <sys/mount.h>
#include <fstab.h>
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#endif

#ifndef HAVE_SETMNTENT
#define setmntent(f,m) fopen(f,m)
#endif
#ifndef HAVE_ENDMNTENT
#define endmntent(f) fclose(f)
#endif

static gboolean
is_in (const char *value, const char *set[])
{
  int i;
  for (i = 0; set[i] != NULL; i++)
    {
      if (strcmp (set[i], value) == 0)
	return TRUE;
    }
  return FALSE;
}

197 198
/**
 * g_unix_is_mount_path_system_internal:
199
 * @mount_path: a mount path, e.g. `/media/disk` or `/usr`
200 201 202 203 204 205
 *
 * Determines if @mount_path is considered an implementation of the
 * OS. This is primarily used for hiding mountable and mounted volumes
 * that only are used in the OS and has little to no relevance to the
 * casual user.
 *
Matthias Clasen's avatar
Matthias Clasen committed
206 207
 * Returns: %TRUE if @mount_path is considered an implementation detail 
 *     of the OS.
208 209 210 211 212
 **/
gboolean
g_unix_is_mount_path_system_internal (const char *mount_path)
{
  const char *ignore_mountpoints[] = {
213
    /* Includes all FHS 2.3 toplevel dirs and other specialized
214 215 216 217 218
     * directories that we want to hide from the user.
     */
    "/",              /* we already have "Filesystem root" in Nautilus */ 
    "/bin",
    "/boot",
219 220
    "/compat/linux/proc",
    "/compat/linux/sys",
221 222 223 224 225
    "/dev",
    "/etc",
    "/home",
    "/lib",
    "/lib64",
226
    "/libexec",
227 228
    "/live/cow",
    "/live/image",
229 230 231
    "/media",
    "/mnt",
    "/opt",
232
    "/rescue",
233 234 235 236 237
    "/root",
    "/sbin",
    "/srv",
    "/tmp",
    "/usr",
238
    "/usr/X11R6",
239
    "/usr/local",
240 241 242 243
    "/usr/obj",
    "/usr/ports",
    "/usr/src",
    "/usr/xobj",
244
    "/var",
245 246 247
    "/var/crash",
    "/var/local",
    "/var/log",
248
    "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
249 250
    "/var/mail",
    "/var/run",
251 252 253 254
    "/var/tmp",       /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
    "/proc",
    "/sbin",
    "/net",
255
    "/sys",
256 257 258 259 260 261
    NULL
  };

  if (is_in (mount_path, ignore_mountpoints))
    return TRUE;
  
262 263 264
  if (g_str_has_prefix (mount_path, "/dev/") ||
      g_str_has_prefix (mount_path, "/proc/") ||
      g_str_has_prefix (mount_path, "/sys/"))
265 266
    return TRUE;

267
  if (g_str_has_suffix (mount_path, "/.gvfs"))
268 269 270 271 272
    return TRUE;

  return FALSE;
}

273 274 275 276 277 278 279 280 281 282
static gboolean
guess_system_internal (const char *mountpoint,
		       const char *fs,
		       const char *device)
{
  const char *ignore_fs[] = {
    "auto",
    "autofs",
    "devfs",
    "devpts",
283
    "ecryptfs",
284
    "fdescfs",
285 286
    "kernfs",
    "linprocfs",
287 288
    "mfs",
    "nullfs",
289 290 291 292 293 294 295 296 297
    "proc",
    "procfs",
    "ptyfs",
    "rootfs",
    "selinuxfs",
    "sysfs",
    "tmpfs",
    "usbfs",
    "nfsd",
298
    "rpc_pipefs",
299
    "zfs",
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    NULL
  };
  const char *ignore_devices[] = {
    "none",
    "sunrpc",
    "devpts",
    "nfsd",
    "/dev/loop",
    "/dev/vn",
    NULL
  };
  
  if (is_in (fs, ignore_fs))
    return TRUE;
  
  if (is_in (device, ignore_devices))
    return TRUE;

318
  if (g_unix_is_mount_path_system_internal (mountpoint))
319 320 321 322 323
    return TRUE;
  
  return FALSE;
}

324 325 326
/* GUnixMounts (ie: mtab) implementations {{{1 */

/* mntent.h (Linux, GNU, NSS) {{{2 */
327 328 329 330 331 332 333 334 335 336 337
#ifdef HAVE_MNTENT_H

static char *
get_mtab_read_file (void)
{
#ifdef _PATH_MOUNTED
# ifdef __linux__
  return "/proc/mounts";
# else
  return _PATH_MOUNTED;
# endif
338
#else
339 340 341 342 343 344 345 346
  return "/etc/mtab";
#endif
}

static char *
get_mtab_monitor_file (void)
{
#ifdef _PATH_MOUNTED
347 348 349
# ifdef __linux__
  return "/proc/mounts";
# else
350
  return _PATH_MOUNTED;
351
# endif
352
#else
353 354 355 356
  return "/etc/mtab";
#endif
}

357
#ifndef HAVE_GETMNTENT_R
358
G_LOCK_DEFINE_STATIC(getmntent);
359
#endif
360 361

static GList *
Matthias Clasen's avatar
Matthias Clasen committed
362
_g_get_unix_mounts (void)
363
{
364 365 366 367
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
368 369 370
  struct mntent *mntent;
  FILE *file;
  char *read_file;
371
  GUnixMountEntry *mount_entry;
372 373 374 375 376 377 378 379 380 381 382 383 384
  GHashTable *mounts_hash;
  GList *return_list;
  
  read_file = get_mtab_read_file ();

  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;

  return_list = NULL;
  
  mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
  
385 386 387
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
388 389
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
390
#endif
391 392 393 394 395
    {
      /* ignore any mnt_fsname that is repeated and begins with a '/'
       *
       * We do this to avoid being fooled by --bind mounts, since
       * these have the same device as the location they bind to.
396
       * It's not an ideal solution to the problem, but it's likely that
397 398 399 400 401 402 403 404
       * the most important mountpoint is first and the --bind ones after
       * that aren't as important. So it should work.
       *
       * The '/' is to handle procfs, tmpfs and other no device mounts.
       */
      if (mntent->mnt_fsname != NULL &&
	  mntent->mnt_fsname[0] == '/' &&
	  g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
405
        continue;
406
      
407
      mount_entry = g_new0 (GUnixMountEntry, 1);
408
      mount_entry->mount_path = g_strdup (mntent->mnt_dir);
409
      if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
410 411 412
        mount_entry->device_path = g_strdup (_resolve_dev_root ());
      else
        mount_entry->device_path = g_strdup (mntent->mnt_fsname);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
      
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
	mount_entry->is_read_only = TRUE;
#endif
      
      mount_entry->is_system_internal =
	guess_system_internal (mount_entry->mount_path,
			       mount_entry->filesystem_type,
			       mount_entry->device_path);
      
      g_hash_table_insert (mounts_hash,
			   mount_entry->device_path,
			   mount_entry->device_path);
      
      return_list = g_list_prepend (return_list, mount_entry);
    }
  g_hash_table_destroy (mounts_hash);
  
  endmntent (file);

435
#ifndef HAVE_GETMNTENT_R
436
  G_UNLOCK (getmntent);
437
#endif
438 439 440 441
  
  return g_list_reverse (return_list);
}

442
/* mnttab.h {{{2 */
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
#elif defined (HAVE_SYS_MNTTAB_H)

G_LOCK_DEFINE_STATIC(getmntent);

static char *
get_mtab_read_file (void)
{
#ifdef _PATH_MOUNTED
  return _PATH_MOUNTED;
#else	
  return "/etc/mnttab";
#endif
}

static char *
get_mtab_monitor_file (void)
{
  return get_mtab_read_file ();
}

static GList *
_g_get_unix_mounts (void)
{
  struct mnttab mntent;
  FILE *file;
  char *read_file;
469
  GUnixMountEntry *mount_entry;
470 471 472 473 474 475 476 477 478 479 480 481 482
  GList *return_list;
  
  read_file = get_mtab_read_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;
  
  return_list = NULL;
  
  G_LOCK (getmntent);
  while (! getmntent (file, &mntent))
    {
483
      mount_entry = g_new0 (GUnixMountEntry, 1);
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
      
      mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
      mount_entry->device_path = g_strdup (mntent.mnt_special);
      mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
      
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
	mount_entry->is_read_only = TRUE;
#endif

      mount_entry->is_system_internal =
	guess_system_internal (mount_entry->mount_path,
			       mount_entry->filesystem_type,
			       mount_entry->device_path);
      
      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endmntent (file);
  
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}

509
/* mntctl.h (AIX) {{{2 */
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
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)

static char *
get_mtab_monitor_file (void)
{
  return NULL;
}

static GList *
_g_get_unix_mounts (void)
{
  struct vfs_ent *fs_info;
  struct vmount *vmount_info;
  int vmount_number;
  unsigned int vmount_size;
  int current;
  GList *return_list;
  
  if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
    {
      g_warning ("Unable to know the number of mounted volumes\n");
      
      return NULL;
    }

  vmount_info = (struct vmount*)g_malloc (vmount_size);

  vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
  
  if (vmount_info->vmt_revision != VMT_REVISION)
    g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);

  if (vmount_number < 0)
    {
      g_warning ("Unable to recover mounted volumes information\n");
      
      g_free (vmount_info);
      return NULL;
    }
  
  return_list = NULL;
  while (vmount_number > 0)
    {
553
      mount_entry = g_new0 (GUnixMountEntry, 1);
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
      
      mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
      mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
      /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
      mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;

      fs_info = getvfsbytype (vmount_info->vmt_gfstype);
      
      if (fs_info == NULL)
	mount_entry->filesystem_type = g_strdup ("unknown");
      else
	mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);

      mount_entry->is_system_internal =
	guess_system_internal (mount_entry->mount_path,
			       mount_entry->filesystem_type,
			       mount_entry->device_path);
      
      return_list = g_list_prepend (return_list, mount_entry);
      
      vmount_info = (struct vmount *)( (char*)vmount_info 
				       + vmount_info->vmt_length);
      vmount_number--;
    }
  
  g_free (vmount_info);
  
  return g_list_reverse (return_list);
}

584
/* sys/mount.h {{{2 */
Patrick Welche's avatar
Patrick Welche committed
585
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
586 587 588 589 590 591 592 593 594 595

static char *
get_mtab_monitor_file (void)
{
  return NULL;
}

static GList *
_g_get_unix_mounts (void)
{
596
#if defined(USE_STATVFS)
597
  struct statvfs *mntent = NULL;
598
#elif defined(USE_STATFS)
Patrick Welche's avatar
Patrick Welche committed
599
  struct statfs *mntent = NULL;
600 601 602
#else
  #error statfs juggling failed
#endif
Patrick Welche's avatar
Patrick Welche committed
603
  size_t bufsize;
604
  int num_mounts, i;
605
  GUnixMountEntry *mount_entry;
606 607
  GList *return_list;
  
Patrick Welche's avatar
Patrick Welche committed
608
  /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
609
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
610
  num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
611
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
612 613 614 615 616 617 618
  num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
    return NULL;

  bufsize = num_mounts * sizeof (*mntent);
  mntent = g_malloc (bufsize);
619
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
620
  num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
621
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
622 623 624
  num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
625 626 627 628 629 630
    return NULL;
  
  return_list = NULL;
  
  for (i = 0; i < num_mounts; i++)
    {
631
      mount_entry = g_new0 (GUnixMountEntry, 1);
632 633 634 635
      
      mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
      mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
      mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
636 637

#if defined(USE_STATVFS)
Patrick Welche's avatar
Patrick Welche committed
638
      if (mntent[i].f_flag & ST_RDONLY)
639
#elif defined(USE_STATFS)
640
      if (mntent[i].f_flags & MNT_RDONLY)
641 642
#else
      #error statfs juggling failed
643
#endif
Patrick Welche's avatar
Patrick Welche committed
644
        mount_entry->is_read_only = TRUE;
645 646

      mount_entry->is_system_internal =
Patrick Welche's avatar
Patrick Welche committed
647 648 649
        guess_system_internal (mount_entry->mount_path,
                               mount_entry->filesystem_type,
                               mount_entry->device_path);
650 651 652
      
      return_list = g_list_prepend (return_list, mount_entry);
    }
Patrick Welche's avatar
Patrick Welche committed
653 654

  g_free (mntent);
655 656 657
  
  return g_list_reverse (return_list);
}
658 659

/* Interix {{{2 */
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
#elif defined(__INTERIX)

static char *
get_mtab_monitor_file (void)
{
  return NULL;
}

static GList *
_g_get_unix_mounts (void)
{
  DIR *dirp;
  GList* return_list = NULL;
  char filename[9 + NAME_MAX];

  dirp = opendir ("/dev/fs");
  if (!dirp)
    {
      g_warning ("unable to read /dev/fs!");
      return NULL;
    }

  while (1)
    {
      struct statvfs statbuf;
      struct dirent entry;
      struct dirent* result;
      
      if (readdir_r (dirp, &entry, &result) || result == NULL)
        break;
      
      strcpy (filename, "/dev/fs/");
      strcat (filename, entry.d_name);
      
      if (statvfs (filename, &statbuf) == 0)
        {
          GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
          
          mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
          mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
          mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
          
          if (statbuf.f_flag & ST_RDONLY)
            mount_entry->is_read_only = TRUE;
          
          return_list = g_list_prepend(return_list, mount_entry);
        }
    }
  
  return_list = g_list_reverse (return_list);
  
  closedir (dirp);

  return return_list;
}
715 716

/* Common code {{{2 */
717 718 719 720
#else
#error No _g_get_unix_mounts() implementation for system
#endif

721 722
/* GUnixMountPoints (ie: fstab) implementations {{{1 */

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
/* _g_get_unix_mount_points():
 * read the fstab.
 * don't return swap and ignore mounts.
 */

static char *
get_fstab_file (void)
{
#if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
  /* AIX */
  return "/etc/filesystems";
#elif defined(_PATH_MNTTAB)
  return _PATH_MNTTAB;
#elif defined(VFSTAB)
  return VFSTAB;
#else
  return "/etc/fstab";
#endif
}

743
/* mntent.h (Linux, GNU, NSS) {{{2 */
744 745 746 747
#ifdef HAVE_MNTENT_H
static GList *
_g_get_unix_mount_points (void)
{
748 749 750 751
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
752 753 754 755 756 757 758 759 760 761 762 763 764 765
  struct mntent *mntent;
  FILE *file;
  char *read_file;
  GUnixMountPoint *mount_entry;
  GList *return_list;
  
  read_file = get_fstab_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;

  return_list = NULL;
  
766 767 768
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
769 770
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
771
#endif
772 773
    {
      if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
774 775
          (strcmp (mntent->mnt_dir, "swap") == 0) ||
          (strcmp (mntent->mnt_dir, "none") == 0))
776
	continue;
777

778
#ifdef HAVE_HASMNTOPT
779 780 781
      /* We ignore bind fstab entries, as we ignore bind mounts anyway */
      if (hasmntopt (mntent, "bind"))
        continue;
782
#endif
783

784 785
      mount_entry = g_new0 (GUnixMountPoint, 1);
      mount_entry->mount_path = g_strdup (mntent->mnt_dir);
786 787 788 789
      if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
        mount_entry->device_path = g_strdup (_resolve_dev_root ());
      else
        mount_entry->device_path = g_strdup (mntent->mnt_fsname);
790
      mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
791
      mount_entry->options = g_strdup (mntent->mnt_opts);
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
      
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
	mount_entry->is_read_only = TRUE;
      
      if (hasmntopt (mntent, "loop") != NULL)
	mount_entry->is_loopback = TRUE;
      
#endif
      
      if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
#ifdef HAVE_HASMNTOPT
	  || (hasmntopt (mntent, "user") != NULL
	      && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
	  || hasmntopt (mntent, "pamconsole") != NULL
	  || hasmntopt (mntent, "users") != NULL
	  || hasmntopt (mntent, "owner") != NULL
#endif
	  )
	mount_entry->is_user_mountable = TRUE;
      
      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endmntent (file);
817 818

#ifndef HAVE_GETMNTENT_R
819
  G_UNLOCK (getmntent);
820
#endif
821 822 823 824
  
  return g_list_reverse (return_list);
}

825
/* mnttab.h {{{2 */
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
#elif defined (HAVE_SYS_MNTTAB_H)

static GList *
_g_get_unix_mount_points (void)
{
  struct mnttab mntent;
  FILE *file;
  char *read_file;
  GUnixMountPoint *mount_entry;
  GList *return_list;
  
  read_file = get_fstab_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;

  return_list = NULL;
  
  G_LOCK (getmntent);
  while (! getmntent (file, &mntent))
    {
      if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
849 850
          (strcmp (mntent.mnt_mountp, "swap") == 0) ||
          (strcmp (mntent.mnt_mountp, "none") == 0))
851 852 853 854 855 856 857
	continue;
      
      mount_entry = g_new0 (GUnixMountPoint, 1);
      
      mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
      mount_entry->device_path = g_strdup (mntent.mnt_special);
      mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
858
      mount_entry->options = g_strdup (mntent.mnt_mntopts);
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
      
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
	mount_entry->is_read_only = TRUE;
      
      if (hasmntopt (&mntent, "lofs") != NULL)
	mount_entry->is_loopback = TRUE;
#endif
      
      if ((mntent.mnt_fstype != NULL)
#ifdef HAVE_HASMNTOPT
	  || (hasmntopt (&mntent, "user") != NULL
	      && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
	  || hasmntopt (&mntent, "pamconsole") != NULL
	  || hasmntopt (&mntent, "users") != NULL
	  || hasmntopt (&mntent, "owner") != NULL
#endif
	  )
	mount_entry->is_user_mountable = TRUE;
      
      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endmntent (file);
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}
887 888

/* mntctl.h (AIX) {{{2 */
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
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)

/* functions to parse /etc/filesystems on aix */

/* read character, ignoring comments (begin with '*', end with '\n' */
static int
aix_fs_getc (FILE *fd)
{
  int c;
  
  while ((c = getc (fd)) == '*')
    {
      while (((c = getc (fd)) != '\n') && (c != EOF))
	;
    }
}

/* eat all continuous spaces in a file */
static int
aix_fs_ignorespace (FILE *fd)
{
  int c;
  
  while ((c = aix_fs_getc (fd)) != EOF)
    {
      if (!g_ascii_isspace (c))
	{
	  ungetc (c,fd);
	  return c;
	}
    }
  
  return EOF;
}

/* read one word from file */
static int
926 927
aix_fs_getword (FILE *fd, 
                char *word)
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
{
  int c;
  
  aix_fs_ignorespace (fd);

  while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
    {
      if (c == '"')
	{
	  while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
	    *word++ = c;
	  else
	    *word++ = c;
	}
    }
  *word = 0;
  
  return c;
}

typedef struct {
  char mnt_mount[PATH_MAX];
  char mnt_special[PATH_MAX];
  char mnt_fstype[16];
  char mnt_options[128];
} AixMountTableEntry;

/* read mount points properties */
static int
957 958
aix_fs_get (FILE               *fd, 
            AixMountTableEntry *prop)
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
{
  static char word[PATH_MAX] = { 0 };
  char value[PATH_MAX];
  
  /* read stanza */
  if (word[0] == 0)
    {
      if (aix_fs_getword (fd, word) == EOF)
	return EOF;
    }

  word[strlen(word) - 1] = 0;
  strcpy (prop->mnt_mount, word);
  
  /* read attributes and value */
  
  while (aix_fs_getword (fd, word) != EOF)
    {
      /* test if is attribute or new stanza */
      if (word[strlen(word) - 1] == ':')
	return 0;
      
      /* read "=" */
      aix_fs_getword (fd, value);
      
      /* read value */
      aix_fs_getword (fd, value);
      
      if (strcmp (word, "dev") == 0)
	strcpy (prop->mnt_special, value);
      else if (strcmp (word, "vfs") == 0)
	strcpy (prop->mnt_fstype, value);
      else if (strcmp (word, "options") == 0)
	strcpy(prop->mnt_options, value);
    }
  
  return 0;
}

static GList *
_g_get_unix_mount_points (void)
{
  struct mntent *mntent;
  FILE *file;
  char *read_file;
  GUnixMountPoint *mount_entry;
  AixMountTableEntry mntent;
  GList *return_list;
  
  read_file = get_fstab_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;
  
  return_list = NULL;
  
  while (!aix_fs_get (file, &mntent))
    {
      if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
	{
	  mount_entry = g_new0 (GUnixMountPoint, 1);
	  
	  mount_entry->mount_path = g_strdup (mntent.mnt_mount);
	  mount_entry->device_path = g_strdup (mntent.mnt_special);
	  mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1025
	  mount_entry->options = g_strdup (mntent.mnt_options);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	  mount_entry->is_read_only = TRUE;
	  mount_entry->is_user_mountable = TRUE;
	  
	  return_list = g_list_prepend (return_list, mount_entry);
	}
    }
	
  endmntent (file);
  
  return g_list_reverse (return_list);
}

Patrick Welche's avatar
Patrick Welche committed
1038
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1039 1040 1041 1042 1043 1044 1045 1046 1047

static GList *
_g_get_unix_mount_points (void)
{
  struct fstab *fstab = NULL;
  GUnixMountPoint *mount_entry;
  GList *return_list;
#ifdef HAVE_SYS_SYSCTL_H
  int usermnt = 0;
1048
  size_t len = sizeof(usermnt);
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 1087 1088
  struct stat sb;
#endif
  
  if (!setfsent ())
    return NULL;

  return_list = NULL;
  
#ifdef HAVE_SYS_SYSCTL_H
#if defined(HAVE_SYSCTLBYNAME)
  sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
#elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
  {
    int mib[2];
    
    mib[0] = CTL_VFS;
    mib[1] = VFS_USERMOUNT;
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
  }
#elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
  {
    int mib[2];
    
    mib[0] = CTL_KERN;
    mib[1] = KERN_USERMOUNT;
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
  }
#endif
#endif
  
  while ((fstab = getfsent ()) != NULL)
    {
      if (strcmp (fstab->fs_vfstype, "swap") == 0)
	continue;
      
      mount_entry = g_new0 (GUnixMountPoint, 1);
      
      mount_entry->mount_path = g_strdup (fstab->fs_file);
      mount_entry->device_path = g_strdup (fstab->fs_spec);
      mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1089
      mount_entry->options = g_strdup (fstab->fs_mntops);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
      
      if (strcmp (fstab->fs_type, "ro") == 0)
	mount_entry->is_read_only = TRUE;

#ifdef HAVE_SYS_SYSCTL_H
      if (usermnt != 0)
	{
	  uid_t uid = getuid ();
	  if (stat (fstab->fs_file, &sb) == 0)
	    {
	      if (uid == 0 || sb.st_uid == uid)
		mount_entry->is_user_mountable = TRUE;
	    }
	}
#endif

      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endfsent ();
  
  return g_list_reverse (return_list);
}
1113
/* Interix {{{2 */
1114 1115 1116 1117 1118 1119
#elif defined(__INTERIX)
static GList *
_g_get_unix_mount_points (void)
{
  return _g_get_unix_mounts ();
}
1120 1121

/* Common code {{{2 */
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
#else
#error No g_get_mount_table() implementation for system
#endif

static guint64
get_mounts_timestamp (void)
{
  const char *monitor_file;
  struct stat buf;

  monitor_file = get_mtab_monitor_file ();
  if (monitor_file)
    {
      if (stat (monitor_file, &buf) == 0)
1136 1137 1138 1139 1140
        return (guint64)buf.st_mtime;
    }
  else
    {
      return mount_poller_time;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    }
  return 0;
}

static guint64
get_mount_points_timestamp (void)
{
  const char *monitor_file;
  struct stat buf;

  monitor_file = get_fstab_file ();
  if (monitor_file)
    {
      if (stat (monitor_file, &buf) == 0)
1155
        return (guint64)buf.st_mtime;
1156 1157 1158 1159 1160
    }
  return 0;
}

/**
1161 1162
 * g_unix_mounts_get: (skip)
 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1163 1164 1165 1166
 *
 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
 * If @time_read is set, it will be filled with the mount
 * timestamp, allowing for checking if the mounts have changed
Matthias Clasen's avatar
Matthias Clasen committed
1167
 * with g_unix_mounts_changed_since().
1168
 *
1169 1170
 * Returns: (element-type GUnixMountEntry) (transfer full):
 *     a #GList of the UNIX mounts.
1171 1172
 **/
GList *
1173
g_unix_mounts_get (guint64 *time_read)
1174 1175 1176 1177 1178 1179 1180 1181
{
  if (time_read)
    *time_read = get_mounts_timestamp ();

  return _g_get_unix_mounts ();
}

/**
1182
 * g_unix_mount_at: (skip)
1183
 * @mount_path: path for a possible unix mount.
1184
 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1185
 * 
1186
 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1187 1188 1189
 * is set, it will be filled with a unix timestamp for checking
 * if the mounts have changed since with g_unix_mounts_changed_since().
 * 
1190
 * Returns: (transfer full): a #GUnixMountEntry.
1191
 **/
1192
GUnixMountEntry *
1193 1194
g_unix_mount_at (const char *mount_path,
		 guint64    *time_read)
1195 1196
{
  GList *mounts, *l;
1197
  GUnixMountEntry *mount_entry, *found;
1198
  
1199
  mounts = g_unix_mounts_get (time_read);
1200 1201 1202 1203 1204 1205

  found = NULL;
  for (l = mounts; l != NULL; l = l->next)
    {
      mount_entry = l->data;

1206
      if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1207
        found = mount_entry;
1208
      else
1209
        g_unix_mount_free (mount_entry);
1210 1211 1212 1213 1214 1215 1216
    }
  g_list_free (mounts);

  return found;
}

/**
1217 1218
 * g_unix_mount_points_get: (skip)
 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1219 1220
 *
 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1221
 * If @time_read is set, it will be filled with the mount timestamp,
1222
 * allowing for checking if the mounts have changed with
Matthias Clasen's avatar
Matthias Clasen committed
1223
 * g_unix_mount_points_changed_since().
1224
 *
1225 1226
 * Returns: (element-type GUnixMountPoint) (transfer full):
 *     a #GList of the UNIX mountpoints.
1227 1228
 **/
GList *
1229
g_unix_mount_points_get (guint64 *time_read)
1230 1231 1232 1233 1234 1235 1236 1237
{
  if (time_read)
    *time_read = get_mount_points_timestamp ();

  return _g_get_unix_mount_points ();
}

/**
1238
 * g_unix_mounts_changed_since:
1239 1240
 * @time: guint64 to contain a timestamp.
 * 
1241 1242
 * Checks if the unix mounts have changed since a given unix time.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1243
 * Returns: %TRUE if the mounts have changed since @time. 
1244 1245 1246 1247 1248 1249 1250 1251
 **/
gboolean
g_unix_mounts_changed_since (guint64 time)
{
  return get_mounts_timestamp () != time;
}

/**
1252
 * g_unix_mount_points_changed_since:
1253 1254
 * @time: guint64 to contain a timestamp.
 * 
1255 1256
 * Checks if the unix mount points have changed since a given unix time.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1257
 * Returns: %TRUE if the mount points have changed since @time. 
1258 1259 1260 1261 1262 1263 1264
 **/
gboolean
g_unix_mount_points_changed_since (guint64 time)
{
  return get_mount_points_timestamp () != time;
}

1265 1266
/* GUnixMountMonitor {{{1 */

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
enum {
  MOUNTS_CHANGED,
  MOUNTPOINTS_CHANGED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

struct _GUnixMountMonitor {
  GObject parent;

1278
  GMainContext *context;
1279 1280 1281 1282 1283 1284 1285 1286 1287
};

struct _GUnixMountMonitorClass {
  GObjectClass parent_class;
};


G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);

1288 1289 1290 1291 1292
static GContextSpecificGroup  mount_monitor_group;
static GFileMonitor          *fstab_monitor;
static GFileMonitor          *mtab_monitor;
static GSource               *proc_mounts_watch_source;
static GList                 *mount_poller_mounts;
1293 1294

static void
1295
fstab_file_changed (GFileMonitor      *monitor,
1296 1297 1298 1299
                    GFile             *file,
                    GFile             *other_file,
                    GFileMonitorEvent  event_type,
                    gpointer           user_data)
1300 1301 1302 1303 1304 1305
{
  if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
      event_type != G_FILE_MONITOR_EVENT_CREATED &&
      event_type != G_FILE_MONITOR_EVENT_DELETED)
    return;

1306
  g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1307 1308 1309
}

static void
1310
mtab_file_changed (GFileMonitor      *monitor,
1311 1312 1313 1314
                   GFile             *file,
                   GFile             *other_file,
                   GFileMonitorEvent  event_type,
                   gpointer           user_data)
1315 1316 1317 1318 1319
{
  if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
      event_type != G_FILE_MONITOR_EVENT_CREATED &&
      event_type != G_FILE_MONITOR_EVENT_DELETED)
    return;
1320 1321

  g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
1322 1323
}

1324 1325 1326 1327 1328
static gboolean
proc_mounts_changed (GIOChannel   *channel,
                     GIOCondition  cond,
                     gpointer      user_data)
{
1329
  if (cond & G_IO_ERR)
1330 1331
    g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);

1332 1333 1334
  return TRUE;
}

1335 1336 1337
static gboolean
mount_change_poller (gpointer user_data)
{
1338
  GList *current_mounts, *new_it, *old_it;
1339 1340 1341 1342
  gboolean has_changed = FALSE;

  current_mounts = _g_get_unix_mounts ();

1343
  for ( new_it = current_mounts, old_it = mount_poller_mounts;
1344 1345
        new_it != NULL && old_it != NULL;
        new_it = g_list_next (new_it), old_it = g_list_next (old_it) )
1346
    {
1347
      if (g_unix_mount_compare (new_it->data, old_it->data) != 0)
1348
        {
1349 1350
          has_changed = TRUE;
          break;
1351 1352
        }
    }
1353 1354 1355
  if (!(new_it == NULL && old_it == NULL))
    has_changed = TRUE;

1356
  g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
1357

1358
  mount_poller_mounts = current_mounts;
1359 1360 1361

  if (has_changed)
    {
1362 1363
      mount_poller_time = (guint64) g_get_monotonic_time ();
      g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1364 1365 1366 1367 1368
    }

  return TRUE;
}

1369

1370
static void
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
mount_monitor_stop (void)
{
  if (fstab_monitor)
    {
      g_file_monitor_cancel (fstab_monitor);
      g_object_unref (fstab_monitor);
    }

  if (proc_mounts_watch_source != NULL)
    g_source_destroy (proc_mounts_watch_source);

  if (mtab_monitor)
    {
      g_file_monitor_cancel (mtab_monitor);
      g_object_unref (mtab_monitor);
    }

  g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
}

static void
mount_monitor_start (void)
1393 1394
{
  GFile *file;
1395

1396 1397 1398
  if (get_fstab_file () != NULL)
    {
      file = g_file_new_for_path (get_fstab_file ());
1399
      fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1400
      g_object_unref (file);
1401 1402

      g_signal_connect (fstab_monitor, "changed", (GCallback)fstab_file_changed, NULL);
1403
    }
1404

1405 1406
  if (get_mtab_monitor_file () != NULL)
    {
1407 1408 1409
      const gchar *mtab_path;

      mtab_path = get_mtab_monitor_file ();
1410 1411 1412
      /* /proc/mounts monitoring is special - can't just use GFileMonitor.
       * See 'man proc' for more details.
       */
1413 1414
      if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
        {
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
          GIOChannel *proc_mounts_channel;
          GError *error = NULL;
          proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
          if (proc_mounts_channel == NULL)
            {
              g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
                         error->message, g_quark_to_string (error->domain), error->code);
              g_error_free (error);
            }
          else
            {
1426 1427
              proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
              g_source_set_callback (proc_mounts_watch_source,
1428
                                     (GSourceFunc) proc_mounts_changed,
1429 1430
                                     NULL, NULL);
              g_source_attach (proc_mounts_watch_source,
1431
                               g_main_context_get_thread_default ());
1432
              g_source_unref (proc_mounts_watch_source);
1433 1434
              g_io_channel_unref (proc_mounts_channel);
            }
1435 1436 1437
        }
      else
        {
1438
          file = g_file_new_for_path (mtab_path);
1439
          mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1440
          g_object_unref (file);
1441
          g_signal_connect (mtab_monitor, "changed", (GCallback)mtab_file_changed, NULL);
1442
        }
1443
    }
1444 1445
  else
    {
1446 1447
      proc_mounts_watch_source = g_timeout_source_new_seconds (3);
      mount_poller_mounts = _g_get_unix_mounts ();
1448
      mount_poller_time = (guint64)g_get_monotonic_time ();
1449 1450 1451 1452
      g_source_set_callback (proc_mounts_watch_source,
                             mount_change_poller,
                             NULL, NULL);
      g_source_attach (proc_mounts_watch_source,
1453
                       g_main_context_get_thread_default ());
1454
      g_source_unref (proc_mounts_watch_source);
1455
    }
1456 1457
}

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
static void
g_unix_mount_monitor_finalize (GObject *object)
{
  GUnixMountMonitor *monitor;

  monitor = G_UNIX_MOUNT_MONITOR (object);

  g_context_specific_group_remove (&mount_monitor_group, monitor->context, monitor, mount_monitor_stop);

  G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
}

static void
g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_unix_mount_monitor_finalize;
 
  /**
   * GUnixMountMonitor::mounts-changed:
   * @monitor: the object on which the signal is emitted
   * 
   * Emitted when the unix mounts have changed.
   */ 
  signals[MOUNTS_CHANGED] =
    g_signal_new ("mounts-changed",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  0,
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  /**
   * GUnixMountMonitor::mountpoints-changed:
   * @monitor: the object on which the signal is emitted
   * 
   * Emitted when the unix mount points have changed.
   */
  signals[MOUNTPOINTS_CHANGED] =
    g_signal_new ("mountpoints-changed",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  0,
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
}

static void
g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
{
}

1513 1514
/**
 * g_unix_mount_monitor_set_rate_limit:
Matthias Clasen's avatar
Matthias Clasen committed
1515 1516 1517
 * @mount_monitor: a #GUnixMountMonitor
 * @limit_msec: a integer with the limit in milliseconds to
 *     poll for changes.
1518
 *
1519 1520 1521 1522 1523 1524 1525
 * This function does nothing.
 *
 * Before 2.44, this was a partially-effective way of controlling the
 * rate at which events would be reported under some uncommon
 * circumstances.  Since @mount_monitor is a singleton, it also meant
 * that calling this function would have side effects for other users of
 * the monitor.
1526 1527
 *
 * Since: 2.18
1528 1529
 *
 * Deprecated:2.44:This function does nothing.  Don't call it.
Matthias Clasen's avatar
Matthias Clasen committed
1530
 */
1531 1532
void
g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
Matthias Clasen's avatar
Matthias Clasen committed
1533
                                     gint               limit_msec)
1534 1535 1536
{
}

1537
/**
1538 1539
 * g_unix_mount_monitor_get:
 *
1540 1541
 * Gets the #GUnixMountMonitor for the current thread-default main
 * context.
1542 1543 1544 1545 1546
 *
 * The mount monitor can be used to monitor for changes to the list of
 * mounted filesystems as well as the list of mount points (ie: fstab
 * entries).
 *
1547 1548 1549
 * You must only call g_object_unref() on the return value from under
 * the same main context as you called this function.
 *
1550 1551 1552 1553
 * Returns: (transfer full): the #GUnixMountMonitor.
 *
 * Since: 2.44
 **/
1554
GUnixMountMonitor *
1555
g_unix_mount_monitor_get (void)
1556
{
1557 1558 1559 1560
  return g_context_specific_group_get (&mount_monitor_group,
                                       G_TYPE_UNIX_MOUNT_MONITOR,
                                       G_STRUCT_OFFSET(GUnixMountMonitor, context),
                                       mount_monitor_start);
1561 1562
}

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
/**
 * g_unix_mount_monitor_new:
 *
 * Deprecated alias for g_unix_mount_monitor_get().
 *
 * This function was never a true constructor, which is why it was
 * renamed.
 *
 * Returns: a #GUnixMountMonitor.
 *
 * Deprecated:2.44:Use g_unix_mount_monitor_get() instead.
 */
GUnixMountMonitor *
g_unix_mount_monitor_new (void)
{
  return g_unix_mount_monitor_get ();
}

1581
/* GUnixMount {{{1 */
1582 1583
/**
 * g_unix_mount_free:
1584
 * @mount_entry: a #GUnixMountEntry.
1585
 * 
1586
 * Frees a unix mount.
Matthias Clasen's avatar
Matthias Clasen committed
1587
 */
1588
void
1589
g_unix_mount_free (GUnixMountEntry *mount_entry)
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
{
  g_return_if_fail (mount_entry != NULL);

  g_free (mount_entry->mount_path);
  g_free (mount_entry->device_path);
  g_free (mount_entry->filesystem_type);
  g_free (mount_entry);
}

/**
 * g_unix_mount_point_free:
1601
 * @mount_point: unix mount point to free.
1602
 * 
1603
 * Frees a unix mount point.
Matthias Clasen's avatar
Matthias Clasen committed
1604
 */
1605 1606 1607 1608 1609 1610 1611 1612
void
g_unix_mount_point_free (GUnixMountPoint *mount_point)
{
  g_return_if_fail (mount_point != NULL);

  g_free (mount_point->mount_path);
  g_free (mount_point->device_path);
  g_free (mount_point->filesystem_type);
1613
  g_free (mount_point->options);
1614 1615 1616 1617 1618
  g_free (mount_point);
}

/**
 * g_unix_mount_compare:
1619 1620
 * @mount1: first #GUnixMountEntry to compare.
 * @mount2: second #GUnixMountEntry to compare.
1621
 * 
1622 1623
 * Compares two unix mounts.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1624
 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1625
 * or less than @mount2, respectively. 
Matthias Clasen's avatar
Matthias Clasen committed
1626
 */
1627
gint
1628 1629
g_unix_mount_compare (GUnixMountEntry *mount1,
		      GUnixMountEntry *mount2)
1630 1631 1632 1633 1634
{
  int res;

  g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
  
1635
  res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1636 1637 1638
  if (res != 0)
    return res;
	
1639
  res = g_strcmp0 (mount1->device_path, mount2->device_path);
1640 1641 1642
  if (res != 0)
    return res;
	
1643
  res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
  if (res != 0)
    return res;

  res =  mount1->is_read_only - mount2->is_read_only;
  if (res != 0)
    return res;
  
  return 0;
}

/**
 * g_unix_mount_get_mount_path:
1656
 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1657
 * 
1658
 * Gets the mount path for a unix mount.
1659
 * 
1660
 * Returns: the mount path for @mount_entry.
Matthias Clasen's avatar
Matthias Clasen committed
1661 1662
 */
const gchar *
1663
g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
{
  g_return_val_if_fail (mount_entry != NULL, NULL);

  return mount_entry->mount_path;
}

/**
 * g_unix_mount_get_device_path:
 * @mount_entry: a #GUnixMount.
 * 
1674 1675 1676
 * Gets the device path for a unix mount.
 * 
 * Returns: a string containing the device path.
Matthias Clasen's avatar
Matthias Clasen committed
1677 1678
 */
const gchar *
1679
g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
{
  g_return_val_if_fail (mount_entry != NULL, NULL);

  return mount_entry->device_path;
}

/**
 * g_unix_mount_get_fs_type:
 * @mount_entry: a #GUnixMount.
 * 
1690 1691 1692
 * Gets the filesystem type for the unix mount.
 * 
 * Returns: a string containing the file system type.
Matthias Clasen's avatar
Matthias Clasen committed
1693 1694
 */
const gchar *
1695
g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
{
  g_return_val_if_fail (mount_entry != NULL, NULL);

  return mount_entry->filesystem_type;
}

/**
 * g_unix_mount_is_readonly:
 * @mount_entry: a #GUnixMount.
 * 
1706
 * Checks if a unix mount is mounted read only.
1707
 * 
1708
 * Returns: %TRUE if @mount_entry is read only.
Matthias Clasen's avatar
Matthias Clasen committed
1709
 */
1710
gboolean
1711
g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
{
  g_return_val_if_fail (mount_entry != NULL, FALSE);

  return mount_entry->is_read_only;
}

/**
 * g_unix_mount_is_system_internal:
 * @mount_entry: a #GUnixMount.
 * 
1722 1723 1724
 * Checks if a unix mount is a system path.
 * 
 * Returns: %TRUE if the unix mount is for a system path.
Matthias Clasen's avatar
Matthias Clasen committed
1725
 */
1726
gboolean
1727
g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1728 1729 1730 1731 1732 1733
{
  g_return_val_if_fail (mount_entry != NULL, FALSE);

  return mount_entry->is_system_internal;
}

1734
/* GUnixMountPoint {{{1 */
1735 1736 1737 1738 1739
/**
 * g_unix_mount_point_compare:
 * @mount1: a #GUnixMount.
 * @mount2: a #GUnixMount.
 * 
1740 1741
 * Compares two unix mount points.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1742
 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1743
 * or less than @mount2, respectively.
Matthias Clasen's avatar
Matthias Clasen committed
1744
 */
1745 1746 1747 1748 1749 1750 1751 1752
gint
g_unix_mount_point_compare (GUnixMountPoint *mount1,
			    GUnixMountPoint *mount2)
{
  int res;

  g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);

1753
  res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1754 1755 1756
  if (res != 0) 
    return res;
	
1757
  res = g_strcmp0 (mount1->device_path, mount2->device_path);
1758 1759 1760
  if (res != 0) 
    return res;
	
1761
  res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1762 1763 1764
  if (res != 0) 
    return res;

1765 1766 1767 1768
  res = g_strcmp0 (mount1->options, mount2->options);
  if (res != 0) 
    return res;

1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  res =  mount1->is_read_only - mount2->is_read_only;
  if (res != 0) 
    return res;

  res = mount1->is_user_mountable - mount2->is_user_mountable;
  if (res != 0) 
    return res;

  res = mount1->is_loopback - mount2->is_loopback;
  if (res != 0)
    return res;
  
  return 0;
}

/**
 * g_unix_mount_point_get_mount_path:
1786 1787 1788
 * @mount_point: a #GUnixMountPoint.
 * 
 * Gets the mount path for a unix mount point.
1789
 * 
1790
 * Returns: a string containing the mount path.
Matthias Clasen's avatar
Matthias Clasen committed
1791 1792
 */
const gchar *
Alexander Larsson's avatar