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

3 4 5 6 7 8 9
/* 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
10
 * version 2.1 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
#include "gunixmounts.h"
67
#include "glocalfileprivate.h"
68 69
#include "gfile.h"
#include "gfilemonitor.h"
70 71
#include "glibintl.h"
#include "gthemedicon.h"
72
#include "gcontextspecificgroup.h"
73

74

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

79 80
/**
 * SECTION:gunixmounts
81
 * @include: gio/gunixmounts.h
Matthias Clasen's avatar
Matthias Clasen committed
82 83
 * @short_description: UNIX mounts
 *
84 85
 * Routines for managing mounted UNIX mount points and paths.
 *
86
 * Note that `<gio/gunixmounts.h>` belongs to the UNIX-specific GIO
87 88
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
 * file when using it.
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
 * 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 {
126 127 128
  char *mount_path;
  char *device_path;
  char *filesystem_type;
129
  char *options;
130 131 132 133
  gboolean is_read_only;
  gboolean is_system_internal;
};

134 135 136
G_DEFINE_BOXED_TYPE (GUnixMountEntry, g_unix_mount_entry,
                     g_unix_mount_copy, g_unix_mount_free)

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

147 148 149
G_DEFINE_BOXED_TYPE (GUnixMountPoint, g_unix_mount_point,
                     g_unix_mount_point_copy, g_unix_mount_point_free)

150 151
static GList *_g_get_unix_mounts (void);
static GList *_g_get_unix_mount_points (void);
152
static gboolean proc_mounts_watch_is_running (void);
153

154 155
static guint64 mount_poller_time = 0;

156 157 158 159 160 161
#ifdef HAVE_SYS_MNTTAB_H
#define MNTOPT_RO	"ro"
#endif

#ifdef HAVE_MNTENT_H
#include <mntent.h>
162
#ifdef HAVE_LIBMOUNT
163
#include <libmount.h>
164
#endif
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#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
180
#if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
181
#include <sys/param.h>
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
#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;
}

209 210
/**
 * g_unix_is_mount_path_system_internal:
211
 * @mount_path: (type filename): a mount path, e.g. `/media/disk` or `/usr`
212 213 214 215 216 217
 *
 * 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
218 219
 * Returns: %TRUE if @mount_path is considered an implementation detail 
 *     of the OS.
220 221 222 223 224
 **/
