GETMNTOPTS(3)

πŸ“¦
version 73 Β· 3 versions
, in nixpkgs unstable

πŸ’… GETMNTOPTS(3) β€” the mount-options page, but make it make sense

okay bestie, so this one lives in the Library Functions Manual and it's from the System Utilities Library β€” that's libutil, and you link it with -lutil. Very demure. Very cutesy. Let's get into it πŸŽ€


✨ NAME ✨

getmntopts β€” scan mount options. that's the whole vibe, that's the job.


πŸ¦‹ LIBRARY πŸ¦‹

System Utilities Library (libutil, -lutil)

like. if you forgot the -lutil the linker WILL be passive aggressive about it. iykyk.


🌸 SYNOPSIS 🌸

#include <mntopts.h>

mntoptparse_t
getmntopts(const char *options, const struct mntopt *mopts, int *flagp,
    int *altflagp);

const char *
getmntoptstr(mntoptparse_t mp, const char *opt);

long
getmntoptnum(mntoptparse_t mp, const char *opt);

void
freemntopts(mntoptparse_t mp);

four functions. one header. zero excuses. πŸ’–


🧚 DESCRIPTION 🧚

okay so picture this: you've got a comma separated option list and you've got a list of valid option names, and getmntopts() is gonna take those two things and compute the bitmasks corresponding to the requested set of options. it's giving matchmaker. it's giving "i know what you meant." πŸ”₯

the string options gets broken down into a sequence of comma separated tokens. each token is looked up in the table described by mopts, and the bits in the word referenced by either flagp or altflagp β€” and which one depends on the m_altloc field of the option's table entry β€” are updated.

⚠️ real talk: the flag words are not initialized by getmntopts(). that's on you. do not come for this function when your bits are garbage. you did that. πŸ™ˆ

the table, mopts, has the following format:

struct mntopt {
        const char *m_option;   /* option name */
        int m_inverse;          /* negative option, e.g., "dev" */
        int m_flag;             /* bit to set, e.g., MNT_RDONLY */
        int m_altloc;           /* use altflagp rather than flagp */
};

and the members of this struct, bestie, one by one:

  • m_option β€” the option name, for example β€œsuid”. the identifier. the name tag at the function. πŸ’…
  • m_inverse β€” this one tells getmntopts() that the name has the inverse meaning of the bit. for example, β€œsuid” is the string, whereas the mount flag is MNT_NOSUID. in this case the sense of the string and the flag are inverted, so the m_inverse flag should be set. it's giving opposites attract. it's giving "no means yes actually." πŸ™ƒ
  • m_flag β€” the value of the bit to be set or cleared in the flag word when the option is recognized. the bit is set when the option is discovered, but cleared if the option name was preceded by the letters β€œno”. and the m_inverse flag causes these two operations to be reversed. so like. no + m_inverse = uno reverse card. πŸƒ
  • m_altloc β€” the bit should be set or cleared in altflagp rather than flagp. different flag word, same drama.

πŸ‘‘ the macros πŸ‘‘

each of the user visible MNT_ flags has a corresponding MOPT_ macro which defines an appropriate struct mntopt entry. and to simplify the program interface and ensure consistency across all programs, there's a general purpose macro, MOPT_STDOPTS, which contains an entry for all the generic VFS options:

MOPT_USERQUOTA,
MOPT_GROUPQUOTA,
MOPT_FSTAB_COMPAT,
MOPT_NODEV,
MOPT_NOEXEC,
MOPT_NOSUID,
MOPT_NOFOLLOW,
MOPT_RDONLY,
MOPT_UNION,
MOPT_BROWSE,
MOPT_AUTOMOUNTED,
MOPT_DEFWRITE,
MOPT_NOATIME,
MOPT_PERMISSIONS,
MOPT_IGNORE_OWNERSHIP,
MOPT_QUARANTINE,
MOPT_CPROTECT

look at that list. seventeen whole options. she ate. 🌈

