Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.76.5/gio/gunixmounts.c
Line
Count
Source
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3
/* GIO - GLib Input, Output and Streaming Library
4
 * 
5
 * Copyright (C) 2006-2007 Red Hat, Inc.
6
 *
7
 * SPDX-License-Identifier: LGPL-2.1-or-later
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General
20
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * Author: Alexander Larsson <alexl@redhat.com>
23
 */
24
25
/* Prologue {{{1 */
26
27
#include "config.h"
28
29
#include <sys/types.h>
30
#include <sys/stat.h>
31
#include <sys/wait.h>
32
#ifndef HAVE_SYSCTLBYNAME
33
#ifdef HAVE_SYS_PARAM_H
34
#include <sys/param.h>
35
#endif
36
#endif
37
#ifdef HAVE_POLL
38
#include <poll.h>
39
#endif
40
#include <stdio.h>
41
#include <unistd.h>
42
#include <sys/time.h>
43
#include <errno.h>
44
#include <string.h>
45
#include <signal.h>
46
#include <gstdio.h>
47
#include <dirent.h>
48
49
#if HAVE_SYS_STATFS_H
50
#include <sys/statfs.h>
51
#endif
52
#if HAVE_SYS_STATVFS_H
53
#include <sys/statvfs.h>
54
#endif
55
#if HAVE_SYS_VFS_H
56
#include <sys/vfs.h>
57
#elif HAVE_SYS_MOUNT_H
58
#if HAVE_SYS_PARAM_H
59
#include <sys/param.h>
60
#endif
61
#include <sys/mount.h>
62
#endif
63
64
#ifndef O_BINARY
65
#define O_BINARY 0
66
#endif
67
68
#include "gunixmounts.h"
69
#include "gfile.h"
70
#include "gfilemonitor.h"
71
#include "glibintl.h"
72
#include "glocalfile.h"
73
#include "gthemedicon.h"
74
#include "gcontextspecificgroup.h"
75
76
77
#ifdef HAVE_MNTENT_H
78
static const char *_resolve_dev_root (void);
79
#endif
80
81
/**
82
 * SECTION:gunixmounts
83
 * @include: gio/gunixmounts.h
84
 * @short_description: UNIX mounts
85
 *
86
 * Routines for managing mounted UNIX mount points and paths.
87
 *
88
 * Note that `<gio/gunixmounts.h>` belongs to the UNIX-specific GIO
89
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
90
 * file when using it.
91
 */
92
93
/**
94
 * GUnixMountType:
95
 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
96
 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
97
 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
98
 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
99
 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
100
 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
101
 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
102
 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
103
 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
104
 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
105
 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
106
 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
107
 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
108
 * 
109
 * Types of UNIX mounts.
110
 **/
111
typedef enum {
112
  G_UNIX_MOUNT_TYPE_UNKNOWN,
113
  G_UNIX_MOUNT_TYPE_FLOPPY,
114
  G_UNIX_MOUNT_TYPE_CDROM,
115
  G_UNIX_MOUNT_TYPE_NFS,
116
  G_UNIX_MOUNT_TYPE_ZIP,
117
  G_UNIX_MOUNT_TYPE_JAZ,
118
  G_UNIX_MOUNT_TYPE_MEMSTICK,
119
  G_UNIX_MOUNT_TYPE_CF,
120
  G_UNIX_MOUNT_TYPE_SM,
121
  G_UNIX_MOUNT_TYPE_SDMMC,
122
  G_UNIX_MOUNT_TYPE_IPOD,
123
  G_UNIX_MOUNT_TYPE_CAMERA,
124
  G_UNIX_MOUNT_TYPE_HD
125
} GUnixMountType;
126
127
struct _GUnixMountEntry {
128
  char *mount_path;
129
  char *device_path;
130
  char *root_path;
131
  char *filesystem_type;
132
  char *options;
133
  gboolean is_read_only;
134
  gboolean is_system_internal;
135
};
136
137
0
G_DEFINE_BOXED_TYPE (GUnixMountEntry, g_unix_mount_entry,
138
0
                     g_unix_mount_copy, g_unix_mount_free)
139
0
140
0
struct _GUnixMountPoint {
141
0
  char *mount_path;
142
0
  char *device_path;
143
0
  char *filesystem_type;
144
0
  char *options;
145
0
  gboolean is_read_only;
146
0
  gboolean is_user_mountable;
147
0
  gboolean is_loopback;
148
0
};
149
0
150
0
G_DEFINE_BOXED_TYPE (GUnixMountPoint, g_unix_mount_point,
151
0
                     g_unix_mount_point_copy, g_unix_mount_point_free)
152
0
153
0
static GList *_g_get_unix_mounts (void);
154
0
static GList *_g_get_unix_mount_points (void);
155
0
static gboolean proc_mounts_watch_is_running (void);
156
0
157
0
G_LOCK_DEFINE_STATIC (proc_mounts_source);
158
0
159
0
/* Protected by proc_mounts_source lock */
160
0
static guint64 mount_poller_time = 0;
161
0
static GSource *proc_mounts_watch_source;
162
0
163
0
#ifdef HAVE_SYS_MNTTAB_H
164
0
#define MNTOPT_RO "ro"
165
0
#endif
166
0
167
0
#ifdef HAVE_MNTENT_H
168
0
#include <mntent.h>
169
0
#ifdef HAVE_LIBMOUNT
170
0
#include <libmount.h>
171
0
#endif
172
0
#elif defined (HAVE_SYS_MNTTAB_H)
173
0
#include <sys/mnttab.h>
174
0
#if defined(__sun) && !defined(mnt_opts)
175
0
#define mnt_opts mnt_mntopts
176
0
#endif
177
0
#endif
178
0
179
0
#ifdef HAVE_SYS_VFSTAB_H
180
0
#include <sys/vfstab.h>
181
0
#endif
182
0
183
0
#if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
184
0
#include <sys/mntctl.h>
185
0
#include <sys/vfs.h>
186
0
#include <sys/vmount.h>
187
0
#include <fshelp.h>
188
0
#endif
189
0
190
0
#if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
191
0
#include <sys/param.h>
192
0
#include <sys/ucred.h>
193
0
#include <sys/mount.h>
194
0
#include <fstab.h>
195
0
#ifdef HAVE_SYS_SYSCTL_H
196
0
#include <sys/sysctl.h>
197
0
#endif
198
0
#endif
199
0
200
0
#ifndef HAVE_SETMNTENT
201
0
#define setmntent(f,m) fopen(f,m)
202
0
#endif
203
0
#ifndef HAVE_ENDMNTENT
204
0
#define endmntent(f) fclose(f)
205
0
#endif
206
0
207
0
static gboolean
208
0
is_in (const char *value, const char *set[])
209
0
{
210
0
  int i;
211
0
  for (i = 0; set[i] != NULL; i++)
212
0
    {
213
0
      if (strcmp (set[i], value) == 0)
214
0
  return TRUE;
215
0
    }
216
0
  return FALSE;
217
0
}
218
219
/**
220
 * g_unix_is_mount_path_system_internal:
221
 * @mount_path: (type filename): a mount path, e.g. `/media/disk` or `/usr`
222
 *
223
 * Determines if @mount_path is considered an implementation of the
224
 * OS. This is primarily used for hiding mountable and mounted volumes
225
 * that only are used in the OS and has little to no relevance to the
226
 * casual user.
227
 *
228
 * Returns: %TRUE if @mount_path is considered an implementation detail 
229
 *     of the OS.
230
 **/
231
gboolean
232
g_unix_is_mount_path_system_internal (const char *mount_path)
233
0
{
234
0
  const char *ignore_mountpoints[] = {
235
    /* Includes all FHS 2.3 toplevel dirs and other specialized
236
     * directories that we want to hide from the user.
237
     */
238
0
    "/",              /* we already have "Filesystem root" in Nautilus */ 
239
0
    "/bin",
240
0
    "/boot",
241
0
    "/compat/linux/proc",
242
0
    "/compat/linux/sys",
243
0
    "/dev",
244
0
    "/etc",
245
0
    "/home",
246
0
    "/lib",
247
0
    "/lib64",
248
0
    "/libexec",
249
0
    "/live/cow",
250
0
    "/live/image",
251
0
    "/media",
252
0
    "/mnt",
253
0
    "/opt",
254
0
    "/rescue",
255
0
    "/root",
256
0
    "/sbin",
257
0
    "/srv",
258
0
    "/tmp",
259
0
    "/usr",
260
0
    "/usr/X11R6",
261
0
    "/usr/local",
262
0
    "/usr/obj",
263
0
    "/usr/ports",
264
0
    "/usr/src",
265
0
    "/usr/xobj",
266
0
    "/var",
267
0
    "/var/crash",
268
0
    "/var/local",
269
0
    GLIB_LOCALSTATEDIR,
270
0
    "/var/log",
271
0
    "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
272
0
    "/var/mail",
273
0
    "/var/run",
274
0
    GLIB_RUNSTATEDIR,
275
0
    "/var/tmp",       /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
276
0
    "/proc",
277
0
    "/sbin",
278
0
    "/net",
279
0
    "/sys",
280
0
    NULL
281
0
  };
282
283
0
  if (is_in (mount_path, ignore_mountpoints))
284
0
    return TRUE;
285
  
286
0
  if (g_str_has_prefix (mount_path, "/dev/") ||
287
0
      g_str_has_prefix (mount_path, "/proc/") ||
288
0
      g_str_has_prefix (mount_path, "/sys/"))
289
0
    return TRUE;
290
291
0
  if (g_str_has_suffix (mount_path, "/.gvfs"))
292
0
    return TRUE;
293
294
0
  return FALSE;
295
0
}
296
297
/**
298
 * g_unix_is_system_fs_type:
299
 * @fs_type: a file system type, e.g. `procfs` or `tmpfs`
300
 *
301
 * Determines if @fs_type is considered a type of file system which is only
302
 * used in implementation of the OS. This is primarily used for hiding
303
 * mounted volumes that are intended as APIs for programs to read, and system
304
 * administrators at a shell; rather than something that should, for example,
305
 * appear in a GUI. For example, the Linux `/proc` filesystem.
306
 *
307
 * The list of file system types considered ‘system’ ones may change over time.
308
 *
309
 * Returns: %TRUE if @fs_type is considered an implementation detail of the OS.
310
 * Since: 2.56
311
 */
312
gboolean
313
g_unix_is_system_fs_type (const char *fs_type)
314
0
{
315
0
  const char *ignore_fs[] = {
316
0
    "adfs",
317
0
    "afs",
318
0
    "auto",
319
0
    "autofs",
320
0
    "autofs4",
321
0
    "cgroup",
322
0
    "configfs",
323
0
    "cxfs",
324
0
    "debugfs",
325
0
    "devfs",
326
0
    "devpts",
327
0
    "devtmpfs",
328
0
    "ecryptfs",
329
0
    "fdescfs",
330
0
    "fusectl",
331
0
    "gfs",
332
0
    "gfs2",
333
0
    "gpfs",
334
0
    "hugetlbfs",
335
0
    "kernfs",
336
0
    "linprocfs",
337
0
    "linsysfs",
338
0
    "lustre",
339
0
    "lustre_lite",
340
0
    "mfs",
341
0
    "mqueue",
342
0
    "ncpfs",
343
0
    "nfsd",
344
0
    "nullfs",
345
0
    "ocfs2",
346
0
    "overlay",
347
0
    "proc",
348
0
    "procfs",
349
0
    "pstore",
350
0
    "ptyfs",
351
0
    "rootfs",
352
0
    "rpc_pipefs",
353
0
    "securityfs",
354
0
    "selinuxfs",
355
0
    "sysfs",
356
0
    "tmpfs",
357
0
    "usbfs",
358
0
    NULL
359
0
  };
360
361
0
  g_return_val_if_fail (fs_type != NULL && *fs_type != '\0', FALSE);
362
363
0
  return is_in (fs_type, ignore_fs);
364
0
}
365
366
/**
367
 * g_unix_is_system_device_path:
368
 * @device_path: a device path, e.g. `/dev/loop0` or `nfsd`
369
 *
370
 * Determines if @device_path is considered a block device path which is only
371
 * used in implementation of the OS. This is primarily used for hiding
372
 * mounted volumes that are intended as APIs for programs to read, and system
373
 * administrators at a shell; rather than something that should, for example,
374
 * appear in a GUI. For example, the Linux `/proc` filesystem.
375
 *
376
 * The list of device paths considered ‘system’ ones may change over time.
377
 *
378
 * Returns: %TRUE if @device_path is considered an implementation detail of
379
 *    the OS.
380
 * Since: 2.56
381
 */
382
gboolean
383
g_unix_is_system_device_path (const char *device_path)
384
0
{
385
0
  const char *ignore_devices[] = {
386
0
    "none",
387
0
    "sunrpc",
388
0
    "devpts",
389
0
    "nfsd",
390
0
    "/dev/loop",
391
0
    "/dev/vn",
392
0
    NULL
393
0
  };
394
395
0
  g_return_val_if_fail (device_path != NULL && *device_path != '\0', FALSE);
396
397
0
  return is_in (device_path, ignore_devices);
398
0
}
399
400
static gboolean
401
guess_system_internal (const char *mountpoint,
402
                       const char *fs,
403
                       const char *device,
404
                       const char *root)