gboolean
g_unix_is_mount_path_system_internal (const char *mount_path)
{
  const char *ignore_mountpoints[] = {
225
    /* Includes all FHS 2.3 toplevel dirs and other specialized
226 227 228 229 230
     * directories that we want to hide from the user.
     */
    "/",              /* we already have "Filesystem root" in Nautilus */ 
    "/bin",
    "/boot",
231 232
    "/compat/linux/proc",
    "/compat/linux/sys",
233 234 235 236 237
    "/dev",
    "/etc",
    "/home",
    "/lib",
    "/lib64",
238
    "/libexec",
239 240
    "/live/cow",
    "/live/image",
241 242 243
    "/media",
    "/mnt",
    "/opt",
244
    "/rescue",
245 246 247 248 249
    "/root",
    "/sbin",
    "/srv",
    "/tmp",
    "/usr",
250
    "/usr/X11R6",
251
    "/usr/local",
252 253 254 255
    "/usr/obj",
    "/usr/ports",
    "/usr/src",
    "/usr/xobj",
256
    "/var",
257 258 259
    "/var/crash",
    "/var/local",
    "/var/log",
260
    "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
261 262
    "/var/mail",
    "/var/run",
263 264 265 266
    "/var/tmp",       /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
    "/proc",
    "/sbin",
    "/net",
267
    "/sys",
268 269 270 271 272 273
    NULL
  };

  if (is_in (mount_path, ignore_mountpoints))
    return TRUE;
  
274 275 276
  if (g_str_has_prefix (mount_path, "/dev/") ||
      g_str_has_prefix (mount_path, "/proc/") ||
      g_str_has_prefix (mount_path, "/sys/"))
277 278
    return TRUE;

279
  if (g_str_has_suffix (mount_path, "/.gvfs"))
280 281 282 283 284
    return TRUE;

  return FALSE;
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/**
 * g_unix_is_system_fs_type:
 * @fs_type: a file system type, e.g. `procfs` or `tmpfs`
 *
 * Determines if @fs_type is considered a type of file system which is only
 * used in implementation of the OS. This is primarily used for hiding
 * mounted volumes that are intended as APIs for programs to read, and system
 * administrators at a shell; rather than something that should, for example,
 * appear in a GUI. For example, the Linux `/proc` filesystem.
 *
 * The list of file system types considered ‘system’ ones may change over time.
 *
 * Returns: %TRUE if @fs_type is considered an implementation detail of the OS.
 * Since: 2.56
 */
gboolean
g_unix_is_system_fs_type (const char *fs_type)
302 303
{
  const char *ignore_fs[] = {
304 305
    "adfs",
    "afs",
306 307
    "auto",
    "autofs",
308 309 310 311 312
    "autofs4",
    "cgroup",
    "configfs",
    "cxfs",
    "debugfs",
313 314
    "devfs",
    "devpts",
315
    "devtmpfs",
316
    "ecryptfs",
317
    "fdescfs",
318 319 320 321 322
    "fusectl",
    "gfs",
    "gfs2",
    "gpfs",
    "hugetlbfs",
323 324
    "kernfs",
    "linprocfs",
325 326 327
    "linsysfs",
    "lustre",
    "lustre_lite",
328
    "mfs",
329 330 331
    "mqueue",
    "ncpfs",
    "nfsd",
332
    "nullfs",
333 334
    "ocfs2",
    "overlay",
335 336
    "proc",
    "procfs",
337
    "pstore",
338 339
    "ptyfs",
    "rootfs",
340 341
    "rpc_pipefs",
    "securityfs",
342 343 344 345
    "selinuxfs",
    "sysfs",
    "tmpfs",
    "usbfs",
346
    "zfs",
347 348
    NULL
  };
349 350 351 352 353 354 355 356

  g_return_val_if_fail (fs_type != NULL && *fs_type != '\0', FALSE);

  return is_in (fs_type, ignore_fs);
}

/**
 * g_unix_is_system_device_path:
357
 * @device_path: a device path, e.g. `/dev/loop0` or `nfsd`
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
 *
 * Determines if @device_path is considered a block device path which is only
 * used in implementation of the OS. This is primarily used for hiding
 * mounted volumes that are intended as APIs for programs to read, and system
 * administrators at a shell; rather than something that should, for example,
 * appear in a GUI. For example, the Linux `/proc` filesystem.
 *
 * The list of device paths considered ‘system’ ones may change over time.
 *
 * Returns: %TRUE if @device_path is considered an implementation detail of
 *    the OS.
 * Since: 2.56
 */
gboolean
g_unix_is_system_device_path (const char *device_path)
{
374 375 376 377 378 379 380 381 382
  const char *ignore_devices[] = {
    "none",
    "sunrpc",
    "devpts",
    "nfsd",
    "/dev/loop",
    "/dev/vn",
    NULL
  };
383 384 385 386 387 388 389 390 391 392 393 394

  g_return_val_if_fail (device_path != NULL && *device_path != '\0', FALSE);

  return is_in (device_path, ignore_devices);
}

static gboolean
guess_system_internal (const char *mountpoint,
		       const char *fs,
		       const char *device)
{
  if (g_unix_is_system_fs_type (fs))
395 396
    return TRUE;
  
397
  if (g_unix_is_system_device_path (device))
398 399
    return TRUE;

400
  if (g_unix_is_mount_path_system_internal (mountpoint))
401 402 403 404 405
    return TRUE;
  
  return FALSE;
}

406 407
/* GUnixMounts (ie: mtab) implementations {{{1 */

408 409 410 411
static GUnixMountEntry *
create_unix_mount_entry (const char *device_path,
                         const char *mount_path,
                         const char *filesystem_type,
412
                         const char *options,
413 414 415 416 417 418 419 420
                         gboolean    is_read_only)
{
  GUnixMountEntry *mount_entry = NULL;

  mount_entry = g_new0 (GUnixMountEntry, 1);
  mount_entry->device_path = g_strdup (device_path);
  mount_entry->mount_path = g_strdup (mount_path);
  mount_entry->filesystem_type = g_strdup (filesystem_type);
421
  mount_entry->options = g_strdup (options);
422 423 424 425 426 427 428 429 430
  mount_entry->is_read_only = is_read_only;

  mount_entry->is_system_internal =
    guess_system_internal (mount_entry->mount_path,
                           mount_entry->filesystem_type,
                           mount_entry->device_path);
  return mount_entry;
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
static GUnixMountPoint *
create_unix_mount_point (const char *device_path,
                         const char *mount_path,
                         const char *filesystem_type,
                         const char *options,
                         gboolean    is_read_only,
                         gboolean    is_user_mountable,
                         gboolean    is_loopback)
{
  GUnixMountPoint *mount_point = NULL;

  mount_point = g_new0 (GUnixMountPoint, 1);
  mount_point->device_path = g_strdup (device_path);
  mount_point->mount_path = g_strdup (mount_path);
  mount_point->filesystem_type = g_strdup (filesystem_type);
  mount_point->options = g_strdup (options);
  mount_point->is_read_only = is_read_only;
  mount_point->is_user_mountable = is_user_mountable;
  mount_point->is_loopback = is_loopback;

  return mount_point;
}

454
/* mntent.h (Linux, GNU, NSS) {{{2 */
455 456
#ifdef HAVE_MNTENT_H

457 458
#ifdef HAVE_LIBMOUNT

459 460 461 462 463
/* For documentation on /proc/self/mountinfo see
 * http://www.kernel.org/doc/Documentation/filesystems/proc.txt
 */
#define PROC_MOUNTINFO_PATH "/proc/self/mountinfo"

464 465
static GList *
_g_get_unix_mounts (void)
466
{
467 468 469 470 471 472
  struct libmnt_table *table = NULL;
  struct libmnt_iter* iter = NULL;
  struct libmnt_fs *fs = NULL;
  GUnixMountEntry *mount_entry = NULL;
  GList *return_list = NULL;

473 474
  table = mnt_new_table ();
  if (mnt_table_parse_mtab (table, NULL) < 0)
475
    goto out;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

  iter = mnt_new_iter (MNT_ITER_FORWARD);
  while (mnt_table_next_fs (table, iter, &fs) == 0)
    {
      const char *device_path = NULL;
      char *mount_options = NULL;
      unsigned long mount_flags = 0;
      gboolean is_read_only = FALSE;

      device_path = mnt_fs_get_source (fs);
      if (g_strcmp0 (device_path, "/dev/root") == 0)
        device_path = _resolve_dev_root ();

      mount_options = mnt_fs_strdup_options (fs);
      if (mount_options)
        {
          mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
          g_free (mount_options);
        }
      is_read_only = (mount_flags & MS_RDONLY) ? TRUE : FALSE;

      mount_entry = create_unix_mount_entry (device_path,
                                             mnt_fs_get_target (fs),
                                             mnt_fs_get_fstype (fs),
500
                                             mnt_fs_get_options (fs),
501 502 503 504 505
                                             is_read_only);

      return_list = g_list_prepend (return_list, mount_entry);
    }
  mnt_free_iter (iter);
506 507

 out:
508
  mnt_free_table (table);
509 510

  return g_list_reverse (return_list);
511 512
}

513 514
#else

515
static const char *
516
get_mtab_read_file (void)
517 518
{
#ifdef _PATH_MOUNTED
519 520 521
# ifdef __linux__
  return "/proc/mounts";
# else
522
  return _PATH_MOUNTED;
523
# endif
524
#else
525 526 527 528
  return "/etc/mtab";
#endif
}

529
#ifndef HAVE_GETMNTENT_R
530
G_LOCK_DEFINE_STATIC(getmntent);
531
#endif
532 533

static GList *
Matthias Clasen's avatar
Matthias Clasen committed
534
_g_get_unix_mounts (void)
535
{
536 537 538 539
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
540 541
  struct mntent *mntent;
  FILE *file;
542
  const char *read_file;
543
  GUnixMountEntry *mount_entry;
544 545 546 547 548 549 550 551 552 553 554 555 556
  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);
  
557 558 559
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
560 561
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
562
#endif
563
    {
564 565 566
      const char *device_path = NULL;
      gboolean is_read_only = FALSE;

567 568 569 570
      /* 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.
571
       * It's not an ideal solution to the problem, but it's likely that
572 573 574 575 576 577 578 579
       * 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))
580
        continue;
581

582
      if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
583
        device_path = _resolve_dev_root ();
584
      else
585 586
        device_path = mntent->mnt_fsname;

587 588
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
589
	is_read_only = TRUE;
590
#endif
591 592 593 594

      mount_entry = create_unix_mount_entry (device_path,
                                             mntent->mnt_dir,
                                             mntent->mnt_type,
595
                                             mntent->mnt_opts,
596 597
                                             is_read_only);

598 599 600
      g_hash_table_insert (mounts_hash,
			   mount_entry->device_path,
			   mount_entry->device_path);
601

602 603 604 605 606 607
      return_list = g_list_prepend (return_list, mount_entry);
    }
  g_hash_table_destroy (mounts_hash);
  
  endmntent (file);

608
#ifndef HAVE_GETMNTENT_R
609
  G_UNLOCK (getmntent);
610
#endif
611 612 613 614
  
  return g_list_reverse (return_list);
}

615 616
#endif /* HAVE_LIBMOUNT */

617
static const char *
618 619
get_mtab_monitor_file (void)
{
620
  static const char *mountinfo_path = NULL;
621 622 623 624 625 626 627 628
#ifdef HAVE_LIBMOUNT
  struct stat buf;
#endif

  if (mountinfo_path != NULL)
    return mountinfo_path;

#ifdef HAVE_LIBMOUNT
629 630 631 632 633 634 635 636 637
  /* The mtab file is still used by some distros, so it has to be monitored in
   * order to avoid races between g_unix_mounts_get and "mounts-changed" signal:
   * https://bugzilla.gnome.org/show_bug.cgi?id=782814
   */
  if (mnt_has_regular_mtab (&mountinfo_path, NULL))
    {
      return mountinfo_path;
    }

638 639 640 641 642 643 644
  if (stat (PROC_MOUNTINFO_PATH, &buf) == 0)
    {
      mountinfo_path = PROC_MOUNTINFO_PATH;
      return mountinfo_path;
    }
#endif

645 646
#ifdef _PATH_MOUNTED
# ifdef __linux__
647
  mountinfo_path = "/proc/mounts";
648
# else
649
  mountinfo_path = _PATH_MOUNTED;
650 651
# endif
#else
652
  mountinfo_path = "/etc/mtab";
653
#endif
654 655

  return mountinfo_path;
656 657
}

658
/* mnttab.h {{{2 */
659 660 661 662
#elif defined (HAVE_SYS_MNTTAB_H)

G_LOCK_DEFINE_STATIC(getmntent);

663
static const char *
664 665 666 667 668 669 670 671 672
get_mtab_read_file (void)
{
#ifdef _PATH_MOUNTED
  return _PATH_MOUNTED;
#else	
  return "/etc/mnttab";
#endif
}

673
static const char *
674 675 676 677 678 679 680 681 682 683
get_mtab_monitor_file (void)
{
  return get_mtab_read_file ();
}

static GList *
_g_get_unix_mounts (void)
{
  struct mnttab mntent;
  FILE *file;
684
  const char *read_file;
685
  GUnixMountEntry *mount_entry;
686 687 688 689 690 691 692 693 694 695 696 697 698
  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))
    {
699 700
      gboolean is_read_only = FALSE;

701 702
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
703
	is_read_only = TRUE;
704 705
#endif

706 707 708
      mount_entry = create_unix_mount_entry (mntent.mnt_special,
                                             mntent.mnt_mountp,
                                             mntent.mnt_fstype,
709
                                             mntent.mnt_opts,
710 711
                                             is_read_only);

712 713 714 715 716 717 718 719 720 721
      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endmntent (file);
  
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}

722
/* mntctl.h (AIX) {{{2 */
723 724
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)

725
static const char *
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
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)
    {
743
      g_warning ("Unable to know the number of mounted volumes");
744 745 746 747 748 749 750 751 752
      
      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)
