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