Coverage Report

Created: 2026-10-31 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/libmount/src/tab_parse.c
Line
Count
Source
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
/*
3
 * This file is part of libmount from util-linux project.
4
 *
5
 * Copyright (C) 2009-2018 Karel Zak <kzak@redhat.com>
6
 *
7
 * libmount is free software; you can redistribute it and/or modify it
8
 * under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation; either version 2.1 of the License, or
10
 * (at your option) any later version.
11
 */
12
#ifdef HAVE_SCANDIRAT
13
#ifndef __USE_GNU
14
#define __USE_GNU
15
#endif  /* !__USE_GNU */
16
#endif  /* HAVE_SCANDIRAT */
17
18
#include <ctype.h>
19
#include <limits.h>
20
#include <dirent.h>
21
#include <fcntl.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <sys/stat.h>
25
26
#include "fileutils.h"
27
#include "mangle.h"
28
#include "mountP.h"
29
#include "pathnames.h"
30
#include "strutils.h"
31
32
struct libmnt_parser {
33
  FILE  *f;   /* fstab, swaps or mountinfo ... */
34
  const char *filename; /* file name or NULL */
35
  char  *buf;   /* buffer (the current line content) */
36
  size_t  bufsiz;   /* size of the buffer */
37
  size_t  line;   /* current line */
38
  int     sysroot_rc; /* rc from mnt_guess_system_root() */
39
  char  *sysroot; /* guess from mmnt_guess_system_root() */
40
};
41
42
static void parser_cleanup(struct libmnt_parser *pa)
43
3.52k
{
44
3.52k
  if (!pa)
45
0
    return;
46
3.52k
  free(pa->buf);
47
3.52k
  free(pa->sysroot);
48
3.52k
  memset(pa, 0, sizeof(*pa));
49
3.52k
}
50
51
static const char *next_s32(const char *s, int *num, int *rc)
52
65.5k
{
53
65.5k
  char *end = NULL;
54
55
65.5k
  if (!s || !*s)
56
4.94k
    return s;
57
58
65.5k
  errno = 0;
59
60.5k
  *rc = -EINVAL;
60
60.5k
  *num = strtol(s, &end, 10);
61
60.5k
  if (end == NULL || s == end)
62
14.4k
         return s;
63
46.1k
  if (errno == 0 && (*end == ' ' || *end == '\t' || *end == '\0'))
64
40.4k
    *rc = 0;
65
46.1k
  return end;
66
60.5k
}
67
68
static const char *next_u64(const char *s, uint64_t *num, int *rc)
69
30.1k
{
70
30.1k
  char *end = NULL;
71
72
30.1k
  if (!s || !*s)
73
1.96k
    return s;
74
75
30.1k
  errno = 0;
76
28.2k
  *rc = -EINVAL;
77
28.2k
  *num = (uint64_t) strtoumax(s, &end, 10);
78
28.2k
  if (end == NULL || s == end)
79
22.0k
         return s;
80
6.18k
  if (errno == 0 && (*end == ' ' || *end == '\t' || *end == '\0'))
81
4.02k
    *rc = 0;
82
6.18k
  return end;
83
28.2k
}
84
85
static inline const char *skip_separator(const char *p)
86
324k
{
87
681k
  while (p && (*p == ' ' || *p == '\t'))
88
356k
    ++p;
89
324k
  return p;
90
324k
}
91
92
static inline const char *skip_nonspearator(const char *p)
93
9.89k
{
94
30.0k
  while (p && *p && !(*p == ' ' || *p == '\t'))
95
20.1k
    p++;
96
9.89k
  return p;
97
9.89k
}
98
99
/*
100
 * Parses one line from {fs,m}tab
101
 */
102
static int mnt_parse_table_line(struct libmnt_fs *fs, const char *s)
103
65.1k
{
104
65.1k
  int rc = 0;
105
65.1k
  char *p = NULL;
106
107
65.1k
  fs->passno = fs->freq = 0;
108
109
  /* (1) source */
110
65.1k
  p = unmangle(s, &s);
111
65.1k
  if (!p || (rc = __mnt_fs_set_source_ptr(fs, p))) {
112
0
    DBG(TAB, ul_debug("tab parse error: [source]"));
113
0
    free(p);
114
0
    goto fail;
115
0
  }
116
117
65.1k
  s = skip_separator(s);
118
119
  /* (2) target */
120
65.1k
  fs->target = unmangle(s, &s);
121
65.1k
  if (!fs->target) {
122
11.5k
    DBG(TAB, ul_debug("tab parse error: [target]"));
123
11.5k
    goto fail;
124
11.5k
  }
125
126
53.6k
  s = skip_separator(s);
127
128
  /* (3) FS type */
129
53.6k
  p = unmangle(s, &s);
130
53.6k
  if (!p || (rc = __mnt_fs_set_fstype_ptr(fs, p))) {
131
3.20k
    DBG(TAB, ul_debug("tab parse error: [fstype]"));
132
3.20k
    free(p);
133
3.20k
    goto fail;
134
3.20k
  }
135
136
50.4k
  s = skip_separator(s);
137
138
  /* (4) options (optional) */
139
50.4k
  p = unmangle(s, &s);
140
50.4k
  if (p && (rc = mnt_fs_set_options(fs, p))) {
141
0
    DBG(TAB, ul_debug("tab parse error: [options]"));
142
0
    free(p);
143
0
    goto fail;
144
0
  }
145
50.4k
  if (!p)
146
40.8k
    goto done;
147
9.57k
  free(p);
148
149
9.57k
  s = skip_separator(s);
150
9.57k
  if (!s || !*s)
151
3.10k
    goto done;
152
153
  /* (5) freq (optional) */
154
6.46k
  s = next_s32(s, &fs->freq, &rc);
155
6.46k
  if (s && *s && rc) {
156
3.02k
    DBG(TAB, ul_debug("tab parse error: [freq]"));
157
3.02k
    goto fail;
158
3.02k
  }
159
160
3.44k
  s = skip_separator(s);
161
3.44k
  if (!s || !*s)
162
1.03k
    goto done;
163
164
  /* (6) passno (optional) */
165
2.41k
  s = next_s32(s, &fs->passno, &rc);
166
2.41k
  if (s && *s && rc) {
167
330
    DBG(TAB, ul_debug("tab parse error: [passno]"));
168
330
    goto fail;
169
330
  }
170
171
47.0k
done:
172
47.0k
  return 0;
173
18.1k
fail:
174
18.1k
  if (rc == 0)
175
14.7k
    rc = -EINVAL;
176
18.1k
  DBG(TAB, ul_debug("tab parse error on: '%s' [rc=%d]", s, rc));
177
18.1k
  return rc;
178
2.41k
}
179
180
181
/*
182
 * Parses one line from a mountinfo file
183
 */
184
static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, const char *s)
185
34.3k
{
186
34.3k
  int rc = 0;
187
34.3k
  unsigned int maj, min;
188
34.3k
  char *p;
189
34.3k
  const char *sep;
190
191
34.3k
  fs->flags |= MNT_FS_KERNEL;
192
34.3k
  mnt_fs_mark_attached(fs);
193
194
  /* (1) id */
195
34.3k
  s = next_s32(s, &fs->id, &rc);
196
34.3k
  if (!s || !*s || rc) {
197
12.6k
    DBG(TAB, ul_debug("tab parse error: [id]"));
198
12.6k
    goto fail;
199
12.6k
  }
200
201
21.6k
  s = skip_separator(s);
202
203
  /* (2) parent */
204
21.6k
  s = next_s32(s, &fs->parent, &rc);
205
21.6k
  if (!s || !*s || rc) {
206
9.43k
    DBG(TAB, ul_debug("tab parse error: [parent]"));
207
9.43k
    goto fail;
208
9.43k
  }
209
210
12.2k
  s = skip_separator(s);
211
212
  /* (3) maj:min */
213
12.2k
  if (sscanf(s, "%u:%u", &maj, &min) != 2) {
214
2.33k
    DBG(TAB, ul_debug("tab parse error: [maj:min]"));
215
2.33k
    goto fail;
216
2.33k
  }
217
9.89k
  fs->devno = makedev(maj, min);
218
9.89k
  s = skip_nonspearator(s);
219
9.89k
  s = skip_separator(s);
220
221
  /* (4) mountroot */
222
9.89k
  fs->root = unmangle(s, &s);
223
9.89k
  if (!fs->root) {
224
220
    DBG(TAB, ul_debug("tab parse error: [mountroot]"));
225
220
    goto fail;
226
220
  }
227
228
9.67k
  s = skip_separator(s);
229
230
  /* (5) target */
231
9.67k
  fs->target = unmangle(s, &s);
232
9.67k
  if (!fs->target) {
233
232
    DBG(TAB, ul_debug("tab parse error: [target]"));
234
232
    goto fail;
235
232
  }
236
237
9.44k
  s = skip_separator(s);
238
239
  /* (6) vfs options (fs-independent) */
240
9.44k
  fs->vfs_optstr = unmangle(s, &s);
241
9.44k
  if (!fs->vfs_optstr) {
242
648
    DBG(TAB, ul_debug("tab parse error: [VFS options]"));
243
648
    goto fail;
244
648
  }
245
246
  /* (7) optional fields, terminated by " - " */
247
8.79k
  sep = strstr(s, " - ");
248
8.79k
  if (!sep) {
249
1.20k
    DBG(TAB, ul_debug("mountinfo parse error: separator not found"));
250
1.20k
    return -EINVAL;
251
1.20k
  }
252
7.58k
  if (sep > s + 1)
253
294
    fs->opt_fields = strndup(s + 1, sep - s - 1);
254
255
7.58k
  s = skip_separator(sep + 3);
256
257
  /* (8) FS type */
258
7.58k
  p = unmangle(s, &s);
259
7.58k
  if (!p || (rc = __mnt_fs_set_fstype_ptr(fs, p))) {
260
403
    DBG(TAB, ul_debug("tab parse error: [fstype]"));
261
403
    free(p);
262
403
    goto fail;
263
403
  }
264
265
  /* (9) source -- maybe empty string */
266
7.18k
  if (!s || !*s) {
267
718
    DBG(TAB, ul_debug("tab parse error: [source]"));
268
718
    goto fail;
269
6.46k
  } else if (*s == ' ' && *(s+1) == ' ') {
270
2.64k
    if ((rc = mnt_fs_set_source(fs, ""))) {
271
0
      DBG(TAB, ul_debug("tab parse error: [empty source]"));
272
0
      goto fail;
273
0
    }
274
3.82k
  } else {
275
3.82k
    s = skip_separator(s);
276
3.82k
    p = unmangle(s, &s);
277
3.82k
    if (!p || (rc = __mnt_fs_set_source_ptr(fs, p))) {
278
426
      DBG(TAB, ul_debug("tab parse error: [regular source]"));
279
426
      free(p);
280
426
      goto fail;
281
426
    }
282
3.82k
  }
283
284
6.03k
  s = skip_separator(s);
285
286
  /* (10) fs options (fs specific) */
287
6.03k
  fs->fs_optstr = unmangle(s, &s);
288
6.03k
  if (!fs->fs_optstr) {
289
1.23k
    DBG(TAB, ul_debug("tab parse error: [FS options]"));
290
1.23k
    goto fail;
291
1.23k
  }
292
293
  /* merge VFS and FS options to one string */
294
4.80k
  fs->optstr = mnt_fs_strdup_options(fs);
295
4.80k
  if (!fs->optstr) {
296
0
    rc = -ENOMEM;
297
0
    DBG(TAB, ul_debug("tab parse error: [merge VFS and FS options]"));
298
0
    goto fail;
299
0
  }
300
301
4.80k
  return 0;
302
28.2k
fail:
303
28.2k
  if (rc == 0)
304
12.3k
    rc = -EINVAL;
305
28.2k
  DBG(TAB, ul_debug("tab parse error on: '%s' [rc=%d]", s, rc));
306
28.2k
  return rc;
307
4.80k
}
308
309
/*
310
 * Parses one line from utab file
311
 */
312
static int mnt_parse_utab_line(struct libmnt_fs *fs, const char *s)
313
0
{
314
0
  const char *p = s;
315
316
0
  assert(fs);
317
0
  assert(s);
318
0
  assert(!fs->source);
319
0
  assert(!fs->target);
320
321
0
  while (p && *p) {
322
0
    const char *end = NULL;
323
324
0
    while (*p == ' ') p++;
325
0
    if (!*p)
326
0
      break;
327
328
0
    if (!fs->uniq_id && !strncmp(p, "UNIQID=", 7)) {
329
0
      int rc = 0;
330
331
0
      end = next_u64(p + 7, &fs->uniq_id, &rc);
332
0
      if (!end || rc)
333
0
        return rc;
334
335
0
    } else if (!fs->id && !strncmp(p, "ID=", 3)) {
336
0
      int rc = 0;
337
338
0
      end = next_s32(p + 3, &fs->id, &rc);
339
0
      if (!end || rc)
340
0
        return rc;
341
342
0
    } else if (!fs->source && !strncmp(p, "SRC=", 4)) {
343
0
      char *v = unmangle(p + 4, &end);
344
0
      if (!v)
345
0
        goto enomem;
346
0
      if (__mnt_fs_set_source_ptr(fs, v))
347
0
        free(v);
348
349
0
    } else if (!fs->target && !strncmp(p, "TARGET=", 7)) {
350
0
      fs->target = unmangle(p + 7, &end);
351
0
      if (!fs->target)
352
0
        goto enomem;
353
354
0
    } else if (!fs->root && !strncmp(p, "ROOT=", 5)) {
355
0
      fs->root = unmangle(p + 5, &end);
356
0
      if (!fs->root)
357
0
        goto enomem;
358
359
0
    } else if (!fs->bindsrc && !strncmp(p, "BINDSRC=", 8)) {
360
0
      fs->bindsrc = unmangle(p + 8, &end);
361
0
      if (!fs->bindsrc)
362
0
        goto enomem;
363
364
0
    } else if (!fs->user_optstr && !strncmp(p, "OPTS=", 5)) {
365
0
      fs->user_optstr = unmangle(p + 5, &end);
366
0
      if (!fs->user_optstr)
367
0
        goto enomem;
368
369
0
    } else if (!fs->attrs && !strncmp(p, "ATTRS=", 6)) {
370
0
      fs->attrs = unmangle(p + 6, &end);
371
0
      if (!fs->attrs)
372
0
        goto enomem;
373
374
0
    } else {
375
      /* unknown variable */
376
0
      while (*p && *p != ' ') p++;
377
0
    }
378
0
    if (end)
379
0
      p = end;
380
0
  }
381
382
0
  return 0;
383
0
enomem:
384
0
  DBG(TAB, ul_debug("utab parse error: ENOMEM"));
385
0
  return -ENOMEM;
386
0
}
387
388
/*
389
 * Parses one line from /proc/swaps
390
 */
391
static int mnt_parse_swaps_line(struct libmnt_fs *fs, const char *s)
392
31.4k
{
393
31.4k
  uint64_t num;
394
31.4k
  int rc = 0;
395
31.4k
  char *p;
396
397
  /* (1) source */
398
31.4k
  p = unmangle(s, &s);
399
31.4k
  if (p) {
400
31.4k
    char *x = (char *) ul_endswith(p, PATH_DELETED_SUFFIX);
401
31.4k
    if (x && *x)
402
280
      *x = '\0';
403
31.4k
  }
404
31.4k
  if (!p || (rc = __mnt_fs_set_source_ptr(fs, p))) {
405
0
    DBG(TAB, ul_debug("tab parse error: [source]"));
406
0
    free(p);
407
0
    goto fail;
408
0
  }
409
410
31.4k
  s = skip_separator(s);
411
412
  /* (2) type */
413
31.4k
  fs->swaptype = unmangle(s, &s);
414
31.4k
  if (!fs->swaptype) {
415
3.94k
    DBG(TAB, ul_debug("tab parse error: [swaptype]"));
416
3.94k
    goto fail;
417
3.94k
  }
418
419
27.5k
  s = skip_separator(s);
420
421
  /* (3) size */
422
27.5k
  s = next_u64(s, &num, &rc);
423
27.5k
  if (!s || !*s || rc) {
424
24.8k
    DBG(TAB, ul_debug("tab parse error: [size]"));
425
24.8k
    goto fail;
426
24.8k
  }
427
2.65k
  fs->size = num;
428
429
2.65k
  s = skip_separator(s);
430
431
  /* (4) size */
432
2.65k
  s = next_u64(s, &num, &rc);
433
2.65k
  if (!s || !*s || rc) {
434
1.97k
    DBG(TAB, ul_debug("tab parse error: [used size]"));
435
1.97k
    goto fail;
436
1.97k
  }
437
676
  fs->usedsize = num;
438
439
676
  s = skip_separator(s);
440
441
  /* (5) priority */
442
676
  s = next_s32(s, &fs->priority, &rc);
443
676
  if (rc) {
444
271
    DBG(TAB, ul_debug("tab parse error: [priority]"));
445
271
    goto fail;
446
271
  }
447
448
405
  mnt_fs_set_fstype(fs, "swap");
449
405
  return 0;
450
31.0k
fail:
451
31.0k
  if (rc == 0)
452
6.60k
    rc = -EINVAL;
453
31.0k
  DBG(TAB, ul_debug("tab parse error on: '%s' [rc=%d]", s, rc));
454
31.0k
  return rc;
455
676
}
456
457
458
/*
459
 * Returns {m,fs}tab or mountinfo file format (MNT_FMT_*)
460
 *
461
 * Note that we aren't trying to guess the utab file format, because this file
462
 * always has to be parsed by private libmount routines with an explicitly defined
463
 * format.
464
 *
465
 * mountinfo: "<number> <number> ... "
466
 */
467
static int guess_table_format(const char *line)
468
3.32k
{
469
3.32k
  unsigned int a, b;
470
471
3.32k
  DBG(TAB, ul_debug("trying to guess table type"));
472
473
3.32k
  if (sscanf(line, "%u %u", &a, &b) == 2)
474
923
    return MNT_FMT_MOUNTINFO;
475
476
2.40k
  if (strncmp(line, "Filename\t", 9) == 0)
477
442
    return MNT_FMT_SWAPS;
478
479
1.96k
  return MNT_FMT_FSTAB;   /* fstab, or /proc/mounts */
480
2.40k
}
481
482
static int is_comment_line(const char *line)
483
74.1k
{
484
74.1k
  const char *p = skip_blank(line);
485
486
74.1k
  if (p && (*p == '#' || *p == '\n'))
487
9.19k
    return 1;
488
64.9k
  return 0;
489
74.1k
}
490
491
/* returns 1 if the last line in the @str is blank */
492
static int is_terminated_by_blank(const char *str)
493
2.61k
{
494
2.61k
  size_t sz = str ? strlen(str) : 0;
495
2.61k
  const char *p = sz ? str + (sz - 1) : NULL;
496
497
2.61k
  if (!sz || !p || *p != '\n')
498
441
    return 0;    /* empty or not terminated by '\n' */
499
2.17k
  if (p == str)
500
1.40k
    return 1;    /* only '\n' */
501
773
  p--;
502
1.36k
  while (p > str && (*p == ' ' || *p == '\t'))
503
592
    p--;
504
773
  return *p == '\n' ? 1 : 0;
505
2.17k
}
506
507
/*
508
 * Reads the next line from the file.
509
 *
510
 * Returns 0 if the line is a comment
511
 *         1 if the line is not a comment
512
 *        <0 on error
513
 */
514
static int next_comment_line(struct libmnt_parser *pa, char **last)
515
9.19k
{
516
9.19k
  if (getline(&pa->buf, &pa->bufsiz, pa->f) < 0)
517
162
    return feof(pa->f) ? 1 : -errno;
518
519
9.02k
  pa->line++;
520
9.02k
  *last = strchr(pa->buf, '\n');
521
522
9.02k
  return is_comment_line(pa->buf) ? 0 : 1;
523
9.19k
}
524
525
static int append_comment(struct libmnt_table *tb,
526
        struct libmnt_fs *fs,
527
        const char *comm,
528
        int eof)