753
    g_warning ("Bad vmount structure revision number, want %d, got %d", VMT_REVISION, vmount_info->vmt_revision);
754 755 756

  if (vmount_number < 0)
    {
757
      g_warning ("Unable to recover mounted volumes information");
758 759 760 761 762 763 764 765
      
      g_free (vmount_info);
      return NULL;
    }
  
  return_list = NULL;
  while (vmount_number > 0)
    {
766
      gboolean is_read_only = FALSE;
767 768 769

      fs_info = getvfsbytype (vmount_info->vmt_gfstype);

770 771 772 773 774 775
      /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
      is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;

      mount_entry = create_unix_mount_entry (vmt2dataptr (vmount_info, VMT_OBJECT),
                                             vmt2dataptr (vmount_info, VMT_STUB),
                                             fs_info == NULL ? "unknown" : fs_info->vfsent_name,
776
                                             NULL,
777 778
                                             is_read_only);

779 780 781 782 783 784 785 786 787 788 789 790
      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);
}

791
/* sys/mount.h {{{2 */
Patrick Welche's avatar
Patrick Welche committed
792
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
793

794
static const char *
795 796 797 798 799 800 801 802
get_mtab_monitor_file (void)
{
  return NULL;
}

static GList *
_g_get_unix_mounts (void)
{
803
#if defined(USE_STATVFS)
804
  struct statvfs *mntent = NULL;
805
#elif defined(USE_STATFS)
Patrick Welche's avatar
Patrick Welche committed
806
  struct statfs *mntent = NULL;
807 808 809
#else
  #error statfs juggling failed
#endif
Patrick Welche's avatar
Patrick Welche committed
810
  size_t bufsize;
811
  int num_mounts, i;
812
  GUnixMountEntry *mount_entry;
813 814
  GList *return_list;
  
Patrick Welche's avatar
Patrick Welche committed
815
  /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
816
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
817
  num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
818
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
819 820 821 822 823 824 825
  num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
    return NULL;

  bufsize = num_mounts * sizeof (*mntent);
  mntent = g_malloc (bufsize);
826
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
827
  num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
828
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
829 830 831
  num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
832 833 834 835 836 837
    return NULL;
  
  return_list = NULL;
  
  for (i = 0; i < num_mounts; i++)
    {
838
      gboolean is_read_only = FALSE;
839 840

#if defined(USE_STATVFS)
Patrick Welche's avatar
Patrick Welche committed
841
      if (mntent[i].f_flag & ST_RDONLY)
842
#elif defined(USE_STATFS)
843
      if (mntent[i].f_flags & MNT_RDONLY)
844 845
#else
      #error statfs juggling failed
846
#endif
847 848 849 850 851
        is_read_only = TRUE;

      mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
                                             mntent[i].f_mntonname,
                                             mntent[i].f_fstypename,
852
                                             NULL,
853
                                             is_read_only);
854 855 856

      return_list = g_list_prepend (return_list, mount_entry);
    }
