Coverage Report

Created: 2026-07-11 07:00

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