529
9.40k
{
530
9.40k
  int rc, intro = mnt_table_get_nents(tb) == 0;
531
532
9.40k
  if (intro && is_terminated_by_blank(mnt_table_get_intro_comment(tb)))
533
1.64k
    intro = 0;
534
535
9.40k
  DBG_OBJ(TAB, tb, ul_debug("appending %s comment",
536
9.40k
      intro ? "intro" :
537
9.40k
      eof ? "trailing" : "fs"));
538
9.40k
  if (intro)
539
974
    rc = mnt_table_append_intro_comment(tb, comm);
540
8.42k
  else if (eof) {
541
151
    rc = mnt_table_set_trailing_comment(tb,
542
151
        mnt_fs_get_comment(fs));
543
151
    if (!rc)
544
151
      rc = mnt_table_append_trailing_comment(tb, comm);
545
151
    if (!rc)
546
151
      rc = mnt_fs_set_comment(fs, NULL);
547
151
  } else
548
8.27k
    rc = mnt_fs_append_comment(fs, comm);
549
9.40k
  return rc;
550
9.40k
}
551
552
/*
553
 * Read and parse the next line from {fs,m}tab or mountinfo
554
 */
555
static int mnt_table_parse_next(struct libmnt_parser *pa,
556
        struct libmnt_table *tb,
557
        struct libmnt_fs *fs)
558
136k
{
559
136k
  char *s;
560
136k
  int rc;
561
562
136k
  assert(tb);
563
136k
  assert(pa);
564
136k
  assert(fs);
565
566
  /* read the next non-blank non-comment line */
567
137k
next_line:
568
149k
  do {
569
149k
    if (getline(&pa->buf, &pa->bufsiz, pa->f) < 0)
570
220
      return -EINVAL;
571
149k
    pa->line++;
572
149k
    s = strchr(pa->buf, '\n');
573
149k
    if (!s) {
574
8.33k
      DBG_OBJ(TAB, tb, ul_debug("%s:%zu: no final newline",
575
8.33k
            pa->filename, pa->line));
576
577
      /* Missing final newline?  Otherwise an extremely */
578
      /* long line - assume file was corrupted */
579
8.33k
      if (feof(pa->f))
580
3.24k
        s = memchr(pa->buf, '\0', pa->bufsiz);
581
582
    /* comments parser */
583
141k
    } else if (tb->comms
584
141k
        && (tb->fmt == MNT_FMT_GUESS || tb->fmt == MNT_FMT_FSTAB)
585
65.1k
        && is_comment_line(pa->buf)) {
586
9.19k
      do {
587
9.19k
        rc = append_comment(tb, fs, pa->buf, feof(pa->f));
588
9.19k
        if (!rc)
589
9.19k
          rc = next_comment_line(pa, &s);
590
9.19k
      } while (rc == 0);
591
592
6.78k
      if (rc == 1 && feof(pa->f))
593
210
        rc = append_comment(tb, fs, NULL, 1);
594
6.78k
      if (rc < 0)
595
0
        return rc;
596
597
6.78k
    }
598
599
149k
    if (!s)
600
5.48k
      goto err;
601
144k
    *s = '\0';
602
144k
    if (s > pa->buf && *(s - 1)  == '\r')
603
631
      *(--s) = '\0';
604
144k
    s = (char *) skip_blank(pa->buf);
605
144k
  } while (*s == '\0' || *s == '#');
606
607
131k
  if (tb->fmt == MNT_FMT_GUESS) {
608
3.32k
    tb->fmt = guess_table_format(s);
609
3.32k
    if (tb->fmt == MNT_FMT_SWAPS)
610
442
      goto next_line;     /* skip swap header */
611
3.32k
  }
612
613
131k
  switch (tb->fmt) {
614
65.1k
  case MNT_FMT_FSTAB:
615
65.1k
    rc = mnt_parse_table_line(fs, s);
616
65.1k
    break;
617
34.3k
  case MNT_FMT_MOUNTINFO:
618
34.3k
    rc = mnt_parse_mountinfo_line(fs, s);
619
34.3k
    break;
620
0
  case MNT_FMT_UTAB:
621
0
    rc = mnt_parse_utab_line(fs, s);
622
0
    break;
623
31.9k
  case MNT_FMT_SWAPS:
624
31.9k
    if (strncmp(s, "Filename\t", 9) == 0)
625
480
      goto next_line;      /* skip swap header */
626
31.4k
    rc = mnt_parse_swaps_line(fs, s);
627
31.4k
    break;
628
0
  default:
629
0
    rc = -1;  /* unknown format */
630
0
    break;
631
131k
  }
632
633
130k
  if (rc == 0)
634
52.3k
    return 0;
635
84.1k
err:
636
84.1k
  DBG_OBJ(TAB, tb, ul_debug("%s:%zu: %s parse error", pa->filename, pa->line,
637
84.1k
        tb->fmt == MNT_FMT_MOUNTINFO ? "mountinfo" :
638
84.1k
        tb->fmt == MNT_FMT_SWAPS ? "swaps" :
639
84.1k
        tb->fmt == MNT_FMT_FSTAB ? "tab" : "utab"));
640
641
  /* by default all errors are recoverable, otherwise behavior depends on
642
   * the errcb() function. See mnt_table_set_parser_errcb().
643
   */
644
84.1k
  return tb->errcb ? tb->errcb(tb, pa->filename, pa->line) : 1;
645
130k
}
646
647
static pid_t path_to_tid(const char *filename)
648
466
{
649
466
  char *path = mnt_resolve_path(filename, NULL);
650
466
  char *p, *end = NULL;
651
466
  pid_t tid = 0;
652
653
466
  if (!path)
654
0
    goto done;
655
466
  p = strrchr(path, '/');
656
466
  if (!p)
657
466
    goto done;
658
0
  *p = '\0';
659
0
  p = strrchr(path, '/');
660
0
  if (!p)
661
0
    goto done;
662
0
  p++;
663
664
0
  errno = 0;
665
0
  tid = strtol(p, &end, 10);
666
0
  if (errno || p == end || (end && *end)) {
667
0
    tid = 0;
668
0
    goto done;
669
0
  }
670
0
  DBG(TAB, ul_debug("TID for %s is %d", filename, tid));
671
466
done:
672
466
  free(path);
673
466
  return tid;
674
0
}
675
676
static int kernel_fs_postparse(struct libmnt_parser *pa,
677
             struct libmnt_table *tb,
678
             struct libmnt_fs *fs, pid_t *tid)
679
4.80k
{
680
4.80k
  int rc = 0;
681
4.80k
  const char *src = mnt_fs_get_srcpath(fs);
682
683
  /* This is a filesystem description from /proc, so we're in some process
684
   * namespace. Let's remember the process PID.
685
   */
686
4.80k
  if (pa->filename && *tid == -1)
687
466
    *tid = path_to_tid(pa->filename);
688
689
4.80k
  fs->tid = *tid;
690
691
  /*
692
   * Convert obscure /dev/root to something more usable
693
   */
694
4.80k
  if (src && strcmp(src, "/dev/root") == 0) {
695
696
    /* We will only call mnt_guess_system_root() if it has not
697
     * been called before. Inside a container, mountinfo can contain
698
     * many lines with /dev/root.
699
     */
700
1.48k
    if (pa->sysroot_rc == 0 && pa->sysroot == NULL)
701
49
      pa->sysroot_rc = mnt_guess_system_root(fs->devno,
702
49
            tb->cache, &pa->sysroot);
703
704
1.48k
    rc = pa->sysroot_rc;
705
1.48k
    if (rc < 0)
706
0
      return rc;
707
708
    /* This means that we already have run the mnt_guess_system_root()
709
     * and that we want to reuse the result.
710
     */
711
1.48k
    if (rc == 0 && pa->sysroot != NULL) {
712
1.48k
      char *real = strdup(pa->sysroot);
713
714
1.48k
      if (!real)
715
0
        return -ENOMEM;
716
717
1.48k
      DBG_OBJ(TAB, tb, ul_debug("canonical root FS: %s", real));
718
1.48k
      rc = __mnt_fs_set_source_ptr(fs, real);
719
720
1.48k
    } else if (rc == 1) {
721
      /* mnt_guess_system_root() returns 1 if not able to convert to
722
       * the real devname; ignore this problem */
723
0
      rc = 0;
724
0
    }
725
1.48k
  }
726
727
4.80k
  return rc;
728
4.80k
}
729
730
/**
731
 * mnt_table_parse_stream:
732
 * @tb: tab pointer
733
 * @f: file stream
734
 * @filename: filename used for debug and error messages
735
 *
736
 * Returns: 0 on success, negative number in case of error.
737
 */
738
int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filename)
739
3.52k
{
740
3.52k
  int rc = -1;
741
3.52k
  int flags = 0;
742
3.52k
  pid_t tid = -1;
743
3.52k
  struct libmnt_parser pa = { .line = 0 };
744
745
3.52k
  assert(tb);
746
3.52k
  assert(f);
747
3.52k
  assert(filename);
748
749
3.52k
  DBG_OBJ(TAB, tb, ul_debug("%s: start parsing [entries=%d, filter=%s]",
750
3.52k
        filename, mnt_table_get_nents(tb),
751
3.52k
        tb->fltrcb ? "yes" : "not"));
752
753
3.52k
  pa.filename = filename;
754
3.52k
  pa.f = f;
755
756
  /* necessary for /proc/mounts only, the /proc/self/mountinfo
757
   * parser sets the flag properly
758
   */
759
3.52k
  if (tb->fmt == MNT_FMT_SWAPS)
760
0
    flags = MNT_FS_SWAP | MNT_FS_STATUS_ATTACH;
761
3.52k
  else if (filename && strcmp(filename, _PATH_PROC_MOUNTS) == 0)
762
0
    flags = MNT_FS_KERNEL | MNT_FS_STATUS_ATTACH;
763
764
140k
  do {
765
140k
    struct libmnt_fs *fs;
766
767
140k
    if (feof(f)) {
768
3.52k
      DBG_OBJ(TAB, tb, ul_debug("end-of-file"));
769
3.52k
      break;
770
3.52k
    }
771
136k
    fs = mnt_new_fs();
772
136k
    if (!fs)
773
0
      goto err;
774
775
    /* parse */
776
136k
    rc = mnt_table_parse_next(&pa, tb, fs);
777
778
136k
    if (rc == 0 && tb->fltrcb && tb->fltrcb(fs, tb->fltrcb_data))
779
0
      rc = 1; /* filtered out by callback... */
780
781
136k
    if (rc == 0 && mnt_table_is_noautofs(tb)) {
782
0
      const char *fstype = mnt_fs_get_fstype(fs);
783
784
0
      if (fstype && strcmp(fstype, "autofs") == 0 &&
785
0
          mnt_fs_get_option(fs, "ignore", NULL, NULL) == 0)
786
0
        rc = 1; /* Skip "ignore" autofs entry */
787
0
    }
788
789
    /* add to the table */
790
136k
    if (rc == 0) {
791
52.3k
      rc = mnt_table_add_fs(tb, fs);
792
52.3k
      fs->flags |= flags;
793
794
52.3k
      if (rc == 0 && tb->fmt == MNT_FMT_MOUNTINFO) {
795
4.80k
        rc = kernel_fs_postparse(&pa, tb, fs, &tid);
796
4.80k
        if (rc)
797
0
          mnt_table_remove_fs(tb, fs);
798
4.80k
      }
799
52.3k
    }
800
801
    /* remove reference (or deallocate on error) */
802
136k
    mnt_unref_fs(fs);
803
804
    /* recoverable error */
805
136k
    if (rc > 0) {
806
84.1k
      DBG_OBJ(TAB, tb, ul_debug("recoverable error (continue)"));
807
84.1k
      continue;
808
84.1k
    }
809
810
    /* fatal errors */
811
52.5k
    if (rc < 0 && !feof(f)) {
812
0
      DBG_OBJ(TAB, tb, ul_debug("fatal error"));
813
0
      goto err;
814
0
    }
815
136k
  } while (1);
816
817
3.52k
  DBG_OBJ(TAB, tb, ul_debug("%s: stop parsing (%d entries)",
818
3.52k
        filename, mnt_table_get_nents(tb)));
819
3.52k
  parser_cleanup(&pa);
820
3.52k
  return 0;
821
0
err:
822
0
  DBG_OBJ(TAB, tb, ul_debug("%s: parse error (rc=%d)", filename, rc));
823
0
  parser_cleanup(&pa);
824
0
  return rc;
825
3.52k
}
826
827
/**
828
 * mnt_table_parse_file:
829
 * @tb: tab pointer
830
 * @filename: file
831
 *
832
 * Parses the whole table (e.g. /etc/fstab) and appends new records to the @tab.
833
 *
834
 * The libmount parser ignores broken (syntax error) lines, these lines are
835
 * reported to the caller by the errcb() function (see mnt_table_set_parser_errcb()).
836
 *
837
 * Returns: 0 on success, negative number in case of error.
838
 */
839
int mnt_table_parse_file(struct libmnt_table *tb, const char *filename)
840
0
{
841
0
  FILE *f;
842
0
  int rc;
843
844
0
  if (!filename || !tb)
845
0
    return -EINVAL;
846
847
0
  f = ul_vfs_fopen(tb->vfs, filename, "r" UL_CLOEXECSTR);
848
0
  if (f) {
849
0
    rc = mnt_table_parse_stream(tb, f, filename);
850
0
    fclose(f);
851
0
  } else
852
0
    rc = -errno;
853
854
0
  DBG_OBJ(TAB, tb, ul_debug("parsing done [filename=%s, rc=%d]", filename, rc));
855
0
  return rc;
856
0
}
857
858
static int mnt_table_parse_dir_filter(const struct dirent *d)
859
0
{
860
0
  size_t namesz;
861
862
0
#ifdef _DIRENT_HAVE_D_TYPE
863
0
  if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG &&
864
0
      d->d_type != DT_LNK)
865
0
    return 0;
866
0
#endif
867
0
  if (*d->d_name == '.')
868
0
    return 0;
869
870
0
#define MNT_MNTTABDIR_EXTSIZ  (sizeof(MNT_MNTTABDIR_EXT) - 1)
871
872
0
  namesz = strlen(d->d_name);
873
0
  if (!namesz || namesz < MNT_MNTTABDIR_EXTSIZ + 1 ||
874
0
      strcmp(d->d_name + (namesz - MNT_MNTTABDIR_EXTSIZ),
875
0
       MNT_MNTTABDIR_EXT) != 0)
876
0
    return 0;
877
878
  /* Accept this */
879
0
  return 1;
880
0
}
881
882
#ifdef HAVE_SCANDIRAT
883
static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
884
0
{
885
0
  int n = 0, i;
886
0
  int dd;
887
0
  struct dirent **namelist = NULL;
888
889
0
  dd = open(dirname, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
890
0
  if (dd < 0)
891
0
          return -errno;
892
893
0
  n = scandirat(dd, ".", &namelist, mnt_table_parse_dir_filter, versionsort);
894
0
  if (n <= 0) {
895
0
          close(dd);
896
0
          return 0;
897
0
  }
898
899
0
  for (i = 0; i < n; i++) {
900
0
    struct dirent *d = namelist[i];
901
0
    struct stat st;
902
0
    FILE *f;
903
904
0
    if (fstatat(dd, d->d_name, &st, 0) ||
905
0
        !S_ISREG(st.st_mode))
906
0
      continue;
907
908
0
    f = fopen_at(dd, d->d_name, O_RDONLY|O_CLOEXEC, "r" UL_CLOEXECSTR);
909
0
    if (f) {
910
0
      mnt_table_parse_stream(tb, f, d->d_name);
911
0
      fclose(f);
912
0
    }
913
0
  }
914
915
0
  for (i = 0; i < n; i++)
916
0
    free(namelist[i]);
917
0
  free(namelist);
918
0
  close(dd);
919
0
  return 0;
920
0
}
921
#else
922
static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
923
{
924
  int n = 0, i, r = 0;
925
  DIR *dir = NULL;
926
  struct dirent **namelist = NULL;
927
928
  n = scandir(dirname, &namelist, mnt_table_parse_dir_filter, versionsort);
929
  if (n <= 0)
930
    return 0;
931
932
  /* let's use "at" functions rather than playing crazy games with paths... */
933
  dir = opendir(dirname);
934
  if (!dir) {
935
    r = -errno;
936
    goto out;
937
  }
938
939
  for (i = 0; i < n; i++) {
940
    struct dirent *d = namelist[i];
941
    struct stat st;
942
    FILE *f;
943
944
    if (fstatat(dirfd(dir), d->d_name, &st, 0) ||
945
        !S_ISREG(st.st_mode))
946
      continue;
947
948
    f = fopen_at(dirfd(dir), d->d_name,
949
        O_RDONLY|O_CLOEXEC, "r" UL_CLOEXECSTR);
950
    if (f) {
951
      mnt_table_parse_stream(tb, f, d->d_name);
952
      fclose(f);
953
    }
954
  }
955
956
out:
957
  for (i = 0; i < n; i++)
958
    free(namelist[i]);
959
  free(namelist);
960
  if (dir)
961
    closedir(dir);
962
  return r;
963
}
964
#endif
965
966
/**
967
 * mnt_table_parse_dir:
968
 * @tb: mount table
969
 * @dirname: directory
970
 *
971
 * The directory:
972
 *  - files are sorted by strverscmp(3)
973
 *  - files that start with "." are ignored (e.g. ".10foo.fstab")
974
 *  - files without the ".fstab" extension are ignored
975
 *
976
 * Returns: 0 on success or negative number in case of error.
977
 */
978
int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
979
0
{
980
0
  return __mnt_table_parse_dir(tb, dirname);
981
0
}
982
983
struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt, int empty_for_enoent)
984
0
{
985
0
  struct libmnt_table *tb;
986
987
0
  if (!filename)
988
0
    return NULL;
989
0
  if (!mnt_is_path(filename))
990
0
    return empty_for_enoent ? mnt_new_table() : NULL;
991
992
0
  tb = mnt_new_table();
993
0
  if (tb) {
994
0
    DBG_OBJ(TAB, tb, ul_debug("new tab for file: %s", filename));
995
0
    tb->fmt = fmt;
996
0
    if (mnt_table_parse_file(tb, filename) != 0) {
997
0
      mnt_unref_table(tb);
998
0
      tb = NULL;
999
0
    }
1000
0
  }
1001
0
  return tb;
1002
0
}
1003
1004
/**
1005
 * mnt_new_table_from_file:
1006
 * @filename: /etc/{m,fs}tab or /proc/self/mountinfo path
1007
 *
1008
 * Same as mnt_new_table() + mnt_table_parse_file(). Use this function for private
1009
 * files only. This function does not allow using the error callback, so you
1010
 * cannot provide any feedback to end-users about broken records in files (e.g.
1011
 * fstab).
1012
 *
1013
 * Returns: newly allocated tab on success and NULL in case of error.
1014
 */
1015
struct libmnt_table *mnt_new_table_from_file(const char *filename)
1016
0
{
1017
0
  if (!filename)
1018
0
    return NULL;
1019
1020
0
  return __mnt_new_table_from_file(filename, MNT_FMT_GUESS, 0);
1021
0
}
1022
1023
/**
1024
 * mnt_new_table_from_dir
1025
 * @dirname: directory with *.fstab files
1026
 *
1027
 * Returns: newly allocated tab on success and NULL in case of error.
1028
 */
1029
struct libmnt_table *mnt_new_table_from_dir(const char *dirname)
1030
0
{
1031
0
  struct libmnt_table *tb;
1032
1033
0
  if (!dirname)
1034
0
    return NULL;
1035
0
  tb = mnt_new_table();
1036
0
  if (tb && mnt_table_parse_dir(tb, dirname) != 0) {
1037
0
    mnt_unref_table(tb);
1038
0
    tb = NULL;
1039
0
  }
1040
0
  return tb;
1041
0
}
1042
1043
/**
1044
 * mnt_table_set_parser_errcb:
1045
 * @tb: pointer to table
1046
 * @cb: pointer to callback function
1047
 *
1048
 * The error callback function is called by table parser (mnt_table_parse_file())
1049
 * in case of a syntax error. The callback function could be used for error
1050
 * evaluation, libmount will continue/stop parsing according to callback return
1051
 * codes:
1052
 *
1053
 *   <0  : fatal error (abort parsing)
1054
 *    0  : success (parsing continues)
1055
 *   >0  : recoverable error (the line is ignored, parsing continues).
1056
 *
1057
 * Returns: 0 on success or negative number in case of error.
1058
 */
1059
int mnt_table_set_parser_errcb(struct libmnt_table *tb,
1060
    int (*cb)(struct libmnt_table *tb, const char *filename, int line))
1061
0
{
1062
0
  if (!tb)
1063
0
    return -EINVAL;
1064
0
  tb->errcb = cb;
1065
0
  return 0;
1066
0
}
1067
1068
/*
1069
 * Filter out entries during tab file parsing. If @cb returns 1, then the entry
1070
 * is ignored.
1071
 */
1072
int mnt_table_set_parser_fltrcb(struct libmnt_table *tb,
1073
    int (*cb)(struct libmnt_fs *, void *),
1074
    void *data)
1075
0
{
1076
0
  if (!tb)
1077
0
    return -EINVAL;
1078
1079
0
  DBG_OBJ(TAB, tb, ul_debug("%s table parser filter", cb ? "set" : "unset"));
1080
0
  tb->fltrcb = cb;
1081
0
  tb->fltrcb_data = data;
1082
0
  return 0;
1083
0
}
1084
1085
/**
1086
 * mnt_table_enable_noautofs:
1087
 * @tb: table
1088
 * @ignore: ignore or don't ignore
1089
 *
1090
 * Enable/disable ignore autofs mount table entries on reading.
1091
 *
1092
 * Returns: 0 on success or negative number in case of error.
1093
 *
1094
 * Since: 2.38
1095
 */
1096
int mnt_table_enable_noautofs(struct libmnt_table *tb, int ignore)
1097
0
{
1098
0
  if (!tb)
1099
0
    return -EINVAL;
1100
0
  tb->noautofs = ignore ? 1 : 0;
1101
0
  return 0;
1102
0
}
1103
1104
/**
1105
 * mnt_table_is_noautofs:
1106
 * @tb: table
1107
 *
1108
 * Returns: 1 if autofs entries are ignored, 0 otherwise.
1109
 *
1110
 * Since: 2.38
1111
 */
1112
int mnt_table_is_noautofs(struct libmnt_table *tb)
1113
52.3k
{
1114
52.3k
  return tb ? tb->noautofs : 0;
1115
52.3k
}
1116
1117
/**
1118
 * mnt_table_disable_useropts:
1119
 * @tb: table
1120
 * @disable: 1 to disable, 0 to enable
1121
 *
1122
 * Disable or enable merging of userspace mount options from utab when
1123
 * populating the mount table. This applies to both mountinfo and
1124
 * listmount-based table sources.
1125
 *
1126
 * Returns: 0 on success, <0 on error.
1127
 *
1128
 * Since: 2.43
1129
 */
1130
int mnt_table_disable_useropts(struct libmnt_table *tb, int disable)
1131
0
{
1132
0
  if (!tb)
1133
0
    return -EINVAL;
1134
0
  tb->nouseropts = disable ? 1 : 0;
1135
0
  return 0;
1136
0
}
1137
1138
/**
1139
 * mnt_table_parse_swaps:
1140
 * @tb: table
1141
 * @filename: overwrites default (/proc/swaps or $LIBMOUNT_SWAPS) or NULL
1142
 *
1143
 * This function parses /proc/swaps and appends new lines to the @tab.
1144
 *
1145
 * See also mnt_table_set_parser_errcb().
1146
 *
1147
 * Returns: 0 on success or negative number in case of error.
1148
 */
1149
int mnt_table_parse_swaps(struct libmnt_table *tb, const char *filename)
1150
0
{
1151
0
  if (!tb)
1152
0
    return -EINVAL;
1153
0
  if (!filename) {
1154
0
    filename = mnt_get_swaps_path();
1155
0
    if (!filename)
1156
0
      return -EINVAL;
1157
0
  }
1158
1159
0
  tb->fmt = MNT_FMT_SWAPS;
1160
1161
0
  return mnt_table_parse_file(tb, filename);
1162
0
}
1163
1164
/**
1165
 * mnt_table_parse_utab:
1166
 * @tb: table
1167
 * @filename: overwrites default or NULL (recommended)
1168
 *
1169
 * This function parses the utab file and appends new lines to the @tb. The utab
1170
 * is a private libmount file that stores userspace mount options. The file
1171
 * location and format are library implementation details and subject to change;
1172
 * use NULL as @filename to let the library determine the correct path.
1173
 *
1174
 * Returns: 0 on success or negative number in case of error.
1175
 *
1176
 * Since: 2.43
1177
 */
1178
int mnt_table_parse_utab(struct libmnt_table *tb, const char *filename)
1179
0
{
1180
0
  if (!tb)
1181
0
    return -EINVAL;
1182
0
  if (!filename)
1183
0
    filename = mnt_get_utab_path();
1184
0
  if (!filename || is_file_empty(filename))
1185
0
    return 0;
1186
1187
0
  tb->fmt = MNT_FMT_UTAB;
1188
1189
0
  return mnt_table_parse_file(tb, filename);
1190
0
}
1191
1192
/**
1193
 * mnt_table_parse_fstab:
1194
 * @tb: table
1195
 * @filename: overwrites default (/etc/fstab or $LIBMOUNT_FSTAB) or NULL
1196
 *
1197
 * This function parses /etc/fstab and appends new lines to the @tab. If the
1198
 * @filename is a directory, then mnt_table_parse_dir() is called.
1199
 *
1200
 * See also mnt_table_set_parser_errcb().
1201
 *
1202
 * Returns: 0 on success or negative number in case of error.
1203
 */
1204
int mnt_table_parse_fstab(struct libmnt_table *tb, const char *filename)
1205
0
{
1206
0
  struct stat st;
1207
0
  int rc = 0;
1208
1209
0
  if (!tb)
1210
0
    return -EINVAL;
1211
0
  if (!filename)
1212
0
    filename = mnt_get_fstab_path();
1213
0
  if (!filename)
1214
0
    return -EINVAL;
1215
0
  if (mnt_safe_stat(filename, &st) != 0)
1216
0
    return -errno;
1217
1218
0
  tb->fmt = MNT_FMT_FSTAB;
1219
1220
0
  if (S_ISREG(st.st_mode))
1221
0
    rc = mnt_table_parse_file(tb, filename);
1222
0
  else if (S_ISDIR(st.st_mode))
1223
0
    rc = mnt_table_parse_dir(tb, filename);
1224
0
  else
1225
0
    rc = -EINVAL;
1226
1227
0
  return rc;
1228
0
}
1229
1230
/*
1231
 * This function uses @uf to find a corresponding record in @tb, then the record
1232
 * from @tb is updated (user specific mount options are added).
1233
 *
1234
 * Note that @uf must contain only user specific mount options instead of
1235
 * VFS options (note that FS options are ignored).
1236
 *
1237
 * Returns modified filesystem (from @tb) or NULL.
1238
 */
1239
static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct libmnt_fs *uf)
1240
0
{
1241
0
  struct libmnt_fs *fs;
1242
0
  struct libmnt_iter itr;
1243
0
  const char *optstr, *src, *target, *root, *attrs;
1244
0
  int id;
1245
0
  uint64_t uniq_id;
1246
1247
0
  if (!tb || !uf)
1248
0
    return NULL;
1249
1250
0
  DBG_OBJ(TAB, tb, ul_debug("merging user fs"));
1251
1252
0
  src = mnt_fs_get_srcpath(uf);
1253
0
  target = mnt_fs_get_target(uf);
1254
0
  optstr = mnt_fs_get_user_options(uf);
1255
0
  attrs = mnt_fs_get_attributes(uf);
1256
0
  root = mnt_fs_get_root(uf);
1257
0
  id = mnt_fs_get_id(uf);
1258
0
  uniq_id = mnt_fs_get_uniq_id(uf);
1259
1260
0
  if (!src || !target || !root || (!attrs && !optstr))
1261
0
    return NULL;
1262
1263
0
  mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
1264
1265
0
  while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
1266
0
    const char *r = mnt_fs_get_root(fs);
1267
1268
0
    if (fs->flags & MNT_FS_MERGED)
1269
0
      continue;
1270
1271
0
    if (uniq_id > 0 && mnt_fs_get_uniq_id(fs)) {
1272
0
      DBG_OBJ(TAB, tb, ul_debug(" using uniq ID"));
1273
0
      if (mnt_fs_get_uniq_id(fs) == uniq_id)
1274
0
        break;
1275
1276
0
    } else if (id > 0 && mnt_fs_get_id(fs)) {
1277
0
      DBG_OBJ(TAB, tb, ul_debug(" using ID"));
1278
0
      if (mnt_fs_get_id(fs) == id)
1279
0
        break;
1280
1281
0
    } else if (r && strcmp(r, root) == 0
1282
0
        && mnt_fs_streq_target(fs, target)
1283
0
        && mnt_fs_streq_srcpath(fs, src))
1284
0
      break;
1285
0
  }
1286
1287
0
  if (fs) {
1288
0
    DBG_OBJ(TAB, tb, ul_debug(" found"));
1289
0
    mnt_optstr_append_option(&fs->user_optstr, optstr, NULL);
1290
0
    free(fs->optstr);
1291
0
    fs->optstr = NULL;
1292
0
    mnt_fs_append_attributes(fs, attrs);
1293
0
    mnt_fs_set_bindsrc(fs, mnt_fs_get_bindsrc(uf));
1294
0
    fs->flags |= MNT_FS_MERGED;
1295
1296
0
    DBG(TAB, mnt_fs_print_debug(fs, stderr));
1297
0
  }
1298
0
  return fs;
1299
0
}
1300
1301
/*
1302
 * Read utab and merge userspace options into the mount table @tb.
1303
 * If @u_tb is provided, use it as utab; otherwise read from
1304
 * /run/mount/utab.
1305
 */
1306
int mnt_table_merge_utab(struct libmnt_table *tb, struct libmnt_table *u_tb)
1307
0
{
1308
0
  int rc = 0, priv_utab = 0;
1309
1310
0
  assert(tb);
1311
1312
0
  if (tb->nouseropts)
1313
0
    return 0;
1314
0
  if (mnt_table_get_nents(tb) == 0)
1315
0
    return 0;
1316
1317
0
  if (!u_tb) {
1318
0
    const char *utab = mnt_get_utab_path();
1319
1320
0
    if (!utab || is_file_empty(utab))
1321
0
      return 0;
1322
1323
0
    u_tb = mnt_new_table();
1324
0
    if (!u_tb)
1325
0
      return -ENOMEM;
1326
1327
0
    u_tb->fmt = MNT_FMT_UTAB;
1328
0
    mnt_table_set_parser_fltrcb(u_tb, tb->fltrcb, tb->fltrcb_data);
1329
1330
0
    rc = mnt_table_parse_file(u_tb, utab);
1331
0
    priv_utab = 1;
1332
0
  }
1333
1334
0
  DBG_OBJ(TAB, tb, ul_debug("merging utab"));
1335
1336
0
  if (rc == 0) {
1337
0
    struct libmnt_fs *u_fs;
1338
0
    struct libmnt_iter itr;
1339
1340
0
    mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
1341
1342
0
    while (mnt_table_next_fs(u_tb, &itr, &u_fs) == 0)
1343
0
      mnt_table_merge_user_fs(tb, u_fs);
1344
0
  }
1345
1346
0
  if (priv_utab)
1347
0
    mnt_unref_table(u_tb);
1348
0
  return 0;
1349
0
}
1350
1351
/* default filename is /proc/self/mountinfo
1352
 */
1353
int __mnt_table_parse_mountinfo(struct libmnt_table *tb, const char *filename,
1354
         struct libmnt_table *u_tb)
1355
0
{
1356
0
  int rc = 0;
1357
0
  int explicit_file = filename ? 1 : 0;
1358
1359
0
  assert(tb);
1360
1361
0
  if (filename)
1362
0
    DBG_OBJ(TAB, tb, ul_debug("%s requested as mount table", filename));
1363
1364
0
  if (!filename || strcmp(filename, _PATH_PROC_MOUNTINFO) == 0) {
1365
0
    filename = _PATH_PROC_MOUNTINFO;
1366
0
    tb->fmt = MNT_FMT_MOUNTINFO;
1367
0
    DBG_OBJ(TAB, tb, ul_debug("mountinfo parse: #1 read mountinfo"));
1368
0
  } else
1369
0
    tb->fmt = MNT_FMT_GUESS;
1370
1371
0
  rc = mnt_table_parse_file(tb, filename);
1372
0
  if (rc) {
1373
0
    if (explicit_file)
1374
0
      return rc;
1375
1376
    /* hmm, old kernel? ...try /proc/mounts */
1377
0
    tb->fmt = MNT_FMT_MTAB;
1378
0
    return mnt_table_parse_file(tb, _PATH_PROC_MOUNTS);
1379
0
  }
1380
1381
0
  if (!is_mountinfo(tb))
1382
0
    return 0;
1383
1384
0
  DBG_OBJ(TAB, tb, ul_debug("mountinfo parse: #2 merge utab"));
1385
0
  return mnt_table_merge_utab(tb, u_tb);
1386
0
}
1387
1388
/**
1389
 * mnt_table_parse_mtab:
1390
 * @tb: table
1391
 * @filename: overwrites default or NULL
1392
 *
1393
 * The default filename is /proc/self/mountinfo. If the mount table is a
1394
 * mountinfo file then /run/mount/utabs is parsed too and both files are merged
1395
 * to the one libmnt_table.
1396
 *
1397
 * The file /etc/mtab is no more used. The function uses "mtab" in the name for
1398
 * backward compatibility only.
1399
 *
1400
 * It's strongly recommended to use NULL as a @filename to keep code portable.
1401
 *
1402
 * See also mnt_table_set_parser_errcb().
1403
 *
1404
 * Returns: 0 on success or negative number in case of error.
1405
 */
1406
int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename)
1407
0
{
1408
  return __mnt_table_parse_mountinfo(tb, filename, NULL);
1409
0
}