Patrick Welche's avatar
Patrick Welche committed
857 858

  g_free (mntent);
859 860 861
  
  return g_list_reverse (return_list);
}
862 863

/* Interix {{{2 */
864 865
#elif defined(__INTERIX)

866
static const char *
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
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;
}
919 920

/* Common code {{{2 */
921 922 923 924
#else
#error No _g_get_unix_mounts() implementation for system
#endif

925 926
/* GUnixMountPoints (ie: fstab) implementations {{{1 */

927 928 929 930 931 932 933 934
/* _g_get_unix_mount_points():
 * read the fstab.
 * don't return swap and ignore mounts.
 */

static char *
get_fstab_file (void)
{
935 936 937
#ifdef HAVE_LIBMOUNT
  return (char *) mnt_get_fstab_path ();
#else
938 939 940 941 942 943 944 945 946 947
#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
948
#endif
949 950
}

951
/* mntent.h (Linux, GNU, NSS) {{{2 */
952
#ifdef HAVE_MNTENT_H
953 954 955 956 957 958 959 960 961 962 963 964

#ifdef HAVE_LIBMOUNT

static GList *
_g_get_unix_mount_points (void)
{
  struct libmnt_table *table = NULL;
  struct libmnt_iter* iter = NULL;
  struct libmnt_fs *fs = NULL;
  GUnixMountPoint *mount_point = NULL;
  GList *return_list = NULL;

965
  table = mnt_new_table ();
966
  if (mnt_table_parse_fstab (table, NULL) < 0)
967
    goto out;
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

  iter = mnt_new_iter (MNT_ITER_FORWARD);
  while (mnt_table_next_fs (table, iter, &fs) == 0)
    {
      const char *device_path = NULL;
      const char *mount_path = NULL;
      const char *mount_fstype = NULL;
      char *mount_options = NULL;
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;
      gboolean is_loopback = FALSE;

      mount_path = mnt_fs_get_target (fs);
      if ((strcmp (mount_path, "ignore") == 0) ||
          (strcmp (mount_path, "swap") == 0) ||
          (strcmp (mount_path, "none") == 0))
        continue;

      mount_fstype = mnt_fs_get_fstype (fs);
      mount_options = mnt_fs_strdup_options (fs);
      if (mount_options)
        {
          unsigned long mount_flags = 0;
          unsigned long userspace_flags = 0;

          mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
          mnt_optstr_get_flags (mount_options, &userspace_flags, mnt_get_builtin_optmap (MNT_USERSPACE_MAP));

          /* We ignore bind fstab entries, as we ignore bind mounts anyway */
          if (mount_flags & MS_BIND)
            {
              g_free (mount_options);
              continue;
            }

          is_read_only = (mount_flags & MS_RDONLY) != 0;
          is_loopback = (userspace_flags & MNT_MS_LOOP) != 0;

          if ((mount_fstype != NULL && g_strcmp0 ("supermount", mount_fstype) == 0) ||
              ((userspace_flags & MNT_MS_USER) &&
               (g_strstr_len (mount_options, -1, "user_xattr") == NULL)) ||
              (g_strstr_len (mount_options, -1, "pamconsole") == NULL) ||
              (userspace_flags & MNT_MS_USERS) ||
              (userspace_flags & MNT_MS_OWNER))
            {
              is_user_mountable = TRUE;
            }
        }

      device_path = mnt_fs_get_source (fs);
      if (g_strcmp0 (device_path, "/dev/root") == 0)
        device_path = _resolve_dev_root ();

      mount_point = create_unix_mount_point (device_path,
                                             mount_path,
                                             mount_fstype,
                                             mount_options,
                                             is_read_only,
                                             is_user_mountable,
                                             is_loopback);
      if (mount_options)
        g_free (mount_options);

      return_list = g_list_prepend (return_list, mount_point);
    }
  mnt_free_iter (iter);
1034 1035

 out:
1036
  mnt_free_table (table);
1037 1038 1039 1040 1041 1042

  return g_list_reverse (return_list);
}

