Coverage Report

Created: 2025-11-24 06:14

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