405
0
{
406
0
  if (g_unix_is_system_fs_type (fs))
407
0
    return TRUE;
408
  
409
0
  if (g_unix_is_system_device_path (device))
410
0
    return TRUE;
411
412
0
  if (g_unix_is_mount_path_system_internal (mountpoint))
413
0
    return TRUE;
414
415
  /* It is not possible to reliably detect mounts which were created by bind
416
   * operation. mntent-based _g_get_unix_mounts() implementation blindly skips
417
   * mounts with a device path that is repeated (e.g. mounts created by bind
418
   * operation, btrfs subvolumes). This usually chooses the most important
419
   * mounts (i.e. which points to the root of filesystem), but it doesn't work
420
   * in all cases and also it is not ideal that those mounts are completely
421
   * ignored (e.g. x-gvfs-show doesn't work for them, trash backend can't handle
422
   * files on btrfs subvolumes). libmount-based _g_get_unix_mounts()
423
   * implementation provides a root path. So there is no need to completely
424
   * ignore those mounts, because e.g. our volume monitors can use the root path
425
   * to not mengle those mounts with the "regular" mounts (i.e. which points to
426
   * the root). But because those mounts usually just duplicate other mounts and
427
   * are completely ignored with mntend-based implementation, let's mark them as
428
   * system internal. Given the different approaches it doesn't mean that all
429
   * mounts which were ignored will be system internal now, but this should work
430
   * in most cases. For more info, see g_unix_mount_get_root_path() annotation,
431
   * comment in mntent-based _g_get_unix_mounts() implementation and the
432
   * https://gitlab.gnome.org/GNOME/glib/issues/1271 issue.
433
   */
434
0
  if (root != NULL && g_strcmp0 (root, "/") != 0)
435
0
    return TRUE;
436
437
0
  return FALSE;
438
0
}
439
440
/* GUnixMounts (ie: mtab) implementations {{{1 */
441
442
static GUnixMountEntry *
443
create_unix_mount_entry (const char *device_path,
444
                         const char *mount_path,
445
                         const char *root_path,
446
                         const char *filesystem_type,
447
                         const char *options,
448
                         gboolean    is_read_only)
449
0
{
450
0
  GUnixMountEntry *mount_entry = NULL;
451
452
0
  mount_entry = g_new0 (GUnixMountEntry, 1);
453
0
  mount_entry->device_path = g_strdup (device_path);
454
0
  mount_entry->mount_path = g_strdup (mount_path);
455
0
  mount_entry->root_path = g_strdup (root_path);
456
0
  mount_entry->filesystem_type = g_strdup (filesystem_type);
457
0
  mount_entry->options = g_strdup (options);
458
0
  mount_entry->is_read_only = is_read_only;
459
460
0
  mount_entry->is_system_internal =
461
0
    guess_system_internal (mount_entry->mount_path,
462
0
                           mount_entry->filesystem_type,
463
0
                           mount_entry->device_path,
464
0
                           mount_entry->root_path);
465
466
0
  return mount_entry;
467
0
}
468
469
static GUnixMountPoint *
470
create_unix_mount_point (const char *device_path,
471
                         const char *mount_path,
472
                         const char *filesystem_type,
473
                         const char *options,
474
                         gboolean    is_read_only,
475
                         gboolean    is_user_mountable,
476
                         gboolean    is_loopback)
477
0
{
478
0
  GUnixMountPoint *mount_point = NULL;
479
480
0
  mount_point = g_new0 (GUnixMountPoint, 1);
481
0
  mount_point->device_path = g_strdup (device_path);
482
0
  mount_point->mount_path = g_strdup (mount_path);
483
0
  mount_point->filesystem_type = g_strdup (filesystem_type);
484
0
  mount_point->options = g_strdup (options);
485
0
  mount_point->is_read_only = is_read_only;
486
0
  mount_point->is_user_mountable = is_user_mountable;
487
0
  mount_point->is_loopback = is_loopback;
488
489
0
  return mount_point;
490
0
}
491
492
/* mntent.h (Linux, GNU, NSS) {{{2 */
493
#ifdef HAVE_MNTENT_H
494
495
#ifdef HAVE_LIBMOUNT
496
497
/* For documentation on /proc/self/mountinfo see
498
 * http://www.kernel.org/doc/Documentation/filesystems/proc.txt
499
 */
500
#define PROC_MOUNTINFO_PATH "/proc/self/mountinfo"
501
502
static GList *
503
_g_get_unix_mounts (void)
504
{
505
  struct libmnt_table *table = NULL;
506
  struct libmnt_iter* iter = NULL;
507
  struct libmnt_fs *fs = NULL;
508
  GUnixMountEntry *mount_entry = NULL;
509
  GList *return_list = NULL;
510
511
  table = mnt_new_table ();
512
  if (mnt_table_parse_mtab (table, NULL) < 0)
513
    goto out;
514
515
  iter = mnt_new_iter (MNT_ITER_FORWARD);
516
  while (mnt_table_next_fs (table, iter, &fs) == 0)
517
    {
518
      const char *device_path = NULL;
519
      char *mount_options = NULL;
520
      unsigned long mount_flags = 0;
521
      gboolean is_read_only = FALSE;
522
523
      device_path = mnt_fs_get_source (fs);
524
      if (g_strcmp0 (device_path, "/dev/root") == 0)
525
        device_path = _resolve_dev_root ();
526
527
      mount_options = mnt_fs_strdup_options (fs);
528
      if (mount_options)
529
        {
530
          mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
531
          g_free (mount_options);
532
        }
533
      is_read_only = (mount_flags & MS_RDONLY) ? TRUE : FALSE;
534
535
      mount_entry = create_unix_mount_entry (device_path,
536
                                             mnt_fs_get_target (fs),
537
                                             mnt_fs_get_root (fs),
538
                                             mnt_fs_get_fstype (fs),
539
                                             mnt_fs_get_options (fs),
540
                                             is_read_only);
541
542
      return_list = g_list_prepend (return_list, mount_entry);
543
    }
544
  mnt_free_iter (iter);
545
546
 out:
547
  mnt_free_table (table);
548
549
  return g_list_reverse (return_list);
550
}
551
552
#else
553
554
static const char *
555
get_mtab_read_file (void)
556
0
{
557
0
#ifdef _PATH_MOUNTED
558
0
# ifdef __linux__
559
0
  return "/proc/mounts";
560
# else
561
  return _PATH_MOUNTED;
562
# endif
563
#else
564
  return "/etc/mtab";
565
#endif
566
0
}
567
568
#ifndef HAVE_GETMNTENT_R
569
G_LOCK_DEFINE_STATIC(getmntent);
570
#endif
571
572
static GList *
573
_g_get_unix_mounts (void)
574
0
{
575
0
#ifdef HAVE_GETMNTENT_R
576
0
  struct mntent ent;
577
0
  char buf[1024];
578
0
#endif
579
0
  struct mntent *mntent;
580
0
  FILE *file;
581
0
  const char *read_file;
582
0
  GUnixMountEntry *mount_entry;
583
0
  GHashTable *mounts_hash;
584
0
  GList *return_list;
585
  
586
0
  read_file = get_mtab_read_file ();
587
588
0
  file = setmntent (read_file, "re");
589
0
  if (file == NULL)
590
0
    return NULL;
591
592
0
  return_list = NULL;
593
  
594
0
  mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
595
  
596
0
#ifdef HAVE_GETMNTENT_R
597
0
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
598
#else
599
  G_LOCK (getmntent);
600
  while ((mntent = getmntent (file)) != NULL)
601
#endif
602
0
    {
603
0
      const char *device_path = NULL;
604
0
      gboolean is_read_only = FALSE;
605
606
      /* ignore any mnt_fsname that is repeated and begins with a '/'
607
       *
608
       * We do this to avoid being fooled by --bind mounts, since
609
       * these have the same device as the location they bind to.
610
       * It's not an ideal solution to the problem, but it's likely that
611
       * the most important mountpoint is first and the --bind ones after
612
       * that aren't as important. So it should work.
613
       *
614
       * The '/' is to handle procfs, tmpfs and other no device mounts.
615
       */
616
0
      if (mntent->mnt_fsname != NULL &&
617
0
    mntent->mnt_fsname[0] == '/' &&
618
0
    g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
619
0
        continue;
620
621
0
      if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
622
0
        device_path = _resolve_dev_root ();
623
0
      else
624
0
        device_path = mntent->mnt_fsname;
625
626
0
#if defined (HAVE_HASMNTOPT)
627
0
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
628
0
  is_read_only = TRUE;
629
0
#endif
630
631
0
      mount_entry = create_unix_mount_entry (device_path,
632
0
                                             mntent->mnt_dir,
633
0
                                             NULL,
634
0
                                             mntent->mnt_type,
635
0
                                             mntent->mnt_opts,
636
0
                                             is_read_only);
637
638
0
      g_hash_table_insert (mounts_hash,
639
0
         mount_entry->device_path,
640
0
         mount_entry->device_path);
641
642
0
      return_list = g_list_prepend (return_list, mount_entry);
643
0
    }
644
0
  g_hash_table_destroy (mounts_hash);
645
  
646
0
  endmntent (file);
647
648
#ifndef HAVE_GETMNTENT_R
649
  G_UNLOCK (getmntent);
650
#endif
651
  
652
0
  return g_list_reverse (return_list);
653
0
}
654
655
#endif /* HAVE_LIBMOUNT */
656
657
static const char *
658
get_mtab_monitor_file (void)
659
0
{
660
0
  static const char *mountinfo_path = NULL;
661
#ifdef HAVE_LIBMOUNT
662
  struct stat buf;
663
#endif
664
665
0
  if (mountinfo_path != NULL)
666
0
    return mountinfo_path;
667
668
#ifdef HAVE_LIBMOUNT
669
  /* The mtab file is still used by some distros, so it has to be monitored in
670
   * order to avoid races between g_unix_mounts_get and "mounts-changed" signal:
671
   * https://bugzilla.gnome.org/show_bug.cgi?id=782814
672
   */
673
  if (mnt_has_regular_mtab (&mountinfo_path, NULL))
674
    {
675
      return mountinfo_path;
676
    }
677
678
  if (stat (PROC_MOUNTINFO_PATH, &buf) == 0)
679
    {
680
      mountinfo_path = PROC_MOUNTINFO_PATH;
681
      return mountinfo_path;
682
    }
683
#endif
684
685
0
#ifdef _PATH_MOUNTED
686
0
# ifdef __linux__
687
0
  mountinfo_path = "/proc/mounts";
688
# else
689
  mountinfo_path = _PATH_MOUNTED;
690
# endif
691
#else
692
  mountinfo_path = "/etc/mtab";
693
#endif
694
695
0
  return mountinfo_path;
696
0
}
697
698
/* mnttab.h {{{2 */
699
#elif defined (HAVE_SYS_MNTTAB_H)
700
701
G_LOCK_DEFINE_STATIC(getmntent);
702
703
static const char *
704
get_mtab_read_file (void)
705
{
706
#ifdef _PATH_MOUNTED
707
  return _PATH_MOUNTED;
708
#else 
709
  return "/etc/mnttab";
710
#endif
711
}
712
713
static const char *
714
get_mtab_monitor_file (void)
715
{
716
  return get_mtab_read_file ();
717
}
718
719
static GList *
720
_g_get_unix_mounts (void)
721
{
722
  struct mnttab mntent;
723
  FILE *file;
724
  const char *read_file;
725
  GUnixMountEntry *mount_entry;
726
  GList *return_list;
727
  
728
  read_file = get_mtab_read_file ();
729
  
730
  file = setmntent (read_file, "re");
731
  if (file == NULL)
732
    return NULL;
733
  
734
  return_list = NULL;
735
  
736
  G_LOCK (getmntent);
737
  while (! getmntent (file, &mntent))
738
    {
739
      gboolean is_read_only = FALSE;
740
741
#if defined (HAVE_HASMNTOPT)
742
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
743
  is_read_only = TRUE;
744
#endif
745
746
      mount_entry = create_unix_mount_entry (mntent.mnt_special,
747
                                             mntent.mnt_mountp,
748
                                             NULL,
749
                                             mntent.mnt_fstype,
750
                                             mntent.mnt_opts,
751
                                             is_read_only);
752
753
      return_list = g_list_prepend (return_list, mount_entry);
754
    }
755
  
756
  endmntent (file);
757
  
758
  G_UNLOCK (getmntent);
759
  
760
  return g_list_reverse (return_list);
761
}
762
763
/* mntctl.h (AIX) {{{2 */
764
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
765
766
static const char *
767
get_mtab_monitor_file (void)
768
{
769
  return NULL;
770
}
771
772
static GList *
773
_g_get_unix_mounts (void)
774
{
775
  struct vfs_ent *fs_info;
776
  struct vmount *vmount_info;
777
  int vmount_number;
778
  unsigned int vmount_size;
779
  int current;
780
  GList *return_list;
781
  
782
  if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
783
    {
784
      g_warning ("Unable to know the number of mounted volumes");
785
      
786
      return NULL;
787
    }
788
789
  vmount_info = (struct vmount*)g_malloc (vmount_size);
790
791
  vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
792
  
793
  if (vmount_info->vmt_revision != VMT_REVISION)
794
    g_warning ("Bad vmount structure revision number, want %d, got %d", VMT_REVISION, vmount_info->vmt_revision);
795
796
  if (vmount_number < 0)
797
    {
798
      g_warning ("Unable to recover mounted volumes information");
799
      
800
      g_free (vmount_info);
801
      return NULL;
802
    }
803
  
804
  return_list = NULL;
805
  while (vmount_number > 0)
806
    {
807
      gboolean is_read_only = FALSE;
808
809
      fs_info = getvfsbytype (vmount_info->vmt_gfstype);
810
811
      /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
812
      is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
813
814
      mount_entry = create_unix_mount_entry (vmt2dataptr (vmount_info, VMT_OBJECT),
815
                                             vmt2dataptr (vmount_info, VMT_STUB),
816
                                             NULL,
817
                                             fs_info == NULL ? "unknown" : fs_info->vfsent_name,
818
                                             NULL,
819
                                             is_read_only);
820
821
      return_list = g_list_prepend (return_list, mount_entry);
822
      
823
      vmount_info = (struct vmount *)( (char*)vmount_info 
824
               + vmount_info->vmt_length);
825
      vmount_number--;
826
    }
827
  
828
  g_free (vmount_info);
829
  
830
  return g_list_reverse (return_list);
831
}
832
833
/* sys/mount.h {{{2 */
834
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
835
836
static const char *
837
get_mtab_monitor_file (void)
838
{
839
  return NULL;
840
}
841
842
static GList *
843
_g_get_unix_mounts (void)
844
{
845
#if defined(USE_STATVFS)
846
  struct statvfs *mntent = NULL;
847
#elif defined(USE_STATFS)
848
  struct statfs *mntent = NULL;
849
#else
850
  #error statfs juggling failed
851
#endif
852
  size_t bufsize;
853
  int num_mounts, i;
854
  GUnixMountEntry *mount_entry;
855
  GList *return_list;
856
  
857
  /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
858
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
859
  num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
860
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
861
  num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
862
#endif
863
  if (num_mounts == -1)
864
    return NULL;
865
866
  bufsize = num_mounts * sizeof (*mntent);
867
  mntent = g_malloc (bufsize);
868
#if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
869
  num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
870
#elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
871
  num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
872
#endif
873
  if (num_mounts == -1)
874
    return NULL;
875
  
876
  return_list = NULL;
877
  
878
  for (i = 0; i < num_mounts; i++)
879
    {
880
      gboolean is_read_only = FALSE;
881
882
#if defined(USE_STATVFS)
883
      if (mntent[i].f_flag & ST_RDONLY)
884
#elif defined(USE_STATFS)
885
      if (mntent[i].f_flags & MNT_RDONLY)
886
#else
887
      #error statfs juggling failed
888
#endif
889
        is_read_only = TRUE;
890
891
      mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
892
                                             mntent[i].f_mntonname,
893
                                             NULL,
894
                                             mntent[i].f_fstypename,
895
                                             NULL,
896
                                             is_read_only);
897
898
      return_list = g_list_prepend (return_list, mount_entry);
899
    }
900
901
  g_free (mntent);
902
  
903
  return g_list_reverse (return_list);
904
}
905
906
/* Interix {{{2 */
907
#elif defined(__INTERIX)
908
909
static const char *
910
get_mtab_monitor_file (void)
911
{
912
  return NULL;
913
}
914
915
static GList *
916
_g_get_unix_mounts (void)
917
{
918
  DIR *dirp;
919
  GList* return_list = NULL;
920
  char filename[9 + NAME_MAX];
921
922
  dirp = opendir ("/dev/fs");
923
  if (!dirp)
924
    {
925
      g_warning ("unable to read /dev/fs!");
926
      return NULL;
927
    }
928
929
  while (1)
930
    {
931
      struct statvfs statbuf;
932
      struct dirent entry;
933
      struct dirent* result;
934
      
935
      if (readdir_r (dirp, &entry, &result) || result == NULL)
936
        break;
937
      
938
      strcpy (filename, "/dev/fs/");
939
      strcat (filename, entry.d_name);
940
      
941
      if (statvfs (filename, &statbuf) == 0)
942
        {
943
          GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
944
          
945
          mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
946
          mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
947
          mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
948
          
949
          if (statbuf.f_flag & ST_RDONLY)
950
            mount_entry->is_read_only = TRUE;
951
          
952
          return_list = g_list_prepend(return_list, mount_entry);
953
        }
954
    }
955
  
956
  return_list = g_list_reverse (return_list);
957
  
958
  closedir (dirp);
959
960
  return return_list;
961
}
962
963
/* QNX {{{2 */
964
#elif defined (HAVE_QNX)
965
966
static char *
967
get_mtab_monitor_file (void)
968
{
969
  /* TODO: Not implemented */
970
  return NULL;
971
}
972
973
static GList *
974
_g_get_unix_mounts (void)
975
{
976
  /* TODO: Not implemented */
977
  return NULL;
978
}
979
980
/* Common code {{{2 */
981
#else
982
#error No _g_get_unix_mounts() implementation for system
983
#endif
984
985
/* GUnixMountPoints (ie: fstab) implementations {{{1 */
986
987
/* _g_get_unix_mount_points():
988
 * read the fstab.
989
 * don't return swap and ignore mounts.
990
 */
