Debian Codegraph @

libmount/samples/listmount.c

raw
1 /*
2 * Copyright (C) 2024 Karel Zak <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7 #include <stdlib.h>
8
9 #include "c.h"
10 #include "strutils.h"
11 #include "timeutils.h"
12 #include "pathnames.h"
13
14 #include "libmount.h"
15
16 #include "mountutils.h" /* fallback for old linux/mount.h */
17
18 static void __iter_table( struct libmnt_table *tb,
19 struct libmnt_iter *itr, int output, int reverse)
20 {
21 struct libmnt_fs *fs = NULL;
22 int rc;
23
24 mnt_reset_iter(itr, reverse ? MNT_ITER_BACKWARD : MNT_ITER_FORWARD);
25 while ((rc = mnt_table_next_fs(tb, itr, &fs)) == 0) {
26 const char *tgt = mnt_fs_get_target(fs);
27 const char *type = mnt_fs_get_fstype(fs);
28
29 if (output)
30 printf (" %15s %s\n", type, tgt);
31 }
32 if (rc < 0)
33 warn("cannot iterate on filesystems");
34 }
35
36 #define fetch_data(t, i) __iter_table(t, i, 0, 0)
37 #define print_table(t, i) __iter_table(t, i, 1, 0)
38 #define print_table_reverse(t, i) __iter_table(t, i, 1, 1)
39
40
41 int main(int argc, char *argv[])
42 {
43 struct libmnt_table *tb;
44 struct libmnt_statmnt *sm;
45 struct libmnt_iter *itr;
46 struct timeval start, end;
47 uint64_t id = 0;
48 double sec_lsmnt, sec_lsstmnt, sec_mountinfo;
49
50 mnt_init_debug(0);
51
52
53 if (argc == 2) {
54 const char *arg = argv[1];
55 if (!isdigit_string(arg))
56 mnt_id_from_path(arg, &id, NULL);
57 else
58 id = strtou64_or_err(arg, "cannot ID");
59 }
60
61 itr = mnt_new_iter(MNT_ITER_FORWARD);
62 if (!itr)
63 err(MNT_EX_SYSERR, "failed to initialize libmount iterator");
64
65 tb = mnt_new_table();
66 if (!tb)
67 err(EXIT_FAILURE, "failed to allocate table handler");
68 if (id)
69 mnt_table_listmount_set_id(tb, id);
70
71 /*
72 * A) listmount() and statmount() based table
73 */
74 sm = mnt_new_statmnt();
75 if (!sm)
76 err(EXIT_FAILURE, "failed to allocate statmnt handler");
77
78 /* Without this mask setting, the library will make two statmount() calls
79 * for each node. */
80 #if defined(STATMOUNT_MNT_POINT) && defined(STATMOUNT_FS_TYPE)
81 mnt_statmnt_set_mask(sm, STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE);
82 #endif
83
84 /* force fetch all data
85 mnt_statmnt_set_mask(sm,
86 STATMOUNT_SB_BASIC |
87 STATMOUNT_MNT_BASIC |
88 STATMOUNT_PROPAGATE_FROM |
89 STATMOUNT_MNT_ROOT |
90 STATMOUNT_MNT_POINT |
91 STATMOUNT_FS_TYPE
92 STATMOUNT_MNT_OPTS);
93 */
94
95 /* enable don-demand statmount() call for all filesystems in the table */
96 mnt_table_refer_statmnt(tb, sm);
97
98 /* listmount() only */
99 gettimeofday(&start, NULL);
100 if (mnt_table_fetch_listmount(tb) != 0)
101 warn("failed to read mount table by listmount()");
102 gettimeofday(&end, NULL);
103 sec_lsmnt = time_diff(&end, &start);
104
105 /* force statmount() for all nodes */
106 fetch_data(tb, itr);
107 gettimeofday(&end, NULL);
108 sec_lsstmnt = time_diff(&end, &start);
109
110 fprintf(stdout, "listmount() based table:\n");
111 print_table(tb, itr);
112
113
114 /* disable statmount() and listmount(); reset table */
115 mnt_statmnt_disable_fetching(sm, 1);
116 mnt_table_enable_listmount(tb, 0);
117 mnt_reset_table(tb);
118
119 /*
120 * B) /proc/self/mountinfo based table
121 */
122 gettimeofday(&start, NULL);
123 mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO);
124 gettimeofday(&end, NULL);
125 sec_mountinfo = time_diff(&end, &start);
126
127 fprintf(stdout, "\nprocfs based table:\n");
128 print_table(tb, itr);
129
130 fprintf(stdout, "\n"
131 "%f sec listmount()\n"
132 "%f sec listmount()+statmount()\n"
133 "%f sec /proc/sef/mountinfo"
134 "\n\n",
135 sec_lsmnt, sec_lsstmnt, sec_mountinfo);
136
137
138 mnt_reset_table(tb);
139
140 /*
141 * C) Instead of reading the entire mount table with one listmount() call, read
142 * it in smaller steps. This is particularly useful for systems with large
143 * mount tables, where we may only need to access one specific node (usually the
144 * last one).
145 *
146 * By default, libmount reads 512 nodes in one call. To test this sample
147 * on normal systems, we will reduce this number to 5 nodes. The listmount()
148 * function is used as a backend for regular mnt_table_next_fs().
149 * There is no need to make any changes to the application, just enable
150 * on-demand listmount() by using mnt_table_enable_listmount().
151 */
152 if (mnt_table_listmount_set_stepsiz(tb, 5) != 0)
153 warn("failed to initialize listmount()");
154 mnt_table_enable_listmount(tb, 1);
155
156 /* enable also statmount() */
157 mnt_statmnt_disable_fetching(sm, 0);
158
159 fprintf(stdout, "\nlistmount() - small steps (reverse):\n");
160 print_table_reverse(tb, itr);
161
162
163 mnt_unref_table(tb);
164 mnt_unref_statmnt(sm);
165 mnt_free_iter(itr);
166
167 return EXIT_SUCCESS;
168 }