#else

1043 1044 1045
static GList *
_g_get_unix_mount_points (void)
{
1046 1047 1048 1049
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
1050 1051 1052
  struct mntent *mntent;
  FILE *file;
  char *read_file;
1053
  GUnixMountPoint *mount_point;
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  GList *return_list;
  
  read_file = get_fstab_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;

  return_list = NULL;
  
1064 1065 1066
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
1067 1068
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
1069
#endif
1070
    {
1071 1072 1073 1074 1075
      const char *device_path = NULL;
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;
      gboolean is_loopback = FALSE;

1076
      if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
1077 1078
          (strcmp (mntent->mnt_dir, "swap") == 0) ||
          (strcmp (mntent->mnt_dir, "none") == 0))
1079
	continue;
1080

1081
#ifdef HAVE_HASMNTOPT
1082 1083 1084
      /* We ignore bind fstab entries, as we ignore bind mounts anyway */
      if (hasmntopt (mntent, "bind"))
        continue;
1085
#endif
1086

1087
      if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
1088
        device_path = _resolve_dev_root ();
1089
      else
1090 1091
        device_path = mntent->mnt_fsname;

1092 1093
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
1094 1095
	is_read_only = TRUE;

1096
      if (hasmntopt (mntent, "loop") != NULL)
1097 1098
	is_loopback = TRUE;

1099
#endif
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109
      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
	  )
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	is_user_mountable = TRUE;

      mount_point = create_unix_mount_point (device_path,
                                             mntent->mnt_dir,
                                             mntent->mnt_type,
                                             mntent->mnt_opts,
                                             is_read_only,
                                             is_user_mountable,
                                             is_loopback);

      return_list = g_list_prepend (return_list, mount_point);