991
992
static char *
993
get_fstab_file (void)
994
0
{
995
#ifdef HAVE_LIBMOUNT
996
  return (char *) mnt_get_fstab_path ();
997
#else
998
#if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
999
  /* AIX */
1000
  return "/etc/filesystems";
1001
#elif defined(_PATH_MNTTAB)
1002
0
  return _PATH_MNTTAB;
1003
#elif defined(VFSTAB)
1004
  return VFSTAB;
1005
#else
1006
  return "/etc/fstab";
1007
#endif
1008
0
#endif
1009
0
}
1010
1011
/* mntent.h (Linux, GNU, NSS) {{{2 */
1012
#ifdef HAVE_MNTENT_H
1013
1014
#ifdef HAVE_LIBMOUNT
1015
1016
static GList *
1017
_g_get_unix_mount_points (void)
1018
{
1019
  struct libmnt_table *table = NULL;
1020
  struct libmnt_iter* iter = NULL;
1021
  struct libmnt_fs *fs = NULL;
1022
  GUnixMountPoint *mount_point = NULL;
1023
  GList *return_list = NULL;
1024
1025
  table = mnt_new_table ();
1026
  if (mnt_table_parse_fstab (table, NULL) < 0)
1027
    goto out;
1028
1029
  iter = mnt_new_iter (MNT_ITER_FORWARD);
1030
  while (mnt_table_next_fs (table, iter, &fs) == 0)
1031
    {
1032
      const char *device_path = NULL;
1033
      const char *mount_path = NULL;
1034
      const char *mount_fstype = NULL;
1035
      char *mount_options = NULL;
1036
      gboolean is_read_only = FALSE;
1037
      gboolean is_user_mountable = FALSE;
1038
      gboolean is_loopback = FALSE;
1039
1040
      mount_path = mnt_fs_get_target (fs);
1041
      if ((strcmp (mount_path, "ignore") == 0) ||
1042
          (strcmp (mount_path, "swap") == 0) ||
1043
          (strcmp (mount_path, "none") == 0))
1044
        continue;
1045
1046
      mount_fstype = mnt_fs_get_fstype (fs);
1047
      mount_options = mnt_fs_strdup_options (fs);
1048
      if (mount_options)
1049
        {
1050
          unsigned long mount_flags = 0;
1051
          unsigned long userspace_flags = 0;
1052
1053
          mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap (MNT_LINUX_MAP));
1054
          mnt_optstr_get_flags (mount_options, &userspace_flags, mnt_get_builtin_optmap (MNT_USERSPACE_MAP));
1055
1056
          /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1057
          if (mount_flags & MS_BIND)
1058
            {
1059
              g_free (mount_options);
1060
              continue;
1061
            }
1062
1063
          is_read_only = (mount_flags & MS_RDONLY) != 0;
1064
          is_loopback = (userspace_flags & MNT_MS_LOOP) != 0;
1065
1066
          if ((mount_fstype != NULL && g_strcmp0 ("supermount", mount_fstype) == 0) ||
1067
              ((userspace_flags & MNT_MS_USER) &&
1068
               (g_strstr_len (mount_options, -1, "user_xattr") == NULL)) ||
1069
              (userspace_flags & MNT_MS_USERS) ||
1070
              (userspace_flags & MNT_MS_OWNER))
1071
            {
1072
              is_user_mountable = TRUE;
1073
            }
1074
        }
1075
1076
      device_path = mnt_fs_get_source (fs);
1077
      if (g_strcmp0 (device_path, "/dev/root") == 0)
1078
        device_path = _resolve_dev_root ();
1079
1080
      mount_point = create_unix_mount_point (device_path,
1081
                                             mount_path,
1082
                                             mount_fstype,
1083
                                             mount_options,
1084
                                             is_read_only,
1085
                                             is_user_mountable,
1086
                                             is_loopback);
1087
      if (mount_options)
1088
        g_free (mount_options);
1089
1090
      return_list = g_list_prepend (return_list, mount_point);
1091
    }
1092
  mnt_free_iter (iter);
1093
1094
 out:
1095
  mnt_free_table (table);
1096
1097
  return g_list_reverse (return_list);
1098
}
1099
1100
#else
1101
1102
static GList *
1103
_g_get_unix_mount_points (void)
1104
0
{
1105
0
#ifdef HAVE_GETMNTENT_R
1106
0
  struct mntent ent;
1107
0
  char buf[1024];
1108
0
#endif
1109
0
  struct mntent *mntent;
1110
0
  FILE *file;
1111
0
  char *read_file;
1112
0
  GUnixMountPoint *mount_point;
1113
0
  GList *return_list;
1114
  
1115
0
  read_file = get_fstab_file ();
1116
  
1117
0
  file = setmntent (read_file, "re");
1118
0
  if (file == NULL)
1119
0
    return NULL;
1120
1121
0
  return_list = NULL;
1122
  
1123
0
#ifdef HAVE_GETMNTENT_R
1124
0
  while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
1125
#else
1126
  G_LOCK (getmntent);
1127
  while ((mntent = getmntent (file)) != NULL)
1128
#endif
1129
0
    {
1130
0
      const char *device_path = NULL;
1131
0
      gboolean is_read_only = FALSE;
1132
0
      gboolean is_user_mountable = FALSE;
1133
0
      gboolean is_loopback = FALSE;
1134
1135
0
      if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
1136
0
          (strcmp (mntent->mnt_dir, "swap") == 0) ||
1137
0
          (strcmp (mntent->mnt_dir, "none") == 0))
1138
0
  continue;
1139
1140
0
#ifdef HAVE_HASMNTOPT
1141
      /* We ignore bind fstab entries, as we ignore bind mounts anyway */
1142
0
      if (hasmntopt (mntent, "bind"))
1143
0
        continue;
1144
0
#endif
1145
1146
0
      if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
1147
0
        device_path = _resolve_dev_root ();
1148
0
      else
1149
0
        device_path = mntent->mnt_fsname;
1150
1151
0
#ifdef HAVE_HASMNTOPT
1152
0
      if (hasmntopt (mntent, MNTOPT_RO) != NULL)
1153
0
  is_read_only = TRUE;
1154
1155
0
      if (hasmntopt (mntent, "loop") != NULL)
1156
0
  is_loopback = TRUE;
1157
1158
0
#endif
1159
1160
0
      if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
1161
0
#ifdef HAVE_HASMNTOPT
1162
0
    || (hasmntopt (mntent, "user") != NULL
1163
0
        && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
1164
0
    || hasmntopt (mntent, "users") != NULL
1165
0
    || hasmntopt (mntent, "owner") != NULL
1166
0
#endif
1167
0
    )
1168
0
  is_user_mountable = TRUE;
1169
1170
0
      mount_point = create_unix_mount_point (device_path,
1171
0
                                             mntent->mnt_dir,
1172
0
                                             mntent->mnt_type,
1173
0
                                             mntent->mnt_opts,
1174
0
                                             is_read_only,
1175
0
                                             is_user_mountable,
1176
0
                                             is_loopback);
1177
1178
0
      return_list = g_list_prepend (return_list, mount_point);
1179
0
    }
1180
  
1181
0
  endmntent (file);
1182
1183
#ifndef HAVE_GETMNTENT_R
1184
  G_UNLOCK (getmntent);
1185
#endif
1186
  
1187
0
  return g_list_reverse (return_list);
1188
0
}
1189
1190
#endif /* HAVE_LIBMOUNT */
1191
1192
/* mnttab.h {{{2 */
1193
#elif defined (HAVE_SYS_MNTTAB_H)
1194
1195
static GList *
1196
_g_get_unix_mount_points (void)
1197
{
1198
  struct mnttab mntent;
1199
  FILE *file;
1200
  char *read_file;
1201
  GUnixMountPoint *mount_point;
1202
  GList *return_list;
1203
  
1204
  read_file = get_fstab_file ();
1205
  
1206
  file = setmntent (read_file, "re");
1207
  if (file == NULL)
1208
    return NULL;
1209
1210
  return_list = NULL;
1211
  
1212
  G_LOCK (getmntent);
1213
  while (! getmntent (file, &mntent))
1214
    {
1215
      gboolean is_read_only = FALSE;
1216
      gboolean is_user_mountable = FALSE;
1217
      gboolean is_loopback = FALSE;
1218
1219
      if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
1220
          (strcmp (mntent.mnt_mountp, "swap") == 0) ||
1221
          (strcmp (mntent.mnt_mountp, "none") == 0))
1222
  continue;
1223
1224
#ifdef HAVE_HASMNTOPT
1225
      if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
1226
  is_read_only = TRUE;
1227
1228
      if (hasmntopt (&mntent, "lofs") != NULL)
1229
  is_loopback = TRUE;
1230
#endif
1231
1232
      if ((mntent.mnt_fstype != NULL)
1233
#ifdef HAVE_HASMNTOPT
1234
    || (hasmntopt (&mntent, "user") != NULL
1235
        && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
1236
    || hasmntopt (&mntent, "users") != NULL
1237
    || hasmntopt (&mntent, "owner") != NULL
1238
#endif
1239
    )
1240
  is_user_mountable = TRUE;
1241
1242
      mount_point = create_unix_mount_point (mntent.mnt_special,
1243
                                             mntent.mnt_mountp,
1244
                                             mntent.mnt_fstype,
1245
                                             mntent.mnt_mntopts,
1246
                                             is_read_only,
1247
                                             is_user_mountable,
1248
                                             is_loopback);
1249
1250
      return_list = g_list_prepend (return_list, mount_point);
1251
    }
1252
  
1253
  endmntent (file);
1254
  G_UNLOCK (getmntent);
1255
  
1256
  return g_list_reverse (return_list);
1257
}
1258
1259
/* mntctl.h (AIX) {{{2 */
1260
#elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
1261
1262
/* functions to parse /etc/filesystems on aix */
1263
1264
/* read character, ignoring comments (begin with '*', end with '\n' */
1265
static int
1266
aix_fs_getc (FILE *fd)
1267
{
1268
  int c;
1269
  
1270
  while ((c = getc (fd)) == '*')
1271
    {
1272
      while (((c = getc (fd)) != '\n') && (c != EOF))
1273
  ;
1274
    }
1275
}
1276
1277
/* eat all continuous spaces in a file */
1278
static int
1279
aix_fs_ignorespace (FILE *fd)
1280
{
1281
  int c;
1282
  
1283
  while ((c = aix_fs_getc (fd)) != EOF)
1284
    {
1285
      if (!g_ascii_isspace (c))
1286
  {
1287
    ungetc (c,fd);
1288
    return c;
1289
  }
1290
    }
1291
  
1292
  return EOF;
1293
}
1294
1295
/* read one word from file */
1296
static int
1297
aix_fs_getword (FILE *fd, 
1298
                char *word)
