]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libmount/src/mountP.h
libmount: fix memory overflow [AddressSanitizer]
[thirdparty/util-linux.git] / libmount / src / mountP.h
CommitLineData
f4ab4ae8
KZ
1/*
2 * mountP.h - private library header file
3 *
90cd46cb 4 * Copyright (C) 2008-2012 Karel Zak <kzak@redhat.com>
f4ab4ae8
KZ
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 */
9
10#ifndef _LIBMOUNT_PRIVATE_H
11#define _LIBMOUNT_PRIVATE_H
12
abc3d154 13#include <errno.h>
486d3972
KZ
14#include <stdlib.h>
15#include <string.h>
16#include <sys/stat.h>
17#include <sys/types.h>
6a52473e 18#include <sys/vfs.h>
486d3972 19#include <unistd.h>
83a78332
KZ
20#include <stdio.h>
21#include <stdarg.h>
cc492b3d 22
486d3972 23#include "c.h"
efab4b61 24#include "list.h"
cedd8e5f 25#include "debug.h"
486d3972 26#include "libmount.h"
69b7e41e 27
f4ab4ae8
KZ
28/* features */
29#define CONFIG_LIBMOUNT_ASSERT
30
31#ifdef CONFIG_LIBMOUNT_ASSERT
4569bbea
KZ
32# include <assert.h>
33#else
34# define assert(x)
f4ab4ae8
KZ
35#endif
36
c12cec75
KZ
37/*
38 * Debug
39 */
35333416 40#define MNT_DEBUG_HELP (1 << 0)
3f31a959
KZ
41#define MNT_DEBUG_INIT (1 << 1)
42#define MNT_DEBUG_CACHE (1 << 2)
43#define MNT_DEBUG_OPTIONS (1 << 3)
44#define MNT_DEBUG_LOCKS (1 << 4)
45#define MNT_DEBUG_TAB (1 << 5)
4e92d2b0 46#define MNT_DEBUG_FS (1 << 6)
35333416
KZ
47#define MNT_DEBUG_UPDATE (1 << 7)
48#define MNT_DEBUG_UTILS (1 << 8)
49#define MNT_DEBUG_CXT (1 << 9)
50#define MNT_DEBUG_DIFF (1 << 10)
3f31a959 51#define MNT_DEBUG_ALL 0xFFFF
c12cec75 52
d7365821 53UL_DEBUG_DECLARE_MASK(libmount);
83a78332
KZ
54#define DBG(m, x) __UL_DBG(libmount, MNT_DEBUG_, m, x)
55#define ON_DBG(m, x) __UL_DBG_CALL(libmount, MNT_DEBUG_, m, x)
56#define DBG_FLUSH __UL_DBG_FLUSH(libmount, MNT_DEBUG_)
3f31a959 57
9354fea3 58/* extension for files in the directory */
9826a637
KZ
59#define MNT_MNTTABDIR_EXT ".fstab"
60
0532ba1d 61/* library private paths */
a362ae60
KZ
62#define MNT_RUNTIME_TOPDIR "/run"
63#define MNT_RUNTIME_TOPDIR_OLD "/dev"
64
65#define MNT_PATH_UTAB MNT_RUNTIME_TOPDIR "/mount/utab"
66#define MNT_PATH_UTAB_OLD MNT_RUNTIME_TOPDIR_OLD "/.mount/utab"
0532ba1d 67
dd369652
KZ
68#define MNT_UTAB_HEADER "# libmount utab file\n"
69
c12cec75 70#ifdef TEST_PROGRAM
68164f6c 71struct libmnt_test {
c12cec75 72 const char *name;
68164f6c 73 int (*body)(struct libmnt_test *ts, int argc, char *argv[]);
c12cec75
KZ
74 const char *usage;
75};
76
77417bc0 77/* test.c */
ba2bdf41 78extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
c12cec75
KZ
79#endif
80
69b7e41e 81/* utils.c */
2c6b25f0 82extern int mnt_valid_tagname(const char *tagname);
40b27864 83extern int append_string(char **a, const char *b);
2c6b25f0 84
6a52473e 85extern const char *mnt_statfs_get_fstype(struct statfs *vfs);
dad88cb3
KZ
86extern int is_file_empty(const char *name);
87
90cd46cb
OO
88extern int mnt_is_readonly(const char *path)
89 __attribute__((nonnull));
90cd46cb 90
ba2bdf41
KZ
91extern int mnt_parse_offset(const char *str, size_t len, uintmax_t *res);
92
93extern int mnt_chdir_to_parent(const char *target, char **filename);
b49103ed 94
69b7e41e 95extern char *mnt_get_username(const uid_t uid);
ba2bdf41
KZ
96extern int mnt_get_uid(const char *username, uid_t *uid);
97extern int mnt_get_gid(const char *groupname, gid_t *gid);
188dc15a 98extern int mnt_in_group(gid_t gid);
a1e8af75 99
ba2bdf41
KZ
100extern char *mnt_get_fs_root(const char *path, const char *mountpoint);
101extern int mnt_open_uniq_filename(const char *filename, char **name);
90cd46cb 102
77417bc0
KZ
103extern int mnt_has_regular_utab(const char **utab, int *writable);
104extern const char *mnt_get_utab_path(void);
0bb44be3 105
ba2bdf41 106extern int mnt_get_filesystems(char ***filesystems, const char *pattern);
0f32f1e2
KZ
107extern void mnt_free_filesystems(char **filesystems);
108
ba2bdf41 109extern char *mnt_get_kernel_cmdline_option(const char *name);
f308ec19 110
c4c66355 111/* tab.c */
4709c9e6
KZ
112extern int mnt_table_set_parser_fltrcb( struct libmnt_table *tb,
113 int (*cb)(struct libmnt_fs *, void *),
ba2bdf41 114 void *data);
4709c9e6 115
c4c66355
KZ
116extern struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb,
117 struct libmnt_fs *fs,
118 unsigned long mountflags,
ba2bdf41 119 char **fsroot);
6a52473e
KZ
120extern int __mnt_table_parse_mtab(struct libmnt_table *tb,
121 const char *filename,
122 struct libmnt_table *u_tb);
123
124
efab4b61
KZ
125/*
126 * Generic iterator
127 */
68164f6c 128struct libmnt_iter {
efab4b61
KZ
129 struct list_head *p; /* current position */
130 struct list_head *head; /* start position */
131 int direction; /* MNT_ITER_{FOR,BACK}WARD */
132};
133
134#define IS_ITER_FORWARD(_i) ((_i)->direction == MNT_ITER_FORWARD)
135#define IS_ITER_BACKWARD(_i) ((_i)->direction == MNT_ITER_BACKWARD)
136
137#define MNT_ITER_INIT(itr, list) \
138 do { \
139 (itr)->p = IS_ITER_FORWARD(itr) ? \
140 (list)->next : (list)->prev; \
141 (itr)->head = (list); \
142 } while(0)
143
144#define MNT_ITER_ITERATE(itr, res, restype, member) \
145 do { \
146 res = list_entry((itr)->p, restype, member); \
147 (itr)->p = IS_ITER_FORWARD(itr) ? \
148 (itr)->p->next : (itr)->p->prev; \
149 } while(0)
150
078edb2d 151
d115ee9b 152/*
d58b3157 153 * This struct represents one entry in a mtab/fstab/mountinfo file.
dd369652 154 * (note that fstab[1] means the first column from fstab, and so on...)
d115ee9b 155 */
68164f6c 156struct libmnt_fs {
d115ee9b
KZ
157 struct list_head ents;
158
26d0c0ae 159 int refcount; /* reference counter */
d115ee9b 160 int id; /* mountinfo[1]: ID */
6ad929bb
PU
161 int parent; /* mountinfo[2]: parent */
162 dev_t devno; /* mountinfo[3]: st_dev */
d115ee9b 163
dd369652
KZ
164 char *bindsrc; /* utab, full path from fstab[1] for bind mounts */
165
ce4dd666 166 char *source; /* fstab[1], mountinfo[10], swaps[1]:
d115ee9b
KZ
167 * source dev, file, dir or TAG */
168 char *tagname; /* fstab[1]: tag name - "LABEL", "UUID", ..*/
169 char *tagval; /* tag value */
170
0b3953a3 171 char *root; /* mountinfo[4]: root of the mount within the FS */
d115ee9b
KZ
172 char *target; /* mountinfo[5], fstab[2]: mountpoint */
173 char *fstype; /* mountinfo[9], fstab[3]: filesystem type */
174
f2b3a3a3 175 char *optstr; /* fstab[4], merged options */
d115ee9b 176 char *vfs_optstr; /* mountinfo[6]: fs-independent (VFS) options */
e47a1931 177 char *opt_fields; /* mountinfo[7]: optional fields */
6ad929bb 178 char *fs_optstr; /* mountinfo[11]: fs-dependent options */
76a06ca4
KZ
179 char *user_optstr; /* userspace mount options */
180 char *attrs; /* mount attributes */
d115ee9b 181
6ad929bb 182 int freq; /* fstab[5]: dump frequency in days */
d115ee9b
KZ
183 int passno; /* fstab[6]: pass number on parallel fsck */
184
ce4dd666
KZ
185 /* /proc/swaps */
186 char *swaptype; /* swaps[2]: device type (partition, file, ...) */
187 off_t size; /* swaps[3]: swaparea size */
188 off_t usedsize; /* swaps[4]: used size */
189 int priority; /* swaps[5]: swap priority */
190
3fca8422 191 int flags; /* MNT_FS_* flags */
f7f29b56 192 pid_t tid; /* /proc/<tid>/mountinfo otherwise zero */
26b4f9e4 193
cb90e24e
OO
194 char *comment; /* fstab comment */
195
26b4f9e4 196 void *userdata; /* library independent data */
d115ee9b
KZ
197};
198
199/*
200 * fs flags
201 */
911238af
KZ
202#define MNT_FS_PSEUDO (1 << 1) /* pseudo filesystem */
203#define MNT_FS_NET (1 << 2) /* network filesystem */
9dd75aa6 204#define MNT_FS_SWAP (1 << 3) /* swap device */
309139c7 205#define MNT_FS_KERNEL (1 << 4) /* data from /proc/{mounts,self/mountinfo} */
a362ae60 206#define MNT_FS_MERGED (1 << 5) /* already merged data from /run/mount/utab */
309139c7 207
c70d9d76
KZ
208#define mnt_fs_is_regular(_f) (!(mnt_fs_is_pseudofs(_f) \
209 || mnt_fs_is_netfs(_f) \
210 || mnt_fs_is_swaparea(_f)))
d115ee9b 211
6bd8b7a7
KZ
212/*
213 * mtab/fstab/mountinfo file
214 */
68164f6c 215struct libmnt_table {
6bd8b7a7 216 int fmt; /* MNT_FMT_* file format */
26d0c0ae 217 int nents; /* number of entries */
c9f1585e 218 int refcount; /* reference counter */
cb90e24e
OO
219 int comms; /* enable/disable comment parsing */
220 char *comm_intro; /* First comment in file */
221 char *comm_tail; /* Last comment in file */
6bd8b7a7 222
68164f6c 223 struct libmnt_cache *cache; /* canonicalized paths/tags cache */
6bd8b7a7 224
68164f6c
KZ
225 int (*errcb)(struct libmnt_table *tb,
226 const char *filename, int line);
9fd75d76 227
4709c9e6
KZ
228 int (*fltrcb)(struct libmnt_fs *fs, void *data);
229 void *fltrcb_data;
230
231
68164f6c 232 struct list_head ents; /* list of entries (libmnt_fs) */
d58b3157 233 void *userdata;
6bd8b7a7
KZ
234};
235
ba2bdf41 236extern struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt);
dd369652
KZ
237
238/*
239 * Tab file format
240 */
241enum {
242 MNT_FMT_GUESS,
243 MNT_FMT_FSTAB, /* /etc/{fs,m}tab */
244 MNT_FMT_MTAB = MNT_FMT_FSTAB, /* alias */
245 MNT_FMT_MOUNTINFO, /* /proc/#/mountinfo */
ce4dd666
KZ
246 MNT_FMT_UTAB, /* /run/mount/utab */
247 MNT_FMT_SWAPS /* /proc/swaps */
dd369652
KZ
248};
249
6498ece0
KZ
250/*
251 * Additional mounts
252 */
253struct libmnt_addmount {
254 unsigned long mountflags;
255
256 struct list_head mounts;
257};
6bd8b7a7 258
1bb1d80b
KZ
259/*
260 * Mount context -- high-level API
261 */
68164f6c 262struct libmnt_context
1bb1d80b
KZ
263{
264 int action; /* MNT_ACT_{MOUNT,UMOUNT} */
1bb1d80b
KZ
265 int restricted; /* root or not? */
266
267 char *fstype_pattern; /* for mnt_match_fstype() */
268 char *optstr_pattern; /* for mnt_match_options() */
269
6ad929bb 270 struct libmnt_fs *fs; /* filesystem description (type, mountpoint, device, ...) */
68164f6c 271
6ad929bb 272 struct libmnt_table *fstab; /* fstab (or mtab for some remounts) entries */
68164f6c 273 struct libmnt_table *mtab; /* mtab entries */
6a52473e 274 struct libmnt_table *utab; /* rarely used by umount only */
1bb1d80b 275
d84508cf
KZ
276 int (*table_errcb)(struct libmnt_table *tb, /* callback for libmnt_table structs */
277 const char *filename, int line);
278
4709c9e6
KZ
279 int (*table_fltrcb)(struct libmnt_fs *fs, void *data); /* callback for libmnt_table structs */
280 void *table_fltrcb_data;
281
1a7a421e
KZ
282 char *(*pwd_get_cb)(struct libmnt_context *); /* get encryption password */
283 void (*pwd_release_cb)(struct libmnt_context *, char *); /* release password */
284
1bb1d80b 285 int optsmode; /* fstab optstr mode MNT_OPTSMODE_{AUTO,FORCE,IGNORE} */
7f8b2bf3 286 int loopdev_fd; /* open loopdev */
1bb1d80b
KZ
287
288 unsigned long mountflags; /* final mount(2) flags */
289 const void *mountdata; /* final mount(2) data, string or binary data */
290
291 unsigned long user_mountflags; /* MNT_MS_* (loop=, user=, ...) */
292
6498ece0
KZ
293 struct list_head addmounts; /* additional mounts */
294
68164f6c
KZ
295 struct libmnt_cache *cache; /* paths cache */
296 struct libmnt_lock *lock; /* mtab lock */
297 struct libmnt_update *update;/* mtab/utab update */
1d0cd73f 298
6ad929bb
PU
299 const char *mtab_path; /* path to mtab */
300 int mtab_writable; /* is mtab writable */
1d0cd73f 301
6ad929bb
PU
302 const char *utab_path; /* path to utab */
303 int utab_writable; /* is utab writable */
1bb1d80b
KZ
304
305 int flags; /* private context flags */
1bb1d80b
KZ
306
307 char *helper; /* name of the used /sbin/[u]mount.<type> helper */
308 int helper_status; /* helper wait(2) status */
97e23b5e 309 int helper_exec_status; /* 1: not called yet, 0: success, <0: -errno */
1bb1d80b
KZ
310
311 char *orig_user; /* original (non-fixed) user= option */
312
d2c97887
KZ
313 pid_t *children; /* "mount -a --fork" PIDs */
314 int nchildren; /* number of children */
315 pid_t pid; /* 0=parent; PID=child */
316
317
97e23b5e 318 int syscall_status; /* 1: not called yet, 0: success, <0: -errno */
1bb1d80b
KZ
319};
320
321/* flags */
322#define MNT_FL_NOMTAB (1 << 1)
323#define MNT_FL_FAKE (1 << 2)
324#define MNT_FL_SLOPPY (1 << 3)
325#define MNT_FL_VERBOSE (1 << 4)
326#define MNT_FL_NOHELPERS (1 << 5)
327#define MNT_FL_LOOPDEL (1 << 6)
328#define MNT_FL_LAZY (1 << 7)
329#define MNT_FL_FORCE (1 << 8)
330#define MNT_FL_NOCANONICALIZE (1 << 9)
ea8f06f9 331#define MNT_FL_RDONLY_UMOUNT (1 << 11) /* remount,ro after EBUSY umount(2) */
d2c97887 332#define MNT_FL_FORK (1 << 12)
e39cbb76 333#define MNT_FL_NOSWAPMATCH (1 << 13)
1bb1d80b 334
1bb1d80b 335#define MNT_FL_MOUNTDATA (1 << 20)
cf94e97f 336#define MNT_FL_TAB_APPLIED (1 << 21) /* mtab/fstab merged to cxt->fs */
1bb1d80b
KZ
337#define MNT_FL_MOUNTFLAGS_MERGED (1 << 22) /* MS_* flags was read from optstr */
338#define MNT_FL_SAVED_USER (1 << 23)
dbde1923 339#define MNT_FL_PREPARED (1 << 24)
8c0797e7 340#define MNT_FL_HELPER (1 << 25) /* [u]mount.<type> */
d58b3157 341#define MNT_FL_LOOPDEV_READY (1 << 26) /* /dev/loop<N> initialized by the library */
dc4dbbf1 342#define MNT_FL_MOUNTOPTS_FIXED (1 << 27)
150e696d 343#define MNT_FL_TABPATHS_CHECKED (1 << 28)
1bb1d80b
KZ
344
345/* default flags */
346#define MNT_FL_DEFAULT 0
347
5eb00eb4 348/* lock.c */
ba2bdf41 349extern int mnt_lock_use_simplelock(struct libmnt_lock *ml, int enable);
5eb00eb4 350
078edb2d 351/* optmap.c */
68164f6c
KZ
352extern const struct libmnt_optmap *mnt_optmap_get_entry(
353 struct libmnt_optmap const **maps,
90cd46cb
OO
354 int nmaps,
355 const char *name,
68164f6c 356 size_t namelen,
ba2bdf41 357 const struct libmnt_optmap **mapent);
078edb2d 358
188dc15a 359/* optstr.c */
ba2bdf41
KZ
360extern int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end);
361extern int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next);
362extern int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next);
363extern int mnt_optstr_fix_secontext(char **optstr, char *value, size_t valsz, char **next);
364extern int mnt_optstr_fix_user(char **optstr);
188dc15a 365
d115ee9b 366/* fs.c */
90cd46cb
OO
367extern struct libmnt_fs *mnt_copy_mtab_fs(const struct libmnt_fs *fs)
368 __attribute__((nonnull));
369extern int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source)
370 __attribute__((nonnull(1)));
371extern int __mnt_fs_set_fstype_ptr(struct libmnt_fs *fs, char *fstype)
372 __attribute__((nonnull(1)));
078edb2d 373
1bb1d80b 374/* context.c */
150e696d
KZ
375extern int mnt_context_mtab_writable(struct libmnt_context *cxt);
376extern int mnt_context_utab_writable(struct libmnt_context *cxt);
377extern const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt);
378
ba2bdf41
KZ
379extern int mnt_context_prepare_srcpath(struct libmnt_context *cxt);
380extern int mnt_context_prepare_target(struct libmnt_context *cxt);
b1f03df7 381extern int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type);
ba2bdf41 382extern int mnt_context_guess_fstype(struct libmnt_context *cxt);
68164f6c 383extern int mnt_context_prepare_helper(struct libmnt_context *cxt,
ba2bdf41
KZ
384 const char *name, const char *type);
385extern int mnt_context_prepare_update(struct libmnt_context *cxt);
386extern int mnt_context_merge_mflags(struct libmnt_context *cxt);
387extern int mnt_context_update_tabs(struct libmnt_context *cxt);
90cd46cb 388
ba2bdf41
KZ
389extern int mnt_context_umount_setopt(struct libmnt_context *cxt, int c, char *arg);
390extern int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg);
90cd46cb
OO
391
392extern int mnt_context_is_loopdev(struct libmnt_context *cxt)
393 __attribute__((nonnull));
ba2bdf41 394
6498ece0
KZ
395extern int mnt_context_propagation_only(struct libmnt_context *cxt)
396 __attribute__((nonnull));
397
398extern struct libmnt_addmount *mnt_new_addmount(void);
399extern void mnt_free_addmount(struct libmnt_addmount *ad);
400
ba2bdf41
KZ
401extern int mnt_context_setup_loopdev(struct libmnt_context *cxt);
402extern int mnt_context_delete_loopdev(struct libmnt_context *cxt);
403extern int mnt_context_clear_loopdev(struct libmnt_context *cxt);
7f8b2bf3 404
d2c97887
KZ
405extern int mnt_fork_context(struct libmnt_context *cxt);
406
4709c9e6 407extern int mnt_context_set_tabfilter(struct libmnt_context *cxt,
90cd46cb 408 int (*fltr)(struct libmnt_fs *, void *),
ba2bdf41 409 void *data);
4709c9e6 410
97e23b5e 411/* tab_update.c */
68164f6c 412extern int mnt_update_set_filename(struct libmnt_update *upd,
ba2bdf41 413 const char *filename, int userspace_only);
7e0c0619
KZ
414extern int mnt_update_already_done(struct libmnt_update *upd,
415 struct libmnt_lock *lc);
97e23b5e 416
078edb2d 417#endif /* _LIBMOUNT_PRIVATE_H */