1121 1122 1123
    }
  
  endmntent (file);
1124 1125

#ifndef HAVE_GETMNTENT_R
1126
  G_UNLOCK (getmntent);
1127
#endif
1128 1129 1130 1131
  
  return g_list_reverse (return_list);
}

1132 1133
#endif /* HAVE_LIBMOUNT */

1134
/* mnttab.h {{{2 */
1135 1136 1137 1138 1139 1140 1141 1142
#elif defined (HAVE_SYS_MNTTAB_H)

static GList *
_g_get_unix_mount_points (void)
{
  struct mnttab mntent;
  FILE *file;
  char *read_file;
1143
  GUnixMountPoint *mount_point;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  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))
    {
1157 1158 1159 1160
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;
      gboolean is_loopback = FALSE;

1161
      if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
1162 1163
          (strcmp (mntent.mnt_mountp, "swap") == 0) ||
          (strcmp (mntent.mnt_mountp, "none") == 0))
1164
	continue;
1165

1166 1167
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
1168 1169
	is_read_only = TRUE;

1170
      if (hasmntopt (&mntent, "lofs") != NULL)
1171
	is_loopback = TRUE;
1172
#endif
1173

1174 1175 1176 1177 1178 1179 1180 1181 1182
      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
	  )
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	is_user_mountable = TRUE;

      mount_point = create_unix_mount_point (mntent.mnt_special,
                                             mntent.mnt_mountp,
                                             mntent.mnt_fstype,
                                             mntent.mnt_mntopts,
                                             is_read_only,
                                             is_user_mountable,
                                             is_loopback);

      return_list = g_list_prepend (return_list, mount_point);
1194 1195 1196 1197 1198 1199 1200
    }
  
  endmntent (file);
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}
1201 1202

/* mntctl.h (AIX) {{{2 */
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
#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
1240 1241
aix_fs_getword (FILE *fd, 
                char *word)
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
{
  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
1271 1272
aix_fs_get (FILE               *fd, 
            AixMountTableEntry *prop)
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
{
  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;
1318
  GUnixMountPoint *mount_point;
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
  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)
	{
1334 1335 1336 1337 1338 1339 1340 1341 1342
          mount_point = create_unix_mount_point (mntent.mnt_special,
                                                 mntent.mnt_mount,
                                                 mntent.mnt_fstype,
                                                 mntent.mnt_options,
                                                 TRUE,
                                                 TRUE,
                                                 FALSE);

	  return_list = g_list_prepend (return_list, mount_point);
1343 1344 1345 1346 1347 1348 1349 1350
	}
    }
	
  endmntent (file);
  
  return g_list_reverse (return_list);
}