1299
{
1300
  int c;
1301
  
1302
  aix_fs_ignorespace (fd);
1303
1304
  while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
1305
    {
1306
      if (c == '"')
1307
  {
1308
    while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
1309
      *word++ = c;
1310
    else
1311
      *word++ = c;
1312
  }
1313
    }
1314
  *word = 0;
1315
  
1316
  return c;
1317
}
1318
1319
typedef struct {
1320
  char mnt_mount[PATH_MAX];
1321
  char mnt_special[PATH_MAX];
1322
  char mnt_fstype[16];
1323
  char mnt_options[128];
1324
} AixMountTableEntry;
1325
1326
/* read mount points properties */
1327
static int
1328
aix_fs_get (FILE               *fd, 
1329
            AixMountTableEntry *prop)
1330
{
1331
  static char word[PATH_MAX] = { 0 };
1332
  char value[PATH_MAX];
1333
  
1334
  /* read stanza */
1335
  if (word[0] == 0)
1336
    {
1337
      if (aix_fs_getword (fd, word) == EOF)
1338
  return EOF;
1339
    }
1340
1341
  word[strlen(word) - 1] = 0;
1342
  strcpy (prop->mnt_mount, word);
1343
  
1344
  /* read attributes and value */
1345
  
1346
  while (aix_fs_getword (fd, word) != EOF)
1347
    {
1348
      /* test if is attribute or new stanza */
1349
      if (word[strlen(word) - 1] == ':')
1350
  return 0;
1351
      
1352
      /* read "=" */
1353
      aix_fs_getword (fd, value);
1354
      
1355
      /* read value */
1356
      aix_fs_getword (fd, value);
1357
      
1358
      if (strcmp (word, "dev") == 0)
1359
  strcpy (prop->mnt_special, value);
1360
      else if (strcmp (word, "vfs") == 0)
1361
  strcpy (prop->mnt_fstype, value);
1362
      else if (strcmp (word, "options") == 0)
1363
  strcpy(prop->mnt_options, value);
1364
    }
1365
  
1366
  return 0;
1367
}
1368
1369
static GList *
1370
_g_get_unix_mount_points (void)
1371
{
1372
  struct mntent *mntent;
1373
  FILE *file;
1374
  char *read_file;
1375
  GUnixMountPoint *mount_point;
1376
  AixMountTableEntry mntent;
1377
  GList *return_list;
1378
  
1379
  read_file = get_fstab_file ();
1380
  
1381
  file = setmntent (read_file, "re");
1382
  if (file == NULL)
1383
    return NULL;
1384
  
1385
  return_list = NULL;
1386
  
1387
  while (!aix_fs_get (file, &mntent))
1388
    {
1389
      if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1390
  {
1391
          mount_point = create_unix_mount_point (mntent.mnt_special,
1392
                                                 mntent.mnt_mount,
1393
                                                 mntent.mnt_fstype,
1394
                                                 mntent.mnt_options,
1395
                                                 TRUE,
1396
                                                 TRUE,
1397
                                                 FALSE);
1398
1399
    return_list = g_list_prepend (return_list, mount_point);
1400
  }
1401
    }
1402
  
1403
  endmntent (file);
1404
  
1405
  return g_list_reverse (return_list);
1406
}
1407
1408
#elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1409
1410
static GList *
1411
_g_get_unix_mount_points (void)
1412
{
1413
  struct fstab *fstab = NULL;
1414
  GUnixMountPoint *mount_point;
1415
  GList *return_list = NULL;
1416
  G_LOCK_DEFINE_STATIC (fsent);
1417
#ifdef HAVE_SYS_SYSCTL_H
1418
  uid_t uid = getuid ();
1419
  int usermnt = 0;
1420
  struct stat sb;
1421
#endif
1422
1423
#ifdef HAVE_SYS_SYSCTL_H
1424
#if defined(HAVE_SYSCTLBYNAME)
1425
  {
1426
    size_t len = sizeof(usermnt);
1427
1428
    sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1429
  }
1430
#elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1431
  {
1432
    int mib[2];
1433
    size_t len = sizeof(usermnt);
1434
    
1435
    mib[0] = CTL_VFS;
1436
    mib[1] = VFS_USERMOUNT;
1437
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
1438
  }
1439
#elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1440
  {
1441
    int mib[2];
1442
    size_t len = sizeof(usermnt);
1443
    
1444
    mib[0] = CTL_KERN;
1445
    mib[1] = KERN_USERMOUNT;
1446
    sysctl (mib, 2, &usermnt, &len, NULL, 0);
1447
  }
1448
#endif
1449
#endif
1450
1451
  G_LOCK (fsent);
1452
  if (!setfsent ())
1453
    {
1454
      G_UNLOCK (fsent);
1455
      return NULL;
1456
    }
1457
1458
  while ((fstab = getfsent ()) != NULL)
1459
    {
1460
      gboolean is_read_only = FALSE;
1461
      gboolean is_user_mountable = FALSE;
1462
1463
      if (strcmp (fstab->fs_vfstype, "swap") == 0)
1464
  continue;
1465
1466
      if (strcmp (fstab->fs_type, "ro") == 0)
1467
  is_read_only = TRUE;
1468
1469
#ifdef HAVE_SYS_SYSCTL_H
1470
      if (usermnt != 0)
1471
        {
1472
          if (uid == 0 ||
1473
              (stat (fstab->fs_file, &sb) == 0 && sb.st_uid == uid))
1474
            {
1475
              is_user_mountable = TRUE;
1476
            }
1477
        }
1478
#endif
1479
1480
      mount_point = create_unix_mount_point (fstab->fs_spec,
1481
                                             fstab->fs_file,
1482
                                             fstab->fs_vfstype,
1483
                                             fstab->fs_mntops,
1484
                                             is_read_only,
1485
                                             is_user_mountable,
1486
                                             FALSE);
1487
1488
      return_list = g_list_prepend (return_list, mount_point);
1489
    }
1490
1491
  endfsent ();
1492
  G_UNLOCK (fsent);
1493
1494
  return g_list_reverse (return_list);
1495
}
1496
/* Interix {{{2 */
1497
#elif defined(__INTERIX)
1498
static GList *
1499
_g_get_unix_mount_points (void)
1500
{
1501
  return _g_get_unix_mounts ();
1502
}
1503
1504
/* QNX {{{2 */
1505
#elif defined (HAVE_QNX)
1506
static GList *
1507
_g_get_unix_mount_points (void)
1508
{
1509
  return _g_get_unix_mounts ();
1510
}
1511
1512
/* Common code {{{2 */
1513
#else
1514
#error No g_get_mount_table() implementation for system
1515
#endif
1516
1517
static guint64
1518
get_mounts_timestamp (void)
1519
0
{
1520
0
  const char *monitor_file;
1521
0
  struct stat buf;
1522
0
  guint64 timestamp = 0;
1523
1524
0
  G_LOCK (proc_mounts_source);
1525
1526
0
  monitor_file = get_mtab_monitor_file ();
1527
  /* Don't return mtime for /proc/ files */
1528
0
  if (monitor_file && !g_str_has_prefix (monitor_file, "/proc/"))
1529
0
    {
1530
0
      if (stat (monitor_file, &buf) == 0)
1531
0
        timestamp = buf.st_mtime;
1532
0
    }
1533
0
  else if (proc_mounts_watch_is_running ())
1534
0
    {
1535
      /* it's being monitored by poll, so return mount_poller_time */
1536
0
      timestamp = mount_poller_time;
1537
0
    }
1538
0
  else
1539
0
    {
1540
      /* Case of /proc/ file not being monitored - Be on the safe side and
1541
       * send a new timestamp to force g_unix_mounts_changed_since() to
1542
       * return TRUE so any application caches depending on it (like eg.
1543
       * the one in GIO) get invalidated and don't hold possibly outdated
1544
       * data - see Bug 787731 */
1545
0
     timestamp = g_get_monotonic_time ();
1546
0
    }
1547
1548
0
  G_UNLOCK (proc_mounts_source);
1549
1550
0
  return timestamp;
1551
0
}
1552
1553
static guint64
1554
get_mount_points_timestamp (void)
1555
0
{
1556
0
  const char *monitor_file;
1557
0
  struct stat buf;
1558
1559
0
  monitor_file = get_fstab_file ();
1560
0
  if (monitor_file)
1561
0
    {
1562
0
      if (stat (monitor_file, &buf) == 0)
1563
0
        return (guint64)buf.st_mtime;
1564
0
    }
1565
0
  return 0;
1566
0
}
1567
1568
/**
1569
 * g_unix_mounts_get:
1570
 * @time_read: (out) (optional): guint64 to contain a timestamp, or %NULL
1571
 *
1572
 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1573
 * If @time_read is set, it will be filled with the mount
1574
 * timestamp, allowing for checking if the mounts have changed
1575
 * with g_unix_mounts_changed_since().
1576
 *
1577
 * Returns: (element-type GUnixMountEntry) (transfer full):
1578
 *     a #GList of the UNIX mounts.
1579
 **/
1580
GList *
1581
g_unix_mounts_get (guint64 *time_read)
1582
0
{
1583
0
  if (time_read)
1584
0
    *time_read = get_mounts_timestamp ();
1585
1586
0
  return _g_get_unix_mounts ();
1587
0
}
1588
1589
/**
1590
 * g_unix_mount_at:
1591
 * @mount_path: (type filename): path for a possible unix mount.
1592
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1593
 * 
1594
 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1595
 * is set, it will be filled with a unix timestamp for checking
1596
 * if the mounts have changed since with g_unix_mounts_changed_since().
1597
 * 
1598
 * If more mounts have the same mount path, the last matching mount
1599
 * is returned.
1600
 *
1601
 * This will return %NULL if there is no mount point at @mount_path.
1602
 *
1603
 * Returns: (transfer full) (nullable): a #GUnixMountEntry.
1604
 **/
1605
GUnixMountEntry *
1606
g_unix_mount_at (const char *mount_path,
1607
     guint64    *time_read)
1608
0
{
1609
0
  GList *mounts, *l;
1610
0
  GUnixMountEntry *mount_entry, *found;
1611
  
1612
0
  mounts = g_unix_mounts_get (time_read);
1613
1614
0
  found = NULL;
1615
0
  for (l = mounts; l != NULL; l = l->next)
1616
0
    {
1617
0
      mount_entry = l->data;
1618
1619
0
      if (strcmp (mount_path, mount_entry->mount_path) == 0)
1620
0
        {
1621
0
          if (found != NULL)
1622
0
            g_unix_mount_free (found);
1623
1624
0
          found = mount_entry;
1625
0
        }
1626
0
      else
1627
0
        g_unix_mount_free (mount_entry);
1628
0
    }
1629
0
  g_list_free (mounts);
1630
1631
0
  return found;
1632
0
}
1633
1634
/**
1635
 * g_unix_mount_for:
1636
 * @file_path: (type filename): file path on some unix mount.
1637
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1638
 *
1639
 * Gets a #GUnixMountEntry for a given file path. If @time_read
1640
 * is set, it will be filled with a unix timestamp for checking
1641
 * if the mounts have changed since with g_unix_mounts_changed_since().
1642
 *
1643
 * If more mounts have the same mount path, the last matching mount
1644
 * is returned.
1645
 *
1646
 * This will return %NULL if looking up the mount entry fails, if
1647
 * @file_path doesn’t exist or there is an I/O error.
1648
 *
1649
 * Returns: (transfer full)  (nullable): a #GUnixMountEntry.
1650
 *
1651
 * Since: 2.52
1652
 **/
1653
GUnixMountEntry *
1654
g_unix_mount_for (const char *file_path,
1655
                  guint64    *time_read)
1656
0
{
1657
0
  GUnixMountEntry *entry;
1658
1659
0
  g_return_val_if_fail (file_path != NULL, NULL);
1660
1661
0
  entry = g_unix_mount_at (file_path, time_read);
1662
0
  if (entry == NULL)
1663
0
    {
1664
0
      char *topdir;
1665
1666
0
      topdir = _g_local_file_find_topdir_for (file_path);
1667
0
      if (topdir != NULL)
1668
0
        {
1669
0
          entry = g_unix_mount_at (topdir, time_read);
1670
0
          g_free (topdir);
1671
0
        }
1672
0
    }
1673
1674
0
  return entry;
1675
0
}
1676
1677
static gpointer
1678
copy_mount_point_cb (gconstpointer src,
1679
                     gpointer      data)