in addition, the macros MOPT_FORCE and MOPT_UPDATE exist to enable the MNT_FORCE and MNT_UPDATE flags to be set. and β€” this part matters β€” finally, the table must be terminated by an entry with a NULL first element. like a periodt at the end of a sentence. no terminator, no closure, no peace. ✨


🫢 what you get back 🫢

getmntopts() returns a mntoptparse_t handle that you can use in subsequent getmntoptstr() and getmntoptnum() calls to fetch a value for an option, and that must be freed with a call to freemntopts(). that's a whole relationship. you gotta close the loop. πŸ’€

if an error occurred, then: if the external integer value getmnt_silent is zero, getmntopts() prints an error message and exits. if getmnt_silent is non-zero, then getmntopts() returns NULL. so like. it either screams in public or hands you a NULL and walks away. both are valid. 😭

getmntoptstr() returns the string value of the named option β€” if such a value was set in the option string. if the value was not set, then if the external integer value getmnt_silent is zero, getmntoptstr() prints an error message and exits; if getmnt_silent is non-zero then getmntoptstr() returns NULL. same energy, different function.

getmntoptnum() returns the long value of the named option, if such a value was set in the option string. if the value was not set, or could not be converted from a string to a long, then if the external integer value getmnt_silent is zero, getmntoptnum() prints an error message and exits; if getmnt_silent is non-zero then getmntoptnum() returns -1. yes bestie, -1. it's a real return value, it's iconic, it's a warning. ⭐

freemntopts() frees the storage used by getmntopts(). be a good citizen. free your handles. 🌸


⭐ RETURN VALUES ⭐

  • getmntopts() returns NULL if an error occurred.
  • > 🚨 but note: some bits may already have been set in flagp and altflagp even if NULL is returned. that's right. it can fail and leave fingerprints. it's giving messy breakup. πŸ’€
  • getmntoptstr() returns NULL if an error occurred.
  • getmntoptnum() returns -1 if an error occurred.

πŸ”₯ EXAMPLES πŸ”₯

most commands will use the standard option set. local filesystems which support the MNT_UPDATE flag would also have an MOPT_UPDATE entry. this can be declared and used as follows β€” and yes i'm giving it to you verbatim because these are the receipts:

#include <mntopts.h>

static const struct mntopt mopts[] = {
        MOPT_STDOPTS,
        MOPT_UPDATE,
        { NULL }
};

...

long val;
mntoptparse_t mp;
mntflags = mntaltflags = 0;

...

mp = getmntopts(options, mopts, &mntflags, &mntaltflags);

if (mp == NULL)
        err(EXIT_FAILURE, "getmntopts");

...

val = getmntoptnum(mp, "rsize");
freemntopts(mp);

see how they zeroed mntflags and mntaltflags first? because the flag words are not initialized by getmntopts(). told you. main character behavior. πŸ‘‘


πŸ’œ DIAGNOSTICS πŸ’œ

if the external integer variable getmnt_silent is zero, then the getmntopts(), getmntoptstr(), and getmntoptnum() functions display an error message and exit if an error occurred. by default getmnt_silent is zero.

so yeah. default setting is "loud and dramatic." you opt into the quiet life. πŸŽ€


πŸ¦‹ SEE ALSO πŸ¦‹

err(3), mount(8)

go read those. they're in the same universe. ✨


🌈 HISTORY 🌈

the getmntopts() function appeared in 4.4BSD. it was moved to the utilities library and enhanced to retrieve option values in NetBSD 2.0. glow-up of the century, honestly. 🫢


<div align="center">β€” πŸŽ€ β€”</div>

and that's the page, bestie!! this one's from the Debian build of the manual, dated May 4, 2010 β€” yes, a 2010 baby, and she's still serving. go free your mntoptparse_ts and hydrate. πŸ’–πŸŒΈπŸ§š

πŸ“¦ where this page comes from
  • nixpkgs unstable: libutil 73
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms