]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/swapon-common.c
2 * SPDX-License-Identifier: GPL-2.0-or-later
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.
9 * Copyright (C) 2012-2023 Karel Zak <kzak@redhat.com>
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.
18 #include "swapon-common.h"
21 * content of /proc/swaps and /etc/fstab
23 static struct libmnt_table
*swaps
, *fstab
;
25 struct libmnt_cache
*mntcache
;
27 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
28 const char *filename
, int line
)
31 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
35 struct libmnt_table
*get_fstab(const char *filename
)
38 fstab
= mnt_new_table();
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)
50 struct libmnt_table
*get_swaps(void)
53 swaps
= mnt_new_table();
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)
65 void free_tables(void)
67 mnt_unref_table(swaps
);
68 mnt_unref_table(fstab
);
71 int match_swap(struct libmnt_fs
*fs
, void *data
__attribute__((unused
)))
73 return fs
&& mnt_fs_is_swaparea(fs
);
76 int is_active_swap(const char *filename
)
78 struct libmnt_table
*st
= get_swaps();
79 return st
&& mnt_table_find_source(st
, filename
, MNT_ITER_BACKWARD
);
83 int cannot_find(const char *special
)
85 warnx(_("cannot find the device for %s"), special
);
90 * Lists with -L and -U option
92 static const char **llist
;
94 static const char **ulist
;
98 void add_label(const char *label
)
100 llist
= xreallocarray(llist
, ++llct
, sizeof(char *));
101 llist
[llct
- 1] = label
;
104 const char *get_label(size_t i
)
106 return i
< llct
? llist
[i
] : NULL
;
109 size_t numof_labels(void)
114 void add_uuid(const char *uuid
)
116 ulist
= xreallocarray(ulist
, ++ulct
, sizeof(char *));
117 ulist
[ulct
- 1] = uuid
;
120 const char *get_uuid(size_t i
)
122 return i
< ulct
? ulist
[i
] : NULL
;
125 size_t numof_uuids(void)