1680
0
{
1681
0
  GUnixMountPoint *src_mount_point = (GUnixMountPoint *) src;
1682
0
  return g_unix_mount_point_copy (src_mount_point);
1683
0
}
1684
1685
/**
1686
 * g_unix_mount_points_get:
1687
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1688
 *
1689
 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1690
 * If @time_read is set, it will be filled with the mount timestamp,
1691
 * allowing for checking if the mounts have changed with
1692
 * g_unix_mount_points_changed_since().
1693
 *
1694
 * Returns: (element-type GUnixMountPoint) (transfer full):
1695
 *     a #GList of the UNIX mountpoints.
1696
 **/
1697
GList *
1698
g_unix_mount_points_get (guint64 *time_read)
1699
0
{
1700
0
  static GList *mnt_pts_last = NULL;
1701
0
  static guint64 time_read_last = 0;
1702
0
  GList *mnt_pts = NULL;
1703
0
  guint64 time_read_now;
1704
0
  G_LOCK_DEFINE_STATIC (unix_mount_points);
1705
1706
0
  G_LOCK (unix_mount_points);
1707
1708
0
  time_read_now = get_mount_points_timestamp ();
1709
0
  if (time_read_now != time_read_last || mnt_pts_last == NULL)
1710
0
    {
1711
0
      time_read_last = time_read_now;
1712
0
      g_list_free_full (mnt_pts_last, (GDestroyNotify) g_unix_mount_point_free);
1713
0
      mnt_pts_last = _g_get_unix_mount_points ();
1714
0
    }
1715
0
  mnt_pts = g_list_copy_deep (mnt_pts_last, copy_mount_point_cb, NULL);
1716
1717
0
  G_UNLOCK (unix_mount_points);
1718
1719
0
  if (time_read)
1720
0
    *time_read = time_read_now;
1721
1722
0
  return mnt_pts;
1723
0
}
1724
1725
/**
1726
 * g_unix_mount_point_at:
1727
 * @mount_path: (type filename): path for a possible unix mount point.
1728
 * @time_read: (out) (optional): guint64 to contain a timestamp.
1729
 *
1730
 * Gets a #GUnixMountPoint for a given mount path. If @time_read is set, it
1731
 * will be filled with a unix timestamp for checking if the mount points have
1732
 * changed since with g_unix_mount_points_changed_since().
1733
 *
1734
 * If more mount points have the same mount path, the last matching mount point
1735
 * is returned.
1736
 *
1737
 * Returns: (transfer full) (nullable): a #GUnixMountPoint, or %NULL if no match
1738
 * is found.
1739
 *
1740
 * Since: 2.66
1741
 **/
1742
GUnixMountPoint *
1743
g_unix_mount_point_at (const char *mount_path,
1744
                       guint64    *time_read)
1745
0
{
1746
0
  GList *mount_points, *l;
1747
0
  GUnixMountPoint *mount_point, *found;
1748
1749
0
  mount_points = g_unix_mount_points_get (time_read);
1750
1751
0
  found = NULL;
1752
0
  for (l = mount_points; l != NULL; l = l->next)
1753
0
    {
1754
0
      mount_point = l->data;
1755
1756
0
      if (strcmp (mount_path, mount_point->mount_path) == 0)
1757
0
        {
1758
0
          if (found != NULL)
1759
0
            g_unix_mount_point_free (found);
1760
1761
0
          found = mount_point;
1762
0
        }
1763
0
      else
1764
0
        g_unix_mount_point_free (mount_point);
1765
0
    }
1766
0
  g_list_free (mount_points);
1767
1768
0
  return found;
1769
0
}
1770
1771
/**
1772
 * g_unix_mounts_changed_since:
1773
 * @time: guint64 to contain a timestamp.
1774
 * 
1775
 * Checks if the unix mounts have changed since a given unix time.
1776
 * 
1777
 * Returns: %TRUE if the mounts have changed since @time. 
1778
 **/
1779
gboolean
1780
g_unix_mounts_changed_since (guint64 time)
1781
0
{
1782
0
  return get_mounts_timestamp () != time;
1783
0
}
1784
1785
/**
1786
 * g_unix_mount_points_changed_since:
1787
 * @time: guint64 to contain a timestamp.
1788
 * 
1789
 * Checks if the unix mount points have changed since a given unix time.
1790
 * 
1791
 * Returns: %TRUE if the mount points have changed since @time. 
1792
 **/
1793
gboolean
1794
g_unix_mount_points_changed_since (guint64 time)
1795
0
{
1796
0
  return get_mount_points_timestamp () != time;
1797
0
}
1798
1799
/* GUnixMountMonitor {{{1 */
1800
1801
enum {
1802
  MOUNTS_CHANGED,
1803
  MOUNTPOINTS_CHANGED,
1804
  LAST_SIGNAL
1805
};
1806
1807
static guint signals[LAST_SIGNAL];
1808
1809
struct _GUnixMountMonitor {
1810
  GObject parent;
1811
1812
  GMainContext *context;
1813
};
1814
1815
struct _GUnixMountMonitorClass {
1816
  GObjectClass parent_class;
1817
};
1818
1819
1820
0
G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT)
1821
0
1822
0
static GContextSpecificGroup  mount_monitor_group;
1823
0
static GFileMonitor          *fstab_monitor;
1824
0
static GFileMonitor          *mtab_monitor;
1825
0
static GList                 *mount_poller_mounts;
1826
0
static guint                  mtab_file_changed_id;
1827
0
1828
0
/* Called with proc_mounts_source lock held. */
1829
0
static gboolean
1830
0
proc_mounts_watch_is_running (void)
1831
0
{
1832
0
  return proc_mounts_watch_source != NULL &&
1833
0
         !g_source_is_destroyed (proc_mounts_watch_source);
1834
0
}
1835
1836
static void
1837
fstab_file_changed (GFileMonitor      *monitor,
1838
                    GFile             *file,
1839
                    GFile             *other_file,
1840
                    GFileMonitorEvent  event_type,
1841
                    gpointer           user_data)
1842
0
{
1843
0
  if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1844
0
      event_type != G_FILE_MONITOR_EVENT_CREATED &&
1845
0
      event_type != G_FILE_MONITOR_EVENT_DELETED)
1846
0
    return;
1847
1848
0
  g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1849
0
}
1850
1851
static gboolean
1852
mtab_file_changed_cb (gpointer user_data)
1853
0
{
1854
0
  mtab_file_changed_id = 0;
1855
0
  g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
1856
1857
0
  return G_SOURCE_REMOVE;
1858
0
}
1859
1860
static void
1861
mtab_file_changed (GFileMonitor      *monitor,
1862
                   GFile             *file,
1863
                   GFile             *other_file,
1864
                   GFileMonitorEvent  event_type,
1865
                   gpointer           user_data)
1866
0
{
1867
0
  GMainContext *context;
1868
0
  GSource *source;
1869
1870
0
  if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1871
0
      event_type != G_FILE_MONITOR_EVENT_CREATED &&
1872
0
      event_type != G_FILE_MONITOR_EVENT_DELETED)
1873
0
    return;
1874
1875
  /* Skip accumulated events from file monitor which we are not able to handle
1876
   * in a real time instead of emitting mounts_changed signal several times.
1877
   * This should behave equally to GIOChannel based monitoring. See Bug 792235.
1878
   */
1879
0
  if (mtab_file_changed_id > 0)
1880
0
    return;
1881
1882
0
  context = g_main_context_get_thread_default ();
1883
0
  if (!context)
1884
0
    context = g_main_context_default ();
1885
1886
0
  source = g_idle_source_new ();
1887
0
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
1888
0
  g_source_set_callback (source, mtab_file_changed_cb, NULL, NULL);
1889
0
  g_source_set_static_name (source, "[gio] mtab_file_changed_cb");
1890
0
  g_source_attach (source, context);
1891
0
  g_source_unref (source);
1892
0
}
1893
1894
static gboolean
1895
proc_mounts_changed (GIOChannel   *channel,
1896
                     GIOCondition  cond,
1897
                     gpointer      user_data)
1898
0
{
1899
0
  if (cond & G_IO_ERR)
1900
0
    {
1901
0
      G_LOCK (proc_mounts_source);
1902
0
      mount_poller_time = (guint64) g_get_monotonic_time ();
1903
0
      G_UNLOCK (proc_mounts_source);
1904
1905
0
      g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTS_CHANGED]);
1906
0
    }
1907
1908
0
  return TRUE;
1909
0
}
1910
1911
static gboolean
1912
mount_change_poller (gpointer user_data)
1913
0
{
1914
0
  GList *current_mounts, *new_it, *old_it;
1915
0
  gboolean has_changed = FALSE;
1916
1917
0
  current_mounts = _g_get_unix_mounts ();
1918
1919
0
  for ( new_it = current_mounts, old_it = mount_poller_mounts;
1920
0
        new_it != NULL && old_it != NULL;
1921
0
        new_it = g_list_next (new_it), old_it = g_list_next (old_it) )
1922
0
    {
1923
0
      if (g_unix_mount_compare (new_it->data, old_it->data) != 0)
1924
0
        {
1925
0
          has_changed = TRUE;
1926
0
          break;
1927
0
        }
1928
0
    }
1929
0
  if (!(new_it == NULL && old_it == NULL))
1930
0
    has_changed = TRUE;
1931
1932
0
  g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
1933
1934
0
  mount_poller_mounts = current_mounts;
1935
1936
0
  if (has_changed)
1937
0
    {
1938
0
      G_LOCK (proc_mounts_source);
1939
0
      mount_poller_time = (guint64) g_get_monotonic_time ();
1940
0
      G_UNLOCK (proc_mounts_source);
1941
1942
0
      g_context_specific_group_emit (&mount_monitor_group, signals[MOUNTPOINTS_CHANGED]);
1943
0
    }
1944
1945
0
  return TRUE;
1946
0
}
1947
1948
1949
static void
1950
mount_monitor_stop (void)
1951
0
{
1952
0
  if (fstab_monitor)
1953
0
    {
1954
0
      g_file_monitor_cancel (fstab_monitor);
1955
0
      g_object_unref (fstab_monitor);
1956
0
    }
1957
1958
0
  G_LOCK (proc_mounts_source);
1959
0
  if (proc_mounts_watch_source != NULL)
1960
0
    {
1961
0
      g_source_destroy (proc_mounts_watch_source);
1962
0
      proc_mounts_watch_source = NULL;
1963
0
    }
1964
0
  G_UNLOCK (proc_mounts_source);
1965
1966
0
  if (mtab_monitor)
1967
0
    {
1968
0
      g_file_monitor_cancel (mtab_monitor);
1969
0
      g_object_unref (mtab_monitor);
1970
0
    }
1971
1972
0
  if (mtab_file_changed_id)
1973
0
    {
1974
0
      g_source_remove (mtab_file_changed_id);
1975
0
      mtab_file_changed_id = 0;
1976
0
    }
1977
1978
0
  g_list_free_full (mount_poller_mounts, (GDestroyNotify) g_unix_mount_free);
1979
0
}
1980
1981
static void
1982
mount_monitor_start (void)
1983
0
{
1984
0
  GFile *file;
1985
1986
0
  if (get_fstab_file () != NULL)
1987
0
    {
1988
0
      file = g_file_new_for_path (get_fstab_file ());
1989
0
      fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1990
0
      g_object_unref (file);
1991
1992
0
      g_signal_connect (fstab_monitor, "changed", (GCallback)fstab_file_changed, NULL);
1993
0
    }
1994
1995
0
  if (get_mtab_monitor_file () != NULL)
1996
0
    {
1997
0
      const gchar *mtab_path;
1998
1999
0
      mtab_path = get_mtab_monitor_file ();
2000
      /* Monitoring files in /proc/ is special - can't just use GFileMonitor.
2001
       * See 'man proc' for more details.
2002
       */
2003
0
      if (g_str_has_prefix (mtab_path, "/proc/"))
2004
0
        {
2005
0
          GIOChannel *proc_mounts_channel;
2006
0
          GError *error = NULL;
2007
0
          proc_mounts_channel = g_io_channel_new_file (mtab_path, "r", &error);
2008
0
          if (proc_mounts_channel == NULL)
2009
0
            {
2010
0
              g_warning ("Error creating IO channel for %s: %s (%s, %d)", mtab_path,
2011
0
                         error->message, g_quark_to_string (error->domain), error->code);
2012
0
              g_error_free (error);
2013
0
            }
2014
0
          else
2015
0
            {
2016
0
              G_LOCK (proc_mounts_source);
2017
2018
0
              proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
2019
0
              mount_poller_time = (guint64) g_get_monotonic_time ();
2020
0
              g_source_set_callback (proc_mounts_watch_source,
2021
0
                                     (GSourceFunc) proc_mounts_changed,
2022
0
                                     NULL, NULL);
2023
0
              g_source_attach (proc_mounts_watch_source,
2024
0
                               g_main_context_get_thread_default ());
2025
0
              g_source_unref (proc_mounts_watch_source);
2026
0
              g_io_channel_unref (proc_mounts_channel);
2027
2028
0
              G_UNLOCK (proc_mounts_source);
2029
0
            }
2030
0
        }
2031
0
      else
2032
0
        {
2033
0
          file = g_file_new_for_path (mtab_path);
2034
0
          mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
2035
0
          g_object_unref (file);
2036
0
          g_signal_connect (mtab_monitor, "changed", (GCallback)mtab_file_changed, NULL);
2037
0
        }
2038
0
    }
2039
0
  else
2040
0
    {
2041
0
      G_LOCK (proc_mounts_source);
2042
2043
0
      proc_mounts_watch_source = g_timeout_source_new_seconds (3);
2044
0
      mount_poller_mounts = _g_get_unix_mounts ();
2045
0
      mount_poller_time = (guint64)g_get_monotonic_time ();
2046
0
      g_source_set_callback (proc_mounts_watch_source,
2047
0
                             mount_change_poller,
2048
0
                             NULL, NULL);
2049
0
      g_source_attach (proc_mounts_watch_source,
2050
0
                       g_main_context_get_thread_default ());
2051
0
      g_source_unref (proc_mounts_watch_source);
2052
2053
0
      G_UNLOCK (proc_mounts_source);
2054
0
    }
2055
0
}
2056
2057
static void
2058
g_unix_mount_monitor_finalize (GObject *object)
2059
0
{
2060
0
  GUnixMountMonitor *monitor;
2061
2062
0
  monitor = G_UNIX_MOUNT_MONITOR (object);
2063
2064
0
  g_context_specific_group_remove (&mount_monitor_group, monitor->context, monitor, mount_monitor_stop);
2065
2066
0
  G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
2067
0
}
2068
2069
static void
2070
g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
2071
0
{
2072
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2073
2074
0
  gobject_class->finalize = g_unix_mount_monitor_finalize;
2075
 
2076
  /**
2077
   * GUnixMountMonitor::mounts-changed:
2078
   * @monitor: the object on which the signal is emitted
2079
   * 
2080
   * Emitted when the unix mounts have changed.
2081
   */ 
2082
0
  signals[MOUNTS_CHANGED] =
2083
0
    g_signal_new (I_("mounts-changed"),
2084
0
      G_TYPE_FROM_CLASS (klass),
2085
0
      G_SIGNAL_RUN_LAST,
2086
0
      0,
2087
0
      NULL, NULL,
2088
0
      NULL,
2089
0
      G_TYPE_NONE, 0);
2090
2091
  /**
2092
   * GUnixMountMonitor::mountpoints-changed:
2093
   * @monitor: the object on which the signal is emitted
2094
   * 
2095
   * Emitted when the unix mount points have changed.
2096
   */
2097
0
  signals[MOUNTPOINTS_CHANGED] =
2098
0
    g_signal_new (I_("mountpoints-changed"),
2099
0
      G_TYPE_FROM_CLASS (klass),
2100
0
      G_SIGNAL_RUN_LAST,
2101
0
      0,
2102
0
      NULL, NULL,
2103
0
      NULL,
2104
0
      G_TYPE_NONE, 0);
2105
0
}
2106
2107
static void
2108
g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
2109
0
{
2110
0
}
2111
2112
/**
2113
 * g_unix_mount_monitor_set_rate_limit:
2114
 * @mount_monitor: a #GUnixMountMonitor
2115
 * @limit_msec: a integer with the limit in milliseconds to
2116
 *     poll for changes.
2117
 *
2118
 * This function does nothing.
2119
 *
2120
 * Before 2.44, this was a partially-effective way of controlling the
2121
 * rate at which events would be reported under some uncommon
2122
 * circumstances.  Since @mount_monitor is a singleton, it also meant
2123
 * that calling this function would have side effects for other users of
2124
 * the monitor.
2125
 *
2126
 * Since: 2.18
2127
 *
2128
 * Deprecated:2.44:This function does nothing.  Don't call it.
2129
 */
2130
void
2131
g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
2132
                                     gint               limit_msec)
2133
0
{
2134
0
}
2135
2136
/**
2137
 * g_unix_mount_monitor_get:
2138
 *
2139
 * Gets the #GUnixMountMonitor for the current thread-default main
2140
 * context.
2141
 *
2142
 * The mount monitor can be used to monitor for changes to the list of
2143
 * mounted filesystems as well as the list of mount points (ie: fstab
2144
 * entries).
2145
 *
2146
 * You must only call g_object_unref() on the return value from under
2147
 * the same main context as you called this function.
2148
 *
2149
 * Returns: (transfer full): the #GUnixMountMonitor.
2150
 *
2151
 * Since: 2.44
2152
 **/
2153
GUnixMountMonitor *
2154
g_unix_mount_monitor_get (void)
2155
0
{
2156
0
  return g_context_specific_group_get (&mount_monitor_group,
2157
0
                                       G_TYPE_UNIX_MOUNT_MONITOR,
2158
0
                                       G_STRUCT_OFFSET(GUnixMountMonitor, context),
2159
0
                                       mount_monitor_start);
2160
0
}
2161
2162
/**
2163
 * g_unix_mount_monitor_new:
2164
 *
2165
 * Deprecated alias for g_unix_mount_monitor_get().
2166
 *
2167
 * This function was never a true constructor, which is why it was
2168
 * renamed.
2169
 *
2170
 * Returns: a #GUnixMountMonitor.
2171
 *
2172
 * Deprecated:2.44:Use g_unix_mount_monitor_get() instead.
2173
 */
2174
GUnixMountMonitor *
2175
g_unix_mount_monitor_new (void)
2176
0
{
2177
0
  return g_unix_mount_monitor_get ();
2178
0
}
2179
2180
/* GUnixMount {{{1 */
2181
/**
2182
 * g_unix_mount_free:
2183
 * @mount_entry: a #GUnixMountEntry.
2184
 * 
2185
 * Frees a unix mount.
2186
 */
2187
void
2188
g_unix_mount_free (GUnixMountEntry *mount_entry)
2189
0
{
2190
0
  g_return_if_fail (mount_entry != NULL);
2191
2192
0
  g_free (mount_entry->mount_path);
2193
0
  g_free (mount_entry->device_path);
2194
0
  g_free (mount_entry->root_path);
2195
0
  g_free (mount_entry->filesystem_type);
2196
0
  g_free (mount_entry->options);
2197
0
  g_free (mount_entry);
2198
0
}
2199
2200
/**
2201
 * g_unix_mount_copy:
2202
 * @mount_entry: a #GUnixMountEntry.
2203
 *
2204
 * Makes a copy of @mount_entry.
2205
 *
2206
 * Returns: (transfer full): a new #GUnixMountEntry
2207
 *
2208
 * Since: 2.54
2209
 */
2210
GUnixMountEntry *
2211
g_unix_mount_copy (GUnixMountEntry *mount_entry)
2212
0
{
2213
0
  GUnixMountEntry *copy;
2214
2215
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2216
2217
0
  copy = g_new0 (GUnixMountEntry, 1);
2218
0
  copy->mount_path = g_strdup (mount_entry->mount_path);
2219
0
  copy->device_path = g_strdup (mount_entry->device_path);
2220
0
  copy->root_path = g_strdup (mount_entry->root_path);
2221
0
  copy->filesystem_type = g_strdup (mount_entry->filesystem_type);
2222
0
  copy->options = g_strdup (mount_entry->options);
2223
0
  copy->is_read_only = mount_entry->is_read_only;
2224
0
  copy->is_system_internal = mount_entry->is_system_internal;
2225
2226
0
  return copy;
2227
0
}
2228
2229
/**
2230
 * g_unix_mount_point_free:
2231
 * @mount_point: unix mount point to free.
2232
 * 
2233
 * Frees a unix mount point.
2234
 */
2235
void
2236
g_unix_mount_point_free (GUnixMountPoint *mount_point)
2237
0
{
2238
0
  g_return_if_fail (mount_point != NULL);
2239
2240
0
  g_free (mount_point->mount_path);
2241
0
  g_free (mount_point->device_path);
2242
0
  g_free (mount_point->filesystem_type);
2243
0
  g_free (mount_point->options);
2244
0
  g_free (mount_point);
2245
0
}
2246
2247
/**
2248
 * g_unix_mount_point_copy:
2249
 * @mount_point: a #GUnixMountPoint.
2250
 *
2251
 * Makes a copy of @mount_point.
2252
 *
2253
 * Returns: (transfer full): a new #GUnixMountPoint
2254
 *
2255
 * Since: 2.54
2256
 */
2257
GUnixMountPoint*
2258
g_unix_mount_point_copy (GUnixMountPoint *mount_point)
2259
0
{
2260
0
  GUnixMountPoint *copy;
2261
2262
0
  g_return_val_if_fail (mount_point != NULL, NULL);
2263
2264
0
  copy = g_new0 (GUnixMountPoint, 1);
2265
0
  copy->mount_path = g_strdup (mount_point->mount_path);
2266
0
  copy->device_path = g_strdup (mount_point->device_path);
2267
0
  copy->filesystem_type = g_strdup (mount_point->filesystem_type);
2268
0
  copy->options = g_strdup (mount_point->options);
2269
0
  copy->is_read_only = mount_point->is_read_only;
2270
0
  copy->is_user_mountable = mount_point->is_user_mountable;
2271
0
  copy->is_loopback = mount_point->is_loopback;
2272
2273
0
  return copy;
2274
0
}
2275
2276
/**
2277
 * g_unix_mount_compare:
2278
 * @mount1: first #GUnixMountEntry to compare.
2279
 * @mount2: second #GUnixMountEntry to compare.
2280
 * 
2281
 * Compares two unix mounts.
2282
 * 
2283
 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
2284
 * or less than @mount2, respectively. 
2285
 */
2286
gint
2287
g_unix_mount_compare (GUnixMountEntry *mount1,
2288
          GUnixMountEntry *mount2)
2289
0
{
2290
0
  int res;
2291
2292
0
  g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
2293
  
2294
0
  res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
2295
0
  if (res != 0)
2296
0
    return res;
2297
  
2298
0
  res = g_strcmp0 (mount1->device_path, mount2->device_path);
2299
0
  if (res != 0)
2300
0
    return res;
2301
2302
0
  res = g_strcmp0 (mount1->root_path, mount2->root_path);
2303
0
  if (res != 0)
2304
0
    return res;
2305
2306
0
  res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
2307
0
  if (res != 0)
2308
0
    return res;
2309
2310
0
  res = g_strcmp0 (mount1->options, mount2->options);
2311
0
  if (res != 0)
2312
0
    return res;
2313
2314
0
  res =  mount1->is_read_only - mount2->is_read_only;
2315
0
  if (res != 0)
2316
0
    return res;
2317
  
2318
0
  return 0;
2319
0
}
2320
2321
/**
2322
 * g_unix_mount_get_mount_path:
2323
 * @mount_entry: input #GUnixMountEntry to get the mount path for.
2324
 * 
2325
 * Gets the mount path for a unix mount.
2326
 * 
2327
 * Returns: (type filename): the mount path for @mount_entry.
2328
 */
2329
const gchar *
2330
g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
2331
0
{
2332
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2333
2334
0
  return mount_entry->mount_path;
2335
0
}
2336
2337
/**
2338
 * g_unix_mount_get_device_path:
2339
 * @mount_entry: a #GUnixMount.
2340
 * 
2341
 * Gets the device path for a unix mount.
2342
 * 
2343
 * Returns: (type filename): a string containing the device path.
2344
 */
2345
const gchar *
2346
g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
2347
0
{
2348
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2349
2350
0
  return mount_entry->device_path;
2351
0
}
2352
2353
/**
2354
 * g_unix_mount_get_root_path:
2355
 * @mount_entry: a #GUnixMountEntry.
2356
 * 
2357
 * Gets the root of the mount within the filesystem. This is useful e.g. for
2358
 * mounts created by bind operation, or btrfs subvolumes.
2359
 * 
2360
 * For example, the root path is equal to "/" for mount created by
2361
 * "mount /dev/sda1 /mnt/foo" and "/bar" for
2362
 * "mount --bind /mnt/foo/bar /mnt/bar".
2363
 *
2364
 * Returns: (nullable): a string containing the root, or %NULL if not supported.
2365
 *
2366
 * Since: 2.60
2367
 */
2368
const gchar *
2369
g_unix_mount_get_root_path (GUnixMountEntry *mount_entry)
2370
0
{
2371
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2372
2373
0
  return mount_entry->root_path;
2374
0
}
2375
2376
/**
2377
 * g_unix_mount_get_fs_type:
2378
 * @mount_entry: a #GUnixMount.
2379
 * 
2380
 * Gets the filesystem type for the unix mount.
2381
 * 
2382
 * Returns: a string containing the file system type.
2383
 */
2384
const gchar *
2385
g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
2386
0
{
2387
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2388
2389
0
  return mount_entry->filesystem_type;
2390
0
}
2391
2392
/**
2393
 * g_unix_mount_get_options:
2394
 * @mount_entry: a #GUnixMountEntry.
2395
 * 
2396
 * Gets a comma-separated list of mount options for the unix mount. For example,
2397
 * `rw,relatime,seclabel,data=ordered`.
2398
 * 
2399
 * This is similar to g_unix_mount_point_get_options(), but it takes
2400
 * a #GUnixMountEntry as an argument.
2401
 * 
2402
 * Returns: (nullable): a string containing the options, or %NULL if not
2403
 * available.
2404
 * 
2405
 * Since: 2.58
2406
 */
2407
const gchar *
2408
g_unix_mount_get_options (GUnixMountEntry *mount_entry)
2409
0
{
2410
0
  g_return_val_if_fail (mount_entry != NULL, NULL);
2411
2412
0
  return mount_entry->options;
2413
0
}
2414
2415
/**
2416
 * g_unix_mount_is_readonly:
2417
 * @mount_entry: a #GUnixMount.
2418
 * 
2419
 * Checks if a unix mount is mounted read only.
2420
 * 
2421
 * Returns: %TRUE if @mount_entry is read only.
2422
 */
