gunixmounts.c 79 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
  char *mount_path;
  char *device_path;
128
  char *root_path;
129
  char *filesystem_type;
130
  char *options;
131 132 133 134
  gboolean is_read_only;
  gboolean is_system_internal;
};

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

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

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

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

155 156
static guint64 mount_poller_time = 0;

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

#ifdef HAVE_MNTENT_H
#include <mntent.h>
163
#ifdef HAVE_LIBMOUNT
164
#include <libmount.h>
165
#endif
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#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
181
#if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
182
#include <sys/param.h>
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#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;
}

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

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

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

  return FALSE;
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/**
 * 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)
303 304
{
  const char *ignore_fs[] = {
305 306
    "adfs",
    "afs",
307 308
    "auto",
    "autofs",
309 310 311 312 313
    "autofs4",
    "cgroup",
    "configfs",
    "cxfs",
    "debugfs",
314 315
    "devfs",
    "devpts",
316
    "devtmpfs",
317
    "ecryptfs",
318
    "fdescfs",
319 320 321 322 323
    "fusectl",
    "gfs",
    "gfs2",
    "gpfs",
    "hugetlbfs",
324 325
    "kernfs",
    "linprocfs",
326 327 328
    "linsysfs",
    "lustre",
    "lustre_lite",
329
    "mfs",
330 331 332
    "mqueue",
    "ncpfs",
    "nfsd",
333
    "nullfs",
334 335
    "ocfs2",
    "overlay",
336 337
    "proc",
    "procfs",
338
    "pstore",
339 340
    "ptyfs",
    "rootfs",
341 342
    "rpc_pipefs",
    "securityfs",
343 344 345 346 347 348
    "selinuxfs",
    "sysfs",
    "tmpfs",
    "usbfs",
    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

  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,
391 392 393
                       const char *fs,
                       const char *device,
                       const char *root)
394 395
{
  if (g_unix_is_system_fs_type (fs))
396 397
    return TRUE;
  
398
  if (g_unix_is_system_device_path (device))
399 400
    return TRUE;

401
  if (g_unix_is_mount_path_system_internal (mountpoint))
402
    return TRUE;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

  /* It is not possible to reliably detect mounts which were created by bind
   * operation. mntent-based _g_get_unix_mounts() implementation blindly skips
   * mounts with a device path that is repeated (e.g. mounts created by bind
   * operation, btrfs subvolumes). This usually chooses the most important
   * mounts (i.e. which points to the root of filesystem), but it doesn't work
   * in all cases and also it is not ideal that those mounts are completely
   * ignored (e.g. x-gvfs-show doesn't work for them, trash backend can't handle
   * files on btrfs subvolumes). libmount-based _g_get_unix_mounts()
   * implementation provides a root path. So there is no need to completely
   * ignore those mounts, because e.g. our volume monitors can use the root path
   * to not mengle those mounts with the "regular" mounts (i.e. which points to
   * the root). But because those mounts usually just duplicate other mounts and
   * are completely ignored with mntend-based implementation, let's mark them as
   * system internal. Given the different approches it doesn't mean that all
   * mounts which were ignored will be system internal now, but this should work
   * in most cases. For more info, see g_unix_mount_get_root_path() annotation,
   * comment in mntent-based _g_get_unix_mounts() implementation and the
   * https://gitlab.gnome.org/GNOME/glib/issues/1271 issue.
   */
  if (root != NULL && g_strcmp0 (root, "/") != 0)
    return TRUE;

426 427 428
  return FALSE;
}

429 430
/* GUnixMounts (ie: mtab) implementations {{{1 */

431 432 433
static GUnixMountEntry *
create_unix_mount_entry (const char *device_path,
                         const char *mount_path,
434
                         const char *root_path,
435
                         const char *filesystem_type,
436
                         const char *options,
437 438 439 440 441 442 443
                         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);
444
  mount_entry->root_path = g_strdup (root_path);
445
  mount_entry->filesystem_type = g_strdup (filesystem_type);
446
  mount_entry->options = g_strdup (options);
447 448 449 450 451
  mount_entry->is_read_only = is_read_only;

  mount_entry->is_system_internal =
    guess_system_internal (mount_entry->mount_path,
                           mount_entry->filesystem_type,
452 453 454
                           mount_entry->device_path,
                           mount_entry->root_path);

455 456 457
  return mount_entry;
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
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;
}

481
/* mntent.h (Linux, GNU, NSS) {{{2 */
482 483
#ifdef HAVE_MNTENT_H

484 485
#ifdef HAVE_LIBMOUNT

486 487 488 489 490
/* For documentation on /proc/self/mountinfo see
 * http://www.kernel.org/doc/Documentation/filesystems/proc.txt
 */
#define PROC_MOUNTINFO_PATH "/proc/self/mountinfo"

491 492
static GList *
_g_get_unix_mounts (void)
493
{
494 495 496 497 498 499
  struct libmnt_table *table = NULL;
  struct libmnt_iter* iter = NULL;
  struct libmnt_fs *fs = NULL;
  GUnixMountEntry *mount_entry = NULL;
  GList *return_list = NULL;

500 501
  table = mnt_new_table ();
  if (mnt_table_parse_mtab (table, NULL) < 0)
502
    goto out;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

  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),
526
                                             mnt_fs_get_root (fs),
527
                                             mnt_fs_get_fstype (fs),
528
                                             mnt_fs_get_options (fs),
529 530 531 532 533
                                             is_read_only);

      return_list = g_list_prepend (return_list, mount_entry);
    }
  mnt_free_iter (iter);
534 535

 out:
536
  mnt_free_table (table);
537 538

  return g_list_reverse (return_list);
539 540
}

541 542
#else

543
static const char *
544
get_mtab_read_file (void)
545 546
{
#ifdef _PATH_MOUNTED
547 548 549
# ifdef __linux__
  return "/proc/mounts";
# else
550
  return _PATH_MOUNTED;
551
# endif
552
#else
553 554 555 556
  return "/etc/mtab";
#endif
}

557
#ifndef HAVE_GETMNTENT_R
558
G_LOCK_DEFINE_STATIC(getmntent);
559
#endif
560 561

static GList *
Matthias Clasen's avatar
Matthias Clasen committed
562
_g_get_unix_mounts (void)
563
{
564 565 566 567
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
568 569
  struct mntent *mntent;
  FILE *file;
570
  const char *read_file;
571
  GUnixMountEntry *mount_entry;
572 573 574 575 576 577 578 579 580 581 582 583 584
  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);
  
