]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/swapon-common.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / swapon-common.c
CommitLineData
9abd5e4b
KZ
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 2012-2023 Karel Zak <kzak@redhat.com>
10 *
11 * Original implementation from Linux 0.99, without License and copyright in
12 * the code. Karel Zak rewrote the code under GPL-2.0-or-later.
13 */
0b0c231f
KZ
14#include "c.h"
15#include "nls.h"
0b0c231f
KZ
16#include "xalloc.h"
17
18b3e549
KZ
18#include "swapon-common.h"
19
0b0c231f
KZ
20/*
21 * content of /proc/swaps and /etc/fstab
22 */
23static struct libmnt_table *swaps, *fstab;
24
25struct libmnt_cache *mntcache;
26
1cd9d0d7
KZ
27static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
28 const char *filename, int line)
29{
30 if (filename)
b779c1ae 31 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7
KZ
32 return 1;
33}
34
a5a9b544 35struct libmnt_table *get_fstab(const char *filename)
0b0c231f
KZ
36{
37 if (!fstab) {
38 fstab = mnt_new_table();
39 if (!fstab)
40 return NULL;
1cd9d0d7 41 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
0b0c231f 42 mnt_table_set_cache(fstab, mntcache);
a5a9b544 43 if (mnt_table_parse_fstab(fstab, filename) != 0)
0b0c231f
KZ
44 return NULL;
45 }
46
47 return fstab;
48}
49
50struct libmnt_table *get_swaps(void)
51{
52 if (!swaps) {
53 swaps = mnt_new_table();
54 if (!swaps)
55 return NULL;
56 mnt_table_set_cache(swaps, mntcache);
1cd9d0d7 57 mnt_table_set_parser_errcb(swaps, table_parser_errcb);
0b0c231f
KZ
58 if (mnt_table_parse_swaps(swaps, NULL) != 0)
59 return NULL;
60 }
61
62 return swaps;
63}
64
65void free_tables(void)
66{
50fccba1
KZ
67 mnt_unref_table(swaps);
68 mnt_unref_table(fstab);
0b0c231f
KZ
69}
70
71int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
72{
73 return fs && mnt_fs_is_swaparea(fs);
74}
75
76int is_active_swap(const char *filename)
77{
78 struct libmnt_table *st = get_swaps();
e7b63bea 79 return st && mnt_table_find_source(st, filename, MNT_ITER_BACKWARD);
0b0c231f
KZ
80}
81
82
83int cannot_find(const char *special)
84{
85 warnx(_("cannot find the device for %s"), special);
86 return -1;
87}
88
89/*
90 * Lists with -L and -U option
91 */
92static const char **llist;
93static size_t llct;
94static const char **ulist;
95static size_t ulct;
96
97
98void add_label(const char *label)
99{
64d6d400 100 llist = xreallocarray(llist, ++llct, sizeof(char *));
0b0c231f
KZ
101 llist[llct - 1] = label;
102}
103
104const char *get_label(size_t i)
105{
106 return i < llct ? llist[i] : NULL;
107}
108
109size_t numof_labels(void)
110{
111 return llct;
112}
113
114void add_uuid(const char *uuid)
115{
64d6d400 116 ulist = xreallocarray(ulist, ++ulct, sizeof(char *));
0b0c231f
KZ
117 ulist[ulct - 1] = uuid;
118}
119
120const char *get_uuid(size_t i)
121{
122 return i < ulct ? ulist[i] : NULL;
123}
124
125size_t numof_uuids(void)
126{
127 return ulct;
128}
129