2423
gboolean
2424
g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
2425
0
{
2426
0
  g_return_val_if_fail (mount_entry != NULL, FALSE);
2427
2428
0
  return mount_entry->is_read_only;
2429
0
}
2430
2431
/**
2432
 * g_unix_mount_is_system_internal:
2433
 * @mount_entry: a #GUnixMount.
2434
 *
2435
 * Checks if a Unix mount is a system mount. This is the Boolean OR of
2436
 * g_unix_is_system_fs_type(), g_unix_is_system_device_path() and
2437
 * g_unix_is_mount_path_system_internal() on @mount_entry’s properties.
2438
 * 
2439
 * The definition of what a ‘system’ mount entry is may change over time as new
2440
 * file system types and device paths are ignored.
2441
 *
2442
 * Returns: %TRUE if the unix mount is for a system path.
2443
 */
2444
gboolean
2445
g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
2446
0
{
2447
0
  g_return_val_if_fail (mount_entry != NULL, FALSE);
2448
2449
0
  return mount_entry->is_system_internal;
2450
0
}
2451
2452
/* GUnixMountPoint {{{1 */
2453
/**
2454
 * g_unix_mount_point_compare:
2455
 * @mount1: a #GUnixMount.
2456
 * @mount2: a #GUnixMount.
2457
 * 
2458
 * Compares two unix mount points.
2459
 * 
2460
 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
2461
 * or less than @mount2, respectively.
2462
 */
2463
gint
2464
g_unix_mount_point_compare (GUnixMountPoint *mount1,
2465
          GUnixMountPoint *mount2)
2466
0
{
2467
0
  int res;
2468
2469
0
  g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
2470
2471
0
  res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
2472
0
  if (res != 0) 
2473
0
    return res;
2474
  
2475
0
  res = g_strcmp0 (mount1->device_path, mount2->device_path);
2476
0
  if (res != 0) 
2477
0
    return res;
2478
  
2479
0
  res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
2480
0
  if (res != 0) 
2481
0
    return res;
2482
2483
0
  res = g_strcmp0 (mount1->options, mount2->options);
2484
0
  if (res != 0) 
2485
0
    return res;
2486
2487
0
  res =  mount1->is_read_only - mount2->is_read_only;
2488
0
  if (res != 0) 
2489
0
    return res;
2490
2491
0
  res = mount1->is_user_mountable - mount2->is_user_mountable;
2492
0
  if (res != 0) 
2493
0
    return res;
2494
2495
0
  res = mount1->is_loopback - mount2->is_loopback;
2496
0
  if (res != 0)
2497
0
    return res;
2498
  
2499
0
  return 0;
2500
0
}
2501
2502
/**
2503
 * g_unix_mount_point_get_mount_path:
2504
 * @mount_point: a #GUnixMountPoint.
2505
 * 
2506
 * Gets the mount path for a unix mount point.
2507
 * 
2508
 * Returns: (type filename): a string containing the mount path.
2509
 */
2510
const gchar *
2511
g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
2512
0
{
2513
0
  g_return_val_if_fail (mount_point != NULL, NULL);
2514
2515
0
  return mount_point->mount_path;
2516
0
}
2517
2518
/**
2519
 * g_unix_mount_point_get_device_path:
2520
 * @mount_point: a #GUnixMountPoint.
2521
 * 
2522
 * Gets the device path for a unix mount point.
2523
 * 
2524
 * Returns: (type filename): a string containing the device path.
2525
 */
2526
const gchar *
2527
g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
2528
0
{
2529
0
  g_return_val_if_fail (mount_point != NULL, NULL);
2530
2531
0
  return mount_point->device_path;
2532
0
}
2533
2534
/**
2535
 * g_unix_mount_point_get_fs_type:
2536
 * @mount_point: a #GUnixMountPoint.
2537
 * 
2538
 * Gets the file system type for the mount point.
2539
 * 
2540
 * Returns: a string containing the file system type.
2541
 */
2542
const gchar *
2543
g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
2544
0
{
2545
0
  g_return_val_if_fail (mount_point != NULL, NULL);
2546
2547
0
  return mount_point->filesystem_type;
2548
0
}
2549
2550
/**
2551
 * g_unix_mount_point_get_options:
2552
 * @mount_point: a #GUnixMountPoint.
2553
 * 
2554
 * Gets the options for the mount point.
2555
 * 
2556
 * Returns: (nullable): a string containing the options.
2557
 *
2558
 * Since: 2.32
2559
 */
2560
const gchar *
2561
g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
2562
0
{
2563
0
  g_return_val_if_fail (mount_point != NULL, NULL);
2564
2565
0
  return mount_point->options;
2566
0
}
2567
2568
/**
2569
 * g_unix_mount_point_is_readonly:
2570
 * @mount_point: a #GUnixMountPoint.
2571
 * 
2572
 * Checks if a unix mount point is read only.
2573
 * 
2574
 * Returns: %TRUE if a mount point is read only.
2575
 */
2576
gboolean
2577
g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
2578
0
{
2579
0
  g_return_val_if_fail (mount_point != NULL, FALSE);
2580
2581
0
  return mount_point->is_read_only;
2582
0
}
2583
2584
/**
2585
 * g_unix_mount_point_is_user_mountable:
2586
 * @mount_point: a #GUnixMountPoint.
2587
 * 
2588
 * Checks if a unix mount point is mountable by the user.
2589
 * 
2590
 * Returns: %TRUE if the mount point is user mountable.
2591
 */
2592
gboolean
2593
g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
2594
0
{
2595
0
  g_return_val_if_fail (mount_point != NULL, FALSE);
2596
2597
0
  return mount_point->is_user_mountable;
2598
0
}
2599
2600
/**
2601
 * g_unix_mount_point_is_loopback:
2602
 * @mount_point: a #GUnixMountPoint.
2603
 * 
2604
 * Checks if a unix mount point is a loopback device.
2605
 * 
2606
 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise. 
2607
 */
2608
gboolean
2609
g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
2610
0
{
2611
0
  g_return_val_if_fail (mount_point != NULL, FALSE);
2612
2613
0
  return mount_point->is_loopback;
2614
0
}
2615
2616
static GUnixMountType
2617
guess_mount_type (const char *mount_path,
2618
      const char *device_path,
2619
      const char *filesystem_type)
2620
0
{
2621
0
  GUnixMountType type;
2622
0
  char *basename;
2623
2624
0
  type = G_UNIX_MOUNT_TYPE_UNKNOWN;
2625
  
2626
0
  if ((strcmp (filesystem_type, "udf") == 0) ||
2627
0
      (strcmp (filesystem_type, "iso9660") == 0) ||
2628
0
      (strcmp (filesystem_type, "cd9660") == 0))
2629
0
    type = G_UNIX_MOUNT_TYPE_CDROM;
2630
0
  else if ((strcmp (filesystem_type, "nfs") == 0) ||
2631
0
           (strcmp (filesystem_type, "nfs4") == 0))
2632
0
    type = G_UNIX_MOUNT_TYPE_NFS;
2633
0
  else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
2634
0
     g_str_has_prefix (device_path, "/dev/fd") ||
2635
0
     g_str_has_prefix (device_path, "/dev/floppy"))
2636
0
    type = G_UNIX_MOUNT_TYPE_FLOPPY;
2637
0
  else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
2638
0
     g_str_has_prefix (device_path, "/dev/acd") ||
2639
0
     g_str_has_prefix (device_path, "/dev/cd"))
2640
0
    type = G_UNIX_MOUNT_TYPE_CDROM;
2641
0
  else if (g_str_has_prefix (device_path, "/vol/"))
2642
0
    {
2643
0
      const char *name = mount_path + strlen ("/");
2644
      
2645
0
      if (g_str_has_prefix (name, "cdrom"))
2646
0
  type = G_UNIX_MOUNT_TYPE_CDROM;
2647
0
      else if (g_str_has_prefix (name, "floppy") ||
2648
0
         g_str_has_prefix (device_path, "/vol/dev/diskette/")) 
2649
0
  type = G_UNIX_MOUNT_TYPE_FLOPPY;
2650
0
      else if (g_str_has_prefix (name, "rmdisk")) 
2651
0
  type = G_UNIX_MOUNT_TYPE_ZIP;
2652
0
      else if (g_str_has_prefix (name, "jaz"))
2653
0
  type = G_UNIX_MOUNT_TYPE_JAZ;
2654
0
      else if (g_str_has_prefix (name, "memstick"))
2655
0
  type = G_UNIX_MOUNT_TYPE_MEMSTICK;
2656
0
    }
2657
0
  else
2658
0
    {
2659
0
      basename = g_path_get_basename (mount_path);
2660
      
2661
0
      if (g_str_has_prefix (basename, "cdr") ||
2662
0
    g_str_has_prefix (basename, "cdwriter") ||
2663
0
    g_str_has_prefix (basename, "burn") ||
2664
0
    g_str_has_prefix (basename, "dvdr"))
2665
0
  type = G_UNIX_MOUNT_TYPE_CDROM;
2666
0
      else if (g_str_has_prefix (basename, "floppy"))
2667
0
  type = G_UNIX_MOUNT_TYPE_FLOPPY;
2668
0
      else if (g_str_has_prefix (basename, "zip"))
2669
0
  type = G_UNIX_MOUNT_TYPE_ZIP;
2670
0
      else if (g_str_has_prefix (basename, "jaz"))
2671
0
  type = G_UNIX_MOUNT_TYPE_JAZ;
2672
0
      else if (g_str_has_prefix (basename, "camera"))
2673
0
  type = G_UNIX_MOUNT_TYPE_CAMERA;
2674
0
      else if (g_str_has_prefix (basename, "memstick") ||
2675
0
         g_str_has_prefix (basename, "memory_stick") ||
2676
0
         g_str_has_prefix (basename, "ram"))
2677
0
  type = G_UNIX_MOUNT_TYPE_MEMSTICK;
2678
0
      else if (g_str_has_prefix (basename, "compact_flash"))
2679
0
  type = G_UNIX_MOUNT_TYPE_CF;
2680
0
      else if (g_str_has_prefix (basename, "smart_media"))
2681
0
  type = G_UNIX_MOUNT_TYPE_SM;
2682
0
      else if (g_str_has_prefix (basename, "sd_mmc"))
2683
0
  type = G_UNIX_MOUNT_TYPE_SDMMC;
2684
0
      else if (g_str_has_prefix (basename, "ipod"))
2685
0
  type = G_UNIX_MOUNT_TYPE_IPOD;
2686
      
2687
0
      g_free (basename);
2688
0
    }
2689
  
2690
0
  if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
2691
0
    type = G_UNIX_MOUNT_TYPE_HD;
2692
  
2693
0
  return type;
2694
0
}
2695
2696
/**
2697
 * g_unix_mount_guess_type:
2698
 * @mount_entry: a #GUnixMount.
2699
 * 
2700
 * Guesses the type of a unix mount. If the mount type cannot be 
2701
 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
2702
 * 
2703
 * Returns: a #GUnixMountType. 
2704
 */
2705
static GUnixMountType
2706
g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
2707
0
{
2708
0
  g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2709
0
  g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2710
0
  g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2711
0
  g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2712
2713
0
  return guess_mount_type (mount_entry->mount_path,
2714
0
         mount_entry->device_path,
2715
0
         mount_entry->filesystem_type);
2716
0
}
2717
2718
/**
2719
 * g_unix_mount_point_guess_type:
2720
 * @mount_point: a #GUnixMountPoint.
2721
 * 
2722
 * Guesses the type of a unix mount point. 
2723
 * If the mount type cannot be determined, 
2724
 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
2725
 * 
2726
 * Returns: a #GUnixMountType.
2727
 */
2728
static GUnixMountType
2729
g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
2730
0
{
2731
0
  g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2732
0
  g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2733
0
  g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2734
0
  g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
2735
2736
0
  return guess_mount_type (mount_point->mount_path,
2737
0
         mount_point->device_path,
2738
0
         mount_point->filesystem_type);
2739
0
}
2740
2741
static const char *
2742
type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
2743
0
{
2744
0
  const char *icon_name;
2745
  
2746
0
  switch (type)
2747
0
    {
2748
0
    case G_UNIX_MOUNT_TYPE_HD:
2749
0
      if (is_mount_point)
2750
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2751
0
      else
2752
0
        icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2753
0
      break;
2754
0
    case G_UNIX_MOUNT_TYPE_FLOPPY:
2755
0
    case G_UNIX_MOUNT_TYPE_ZIP:
2756
0
    case G_UNIX_MOUNT_TYPE_JAZ:
2757
0
      if (is_mount_point)
2758
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2759
0
      else
2760
0
        icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
2761
0
      break;
2762
0
    case G_UNIX_MOUNT_TYPE_CDROM:
2763
0
      if (is_mount_point)
2764
0
        icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
2765
0
      else
2766
0
        icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
2767
0
      break;
2768
0
    case G_UNIX_MOUNT_TYPE_NFS:
2769
0
        icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
2770
0
      break;
2771
0
    case G_UNIX_MOUNT_TYPE_MEMSTICK:
2772
0
      if (is_mount_point)
2773
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2774
0
      else
2775
0
        icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
2776
0
      break;
2777
0
    case G_UNIX_MOUNT_TYPE_CAMERA:
2778
0
      if (is_mount_point)
2779
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2780
0
      else
2781
0
        icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
2782
0
      break;
2783
0
    case G_UNIX_MOUNT_TYPE_IPOD:
2784
0
      if (is_mount_point)
2785
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2786
0
      else
2787
0
        icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
2788
0
      break;
2789
0
    case G_UNIX_MOUNT_TYPE_UNKNOWN:
2790
0
    default:
2791
0
      if (is_mount_point)
2792
0
        icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2793
0
      else
2794
0
        icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2795
0
      break;
2796
0
    }
2797
2798
0
  return icon_name;
2799
0
}
2800
2801
/**
2802
 * g_unix_mount_guess_name:
2803
 * @mount_entry: a #GUnixMountEntry
2804
 * 
2805
 * Guesses the name of a Unix mount. 
2806
 * The result is a translated string.
2807
 *
2808
 * Returns: A newly allocated string that must
2809
 *     be freed with g_free()
2810
 */
