]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/swapon-common.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / swapon-common.c
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 */
14 #include "c.h"
15 #include "nls.h"
16 #include "xalloc.h"
17
18 #include "swapon-common.h"
19
20 /*
21 * content of /proc/swaps and /etc/fstab
22 */
23 static struct libmnt_table *swaps, *fstab;
24
25 struct libmnt_cache *mntcache;
26
27 static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
28 const char *filename, int line)
29 {
30 if (filename)
31 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
32 return 1;
33 }
34
35 struct libmnt_table *get_fstab(const char *filename)
36 {
37 if (!fstab) {
38 fstab = mnt_new_table();
39 if (!fstab)
40 return NULL;
41 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
42 mnt_table_set_cache(fstab, mntcache);
43 if (mnt_table_parse_fstab(fstab, filename) != 0)
44 return NULL;
45 }
46
47 return fstab;
48 }
49
50 struct 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);
57 mnt_table_set_parser_errcb(swaps, table_parser_errcb);
58 if (mnt_table_parse_swaps(swaps, NULL) != 0)
59 return NULL;
60 }
61
62 return swaps;
63 }
64
65 void free_tables(void)
66 {
67 mnt_unref_table(swaps);
68 mnt_unref_table(fstab);
69 }
70
71 int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
72 {
73 return fs && mnt_fs_is_swaparea(fs);
74 }
75
76 int is_active_swap(const char *filename)
77 {
78 struct libmnt_table *st = get_swaps();
79 return st && mnt_table_find_source(st, filename, MNT_ITER_BACKWARD);
80 }
81
82
83 int 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 */
92 static const char **llist;
93 static size_t llct;
94 static const char **ulist;
95 static size_t ulct;
96
97
98 void add_label(const char *label)
99 {
100 llist = xreallocarray(llist, ++llct, sizeof(char *));
101 llist[llct - 1] = label;
102 }
103
104 const char *get_label(size_t i)
105 {
106 return i < llct ? llist[i] : NULL;
107 }
108
109 size_t numof_labels(void)
110 {
111 return llct;
112 }
113
114 void add_uuid(const char *uuid)
115 {
116 ulist = xreallocarray(ulist, ++ulct, sizeof(char *));
117 ulist[ulct - 1] = uuid;
118 }
119
120 const char *get_uuid(size_t i)
121 {
122 return i < ulct ? ulist[i] : NULL;
123 }
124
125 size_t numof_uuids(void)
126 {
127 return ulct;
128 }
129