2
* Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
4
* This file may be redistributed under the terms of the
5
* GNU Lesser General Public License.
10
* @title: Options string
11
* @short_description: low-level API for work with mount options
13
* This is simple and low-level API to work with mount options that are stored
14
* in string. This API is independent on the high-level options container and
22
#include <sys/types.h>
25
#ifdef HAVE_LIBSELINUX
26
#include <selinux/selinux.h>
27
#include <selinux/context.h>
36
struct libmnt_optloc {
44
#define mnt_init_optloc(_ol) (memset((_ol), 0, sizeof(struct libmnt_optloc)))
47
* Parses the first option from @optstr. The @optstr pointer is set to begin of
50
* Returns -EINVAL on parse error, 1 at the end of optstr and 0 on success.
52
static int mnt_optstr_parse_next(char **optstr, char **name, size_t *namesz,
53
char **value, size_t *valsz)
56
char *start = NULL, *stop = NULL, *p, *sep = NULL;
73
for (p = optstr0; p && *p; p++) {
75
start = p; /* begin of the option item */
77
open_quote ^= 1; /* reverse the status */
79
continue; /* still in quoted block */
80
if (!sep && *p == '=')
81
sep = p; /* name and value separator */
83
stop = p; /* terminate the option item */
84
else if (*(p + 1) == '\0')
85
stop = p + 1; /* end of optstr */
94
*namesz = sep ? sep - start : stop - start;
95
*optstr = *stop ? stop + 1 : stop;
101
*valsz = stop - sep - 1;
106
return 1; /* end of optstr */
109
DBG(OPTIONS, mnt_debug("parse error: \"%s\"", optstr0));
114
* Locates the first option that match with @name. The @end is set to
115
* char behind the option (it means ',' or \0).
117
* Returns negative number on parse error, 1 when not found and 0 on success.
119
static int mnt_optstr_locate_option(char *optstr, const char *name,
120
struct libmnt_optloc *ol)
132
namesz = strlen(name);
135
rc = mnt_optstr_parse_next(&optstr, &n, &nsz,
136
&ol->value, &ol->valsz);
140
if (namesz == nsz && strncmp(n, name, nsz) == 0) {
142
ol->end = *(optstr - 1) == ',' ? optstr - 1 : optstr;
152
* mnt_optstr_next_option:
153
* @optstr: option string, returns position to next option
154
* @name: returns option name
155
* @namesz: returns option name length
156
* @value: returns option value or NULL
157
* @valuesz: returns option value length or zero
159
* Parses the first option in @optstr.
161
* Returns: 0 on success, 1 at the end of @optstr or negative number in case of
164
int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz,
165
char **value, size_t *valuesz)
167
if (!optstr || !*optstr)
169
return mnt_optstr_parse_next(optstr, name, namesz, value, valuesz);
172
static int __mnt_optstr_append_option(char **optstr,
173
const char *name, size_t nsz,
174
const char *value, size_t vsz)
181
osz = *optstr ? strlen(*optstr) : 0;
183
sz = osz + nsz + 1; /* 1: '\0' */
185
sz++; /* ',' options separator */
187
sz += vsz + 1; /* 1: '=' */
189
p = realloc(*optstr, sz);
199
memcpy(p, name, nsz);
204
memcpy(p, value, vsz);
213
* mnt_optstr_append_option:
214
* @optstr: option string or NULL, returns reallocated string
218
* Returns: 0 on success or -1 in case of error. After error the @optstr should
221
int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
229
vsz = value ? strlen(value) : 0;
231
return __mnt_optstr_append_option(optstr, name, nsz, value, vsz);
235
* mnt_optstr_prepend_option:
236
* @optstr: option string or NULL, returns reallocated string
240
* Returns: 0 on success or -1 in case of error. After error the @optstr should
243
int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value)
250
rc = mnt_optstr_append_option(optstr, name, value);
251
if (!rc && tmp && *tmp)
252
rc = mnt_optstr_append_option(optstr, tmp, NULL);
261
DBG(OPTIONS, mnt_debug("failed to prepend '%s[=%s]' to '%s'",
262
name, value, *optstr));
267
* mnt_optstr_get_option:
268
* @optstr: string with comma separated list of options
269
* @name: requested option name
270
* @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL
271
* @valsz: returns size of the value or 0
273
* Returns: 0 on success, 1 when not found the @name or negative number in case
276
int mnt_optstr_get_option(char *optstr, const char *name,
277
char **value, size_t *valsz)
279
struct libmnt_optloc ol;
282
mnt_init_optloc(&ol);
284
rc = mnt_optstr_locate_option(optstr, name, &ol);
295
* The result never starts or ends with comma or contains two commas
296
* (e.g. ",aaa,bbb" or "aaa,,bbb" or "aaa,")
298
int mnt_optstr_remove_option_at(char **optstr, char *begin, char *end)
302
if (!optstr || !begin || !end)
305
if ((begin == *optstr || *(begin - 1) == ',') && *end == ',')
310
memmove(begin, end, sz + 1);
311
if (!*begin && *(begin - 1) == ',')
317
/* insert 'substr' or '=substr' to @str on position @pos */
318
static int insert_value(char **str, char *pos, const char *substr, char **next)
320
size_t subsz = strlen(substr); /* substring size */
321
size_t strsz = strlen(*str);
322
size_t possz = strlen(pos);
327
/* is it necessary to prepend '=' before the substring ? */
328
sep = !(pos > *str && *(pos - 1) == '=');
330
/* save an offset of the place where we need add substr */
333
p = realloc(*str, strsz + sep + subsz + 1);
337
/* zeroize new allocated memory -- valgind loves is... */
338
memset(p + strsz, 0, sep + subsz + 1);
340
/* set pointers to the reallocated string */
345
/* create a room for new substring */
346
memmove(pos + subsz + sep, pos, possz + 1);
350
memcpy(pos, substr, subsz);
353
/* set pointer to the next option */
354
*next = pos + subsz + sep + 1;
362
* mnt_optstr_set_option:
363
* @optstr: string with comma separated list of options
364
* @name: requested option
365
* @value: new value or NULL
367
* Set or unset option @value.
369
* Returns: 0 on success, 1 when not found the @name or negative number in case
372
int mnt_optstr_set_option(char **optstr, const char *name, const char *value)
374
struct libmnt_optloc ol;
381
mnt_init_optloc(&ol);
384
rc = mnt_optstr_locate_option(*optstr, name, &ol);
386
return rc; /* parse error */
388
return mnt_optstr_append_option(optstr, name, value); /* not found */
390
nameend = ol.begin + ol.namesz;
392
if (value == NULL && ol.value && ol.valsz)
393
/* remove unwanted "=value" */
394
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
396
else if (value && ol.value == NULL)
397
/* insert "=value" */
398
rc = insert_value(optstr, nameend, value, NULL);
400
else if (value && ol.value && strlen(value) == ol.valsz)
401
/* simply replace =value */
402
memcpy(ol.value, value, ol.valsz);
404
else if (value && ol.value) {
405
mnt_optstr_remove_option_at(optstr, nameend, ol.end);
406
rc = insert_value(optstr, nameend, value, NULL);
412
* mnt_optstr_remove_option:
413
* @optstr: string with comma separated list of options
414
* @name: requested option name
416
* Returns: 0 on success, 1 when not found the @name or negative number in case
419
int mnt_optstr_remove_option(char **optstr, const char *name)
421
struct libmnt_optloc ol;
424
mnt_init_optloc(&ol);
426
rc = mnt_optstr_locate_option(*optstr, name, &ol);
430
mnt_optstr_remove_option_at(optstr, ol.begin, ol.end);
436
* @optstr: string with comma separated list of options
437
* @user: returns newly allocated string with userspace options
438
* @vfs: returns newly allocated string with VFS options
439
* @fs: returns newly allocated string with FS options
440
* @ignore_user: option mask for options that should be ignored
441
* @ignore_vfs: option mask for options that should be ignored
445
* mnt_split_optstr(optstr, &u, NULL, NULL, MNT_NOMTAB, 0);
447
* returns all userspace options, the options that does not belong to
450
* Note that FS options are all options that are undefined in MNT_USERSPACE_MAP
453
* Returns: 0 on success, or negative number in case of error.
455
int mnt_split_optstr(const char *optstr, char **user, char **vfs,
456
char **fs, int ignore_user, int ignore_vfs)
458
char *name, *val, *str = (char *) optstr;
459
size_t namesz, valsz;
460
struct libmnt_optmap const *maps[2];
467
maps[0] = mnt_get_builtin_optmap(MNT_LINUX_MAP);
468
maps[1] = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
477
while(!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
479
const struct libmnt_optmap *ent = NULL;
480
const struct libmnt_optmap *m =
481
mnt_optmap_get_entry(maps, 2, name, namesz, &ent);
484
continue; /* ignore undefined options (comments) */
486
if (ent && m && m == maps[0] && vfs) {
487
if (ignore_vfs && (ent->mask & ignore_vfs))
489
rc = __mnt_optstr_append_option(vfs, name, namesz,
491
} else if (ent && m && m == maps[1] && user) {
492
if (ignore_user && (ent->mask & ignore_user))
494
rc = __mnt_optstr_append_option(user, name, namesz,
497
rc = __mnt_optstr_append_option(fs, name, namesz,
514
* mnt_optstr_get_options
515
* @optstr: string with comma separated list of options
516
* @subset: returns newly allocated string with options
518
* @ignore: mask of the options that should be ignored
520
* Extracts options from @optstr that belongs to the @map, for example:
522
* mnt_split_optstr_by_map(optstr, &p,
523
* mnt_get_builtin_optmap(MNT_LINUX_MAP),
526
* returns all VFS options, the options that does not belong to mtab
529
* Returns: 0 on success, or negative number in case of error.
531
int mnt_optstr_get_options(const char *optstr, char **subset,
532
const struct libmnt_optmap *map, int ignore)
534
struct libmnt_optmap const *maps[1];
535
char *name, *val, *str = (char *) optstr;
536
size_t namesz, valsz;
538
if (!optstr || !subset)
544
while(!mnt_optstr_next_option(&str, &name, &namesz, &val, &valsz)) {
546
const struct libmnt_optmap *ent;
548
mnt_optmap_get_entry(maps, 1, name, namesz, &ent);
550
if (!ent || !ent->id)
551
continue; /* ignore undefined options (comments) */
553
if (ignore && (ent->mask & ignore))
555
rc = __mnt_optstr_append_option(subset, name, namesz, val, valsz);
567
* mnt_optstr_get_flags:
568
* @optstr: string with comma separated list of options
569
* @flags: returns mount flags
572
* Returns in @flags IDs of options from @optstr as defined in the @map.
576
* "bind,exec,foo,bar" --returns-> MS_BIND
578
* "bind,noexec,foo,bar" --returns-> MS_BIND|MS_NOEXEC
580
* Note that @flags are not zeroized by this function! This function set/unset
581
* bites in the @flags only.
583
* Returns: 0 on success or negative number in case of error
585
int mnt_optstr_get_flags(const char *optstr, unsigned long *flags,
586
const struct libmnt_optmap *map)
588
struct libmnt_optmap const *maps[1];
589
char *name, *str = (char *) optstr;
594
if (!optstr || !flags || !map)
599
while(!mnt_optstr_next_option(&str, &name, &namesz, NULL, NULL)) {
600
const struct libmnt_optmap *ent;
602
if (mnt_optmap_get_entry(maps, 1, name, namesz, &ent)) {
605
if (ent->mask & MNT_INVERT)
616
* mnt_optstr_apply_flags:
617
* @optstr: string with comma separated list of options
618
* @flags: returns mount flags
621
* Removes/adds options to the @optstr according to flags. For example:
623
* MS_NOATIME and "foo,bar,noexec" --returns-> "foo,bar,noatime"
625
* Returns: 0 on success or negative number in case of error.
627
int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
628
const struct libmnt_optmap *map)
630
struct libmnt_optmap const *maps[1];
631
char *name, *next, *val;
632
size_t namesz = 0, valsz = 0;
641
DBG(CXT, mnt_debug("appling 0x%08lu flags '%s'", flags, *optstr));
648
* There is a convetion that 'rw/ro' flags is always at the begin of
649
* the string (athough the 'rw' is unnecessary).
651
if (map == mnt_get_builtin_optmap(MNT_LINUX_MAP)) {
652
const char *o = (fl & MS_RDONLY) ? "ro" : "rw";
655
(!strncmp(next, "rw", 2) || !strncmp(next, "ro", 2)) &&
656
(*(next + 2) == '\0' || *(next + 2) == ',')) {
658
/* already set, be paranoid and fix it */
661
rc = mnt_optstr_prepend_option(optstr, o, NULL);
664
next = *optstr; /* because realloc() */
674
* scan @optstr and remove options that are missing in
677
while(!mnt_optstr_next_option(&next, &name, &namesz,
679
const struct libmnt_optmap *ent;
681
if (mnt_optmap_get_entry(maps, 1, name, namesz, &ent)) {
683
* remove unwanted option (rw/ro is already set)
687
if (ent->id == MS_RDONLY ||
688
(ent->mask & MNT_INVERT) ||
691
char *end = val ? val + valsz :
694
rc = mnt_optstr_remove_option_at(
699
if (!(ent->mask & MNT_INVERT))
705
/* add missing options */
707
const struct libmnt_optmap *ent;
710
for (ent = map; ent && ent->name; ent++) {
711
if ((ent->mask & MNT_INVERT) || !(fl & ent->id))
714
/* don't add options which require values (e.g. offset=%d) */
715
p = strchr(ent->name, '=');
720
continue; /* name= */
722
p = strndup(ent->name, p - ent->name);
727
mnt_optstr_append_option(optstr, p, NULL);
730
mnt_optstr_append_option(optstr, ent->name, NULL);
736
DBG(CXT, mnt_debug("failed to apply flags [rc=%d]", rc));
741
* @optstr: string with comma separated list of options
742
* @value: pointer to the begin of the context value
743
* @valsz: size of the value
744
* @next: returns pointer to the next option (optional argument)
746
* Translates SELinux context from human to raw format. The function does not
747
* modify @optstr and returns zero if libmount is compiled without SELinux
750
* Returns: 0 on success, negative number in case of error.
752
int mnt_optstr_fix_secontext(char **optstr, char *value, size_t valsz, char **next)
756
#ifdef HAVE_LIBSELINUX
757
security_context_t raw = NULL;
758
char *p, *val, *begin, *end;
761
if (!optstr || !*optstr || !value || !valsz)
764
DBG(CXT, mnt_debug("fixing SELinux context"));
769
/* the selinux contexts are quoted */
771
if (valsz <= 2 || *(value + valsz - 1) != '"')
772
return -EINVAL; /* improperly quoted option string */
777
p = strndup(value, valsz);
782
/* translate the context */
783
rc = selinux_trans_to_raw_context((security_context_t) p, &raw);
785
DBG(CXT, mnt_debug("SELinux context '%s' translated to '%s'",
786
p, rc == -1 ? "FAILED" : (char *) raw));
789
if (rc == -1 || !raw)
793
/* create quoted string from the raw context */
794
sz = strlen((char *) raw);
798
p = val = malloc(valsz + 3);
810
/* set new context */
811
mnt_optstr_remove_option_at(optstr, begin, end);
812
rc = insert_value(optstr, begin, val, next);
818
static int set_uint_value(char **optstr, unsigned int num,
819
char *begin, char *end, char **next)
822
snprintf(buf, sizeof(buf), "%u", num);
824
mnt_optstr_remove_option_at(optstr, begin, end);
825
return insert_value(optstr, begin, buf, next);
829
* @optstr: string with comma separated list of options
830
* @value: pointer to the begin of the uid value
831
* @valsz: size of the value
832
* @next: returns pointer to the next option (optional argument)
834
* Translates "<username>" or "useruid" to the real UID.
837
* if (!mnt_optstr_get_option(optstr, "uid", &val, &valsz))
838
* mnt_optstr_fix_uid(&optstr, val, valsz, NULL);
840
* Returns: 0 on success, negative number in case of error.
842
int mnt_optstr_fix_uid(char **optstr, char *value, size_t valsz, char **next)
847
if (!optstr || !*optstr || !value || !valsz)
850
DBG(CXT, mnt_debug("fixing uid"));
854
if (valsz == 7 && !strncmp(value, "useruid", 7) &&
855
(*(value + 7) == ',' || !*(value + 7)))
856
rc = set_uint_value(optstr, getuid(), value, end, next);
858
else if (!isdigit(*value)) {
860
char *p = strndup(value, valsz);
863
rc = mnt_get_uid(p, &id);
867
rc = set_uint_value(optstr, id, value, end, next);
871
*next = value + valsz;
880
* @optstr: string with comma separated list of options
881
* @value: pointer to the begin of the uid value
882
* @valsz: size of the value
883
* @next: returns pointer to the next option (optional argument)
885
* Translates "<groupname>" or "usergid" to the real GID.
887
* Returns: 0 on success, negative number in case of error.
889
int mnt_optstr_fix_gid(char **optstr, char *value, size_t valsz, char **next)
894
if (!optstr || !*optstr || !value || !valsz)
897
DBG(CXT, mnt_debug("fixing gid"));
901
if (valsz == 7 && !strncmp(value, "usergid", 7) &&
902
(*(value + 7) == ',' || !*(value + 7)))
903
rc = set_uint_value(optstr, getgid(), value, end, next);
905
else if (!isdigit(*value)) {
907
char *p = strndup(value, valsz);
910
rc = mnt_get_gid(p, &id);
914
rc = set_uint_value(optstr, id, value, end, next);
918
*next = value + valsz;
926
* Converts "user" to "user=<username>".
928
* Returns: 0 on success, negative number in case of error.
930
int mnt_optstr_fix_user(char **optstr)
933
struct libmnt_optloc ol;
936
DBG(CXT, mnt_debug("fixing user"));
938
mnt_init_optloc(&ol);
940
rc = mnt_optstr_locate_option(*optstr, "user", &ol);
942
return rc == 1 ? 0 : rc; /* 1: user= not found */
944
username = mnt_get_username(getuid());
948
if (!ol.valsz || (ol.value && strncmp(ol.value, username, ol.valsz))) {
950
/* remove old value */
951
mnt_optstr_remove_option_at(optstr, ol.value, ol.end);
953
rc = insert_value(optstr, ol.value ? ol.value : ol.end,
963
int test_append(struct libmnt_test *ts, int argc, char *argv[])
965
const char *value = NULL, *name;
971
optstr = strdup(argv[1]);
977
rc = mnt_optstr_append_option(&optstr, name, value);
979
printf("result: >%s<\n", optstr);
983
int test_prepend(struct libmnt_test *ts, int argc, char *argv[])
985
const char *value = NULL, *name;
991
optstr = strdup(argv[1]);
997
rc = mnt_optstr_prepend_option(&optstr, name, value);
999
printf("result: >%s<\n", optstr);
1003
int test_split(struct libmnt_test *ts, int argc, char *argv[])
1005
char *optstr, *user = NULL, *fs = NULL, *vfs = NULL;
1011
optstr = strdup(argv[1]);
1013
rc = mnt_split_optstr(optstr, &user, &vfs, &fs, 0, 0);
1015
printf("user : %s\n", user);
1016
printf("vfs : %s\n", vfs);
1017
printf("fs : %s\n", fs);
1027
int test_flags(struct libmnt_test *ts, int argc, char *argv[])
1031
unsigned long fl = 0;
1036
optstr = strdup(argv[1]);
1038
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_LINUX_MAP));
1041
printf("mountflags: 0x%08lx\n", fl);
1044
rc = mnt_optstr_get_flags(optstr, &fl, mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
1047
printf("userspace-mountflags: 0x%08lx\n", fl);
1053
int test_apply(struct libmnt_test *ts, int argc, char *argv[])
1057
unsigned long flags;
1062
if (!strcmp(argv[1], "--user"))
1063
map = MNT_USERSPACE_MAP;
1064
else if (!strcmp(argv[1], "--linux"))
1065
map = MNT_LINUX_MAP;
1067
fprintf(stderr, "unknown option '%s'\n", argv[1]);
1071
optstr = strdup(argv[2]);
1072
flags = strtoul(argv[3], NULL, 16);
1074
printf("flags: 0x%08lx\n", flags);
1076
rc = mnt_optstr_apply_flags(&optstr, flags, mnt_get_builtin_optmap(map));
1077
printf("optstr: %s\n", optstr);
1083
int test_set(struct libmnt_test *ts, int argc, char *argv[])
1085
const char *value = NULL, *name;
1091
optstr = strdup(argv[1]);
1097
rc = mnt_optstr_set_option(&optstr, name, value);
1099
printf("result: >%s<\n", optstr);
1104
int test_get(struct libmnt_test *ts, int argc, char *argv[])
1117
rc = mnt_optstr_get_option(optstr, name, &val, &sz);
1119
printf("found; name: %s", name);
1121
printf(", argument: size=%zd data=", sz);
1122
if (fwrite(val, 1, sz, stdout) != sz)
1127
printf("%s: not found\n", name);
1129
printf("parse error: %s\n", optstr);
1133
int test_remove(struct libmnt_test *ts, int argc, char *argv[])
1141
optstr = strdup(argv[1]);
1144
rc = mnt_optstr_remove_option(&optstr, name);
1146
printf("result: >%s<\n", optstr);
1150
int test_fix(struct libmnt_test *ts, int argc, char *argv[])
1154
char *name, *val, *next;
1155
size_t valsz, namesz;
1160
next = optstr = strdup(argv[1]);
1162
printf("optstr: %s\n", optstr);
1164
while (!mnt_optstr_next_option(&next, &name, &namesz, &val, &valsz)) {
1166
if (!strncmp(name, "uid", 3))
1167
rc = mnt_optstr_fix_uid(&optstr, val, valsz, &next);
1168
else if (!strncmp(name, "gid", 3))
1169
rc = mnt_optstr_fix_gid(&optstr, val, valsz, &next);
1170
else if (!strncmp(name, "context", 7))
1171
rc = mnt_optstr_fix_secontext(&optstr, val, valsz, &next);
1176
rc = mnt_optstr_fix_user(&optstr);
1178
printf("fixed: %s\n", optstr);
1185
int main(int argc, char *argv[])
1187
struct libmnt_test tss[] = {
1188
{ "--append", test_append, "<optstr> <name> [<value>] append value to optstr" },
1189
{ "--prepend",test_prepend,"<optstr> <name> [<value>] prepend value to optstr" },
1190
{ "--set", test_set, "<optstr> <name> [<value>] (un)set value" },
1191
{ "--get", test_get, "<optstr> <name> search name in optstr" },
1192
{ "--remove", test_remove, "<optstr> <name> remove name in optstr" },
1193
{ "--split", test_split, "<optstr> split into FS, VFS and userspace" },
1194
{ "--flags", test_flags, "<optstr> convert options to MS_* flags" },
1195
{ "--apply", test_apply, "--{linux,user} <optstr> <mask> apply mask to optstr" },
1196
{ "--fix", test_fix, "<optstr> fix uid=, gid=, user, and context=" },
1200
return mnt_run_test(tss, argc, argv);
1202
#endif /* TEST_PROGRAM */