Patrick Welche's avatar
Patrick Welche committed
1351
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1352 1353 1354 1355 1356

static GList *
_g_get_unix_mount_points (void)
{
  struct fstab *fstab = NULL;
1357
  GUnixMountPoint *mount_point;
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
  GList *return_list;
#ifdef HAVE_SYS_SYSCTL_H
  int usermnt = 0;
  struct stat sb;
#endif
  
  if (!setfsent ())
    return NULL;

  return_list = NULL;
  
#ifdef HAVE_SYS_SYSCTL_H
#if defined(HAVE_SYSCTLBYNAME)
1371 1372 1373 1374 1375
  {
    size_t len = sizeof(usermnt);

    sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
  }
1376 1377 1378
#elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
  {
    int mib[2];
1379
    size_t len = sizeof(usermnt);
1380 1381 1382 1383 1384 1385 1386 1387
    
    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];
1388
    size_t len = sizeof(usermnt);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    
    mib[0] = CTL_KERN;
    mib[1] = KERN_USERMOUNT;
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
  }
#endif
#endif
  
  while ((fstab = getfsent ()) != NULL)
    {
1399 1400 1401
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;

1402 1403
      if (strcmp (fstab->fs_vfstype, "swap") == 0)
	continue;
1404

1405
      if (strcmp (fstab->fs_type, "ro") == 0)
1406
	is_read_only = TRUE;
1407 1408 1409 1410 1411 1412 1413 1414

#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)
1415
		is_user_mountable = TRUE;
1416 1417 1418 1419
	    }
	}
#endif

1420 1421 1422 1423 1424 1425 1426 1427 1428
      mount_point = create_unix_mount_point (fstab->fs_spec,
                                             fstab->fs_file,
                                             fstab->fs_vfstype,
                                             fstab->fs_mntops,
                                             is_read_only,
                                             is_user_mountable,
                                             FALSE);

      return_list = g_list_prepend (return_list, mount_point);
1429 1430 1431 1432 1433 1434
    }
  
  endfsent ();
  
  return g_list_reverse (return_list);
}
1435
/* Interix {{{2 */
1436 1437 1438 1439 1440 1441
#elif defined(__INTERIX)
static GList *
_g_get_unix_mount_points (void)
{
  return _g_get_unix_mounts ();
}
1442 1443

/* Common code {{{2 */
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
#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 ();
1455 1456
  /* Don't return mtime for /proc/ files */
  if (monitor_file && !g_str_has_prefix (monitor_file, "/proc/"))
1457 1458
    {
      if (stat (monitor_file, &buf) == 0)
1459 1460
        return (guint64)buf.st_mtime;
    }
1461
  else if (proc_mounts_watch_is_running ())
1462
    {
1463
      /* it's being monitored by poll, so return mount_poller_time */
1464
      return mount_poller_time;
1465
    }
1466 1467 1468 1469 1470 1471 1472 1473 1474
  else
    {
      /* Case of /proc/ file not being monitored - Be on the safe side and
       * send a new timestamp to force g_unix_mounts_changed_since() to
       * return TRUE so any application caches depending on it (like eg.
       * the one in GIO) get invalidated and don't hold possibly outdated
       * data - see Bug 787731 */
     return (guint64) g_get_monotonic_time ();
    }
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  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)
1488
        return (guint64)buf.st_mtime;
1489 1490 1491 1492 1493
    }
  return 0;
}

/**
1494
 * g_unix_mounts_get:
1495
 * @time_read: (out) (optional): guint64 to contain a timestamp, or %NULL
1496 1497 1498 1499
 *
 * 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
1500
 * with g_unix_mounts_changed_since().
1501
 *
1502 1503
 * Returns: (element-type GUnixMountEntry) (transfer full):
 *     a #GList of the UNIX mounts.
1504 1505
 **/
GList *
1506
g_unix_mounts_get (guint64 *time_read)
1507 1508 1509 1510 1511 1512 1513 1514
{
  if (time_read)
    *time_read = get_mounts_timestamp ();

  return _g_get_unix_mounts ();
}

/**
1515 1516
 * g_unix_mount_at:
 * @mount_path: (type filename): path for a possible unix mount.
1517
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1518
 * 
1519
 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1520 1521 1522
 * is set, it will be filled with a unix timestamp for checking
 * if the mounts have changed since with g_unix_mounts_changed_since().
 * 
1523
 * Returns: (transfer full): a #GUnixMountEntry.
1524
 **/
1525
GUnixMountEntry *
1526 1527
g_unix_mount_at (const char *mount_path,
		 guint64    *time_read)