585 586 587
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
588 589
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
590
#endif
591
    {
592 593 594
      const char *device_path = NULL;
      gboolean is_read_only = FALSE;

595 596 597 598
      /* 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.
599
       * It's not an ideal solution to the problem, but it's likely that
600 601 602 603 604 605 606 607
       * 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))
608
        continue;
609

610
      if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
611
        device_path = _resolve_dev_root ();
612
      else
613 614
        device_path = mntent->mnt_fsname;

615 616
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
617
	is_read_only = TRUE;
618
#endif
619 620 621

      mount_entry = create_unix_mount_entry (device_path,
                                             mntent->mnt_dir,
622
                                             NULL,
623
                                             mntent->mnt_type,
624
                                             mntent->mnt_opts,
625 626
                                             is_read_only);

627 628 629
      g_hash_table_insert (mounts_hash,
			   mount_entry->device_path,
			   mount_entry->device_path);
630

631 632 633 634 635 636
      return_list = g_list_prepend (return_list, mount_entry);
    }
  g_hash_table_destroy (mounts_hash);
  
  endmntent (file);

637
#ifndef HAVE_GETMNTENT_R
638
  G_UNLOCK (getmntent);
639
#endif
640 641 642 643
  
  return g_list_reverse (return_list);
}

644 645
#endif /* HAVE_LIBMOUNT */

646
static const char *
647 648
get_mtab_monitor_file (void)
{
649
  static const char *mountinfo_path = NULL;
650 651 652 653 654 655 656 657
#ifdef HAVE_LIBMOUNT
  struct stat buf;
#endif

  if (mountinfo_path != NULL)
    return mountinfo_path;

#ifdef HAVE_LIBMOUNT
658 659 660 661 662 663 664 665 666
  /* 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;
    }

667 668 669 670 671 672 673
  if (stat (PROC_MOUNTINFO_PATH, &buf) == 0)
    {
      mountinfo_path = PROC_MOUNTINFO_PATH;
      return mountinfo_path;
    }
#endif

674 675
#ifdef _PATH_MOUNTED
# ifdef __linux__
676
  mountinfo_path = "/proc/mounts";
677
# else
678
  mountinfo_path = _PATH_MOUNTED;
679 680
# endif
#else
681
  mountinfo_path = "/etc/mtab";
682
#endif
683 684

  return mountinfo_path;
685 686
}

687
/* mnttab.h {{{2 */
688 689 690 691
#elif defined (HAVE_SYS_MNTTAB_H)

G_LOCK_DEFINE_STATIC(getmntent);

692
static const char *
693 694 695 696 697 698 699 700 701
get_mtab_read_file (void)
{
#ifdef _PATH_MOUNTED
  return _PATH_MOUNTED;
#else	
  return "/etc/mnttab";
#endif
}

702
static const char *
703 704 705 706 707 708 709 710 711 712
get_mtab_monitor_file (void)
{
  return get_mtab_read_file ();
}

static GList *
_g_get_unix_mounts (void)
{
  struct mnttab mntent;
  FILE *file;
713
  const char *read_file;
714
  GUnixMountEntry *mount_entry;
715 716 717 718 719 720 721 722 723 724 725 726 727
  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))
    {
728 729
      gboolean is_read_only = FALSE;

730 731
#if defined (HAVE_HASMNTOPT)
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
732
	is_read_only = TRUE;
733 734
#endif

735 736
      mount_entry = create_unix_mount_entry (mntent.mnt_special,
                                             mntent.mnt_mountp,
737
                                             NULL,
738
                                             mntent.mnt_fstype,
739
                                             mntent.mnt_opts,
740 741
                                             is_read_only);

742 743 744 745 746 747 748 749 750 751
      return_list = g_list_prepend (return_list, mount_entry);
    }
  
  endmntent (file);
  
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}

752
/* mntctl.h (AIX) {{{2 */
753 754
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)

755
static const char *
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
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)
    {
773
      g_warning ("Unable to know the number of mounted volumes");
774 775 776 777 778 779 780 781 782
      
      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)
783
    g_warning ("Bad vmount structure revision number, want %d, got %d", VMT_REVISION, vmount_info->vmt_revision);
784 785 786

  if (vmount_number < 0)
    {
787
      g_warning ("Unable to recover mounted volumes information");
788 789 790 791 792 793 794 795
      
      g_free (vmount_info);
      return NULL;
    }
  
  return_list = NULL;
  while (vmount_number > 0)
    {
796
      gboolean is_read_only = FALSE;
797 798 799

      fs_info = getvfsbytype (vmount_info->vmt_gfstype);

800 801 802 803 804
      /* 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),
805
                                             NULL,
806
                                             fs_info == NULL ? "unknown" : fs_info->vfsent_name,
807
                                             NULL,
808 809
                                             is_read_only);

810 811 812 813 814 815 816 817 818 819 820 821
      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);
}

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

825
static const char *
826 827 828 829 830 831 832 833
get_mtab_monitor_file (void)
{
  return NULL;
}

static GList *
_g_get_unix_mounts (void)
{
834
#if defined(USE_STATVFS)
835
  struct statvfs *mntent = NULL;
836
#elif defined(USE_STATFS)
Patrick Welche's avatar
Patrick Welche committed
837
  struct statfs *mntent = NULL;
838 839 840
#else
  #error statfs juggling failed
#endif
Patrick Welche's avatar
Patrick Welche committed
841
  size_t bufsize;
842
  int num_mounts, i;
843
  GUnixMountEntry *mount_entry;
844 845
  GList *return_list;
  
Patrick Welche's avatar
Patrick Welche committed
846
  /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
847
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
848
  num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
849
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
850 851 852 853 854 855 856
  num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
    return NULL;

  bufsize = num_mounts * sizeof (*mntent);
  mntent = g_malloc (bufsize);
857
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
858
  num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
859
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
Patrick Welche's avatar
Patrick Welche committed
860 861 862
  num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
#endif
  if (num_mounts == -1)
863 864 865 866 867 868
    return NULL;
  
  return_list = NULL;
  
  for (i = 0; i < num_mounts; i++)
    {
869
      gboolean is_read_only = FALSE;
870 871

#if defined(USE_STATVFS)
Patrick Welche's avatar
Patrick Welche committed
872
      if (mntent[i].f_flag & ST_RDONLY)
873
#elif defined(USE_STATFS)
874
      if (mntent[i].f_flags & MNT_RDONLY)
875 876
#else
      #error statfs juggling failed
877
#endif
878 879 880 881
        is_read_only = TRUE;

      mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
                                             mntent[i].f_mntonname,
882
                                             NULL,
883
                                             mntent[i].f_fstypename,
884
                                             NULL,
885
                                             is_read_only);
886 887 888

      return_list = g_list_prepend (return_list, mount_entry);
    }
Patrick Welche's avatar
Patrick Welche committed
889 890

  g_free (mntent);
891 892 893
  
  return g_list_reverse (return_list);
}
894 895

/* Interix {{{2 */
896 897
#elif defined(__INTERIX)

898
static const char *
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
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;
}
951 952

/* Common code {{{2 */
953 954 955 956
#else
#error No _g_get_unix_mounts() implementation for system
#endif

957 958
/* GUnixMountPoints (ie: fstab) implementations {{{1 */

959 960 961 962 963 964 965 966
/* _g_get_unix_mount_points():
 * read the fstab.
 * don't return swap and ignore mounts.
 */

static char *
get_fstab_file (void)
{
967 968 969
#ifdef HAVE_LIBMOUNT
  return (char *) mnt_get_fstab_path ();
#else
970 971 972 973 974 975 976 977 978 979
#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
980
#endif
981 982
}

983
/* mntent.h (Linux, GNU, NSS) {{{2 */
984
#ifdef HAVE_MNTENT_H
985 986 987 988 989 990 991 992 993 994 995 996

#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;

997
  table = mnt_new_table ();
998
  if (mnt_table_parse_fstab (table, NULL) < 0)
999
    goto out;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

  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);
1066 1067

 out:
1068
  mnt_free_table (table);
1069 1070 1071 1072 1073 1074

  return g_list_reverse (return_list);
}

#else

1075 1076 1077
static GList *
_g_get_unix_mount_points (void)
{
1078 1079 1080 1081
#ifdef HAVE_GETMNTENT_R
  struct mntent ent;
  char buf[1024];
#endif
1082 1083 1084
  struct mntent *mntent;
  FILE *file;
  char *read_file;
1085
  GUnixMountPoint *mount_point;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  GList *return_list;
  
  read_file = get_fstab_file ();
  
  file = setmntent (read_file, "r");
  if (file == NULL)
    return NULL;

  return_list = NULL;
  
1096 1097 1098
#ifdef HAVE_GETMNTENT_R
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
#else
1099 1100
  G_LOCK (getmntent);
  while ((mntent = getmntent (file)) != NULL)
1101
#endif
1102
    {
1103 1104 1105 1106 1107
      const char *device_path = NULL;
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;
      gboolean is_loopback = FALSE;

1108
      if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
1109 1110
          (strcmp (mntent->mnt_dir, "swap") == 0) ||
          (strcmp (mntent->mnt_dir, "none") == 0))
1111
	continue;
1112

1113
#ifdef HAVE_HASMNTOPT
1114 1115 1116
      /* We ignore bind fstab entries, as we ignore bind mounts anyway */
      if (hasmntopt (mntent, "bind"))
        continue;
1117
#endif
1118

1119
      if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
1120
        device_path = _resolve_dev_root ();
1121
      else
1122 1123
        device_path = mntent->mnt_fsname;

1124 1125
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
1126 1127
	is_read_only = TRUE;

1128
      if (hasmntopt (mntent, "loop") != NULL)
1129 1130
	is_loopback = TRUE;

1131
#endif
1132

1133 1134 1135 1136 1137 1138 1139 1140 1141
      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
	  )
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	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);
1153 1154 1155
    }
  
  endmntent (file);
1156 1157

#ifndef HAVE_GETMNTENT_R
1158
  G_UNLOCK (getmntent);
1159
#endif
1160 1161 1162 1163
  
  return g_list_reverse (return_list);
}

1164 1165
#endif /* HAVE_LIBMOUNT */

1166
/* mnttab.h {{{2 */
1167 1168 1169 1170 1171 1172 1173 1174
#elif defined (HAVE_SYS_MNTTAB_H)

static GList *
_g_get_unix_mount_points (void)
{
  struct mnttab mntent;
  FILE *file;
  char *read_file;
1175
  GUnixMountPoint *mount_point;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
  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))
    {
1189 1190 1191 1192
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;
      gboolean is_loopback = FALSE;

1193
      if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
1194 1195
          (strcmp (mntent.mnt_mountp, "swap") == 0) ||
          (strcmp (mntent.mnt_mountp, "none") == 0))
1196
	continue;
1197

1198 1199
#ifdef HAVE_HASMNTOPT
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
1200 1201
	is_read_only = TRUE;

1202
      if (hasmntopt (&mntent, "lofs") != NULL)
1203
	is_loopback = TRUE;
1204
#endif
1205

1206 1207 1208 1209 1210 1211 1212 1213 1214
      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
	  )
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	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);
1226 1227 1228 1229 1230 1231 1232
    }
  
  endmntent (file);
  G_UNLOCK (getmntent);
  
  return g_list_reverse (return_list);
}
1233 1234

/* mntctl.h (AIX) {{{2 */
1235 1236 1237 1238 1239 1240 1241 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 1271
#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
1272 1273
aix_fs_getword (FILE *fd, 
                char *word)
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
{
  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
1303 1304
aix_fs_get (FILE               *fd, 
            AixMountTableEntry *prop)
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
  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;
1350
  GUnixMountPoint *mount_point;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
  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)
	{
1366 1367 1368 1369 1370 1371 1372 1373 1374
          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);
1375 1376 1377 1378 1379 1380 1381 1382
	}
    }
	
  endmntent (file);
  
  return g_list_reverse (return_list);
}

Patrick Welche's avatar
Patrick Welche committed
1383
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1384 1385 1386 1387 1388

static GList *
_g_get_unix_mount_points (void)
{
  struct fstab *fstab = NULL;
1389
  GUnixMountPoint *mount_point;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
  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)
1403 1404 1405 1406 1407
  {
    size_t len = sizeof(usermnt);

    sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
  }
1408 1409 1410
#elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
  {
    int mib[2];
1411
    size_t len = sizeof(usermnt);
1412 1413 1414 1415 1416 1417 1418 1419
    
    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];
1420
    size_t len = sizeof(usermnt);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    
    mib[0] = CTL_KERN;
    mib[1] = KERN_USERMOUNT;
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
  }
#endif
#endif
  
  while ((fstab = getfsent ()) != NULL)
    {
1431 1432 1433
      gboolean is_read_only = FALSE;
      gboolean is_user_mountable = FALSE;

1434 1435
      if (strcmp (fstab->fs_vfstype, "swap") == 0)
	continue;
1436

1437
      if (strcmp (fstab->fs_type, "ro") == 0)
1438
	is_read_only = TRUE;
1439 1440 1441 1442 1443 1444 1445 1446

#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)
1447
		is_user_mountable = TRUE;
1448 1449 1450 1451
	    }
	}
#endif

1452 1453 1454 1455 1456 1457 1458 1459 1460
      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);
1461 1462 1463 1464 1465 1466
    }
  
  endfsent ();
  
  return g_list_reverse (return_list);
}
1467
/* Interix {{{2 */
1468 1469 1470 1471 1472 1473
#elif defined(__INTERIX)
static GList *
_g_get_unix_mount_points (void)
{
  return _g_get_unix_mounts ();
}
1474 1475

/* Common code {{{2 */
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
#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 ();
1487 1488
  /* Don't return mtime for /proc/ files */
  if (monitor_file && !g_str_has_prefix (monitor_file, "/proc/"))
1489 1490
    {
      if (stat (monitor_file, &buf) == 0)
1491 1492
        return (guint64)buf.st_mtime;
    }
1493
  else if (proc_mounts_watch_is_running ())
1494
    {
1495
      /* it's being monitored by poll, so return mount_poller_time */
1496
      return mount_poller_time;
1497
    }
1498 1499 1500 1501 1502 1503 1504 1505 1506
  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 ();
    }
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  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)
1520
        return (guint64)buf.st_mtime;
1521 1522 1523 1524 1525
    }
  return 0;
}

/**
1526
 * g_unix_mounts_get:
1527
 * @time_read: (out) (optional): guint64 to contain a timestamp, or %NULL
1528 1529 1530 1531
 *
 * 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
1532
 * with g_unix_mounts_changed_since().
1533
 *
1534 1535
 * Returns: (element-type GUnixMountEntry) (transfer full):
 *     a #GList of the UNIX mounts.
1536 1537
 **/
GList *
1538
g_unix_mounts_get (guint64 *time_read)
1539 1540 1541 1542 1543 1544 1545 1546
{
  if (time_read)
    *time_read = get_mounts_timestamp ();

  return _g_get_unix_mounts ();
}

/**
1547 1548
 * g_unix_mount_at:
 * @mount_path: (type filename): path for a possible unix mount.
1549
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1550
 * 
1551
 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1552 1553 1554
 * is set, it will be filled with a unix timestamp for checking
 * if the mounts have changed since with g_unix_mounts_changed_since().
 * 
1555 1556 1557
 * If more mounts have the same mount path, the last matching mount
 * is returned.
 *
1558
 * Returns: (transfer full): a #GUnixMountEntry.
1559
 **/
1560
GUnixMountEntry *
1561 1562
g_unix_mount_at (const char *mount_path,
		 guint64    *time_read)
1563 1564
{
  GList *mounts, *l;
1565
  GUnixMountEntry *mount_entry, *found;
1566
  
1567
  mounts = g_unix_mounts_get (time_read);
1568 1569 1570 1571 1572 1573

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

1574 1575 1576 1577 1578 1579 1580
      if (strcmp (mount_path, mount_entry->mount_path) == 0)
        {
          if (found != NULL)
            g_unix_mount_free (found);

          found = mount_entry;
        }
1581
      else
1582
        g_unix_mount_free (mount_entry);
1583 1584 1585 1586 1587 1588
    }
  g_list_free (mounts);

  return found;
}

1589
/**
1590 1591
 * g_unix_mount_for:
 * @file_path: (type filename): file path on some unix mount.
1592
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1593 1594 1595 1596 1597
 *
 * 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().
 *
1598 1599 1600
 * If more mounts have the same mount path, the last matching mount
 * is returned.
 *
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
 * 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);
1619 1620 1621 1622 1623
      if (topdir != NULL)
        {
          entry = g_unix_mount_at (topdir, time_read);
          g_free (topdir);
        }
1624 1625 1626 1627 1628
    }

  return entry;
}

1629
/**
1630
 * g_unix_mount_points_get:
1631
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1632 1633
 *
 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1634
 * If @time_read is set, it will be filled with the mount timestamp,
1635
 * allowing for checking if the mounts have changed with
Matthias Clasen's avatar
Matthias Clasen committed
1636
 * g_unix_mount_points_changed_since().
1637
 *
1638 1639
 * Returns: (element-type GUnixMountPoint) (transfer full):
 *     a #GList of the UNIX mountpoints.
1640 1641
 **/
GList *
1642
g_unix_mount_points_get (guint64 *time_read)
1643 1644 1645 1646 1647 1648 1649 1650
{
  if (time_read)
    *time_read = get_mount_points_timestamp ();

  return _g_get_unix_mount_points ();
}

/**
1651
 * g_unix_mounts_changed_since:
1652 1653
 * @time: guint64 to contain a timestamp.
 * 
1654 1655
 * Checks if the unix mounts have changed since a given unix time.
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1656
 * Returns: %TRUE if the mounts have changed since @time. 
1657 1658 1659 1660 1661 1662 1663 1664
 **/
gboolean
g_unix_mounts_changed_since (guint64 time)
{
  return get_mounts_timestamp () != time