]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/mountP.h
libmount: remove fstab.d support
[thirdparty/util-linux.git] / libmount / src / mountP.h
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
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include "c.h"
21 #include "list.h"
22 #include "libmount.h"
23
24 /* features */
25 #define CONFIG_LIBMOUNT_ASSERT
26 #define CONFIG_LIBMOUNT_DEBUG
27
28 #ifdef CONFIG_LIBMOUNT_ASSERT
29 #include <assert.h>
30 #endif
31
32 /*
33 * Debug
34 */
35 #if defined(TEST_PROGRAM) && !defined(LIBMOUNT_DEBUG)
36 #define CONFIG_LIBMOUNT_DEBUG
37 #endif
38
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)
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)
49 #define MNT_DEBUG_DIFF (1 << 11)
50 #define MNT_DEBUG_ALL 0xFFFF
51
52 #ifdef CONFIG_LIBMOUNT_DEBUG
53 # include <stdio.h>
54 # include <stdarg.h>
55
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) { \
64 fprintf(stderr, "%d: libmount: %8s: ", getpid(), # m); \
65 x; \
66 } \
67 } while (0)
68
69 # define DBG_FLUSH do { \
70 if (libmount_debug_mask && \
71 libmount_debug_mask != MNT_DEBUG_INIT) \
72 fflush(stderr); \
73 } while(0)
74
75 extern int libmount_debug_mask;
76
77 static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
78 mnt_debug(const char *mesg, ...)
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
87 static inline void __attribute__ ((__format__ (__printf__, 2, 3)))
88 mnt_debug_h(void *handler, const char *mesg, ...)
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 */
100 # define ON_DBG(m,x) do { ; } while (0)
101 # define DBG(m,x) do { ; } while (0)
102 # define DBG_FLUSH do { ; } while(0)
103 #endif
104
105 /* extension for files in the directory */
106 #define MNT_MNTTABDIR_EXT ".fstab"
107
108 /* library private paths */
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"
114
115 #define MNT_UTAB_HEADER "# libmount utab file\n"
116
117 #ifdef TEST_PROGRAM
118 struct libmnt_test {
119 const char *name;
120 int (*body)(struct libmnt_test *ts, int argc, char *argv[]);
121 const char *usage;
122 };
123
124 /* test.c */
125 extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
126 #endif
127
128 /* utils.c */
129 extern int endswith(const char *s, const char *sx);
130 extern int startswith(const char *s, const char *sx);
131
132 extern int mnt_is_readonly(const char *path);
133
134 extern int mnt_parse_offset(const char *str, size_t len, uintmax_t *res);
135
136 extern int mnt_chdir_to_parent(const char *target, char **filename);
137 extern char *mnt_get_username(const uid_t uid);
138 extern int mnt_get_uid(const char *username, uid_t *uid);
139 extern int mnt_get_gid(const char *groupname, gid_t *gid);
140 extern int mnt_in_group(gid_t gid);
141
142 extern char *mnt_get_mountpoint(const char *path);
143 extern char *mnt_get_fs_root(const char *path, const char *mountpoint);
144 extern int mnt_open_uniq_filename(const char *filename, char **name);
145 extern int mnt_has_regular_utab(const char **utab, int *writable);
146 extern const char *mnt_get_utab_path(void);
147
148 extern int mnt_get_filesystems(char ***filesystems, const char *pattern);
149 extern void mnt_free_filesystems(char **filesystems);
150
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
157 /*
158 * Generic iterator
159 */
160 struct libmnt_iter {
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
183
184 /*
185 * This struct represents one entry in mtab/fstab/mountinfo file.
186 * (note that fstab[1] means the first column from fstab, and so on...)
187 */
188 struct libmnt_fs {
189 struct list_head ents;
190
191 int id; /* mountinfo[1]: ID */
192 int parent; /* mountinfo[2]: parent */
193 dev_t devno; /* mountinfo[3]: st_dev */
194
195 char *bindsrc; /* utab, full path from fstab[1] for bind mounts */
196
197 char *source; /* fstab[1], mountinfo[10]:
198 * source dev, file, dir or TAG */
199 char *tagname; /* fstab[1]: tag name - "LABEL", "UUID", ..*/
200 char *tagval; /* tag value */
201
202 char *root; /* mountinfo[4]: root of the mount within the FS */
203 char *target; /* mountinfo[5], fstab[2]: mountpoint */
204 char *fstype; /* mountinfo[9], fstab[3]: filesystem type */
205
206 char *optstr; /* fstab[4], merged options */
207 char *vfs_optstr; /* mountinfo[6]: fs-independent (VFS) options */
208 char *fs_optstr; /* mountinfo[11]: fs-dependent options */
209 char *user_optstr; /* userspace mount options */
210 char *attrs; /* mount attributes */
211
212 int freq; /* fstab[5]: dump frequency in days */
213 int passno; /* fstab[6]: pass number on parallel fsck */
214
215 int flags; /* MNT_FS_* flags */
216
217 void *userdata; /* library independent data */
218 };
219
220 /*
221 * fs flags
222 */
223 #define MNT_FS_PSEUDO (1 << 1) /* pseudo filesystem */
224 #define MNT_FS_NET (1 << 2) /* network filesystem */
225 #define MNT_FS_SWAP (1 << 3) /* swap device */
226 #define MNT_FS_KERNEL (1 << 4) /* data from /proc/{mounts,self/mountinfo} */
227 #define MNT_FS_MERGED (1 << 5) /* already merged data from /run/mount/utab */
228
229 #define mnt_fs_is_regular(_f) (!(mnt_fs_is_pseudofs(_f) \
230 || mnt_fs_is_netfs(_f) \
231 || mnt_fs_is_swaparea(_f)))
232
233 /*
234 * mtab/fstab/mountinfo file
235 */
236 struct libmnt_table {
237 int fmt; /* MNT_FMT_* file format */
238 int nents; /* number of valid entries */
239
240 struct libmnt_cache *cache; /* canonicalized paths/tags cache */
241
242 int (*errcb)(struct libmnt_table *tb,
243 const char *filename, int line);
244
245 struct list_head ents; /* list of entries (libmnt_fs) */
246 };
247
248 extern struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt);
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
261
262 /*
263 * Mount context -- high-level API
264 */
265 struct libmnt_context
266 {
267 int action; /* MNT_ACT_{MOUNT,UMOUNT} */
268 int restricted; /* root or not? */
269
270 char *fstype_pattern; /* for mnt_match_fstype() */
271 char *optstr_pattern; /* for mnt_match_options() */
272
273 struct libmnt_fs *fs; /* filesystem description (type, mountpoint, device, ...) */
274
275 struct libmnt_table *fstab; /* fstab (or mtab for some remounts) entries */
276 struct libmnt_table *mtab; /* mtab entries */
277
278 int (*table_errcb)(struct libmnt_table *tb, /* callback for libmnt_table structs */
279 const char *filename, int line);
280
281 char *(*pwd_get_cb)(struct libmnt_context *); /* get encryption password */
282 void (*pwd_release_cb)(struct libmnt_context *, char *); /* release password */
283
284 int optsmode; /* fstab optstr mode MNT_OPTSMODE_{AUTO,FORCE,IGNORE} */
285 int loopdev_fd; /* open loopdev */
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
292 struct libmnt_cache *cache; /* paths cache */
293 struct libmnt_lock *lock; /* mtab lock */
294 struct libmnt_update *update;/* mtab/utab update */
295
296 const char *mtab_path; /* path to mtab */
297 int mtab_writable; /* is mtab writable */
298
299 const char *utab_path; /* path to utab */
300 int utab_writable; /* is utab writable */
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 */
307 int helper_exec_status; /* 1: not called yet, 0: success, <0: -errno */
308
309 char *orig_user; /* original (non-fixed) user= option */
310
311 pid_t *children; /* "mount -a --fork" PIDs */
312 int nchildren; /* number of children */
313 pid_t pid; /* 0=parent; PID=child */
314
315
316 int syscall_status; /* 1: not called yet, 0: success, <0: -errno */
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)
329 #define MNT_FL_RDONLY_UMOUNT (1 << 11) /* remount,ro after EBUSY umount(2) */
330 #define MNT_FL_FORK (1 << 12)
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)
337 #define MNT_FL_TAB_APPLIED (1 << 21) /* mtab/fstab merged to cxt->fs */
338 #define MNT_FL_MOUNTFLAGS_MERGED (1 << 22) /* MS_* flags was read from optstr */
339 #define MNT_FL_SAVED_USER (1 << 23)
340 #define MNT_FL_PREPARED (1 << 24)
341 #define MNT_FL_HELPER (1 << 25) /* [u]mount.<type> */
342 #define MNT_FL_LOOPDEV_READY (1 << 26) /* /dev/loop<N> initialized by library */
343 #define MNT_FL_MOUNTOPTS_FIXED (1 << 27)
344
345 /* default flags */
346 #define MNT_FL_DEFAULT 0
347
348 /* lock.c */
349 extern int mnt_lock_use_simplelock(struct libmnt_lock *ml, int enable);
350
351 /* optmap.c */
352 extern const struct libmnt_optmap *mnt_optmap_get_entry(
353 struct libmnt_optmap const **maps,
354 int nmaps, const char *name,
355 size_t namelen,
356 const struct libmnt_optmap **mapent);
357
358 /* optstr.c */
359 extern int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end);
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);
364
365 /* fs.c */
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);
369
370 /* context.c */
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);
380
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
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
389 extern int mnt_fork_context(struct libmnt_context *cxt);
390
391 /* tab_update.c */
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);
395
396 #endif /* _LIBMOUNT_PRIVATE_H */