2811
gchar *
2812
g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
2813
0
{
2814
0
  char *name;
2815
2816
0
  if (strcmp (mount_entry->mount_path, "/") == 0)
2817
0
    name = g_strdup (_("Filesystem root"));
2818
0
  else
2819
0
    name = g_filename_display_basename (mount_entry->mount_path);
2820
2821
0
  return name;
2822
0
}
2823
2824
/**
2825
 * g_unix_mount_guess_icon:
2826
 * @mount_entry: a #GUnixMountEntry
2827
 * 
2828
 * Guesses the icon of a Unix mount. 
2829
 *
2830
 * Returns: (transfer full): a #GIcon
2831
 */
2832
GIcon *
2833
g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2834
0
{
2835
0
  return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2836
0
}
2837
2838
/**
2839
 * g_unix_mount_guess_symbolic_icon:
2840
 * @mount_entry: a #GUnixMountEntry
2841
 *
2842
 * Guesses the symbolic icon of a Unix mount.
2843
 *
2844
 * Returns: (transfer full): a #GIcon
2845
 *
2846
 * Since: 2.34
2847
 */
2848
GIcon *
2849
g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2850
0
{
2851
0
  return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2852
0
}
2853
2854
/**
2855
 * g_unix_mount_point_guess_name:
2856
 * @mount_point: a #GUnixMountPoint
2857
 * 
2858
 * Guesses the name of a Unix mount point. 
2859
 * The result is a translated string.
2860
 *
2861
 * Returns: A newly allocated string that must 
2862
 *     be freed with g_free()
2863
 */
2864
gchar *
2865
g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2866
0
{
2867
0
  char *name;
2868
2869
0
  if (strcmp (mount_point->mount_path, "/") == 0)
2870
0
    name = g_strdup (_("Filesystem root"));
2871
0
  else
2872
0
    name = g_filename_display_basename (mount_point->mount_path);
2873
2874
0
  return name;
2875
0
}
2876
2877
/**
2878
 * g_unix_mount_point_guess_icon:
2879
 * @mount_point: a #GUnixMountPoint
2880
 * 
2881
 * Guesses the icon of a Unix mount point. 
2882
 *
2883
 * Returns: (transfer full): a #GIcon
2884
 */
2885
GIcon *
2886
g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2887
0
{
2888
0
  return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2889
0
}
2890
2891
/**
2892
 * g_unix_mount_point_guess_symbolic_icon:
2893
 * @mount_point: a #GUnixMountPoint
2894
 *
2895
 * Guesses the symbolic icon of a Unix mount point.
2896
 *
2897
 * Returns: (transfer full): a #GIcon
2898
 *
2899
 * Since: 2.34
2900
 */
2901
GIcon *
2902
g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2903
0
{
2904
0
  return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2905
0
}
2906
2907
/**
2908
 * g_unix_mount_guess_can_eject:
2909
 * @mount_entry: a #GUnixMountEntry
2910
 * 
2911
 * Guesses whether a Unix mount can be ejected.
2912
 *
2913
 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2914
 */
2915
gboolean
2916
g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2917
0
{
2918
0
  GUnixMountType guessed_type;
2919
2920
0
  guessed_type = g_unix_mount_guess_type (mount_entry);
2921
0
  if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2922
0
      guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2923
0
    return TRUE;
2924
2925
0
  return FALSE;
2926
0
}
2927
2928
/**
2929
 * g_unix_mount_guess_should_display:
2930
 * @mount_entry: a #GUnixMountEntry
2931
 * 
2932
 * Guesses whether a Unix mount should be displayed in the UI.
2933
 *
2934
 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2935
 */
2936
gboolean
2937
g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2938
0
{
2939
0
  const char *mount_path;
2940
0
  const gchar *user_name;
2941
0
  gsize user_name_len;
2942
2943
  /* Never display internal mountpoints */
2944
0
  if (g_unix_mount_is_system_internal (mount_entry))
2945
0
    return FALSE;
2946
  
2947
  /* Only display things in /media (which are generally user mountable)
2948
     and home dir (fuse stuff) and /run/media/$USER */
2949
0
  mount_path = mount_entry->mount_path;
2950
0
  if (mount_path != NULL)
2951
0
    {
2952
0
      const gboolean running_as_root = (getuid () == 0);
2953
0
      gboolean is_in_runtime_dir = FALSE;
2954
2955
      /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2956
0
      if (g_strstr_len (mount_path, -1, "/.") != NULL)
2957
0
        return FALSE;
2958
2959
      /* Check /run/media/$USER/. If running as root, display any mounts below
2960
       * /run/media/. */
2961
0
      if (running_as_root)
2962
0
        {
2963
0
          if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0)
2964
0
            is_in_runtime_dir = TRUE;
2965
0
        }
2966
0
      else
2967
0
        {
2968
0
          user_name = g_get_user_name ();
2969
0
          user_name_len = strlen (user_name);
2970
0
          if (strncmp (mount_path, "/run/media/", strlen ("/run/media/")) == 0 &&
2971
0
              strncmp (mount_path + strlen ("/run/media/"), user_name, user_name_len) == 0 &&
2972
0
              mount_path[strlen ("/run/media/") + user_name_len] == '/')
2973
0
            is_in_runtime_dir = TRUE;
2974
0
        }
2975
2976
0
      if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2977
0
        {
2978
0
          char *path;
2979
          /* Avoid displaying mounts that are not accessible to the user.
2980
           *
2981
           * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2982
           * want to avoid g_access() for mount points which can potentially
2983
           * block or fail stat()'ing, such as network mounts.
2984
           */
2985
0
          path = g_path_get_dirname (mount_path);
2986
0
          if (g_str_has_prefix (path, "/media/"))
2987
0
            {
2988
0
              if (g_access (path, R_OK|X_OK) != 0) 
2989
0
                {
2990
0
                  g_free (path);
2991
0
                  return FALSE;
2992
0
                }
2993
0
            }
2994
0
          g_free (path);
2995
2996
0
          if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2997
0
           {
2998
0
             struct stat st;
2999
0
             if (g_stat (mount_entry->device_path, &st) == 0 &&
3000
0
                 S_ISBLK(st.st_mode) &&
3001
0
                 g_access (mount_path, R_OK|X_OK) != 0)
3002
0
               return FALSE;
3003
0
           }
3004
0
          return TRUE;
3005
0
        }
3006
      
3007
0
      if (g_str_has_prefix (mount_path, g_get_home_dir ()) && 
3008
0
          mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
3009
0
        return TRUE;
3010
0
    }
3011
  
3012
0
  return FALSE;
3013
0
}
3014
3015
/**
3016
 * g_unix_mount_point_guess_can_eject:
3017
 * @mount_point: a #GUnixMountPoint
3018
 * 
3019
 * Guesses whether a Unix mount point can be ejected.
3020
 *
3021
 * Returns: %TRUE if @mount_point is deemed to be ejectable.
3022
 */
3023
gboolean
3024
g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
3025
0
{
3026
0
  GUnixMountType guessed_type;
3027
3028
0
  guessed_type = g_unix_mount_point_guess_type (mount_point);
3029
0
  if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
3030
0
      guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
3031
0
    return TRUE;
3032
3033
0
  return FALSE;
3034
0
}
3035
3036
/* Utility functions {{{1 */
3037
3038
#ifdef HAVE_MNTENT_H
3039
/* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
3040
static void
3041
_canonicalize_filename (gchar *filename)
3042
0
{
3043
0
  gchar *p, *q;
3044
0
  gboolean last_was_slash = FALSE;
3045
  
3046
0
  p = filename;
3047
0
  q = filename;
3048
  
3049
0
  while (*p)
3050
0
    {
3051
0
      if (*p == G_DIR_SEPARATOR)
3052
0
        {
3053
0
          if (!last_was_slash)
3054
0
            *q++ = G_DIR_SEPARATOR;
3055
          
3056
0
          last_was_slash = TRUE;
3057
0
        }
3058
0
      else
3059
0
        {
3060
0
          if (last_was_slash && *p == '.')
3061
0
            {
3062
0
              if (*(p + 1) == G_DIR_SEPARATOR ||
3063
0
                  *(p + 1) == '\0')
3064
0
                {
3065
0
                  if (*(p + 1) == '\0')
3066
0
                    break;
3067
                  
3068
0
                  p += 1;
3069
0
                }
3070
0
              else if (*(p + 1) == '.' &&
3071
0
                       (*(p + 2) == G_DIR_SEPARATOR ||
3072
0
                        *(p + 2) == '\0'))
3073
0
                {
3074
0
                  if (q > filename + 1)
3075
0
                    {
3076
0
                      q--;
3077
0
                      while (q > filename + 1 &&
3078
0
                             *(q - 1) != G_DIR_SEPARATOR)
3079
0
                        q--;
3080
0
                    }
3081
                  
3082
0
                  if (*(p + 2) == '\0')
3083
0
                    break;
3084
                  
3085
0
                  p += 2;
3086
0
                }
3087
0
              else
3088
0
                {
3089
0
                  *q++ = *p;
3090
0
                  last_was_slash = FALSE;
3091
0
                }
3092
0
            }
3093
0
          else
3094
0
            {
3095
0
              *q++ = *p;
3096
0
              last_was_slash = FALSE;
3097
0
            }
3098
0
        }
3099
      
3100
0
      p++;
3101
0
    }
3102
  
3103
0
  if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
3104
0
    q--;
3105
  
3106
0
  *q = '\0';
3107
0
}
3108
3109
static char *
3110
_resolve_symlink (const char *file)
3111
0
{
3112
0
  GError *error;
3113
0
  char *dir;
3114
0
  char *link;
3115
0
  char *f;
3116
0
  char *f1;
3117
  
3118
0
  f = g_strdup (file);
3119
  
3120
0
  while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) 
3121
0
    {
3122
0
      link = g_file_read_link (f, &error);
3123
0
      if (link == NULL) 
3124
0
        {
3125
0
          g_error_free (error);
3126
0
          g_free (f);
3127
0
          f = NULL;
3128
0
          goto out;
3129
0
        }
3130
    
3131
0
      dir = g_path_get_dirname (f);
3132
0
      f1 = g_strdup_printf ("%s/%s", dir, link);
3133
0
      g_free (dir);
3134
0
      g_free (link);
3135
0
      g_free (f);
3136
0
      f = f1;
3137
0
    }
3138
  
3139
0
 out:
3140
0
  if (f != NULL)
3141
0
    _canonicalize_filename (f);
3142
0
  return f;
3143
0
}
3144
3145
static const char *
3146
_resolve_dev_root (void)
3147
0
{
3148
0
  static gboolean have_real_dev_root = FALSE;
3149
0
  static char real_dev_root[256];
3150
0
  struct stat statbuf;
3151
  
3152
  /* see if it's cached already */
3153
0
  if (have_real_dev_root)
3154
0
    goto found;
3155
  
3156
  /* otherwise we're going to find it right away.. */
3157
0
  have_real_dev_root = TRUE;
3158
  
3159
0
  if (stat ("/dev/root", &statbuf) == 0) 
3160
0
    {
3161
0
      if (! S_ISLNK (statbuf.st_mode)) 
3162
0
        {
3163
0
          dev_t root_dev = statbuf.st_dev;
3164
0
          FILE *f;
3165
      
3166
          /* see if device with similar major:minor as /dev/root is mention
3167
           * in /etc/mtab (it usually is) 
3168
           */
3169
0
          f = fopen ("/etc/mtab", "re");
3170
0
          if (f != NULL) 
3171
0
            {
3172
0
        struct mntent *entp;
3173
0
#ifdef HAVE_GETMNTENT_R        
3174
0
              struct mntent ent;
3175
0
              char buf[1024];
3176
0
              while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL) 
3177
0
                {
3178
#else
3179
        G_LOCK (getmntent);
3180
        while ((entp = getmntent (f)) != NULL) 
3181
                { 
3182
#endif          
3183
0
                  if (stat (entp->mnt_fsname, &statbuf) == 0 &&
3184
0
                      statbuf.st_dev == root_dev) 
3185
0
                    {
3186
0
                      strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
3187
0
                      real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3188
0
                      fclose (f);
3189
0
                      goto found;
3190
0
                    }
3191
0
                }
3192
3193
0
              endmntent (f);
3194
3195
#ifndef HAVE_GETMNTENT_R
3196
        G_UNLOCK (getmntent);
3197
#endif
3198
0
            }                                        
3199
      
3200
          /* no, that didn't work.. next we could scan /dev ... but I digress.. */
3201
      
3202
0
        } 
3203
0
       else 
3204
0
        {
3205
0
          char *resolved;
3206
0
          resolved = _resolve_symlink ("/dev/root");
3207
0
          if (resolved != NULL)
3208
0
            {
3209
0
              strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
3210
0
              real_dev_root[sizeof (real_dev_root) - 1] = '\0';
3211
0
              g_free (resolved);
3212
0
              goto found;
3213
0
            }
3214
0
        }
3215
0
    }
3216
  
3217
  /* bah sucks.. */
3218
0
  strcpy (real_dev_root, "/dev/root");
3219
  
3220
0
found:
3221
0
  return real_dev_root;
3222
0
}
3223
#endif
3224
3225
/* Epilogue {{{1 */
3226
/* vim:set foldmethod=marker: */