]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/mountP.h
dd16f8c96c97e0403ed18bae1305b92046341f8a
[thirdparty/util-linux.git] / libmount / src / mountP.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3 * mountP.h - private library header file
4 *
5 * This file is part of libmount from util-linux project.
6 *
7 * Copyright (C) 2008-2018 Karel Zak <kzak@redhat.com>
8 *
9 * libmount is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 */
14 #ifndef _LIBMOUNT_PRIVATE_H
15 #define _LIBMOUNT_PRIVATE_H
16
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/vfs.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "c.h"
28 #include "list.h"
29 #include "debug.h"
30 #include "libmount.h"
31
32 /*
33 * Debug
34 */
35 #define MNT_DEBUG_HELP (1 << 0)
36 #define MNT_DEBUG_INIT (1 << 1)
37 #define MNT_DEBUG_CACHE (1 << 2)
38 #define MNT_DEBUG_OPTIONS (1 << 3)
39 #define MNT_DEBUG_LOCKS (1 << 4)
40 #define MNT_DEBUG_TAB (1 << 5)
41 #define MNT_DEBUG_FS (1 << 6)
42 #define MNT_DEBUG_UPDATE (1 << 7)
43 #define MNT_DEBUG_UTILS (1 << 8)
44 #define MNT_DEBUG_CXT (1 << 9)
45 #define MNT_DEBUG_DIFF (1 << 10)
46 #define MNT_DEBUG_MONITOR (1 << 11)
47 #define MNT_DEBUG_BTRFS (1 << 12)
48 #define MNT_DEBUG_LOOP (1 << 13)
49
50 #define MNT_DEBUG_ALL 0xFFFF
51
52 UL_DEBUG_DECLARE_MASK(libmount);
53 #define DBG(m, x) __UL_DBG(libmount, MNT_DEBUG_, m, x)
54 #define ON_DBG(m, x) __UL_DBG_CALL(libmount, MNT_DEBUG_, m, x)
55 #define DBG_FLUSH __UL_DBG_FLUSH(libmount, MNT_DEBUG_)
56
57 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(libmount)
58 #include "debugobj.h"
59
60 /*
61 * NLS -- the library has to be independent on main program, so define
62 * UL_TEXTDOMAIN_EXPLICIT before you include nls.h.
63 *
64 * Now we use util-linux.po (=PACKAGE), rather than maintain the texts
65 * in the separate libmount.po file.
66 */
67 #define LIBMOUNT_TEXTDOMAIN PACKAGE
68 #define UL_TEXTDOMAIN_EXPLICIT LIBMOUNT_TEXTDOMAIN
69 #include "nls.h"
70
71
72 /* extension for files in the directory */
73 #define MNT_MNTTABDIR_EXT ".fstab"
74
75 /* library private paths */
76 #define MNT_RUNTIME_TOPDIR "/run"
77 #define MNT_RUNTIME_TOPDIR_OLD "/dev"
78
79 #define MNT_PATH_UTAB MNT_RUNTIME_TOPDIR "/mount/utab"
80 #define MNT_PATH_UTAB_OLD MNT_RUNTIME_TOPDIR_OLD "/.mount/utab"
81
82 #define MNT_UTAB_HEADER "# libmount utab file\n"
83
84 #ifdef TEST_PROGRAM
85 struct libmnt_test {
86 const char *name;
87 int (*body)(struct libmnt_test *ts, int argc, char *argv[]);
88 const char *usage;
89 };
90
91 /* test.c */
92 extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
93 #endif
94
95 /* utils.c */
96 extern int mnt_valid_tagname(const char *tagname);
97 extern int append_string(char **a, const char *b);
98
99 extern const char *mnt_statfs_get_fstype(struct statfs *vfs);
100 extern int is_procfs_fd(int fd);
101 extern int is_file_empty(const char *name);
102
103 extern int mnt_is_readonly(const char *path)
104 __attribute__((nonnull));
105
106 extern int mnt_parse_offset(const char *str, size_t len, uintmax_t *res);
107
108 extern int mnt_chdir_to_parent(const char *target, char **filename);
109
110 extern char *mnt_get_username(const uid_t uid);
111 extern int mnt_get_uid(const char *username, uid_t *uid);
112 extern int mnt_get_gid(const char *groupname, gid_t *gid);
113 extern int mnt_in_group(gid_t gid);
114
115 extern int mnt_open_uniq_filename(const char *filename, char **name);
116
117 extern int mnt_has_regular_utab(const char **utab, int *writable);
118 extern const char *mnt_get_utab_path(void);
119
120 extern int mnt_get_filesystems(char ***filesystems, const char *pattern);
121 extern void mnt_free_filesystems(char **filesystems);
122
123 extern char *mnt_get_kernel_cmdline_option(const char *name);
124 extern int mnt_stat_mountpoint(const char *target, struct stat *st);
125 extern FILE *mnt_get_procfs_memstream(int fd, char **membuf);
126
127 /* tab.c */
128 extern int is_mountinfo(struct libmnt_table *tb);
129 extern int mnt_table_set_parser_fltrcb( struct libmnt_table *tb,
130 int (*cb)(struct libmnt_fs *, void *),
131 void *data);
132
133 extern int __mnt_table_parse_mtab(struct libmnt_table *tb,
134 const char *filename,
135 struct libmnt_table *u_tb);
136
137 extern struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb,
138 struct libmnt_fs *fs,
139 unsigned long mountflags,
140 char **fsroot);
141
142 extern int __mnt_table_is_fs_mounted( struct libmnt_table *tb,
143 struct libmnt_fs *fstab_fs,
144 const char *tgt_prefix);
145
146 /*
147 * Generic iterator
148 */
149 struct libmnt_iter {
150 struct list_head *p; /* current position */
151 struct list_head *head; /* start position */
152 int direction; /* MNT_ITER_{FOR,BACK}WARD */
153 };
154
155 #define IS_ITER_FORWARD(_i) ((_i)->direction == MNT_ITER_FORWARD)
156 #define IS_ITER_BACKWARD(_i) ((_i)->direction == MNT_ITER_BACKWARD)
157
158 #define MNT_ITER_INIT(itr, list) \
159 do { \
160 (itr)->p = IS_ITER_FORWARD(itr) ? \
161 (list)->next : (list)->prev; \
162 (itr)->head = (list); \
163 } while(0)
164
165 #define MNT_ITER_ITERATE(itr, res, restype, member) \
166 do { \
167 res = list_entry((itr)->p, restype, member); \
168 (itr)->p = IS_ITER_FORWARD(itr) ? \
169 (itr)->p->next : (itr)->p->prev; \
170 } while(0)
171
172
173 /*
174 * This struct represents one entry in a mtab/fstab/mountinfo file.
175 * (note that fstab[1] means the first column from fstab, and so on...)
176 */
177 struct libmnt_fs {
178 struct list_head ents;
179 struct libmnt_table *tab;
180
181 int refcount; /* reference counter */
182 int id; /* mountinfo[1]: ID */
183 int parent; /* mountinfo[2]: parent */
184 dev_t devno; /* mountinfo[3]: st_dev */
185
186 char *bindsrc; /* utab, full path from fstab[1] for bind mounts */
187
188 char *source; /* fstab[1], mountinfo[10], swaps[1]:
189 * source dev, file, dir or TAG */
190 char *tagname; /* fstab[1]: tag name - "LABEL", "UUID", ..*/
191 char *tagval; /* tag value */
192
193 char *root; /* mountinfo[4]: root of the mount within the FS */
194 char *target; /* mountinfo[5], fstab[2]: mountpoint */
195 char *fstype; /* mountinfo[9], fstab[3]: filesystem type */
196
197 char *optstr; /* fstab[4], merged options */
198 char *vfs_optstr; /* mountinfo[6]: fs-independent (VFS) options */
199 char *opt_fields; /* mountinfo[7]: optional fields */
200 char *fs_optstr; /* mountinfo[11]: fs-dependent options */
201 char *user_optstr; /* userspace mount options */
202 char *attrs; /* mount attributes */
203
204 int freq; /* fstab[5]: dump frequency in days */
205 int passno; /* fstab[6]: pass number on parallel fsck */
206
207 /* /proc/swaps */
208 char *swaptype; /* swaps[2]: device type (partition, file, ...) */
209 off_t size; /* swaps[3]: swaparea size */
210 off_t usedsize; /* swaps[4]: used size */
211 int priority; /* swaps[5]: swap priority */
212
213 int flags; /* MNT_FS_* flags */
214 pid_t tid; /* /proc/<tid>/mountinfo otherwise zero */
215
216 char *comment; /* fstab comment */
217
218 void *userdata; /* library independent data */
219 };
220
221 /*
222 * fs flags
223 */
224 #define MNT_FS_PSEUDO (1 << 1) /* pseudo filesystem */
225 #define MNT_FS_NET (1 << 2) /* network filesystem */
226 #define MNT_FS_SWAP (1 << 3) /* swap device */
227 #define MNT_FS_KERNEL (1 << 4) /* data from /proc/{mounts,self/mountinfo} */
228 #define MNT_FS_MERGED (1 << 5) /* already merged data from /run/mount/utab */
229
230 #define mnt_fs_is_regular(_f) (!(mnt_fs_is_pseudofs(_f) \
231 || mnt_fs_is_netfs(_f) \
232 || mnt_fs_is_swaparea(_f)))
233
234 /*
235 * mtab/fstab/mountinfo file
236 */
237 struct libmnt_table {
238 int fmt; /* MNT_FMT_* file format */
239 int nents; /* number of entries */
240 int refcount; /* reference counter */
241 int comms; /* enable/disable comment parsing */
242 char *comm_intro; /* First comment in file */
243 char *comm_tail; /* Last comment in file */
244
245 struct libmnt_cache *cache; /* canonicalized paths/tags cache */
246
247 int (*errcb)(struct libmnt_table *tb,
248 const char *filename, int line);
249
250 int (*fltrcb)(struct libmnt_fs *fs, void *data);
251 void *fltrcb_data;
252
253
254 struct list_head ents; /* list of entries (libmnt_fs) */
255 void *userdata;
256 };
257
258 extern struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt, int empty_for_enoent);
259
260 /*
261 * Tab file format
262 */
263 enum {
264 MNT_FMT_GUESS,
265 MNT_FMT_FSTAB, /* /etc/{fs,m}tab */
266 MNT_FMT_MTAB = MNT_FMT_FSTAB, /* alias */
267 MNT_FMT_MOUNTINFO, /* /proc/#/mountinfo */
268 MNT_FMT_UTAB, /* /run/mount/utab */
269 MNT_FMT_SWAPS /* /proc/swaps */
270 };
271
272 /*
273 * Additional mounts
274 */
275 struct libmnt_addmount {
276 unsigned long mountflags;
277
278 struct list_head mounts;
279 };
280
281 struct libmnt_ns {
282 int fd; /* file descriptor of namespace, -1 when inactive */
283 struct libmnt_cache *cache; /* paths cache associated with NS */
284 };
285
286 /*
287 * Mount context -- high-level API
288 */
289 struct libmnt_context
290 {
291 int action; /* MNT_ACT_{MOUNT,UMOUNT} */
292 int restricted; /* root or not? */
293
294 char *fstype_pattern; /* for mnt_match_fstype() */
295 char *optstr_pattern; /* for mnt_match_options() */
296
297 struct libmnt_fs *fs; /* filesystem description (type, mountpoint, device, ...) */
298 struct libmnt_fs *fs_template; /* used for @fs on mnt_reset_context() */
299
300 struct libmnt_table *fstab; /* fstab (or mtab for some remounts) entries */
301 struct libmnt_table *mtab; /* mtab entries */
302 struct libmnt_table *utab; /* rarely used by umount only */
303
304 int (*table_errcb)(struct libmnt_table *tb, /* callback for libmnt_table structs */
305 const char *filename, int line);
306
307 int (*table_fltrcb)(struct libmnt_fs *fs, void *data); /* callback for libmnt_table structs */
308 void *table_fltrcb_data;
309
310 char *(*pwd_get_cb)(struct libmnt_context *); /* get encryption password */
311 void (*pwd_release_cb)(struct libmnt_context *, char *); /* release password */
312
313 int optsmode; /* fstab optstr mode MNT_OPTSMODE_{AUTO,FORCE,IGNORE} */
314 int loopdev_fd; /* open loopdev */
315
316 unsigned long mountflags; /* final mount(2) flags */
317 const void *mountdata; /* final mount(2) data, string or binary data */
318
319 unsigned long user_mountflags; /* MNT_MS_* (loop=, user=, ...) */
320
321 struct list_head addmounts; /* additional mounts */
322
323 struct libmnt_cache *cache; /* paths cache */
324 struct libmnt_lock *lock; /* mtab lock */
325 struct libmnt_update *update;/* mtab/utab update */
326
327 const char *mtab_path; /* path to mtab */
328 int mtab_writable; /* is mtab writable */
329
330 const char *utab_path; /* path to utab */
331 int utab_writable; /* is utab writable */
332
333 char *tgt_prefix; /* path used for all targets */
334
335 int flags; /* private context flags */
336
337 char *helper; /* name of the used /sbin/[u]mount.<type> helper */
338 int helper_status; /* helper wait(2) status */
339 int helper_exec_status; /* 1: not called yet, 0: success, <0: -errno */
340
341 char *orig_user; /* original (non-fixed) user= option */
342
343 pid_t *children; /* "mount -a --fork" PIDs */
344 int nchildren; /* number of children */
345 pid_t pid; /* 0=parent; PID=child */
346
347
348 int syscall_status; /* 1: not called yet, 0: success, <0: -errno */
349
350 struct libmnt_ns ns_orig; /* original namespace */
351 struct libmnt_ns ns_tgt; /* target namespace */
352 struct libmnt_ns *ns_cur; /* pointer to current namespace */
353
354 unsigned int enabled_textdomain : 1; /* bindtextdomain() called */
355 };
356
357 /* flags */
358 #define MNT_FL_NOMTAB (1 << 1)
359 #define MNT_FL_FAKE (1 << 2)
360 #define MNT_FL_SLOPPY (1 << 3)
361 #define MNT_FL_VERBOSE (1 << 4)
362 #define MNT_FL_NOHELPERS (1 << 5)
363 #define MNT_FL_LOOPDEL (1 << 6)
364 #define MNT_FL_LAZY (1 << 7)
365 #define MNT_FL_FORCE (1 << 8)
366 #define MNT_FL_NOCANONICALIZE (1 << 9)
367 #define MNT_FL_RDONLY_UMOUNT (1 << 11) /* remount,ro after EBUSY umount(2) */
368 #define MNT_FL_FORK (1 << 12)
369 #define MNT_FL_NOSWAPMATCH (1 << 13)
370 #define MNT_FL_RWONLY_MOUNT (1 << 14) /* explicit mount -w; never try read-only */
371
372 #define MNT_FL_MOUNTDATA (1 << 20)
373 #define MNT_FL_TAB_APPLIED (1 << 21) /* mtab/fstab merged to cxt->fs */
374 #define MNT_FL_MOUNTFLAGS_MERGED (1 << 22) /* MS_* flags was read from optstr */
375 #define MNT_FL_SAVED_USER (1 << 23)
376 #define MNT_FL_PREPARED (1 << 24)
377 #define MNT_FL_HELPER (1 << 25) /* [u]mount.<type> */
378 #define MNT_FL_LOOPDEV_READY (1 << 26) /* /dev/loop<N> initialized by the library */
379 #define MNT_FL_MOUNTOPTS_FIXED (1 << 27)
380 #define MNT_FL_TABPATHS_CHECKED (1 << 28)
381 #define MNT_FL_FORCED_RDONLY (1 << 29) /* mounted read-only on write-protected device */
382
383 /* default flags */
384 #define MNT_FL_DEFAULT 0
385
386 /* Flags usable with MS_BIND|MS_REMOUNT */
387 #define MNT_BIND_SETTABLE (MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOATIME|MS_NODIRATIME|MS_RELATIME|MS_RDONLY)
388
389 /* lock.c */
390 extern int mnt_lock_use_simplelock(struct libmnt_lock *ml, int enable);
391
392 /* optmap.c */
393 extern const struct libmnt_optmap *mnt_optmap_get_entry(
394 struct libmnt_optmap const **maps,
395 int nmaps,
396 const char *name,
397 size_t namelen,
398 const struct libmnt_optmap **mapent);
399
400 /* optstr.c */
401 extern int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end);
402 extern int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next);
403 extern int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next);
404 extern int mnt_optstr_fix_secontext(char **optstr, char *value, size_t valsz, char **next);
405 extern int mnt_optstr_fix_user(char **optstr);
406
407 /* fs.c */
408 extern struct libmnt_fs *mnt_copy_mtab_fs(const struct libmnt_fs *fs)
409 __attribute__((nonnull));
410 extern int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source)
411 __attribute__((nonnull(1)));
412 extern int __mnt_fs_set_fstype_ptr(struct libmnt_fs *fs, char *fstype)
413 __attribute__((nonnull(1)));
414
415 /* context.c */
416 extern struct libmnt_context *mnt_copy_context(struct libmnt_context *o);
417 extern int mnt_context_mtab_writable(struct libmnt_context *cxt);
418 extern int mnt_context_utab_writable(struct libmnt_context *cxt);
419 extern const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt);
420
421 extern int mnt_context_get_mtab_for_target(struct libmnt_context *cxt,
422 struct libmnt_table **mtab, const char *tgt);
423
424 extern int mnt_context_prepare_srcpath(struct libmnt_context *cxt);
425 extern int mnt_context_prepare_target(struct libmnt_context *cxt);
426 extern int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type);
427 extern int mnt_context_guess_fstype(struct libmnt_context *cxt);
428 extern int mnt_context_prepare_helper(struct libmnt_context *cxt,
429 const char *name, const char *type);
430 extern int mnt_context_prepare_update(struct libmnt_context *cxt);
431 extern int mnt_context_merge_mflags(struct libmnt_context *cxt);
432 extern int mnt_context_update_tabs(struct libmnt_context *cxt);
433
434 extern int mnt_context_umount_setopt(struct libmnt_context *cxt, int c, char *arg);
435 extern int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg);
436
437 extern int mnt_context_is_loopdev(struct libmnt_context *cxt)
438 __attribute__((nonnull));
439
440 extern int mnt_context_propagation_only(struct libmnt_context *cxt)
441 __attribute__((nonnull));
442
443 extern struct libmnt_addmount *mnt_new_addmount(void);
444 extern void mnt_free_addmount(struct libmnt_addmount *ad);
445
446 extern int mnt_context_setup_loopdev(struct libmnt_context *cxt);
447 extern int mnt_context_delete_loopdev(struct libmnt_context *cxt);
448 extern int mnt_context_clear_loopdev(struct libmnt_context *cxt);
449
450 extern int mnt_fork_context(struct libmnt_context *cxt);
451
452 extern int mnt_context_set_tabfilter(struct libmnt_context *cxt,
453 int (*fltr)(struct libmnt_fs *, void *),
454 void *data);
455
456 extern int mnt_context_get_generic_excode(int rc, char *buf, size_t bufsz, char *fmt, ...);
457 extern int mnt_context_get_mount_excode(struct libmnt_context *cxt, int mntrc, char *buf, size_t bufsz);
458 extern int mnt_context_get_umount_excode(struct libmnt_context *cxt, int mntrc, char *buf, size_t bufsz);
459
460 extern int mnt_context_has_template(struct libmnt_context *cxt);
461 extern int mnt_context_apply_template(struct libmnt_context *cxt);
462 extern int mnt_context_save_template(struct libmnt_context *cxt);
463
464 extern int mnt_context_apply_fs(struct libmnt_context *cxt, struct libmnt_fs *fs);
465
466 /* tab_update.c */
467 extern int mnt_update_set_filename(struct libmnt_update *upd,
468 const char *filename, int userspace_only);
469 extern int mnt_update_already_done(struct libmnt_update *upd,
470 struct libmnt_lock *lc);
471
472 #if __linux__
473 /* btrfs.c */
474 extern uint64_t btrfs_get_default_subvol_id(const char *path);
475 #endif
476
477 #endif /* _LIBMOUNT_PRIVATE_H */