1528 1529
{
  GList *mounts, *l;
1530
  GUnixMountEntry *mount_entry, *found;
1531
  
1532
  mounts = g_unix_mounts_get (time_read);
1533 1534 1535 1536 1537 1538

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

1539
      if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1540
        found = mount_entry;
1541
      else
1542
        g_unix_mount_free (mount_entry);
1543 1544 1545 1546 1547 1548
    }
  g_list_free (mounts);

  return found;
}

1549
/**
1550 1551
 * g_unix_mount_for:
 * @file_path: (type filename): file path on some unix mount.
1552
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
 *
 * Gets a #GUnixMountEntry for a given file path. If @time_read
 * is set, it will be filled with a unix timestamp for checking
 * if the mounts have changed since with g_unix_mounts_changed_since().
 *
 * Returns: (transfer full): a #GUnixMountEntry.
 *
 * Since: 2.52
 **/
GUnixMountEntry *
g_unix_mount_for (const char *file_path,
                  guint64    *time_read)
{
  GUnixMountEntry *entry;

  g_return_val_if_fail (file_path != NULL, NULL);

  entry = g_unix_mount_at (file_path, time_read);
  if (entry == NULL)
    {
      char *topdir;

      topdir = _g_local_file_find_topdir_for (file_path);
1576 1577 1578 1579 1580
      if (topdir != NULL)
        {
          entry = g_unix_mount_at (topdir, time_read);
          g_free (topdir);
        }
1581 1582 1583 1584 1585
    }

  return entry;
}

1586
/**
1587
 * g_unix_mount_points_get:
1588
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1589 1590
 *
 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1591
 * If @time_read is set, it will be filled with the mount timestamp,
1592
 * allowing for checking if the mounts have changed with
Matthias Clasen's avatar
Matthias Clasen committed
1593
 * g_unix_mount_points_changed_since().
1594
 *
1595 1596
 * Returns: (element-type GUnixMountPoint) (transfer full):
 *     a #GList of the UNIX mountpoints.
1597 1598
 **/
GList *
1599
g_unix_mount_points_get (guint64 *time_read)
1600 1601 1602 1603 1604 1605 1606 1607
{
  if (time_read)
    *time_read = get_mount_points_timestamp ();

  return _g_get_unix_mount_points ();
}

/**
1608
 * g_unix_mounts_changed_since:
1609 1610
 * @time: guint64 to contain a timestamp.
 * 
1611 1612
 * Checks if the unix mounts have changed since a given unix time.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1613
 * Returns: %TRUE if the mounts have changed since @time. 
1614 1615 1616 1617 1618 1619 1620 1621
 **/
gboolean
g_unix_mounts_changed_since (guint64 time)
{
  return get_mounts_timestamp () != time;
}

/**
1622
 * g_unix_mount_points_changed_since:
1623 1624
 * @time: guint64 to contain a timestamp.
 * 
1625 1626
 * Checks if the unix mount points have changed since a given unix time.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1627
 * Returns: %TRUE if the mount points have changed since @time. 
1628 1629 1630 1631 1632 1633 1634
 **/
gboolean
g_unix_mount_points_changed_since (guint64 time)
{
  return get_mount_points_timestamp () != time;
}

1635 1636
/* GUnixMountMonitor {{{1 */

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
enum {
  MOUNTS_CHANGED,
  MOUNTPOINTS_CHANGED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

struct _GUnixMountMonitor {
  GObject parent;

1648
  GMainContext *context;
1649 1650 1651 1652 1653 1654 1655
};

struct _GUnixMountMonitorClass {
  GObjectClass parent_class;
};


1656
G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT)
1657

1658 1659 1660 1661 1662
static GContextSpecificGroup  mount_monitor_group;
static GFileMonitor          *fstab_monitor;
static GFileMonitor          *mtab_monitor;
static GSource               *proc_mounts_watch_source;
static GList                 *mount_poller_mounts;
1663
static guint                  mtab_file_changed_id;
1664

1665 1666 1667 1668 1669 1670 1671
static gboolean
proc_mounts_watch_is_running (void)
{
  return proc_mounts_watch_source != NULL &&
         !g_source_is_destroyed (proc_mounts_watch_source);
}

1672
static void
1673
fstab_file_changed (GFileMonitor      *monitor,
1674 1675 1676 1677
                    GFile             *file,
                    GFile             *other_file,
                    GFileMonitorEvent  event_type,
                    gpointer           user_data)
1678 1679 1680 1681 1682 1683
{
  if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
      event_type != G_FILE_MONITOR_EVENT_CREATED &&
      event_type != G_FILE_MONITOR_EVENT_DELETED)
    return;