]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/sysinfo.texi
x86_64: Fix missing wcsncat function definition without multiarch (x86-64-v4)
[thirdparty/glibc.git] / manual / sysinfo.texi
CommitLineData
faf2289f
UD
1@node System Management, System Configuration, Users and Groups, Top
2@c %MENU% Controlling the system and getting information about it
3@chapter System Management
4
5This chapter describes facilities for controlling the system that
6underlies a process (including the operating system and hardware) and
7for getting information about it. Anyone can generally use the
8informational facilities, but usually only a properly privileged process
9can make changes.
28f540f4 10
28f540f4
RM
11
12@menu
13* Host Identification:: Determining the name of the machine.
99a20616
UD
14* Platform Type:: Determining operating system and basic
15 machine type
faf2289f 16* Filesystem Handling:: Controlling/querying mounts
28f540f4
RM
17@end menu
18
faf2289f
UD
19To get information on parameters of the system that are built into the
20system, such as the maximum length of a filename, @ref{System
21Configuration}.
28f540f4
RM
22
23@node Host Identification
24@section Host Identification
25
4b9a6d7c
UD
26This section explains how to identify the particular system on which your
27program is running. First, let's review the various ways computer systems
28are named, which is a little complicated because of the history of the
29development of the Internet.
30
31Every Unix system (also known as a host) has a host name, whether it's
32connected to a network or not. In its simplest form, as used before
33computer networks were an issue, it's just a word like @samp{chicken}.
34@cindex host name
35
36But any system attached to the Internet or any network like it conforms
37to a more rigorous naming convention as part of the Domain Name System
61547c48 38(DNS). In the DNS, every host name is composed of two parts:
4b9a6d7c
UD
39@cindex DNS
40@cindex Domain Name System
41
42@enumerate
43@item
68979757 44hostname
4b9a6d7c
UD
45@cindex hostname
46@item
47domain name
48@cindex domain name
49@end enumerate
50
51You will note that ``hostname'' looks a lot like ``host name'', but is
52not the same thing, and that people often incorrectly refer to entire
53host names as ``domain names.''
54
61547c48 55In the DNS, the full host name is properly called the FQDN (Fully Qualified
4b9a6d7c
UD
56Domain Name) and consists of the hostname, then a period, then the
57domain name. The domain name itself usually has multiple components
58separated by periods. So for example, a system's hostname may be
68979757 59@samp{chicken} and its domain name might be @samp{ai.mit.edu}, so
4b9a6d7c
UD
60its FQDN (which is its host name) is @samp{chicken.ai.mit.edu}.
61@cindex FQDN
62
61547c48 63Adding to the confusion, though, is that the DNS is not the only name space
68979757 64in which a computer needs to be known. Another name space is the
4b9a6d7c
UD
65NIS (aka YP) name space. For NIS purposes, there is another domain
66name, which is called the NIS domain name or the YP domain name. It
67need not have anything to do with the DNS domain name.
68@cindex YP
69@cindex NIS
70@cindex NIS domain name
71@cindex YP domain name
72
61547c48 73Confusing things even more is the fact that in the DNS, it is possible for
4b9a6d7c
UD
74multiple FQDNs to refer to the same system. However, there is always
75exactly one of them that is the true host name, and it is called the
68979757 76canonical FQDN.
4b9a6d7c
UD
77
78In some contexts, the host name is called a ``node name.''
79
88197030 80For more information on DNS host naming, see @ref{Host Names}.
28f540f4
RM
81
82@pindex hostname
83@pindex hostid
84@pindex unistd.h
68979757 85Prototypes for these functions appear in @file{unistd.h}.
4b9a6d7c
UD
86
87The programs @code{hostname}, @code{hostid}, and @code{domainname} work
88by calling these functions.
28f540f4 89
28f540f4 90@deftypefun int gethostname (char *@var{name}, size_t @var{size})
d08a7e4c 91@standards{BSD, unistd.h}
6af8bab7
AO
92@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
93@c Direct syscall on unix; implemented in terms of uname on posix and of
94@c hurd_get_host_config on hurd.
4b9a6d7c
UD
95This function returns the host name of the system on which it is called,
96in the array @var{name}. The @var{size} argument specifies the size of
97this array, in bytes. Note that this is @emph{not} the DNS hostname.
61547c48 98If the system participates in the DNS, this is the FQDN (see above).
28f540f4
RM
99
100The return value is @code{0} on success and @code{-1} on failure. In
1f77f049 101@theglibc{}, @code{gethostname} fails if @var{size} is not large
28f540f4
RM
102enough; then you can try again with a larger array. The following
103@code{errno} error condition is defined for this function:
104
105@table @code
106@item ENAMETOOLONG
107The @var{size} argument is less than the size of the host name plus one.
108@end table
109
110@pindex sys/param.h
111On some systems, there is a symbol for the maximum possible host name
112length: @code{MAXHOSTNAMELEN}. It is defined in @file{sys/param.h}.
113But you can't count on this to exist, so it is cleaner to handle
114failure and try again.
115
116@code{gethostname} stores the beginning of the host name in @var{name}
117even if the host name won't entirely fit. For some purposes, a
118truncated host name is good enough. If it is, you can ignore the
119error code.
120@end deftypefun
121
28f540f4 122@deftypefun int sethostname (const char *@var{name}, size_t @var{length})
d08a7e4c 123@standards{BSD, unistd.h}
6af8bab7
AO
124@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
125@c Direct syscall on unix; implemented in terms of hurd_set_host_config
126@c on hurd.
4b9a6d7c
UD
127The @code{sethostname} function sets the host name of the system that
128calls it to @var{name}, a string with length @var{length}. Only
129privileged processes are permitted to do this.
130
131Usually @code{sethostname} gets called just once, at system boot time.
132Often, the program that calls it sets it to the value it finds in the
133file @code{/etc/hostname}.
134@cindex /etc/hostname
135
136Be sure to set the host name to the full host name, not just the DNS
137hostname (see above).
28f540f4
RM
138
139The return value is @code{0} on success and @code{-1} on failure.
140The following @code{errno} error condition is defined for this function:
141
142@table @code
143@item EPERM
144This process cannot set the host name because it is not privileged.
145@end table
146@end deftypefun
147
4b9a6d7c 148@deftypefun int getdomainnname (char *@var{name}, size_t @var{length})
d08a7e4c 149@standards{???, unistd.h}
6af8bab7
AO
150@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
151@c Syscalls uname, then strlen and memcpy.
4b9a6d7c
UD
152@cindex NIS domain name
153@cindex YP domain name
154
155@code{getdomainname} returns the NIS (aka YP) domain name of the system
156on which it is called. Note that this is not the more popular DNS
157domain name. Get that with @code{gethostname}.
158
159The specifics of this function are analogous to @code{gethostname}, above.
160
161@end deftypefun
162
87b56f36 163@deftypefun int setdomainname (const char *@var{name}, size_t @var{length})
d08a7e4c 164@standards{???, unistd.h}
6af8bab7
AO
165@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
166@c Direct syscall.
4b9a6d7c
UD
167@cindex NIS domain name
168@cindex YP domain name
169
61547c48 170@code{setdomainname} sets the NIS (aka YP) domain name of the system
4b9a6d7c
UD
171on which it is called. Note that this is not the more popular DNS
172domain name. Set that with @code{sethostname}.
173
174The specifics of this function are analogous to @code{sethostname}, above.
175
176@end deftypefun
177
28f540f4 178@deftypefun {long int} gethostid (void)
d08a7e4c 179@standards{BSD, unistd.h}
6af8bab7
AO
180@safety{@prelim{}@mtsafe{@mtshostid{} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
181@c On HURD, calls _hurd_get_host_config and strtol. On Linux, open
182@c HOSTIDFILE, reads an int32_t and closes; if that fails, it calls
183@c gethostname and gethostbyname_r to use the h_addr.
28f540f4 184This function returns the ``host ID'' of the machine the program is
4b9a6d7c 185running on. By convention, this is usually the primary Internet IP address
04b9968b 186of that machine, converted to a @w{@code{long int}}. However, on some
28f540f4
RM
187systems it is a meaningless but unique number which is hard-coded for
188each machine.
4b9a6d7c
UD
189
190This is not widely used. It arose in BSD 4.2, but was dropped in BSD 4.4.
191It is not required by POSIX.
192
193The proper way to query the IP address is to use @code{gethostbyname}
194on the results of @code{gethostname}. For more information on IP addresses,
195@xref{Host Addresses}.
28f540f4
RM
196@end deftypefun
197
28f540f4 198@deftypefun int sethostid (long int @var{id})
d08a7e4c 199@standards{BSD, unistd.h}
6af8bab7 200@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtshostid{}}}@asunsafe{}@acunsafe{@acucorrupt{} @acsfd{}}}
28f540f4 201The @code{sethostid} function sets the ``host ID'' of the host machine
4b9a6d7c 202to @var{id}. Only privileged processes are permitted to do this. Usually
28f540f4
RM
203it happens just once, at system boot time.
204
4b9a6d7c 205The proper way to establish the primary IP address of a system
68979757 206is to configure the IP address resolver to associate that IP address with
4b9a6d7c
UD
207the system's host name as returned by @code{gethostname}. For example,
208put a record for the system in @file{/etc/hosts}.
209
210See @code{gethostid} above for more information on host ids.
211
28f540f4 212The return value is @code{0} on success and @code{-1} on failure.
04b9968b 213The following @code{errno} error conditions are defined for this function:
28f540f4
RM
214
215@table @code
216@item EPERM
217This process cannot set the host name because it is not privileged.
218
219@item ENOSYS
220The operating system does not support setting the host ID. On some
221systems, the host ID is a meaningless but unique number hard-coded for
222each machine.
223@end table
224@end deftypefun
225
99a20616
UD
226@node Platform Type
227@section Platform Type Identification
28f540f4
RM
228
229You can use the @code{uname} function to find out some information about
230the type of computer your program is running on. This function and the
231associated data type are declared in the header file
232@file{sys/utsname.h}.
233@pindex sys/utsname.h
234
68979757 235As a bonus, @code{uname} also gives some information identifying the
4b9a6d7c 236particular system your program is running on. This is the same information
9dcc8f11 237which you can get with functions targeted to this purpose described in
4b9a6d7c
UD
238@ref{Host Identification}.
239
240
28f540f4 241@deftp {Data Type} {struct utsname}
d08a7e4c 242@standards{POSIX.1, sys/utsname.h}
28f540f4
RM
243The @code{utsname} structure is used to hold information returned
244by the @code{uname} function. It has the following members:
245
246@table @code
247@item char sysname[]
248This is the name of the operating system in use.
249
28f540f4
RM
250@item char release[]
251This is the current release level of the operating system implementation.
252
253@item char version[]
254This is the current version level within the release of the operating
255system.
256
257@item char machine[]
258This is a description of the type of hardware that is in use.
259
260Some systems provide a mechanism to interrogate the kernel directly for
1f77f049
JM
261this information. On systems without such a mechanism, @theglibc{}
262fills in this field based on the configuration name that was
28f540f4
RM
263specified when building and installing the library.
264
265GNU uses a three-part name to describe a system configuration; the three
266parts are @var{cpu}, @var{manufacturer} and @var{system-type}, and they
267are separated with dashes. Any possible combination of three names is
268potentially meaningful, but most such combinations are meaningless in
269practice and even the meaningful ones are not necessarily supported by
270any particular GNU program.
271
272Since the value in @code{machine} is supposed to describe just the
273hardware, it consists of the first two parts of the configuration name:
274@samp{@var{cpu}-@var{manufacturer}}. For example, it might be one of these:
275
276@quotation
37742e84 277@code{"sparc-sun"},
28f540f4 278@code{"i386-@var{anything}"},
37742e84 279@code{"m68k-hp"},
28f540f4
RM
280@code{"m68k-sony"},
281@code{"m68k-sun"},
282@code{"mips-dec"}
283@end quotation
4b9a6d7c
UD
284
285@item char nodename[]
1f77f049
JM
286This is the host name of this particular computer. In @theglibc{},
287the value is the same as that returned by @code{gethostname};
4b9a6d7c
UD
288see @ref{Host Identification}.
289
61547c48 290@code{gethostname} is implemented with a call to @code{uname}.
4b9a6d7c
UD
291
292@item char domainname[]
293This is the NIS or YP domain name. It is the same value returned by
68979757 294@code{getdomainname}; see @ref{Host Identification}. This element
4b9a6d7c
UD
295is a relatively recent invention and use of it is not as portable as
296use of the rest of the structure.
297
298@c getdomainname() is implemented with a call to uname().
299
28f540f4
RM
300@end table
301@end deftp
302
28f540f4 303@deftypefun int uname (struct utsname *@var{info})
d08a7e4c 304@standards{POSIX.1, sys/utsname.h}
6af8bab7
AO
305@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
306@c Direct syscall on unix; the posix fallback is to call gethostname and
307@c then fills in the other fields with constants; on HURD, it calls
308@c proc_uname and then gethostname.
28f540f4
RM
309The @code{uname} function fills in the structure pointed to by
310@var{info} with information about the operating system and host machine.
61547c48 311A non-negative return value indicates that the data was successfully stored.
28f540f4 312
61547c48 313@code{-1} as the return value indicates an error. The only error possible is
28f540f4
RM
314@code{EFAULT}, which we normally don't mention as it is always a
315possibility.
316@end deftypefun
37742e84
UD
317
318
faf2289f 319@node Filesystem Handling
99a20616 320@section Controlling and Querying Mounts
faf2289f
UD
321
322All files are in filesystems, and before you can access any file, its
323filesystem must be mounted. Because of Unix's concept of
324@emph{Everything is a file}, mounting of filesystems is central to doing
325almost anything. This section explains how to find out what filesystems
326are currently mounted and what filesystems are available for mounting,
327and how to change what is mounted.
328
329The classic filesystem is the contents of a disk drive. The concept is
330considerably more abstract, though, and lots of things other than disk
dbacafe5 331drives can be mounted.
faf2289f
UD
332
333Some block devices don't correspond to traditional devices like disk
334drives. For example, a loop device is a block device whose driver uses
335a regular file in another filesystem as its medium. So if that regular
336file contains appropriate data for a filesystem, you can by mounting the
337loop device essentially mount a regular file.
338
339Some filesystems aren't based on a device of any kind. The ``proc''
340filesystem, for example, contains files whose data is made up by the
341filesystem driver on the fly whenever you ask for it. And when you
342write to it, the data you write causes changes in the system. No data
343gets stored.
344
345@c It would be good to mention NFS mounts here.
346
347@menu
348* Mount Information:: What is or could be mounted?
99a20616 349* Mount-Unmount-Remount:: Controlling what is mounted and how
faf2289f 350@end menu
37742e84 351
99a20616 352@node Mount Information, Mount-Unmount-Remount, , Filesystem Handling
f126ef67 353@subsection Mount Information
faf2289f
UD
354
355For some programs it is desirable and necessary to access information
356about whether a certain filesystem is mounted and, if it is, where, or
1f77f049 357simply to get lists of all the available filesystems. @Theglibc{}
faf2289f 358provides some functions to retrieve this information portably.
37742e84
UD
359
360Traditionally Unix systems have a file named @file{/etc/fstab} which
361describes all possibly mounted filesystems. The @code{mount} program
d15b801c
RM
362uses this file to mount at startup time of the system all the
363necessary filesystems. The information about all the filesystems
364actually mounted is normally kept in a file named either
365@file{/var/run/mtab} or @file{/etc/mtab}. Both files share the same
366syntax and it is crucial that this syntax is followed all the time.
61547c48 367Therefore it is best to never directly write to the files. The functions
d15b801c
RM
368described in this section can do this and they also provide the
369functionality to convert the external textual representation to the
37742e84
UD
370internal representation.
371
faf2289f
UD
372Note that the @file{fstab} and @file{mtab} files are maintained on a
373system by @emph{convention}. It is possible for the files not to exist
374or not to be consistent with what is really mounted or available to
375mount, if the system's administration policy allows it. But programs
376that mount and unmount filesystems typically maintain and use these
377files as described herein.
378
37742e84
UD
379@vindex _PATH_FSTAB
380@vindex _PATH_MNTTAB
37742e84 381@vindex _PATH_MOUNTED
d15b801c
RM
382@vindex FSTAB
383@vindex MNTTAB
384@vindex MOUNTED
37742e84 385The filenames given above should never be used directly. The portable
61547c48 386way to handle these files is to use the macros @code{_PATH_FSTAB},
d15b801c
RM
387defined in @file{fstab.h}, or @code{_PATH_MNTTAB}, defined in
388@file{mntent.h} and @file{paths.h}, for @file{fstab}; and the macro
389@code{_PATH_MOUNTED}, also defined in @file{mntent.h} and
390@file{paths.h}, for @file{mtab}. There are also two alternate macro
391names @code{FSTAB}, @code{MNTTAB}, and @code{MOUNTED} defined but
392these names are deprecated and kept only for backward compatibility.
393The names @code{_PATH_MNTTAB} and @code{_PATH_MOUNTED} should always be used.
37742e84 394
faf2289f
UD
395@menu
396* fstab:: The @file{fstab} file
397* mtab:: The @file{mtab} file
398* Other Mount Information:: Other (non-libc) sources of mount information
399@end menu
400
401@node fstab
f126ef67 402@subsubsection The @file{fstab} file
faf2289f 403
37742e84
UD
404The internal representation for entries of the file is @w{@code{struct
405fstab}}, defined in @file{fstab.h}.
406
37742e84 407@deftp {Data Type} {struct fstab}
d08a7e4c 408@standards{BSD, fstab.h}
37742e84
UD
409This structure is used with the @code{getfsent}, @code{getfsspec}, and
410@code{getfsfile} functions.
411
412@table @code
413@item char *fs_spec
fed8f7f7 414This element describes the device from which the filesystem is mounted.
37742e84
UD
415Normally this is the name of a special device, such as a hard disk
416partition, but it could also be a more or less generic string. For
417@dfn{NFS} it would be a hostname and directory name combination.
418
419Even though the element is not declared @code{const} it shouldn't be
420modified. The missing @code{const} has historic reasons, since this
421function predates @w{ISO C}. The same is true for the other string
422elements of this structure.
423
424@item char *fs_file
fed8f7f7
UD
425This describes the mount point on the local system. I.e., accessing any
426file in this filesystem has implicitly or explicitly this string as a
37742e84
UD
427prefix.
428
429@item char *fs_vfstype
430This is the type of the filesystem. Depending on what the underlying
431kernel understands it can be any string.
432
433@item char *fs_mntops
434This is a string containing options passed to the kernel with the
435@code{mount} call. Again, this can be almost anything. There can be
436more than one option, separated from the others by a comma. Each option
437consists of a name and an optional value part, introduced by an @code{=}
438character.
439
04b9968b 440If the value of this element must be processed it should ideally be done
37742e84
UD
441using the @code{getsubopt} function; see @ref{Suboptions}.
442
443@item const char *fs_type
c756c71c 444This name is poorly chosen. This element points to a string (possibly
37742e84
UD
445in the @code{fs_mntops} string) which describes the modes with which the
446filesystem is mounted. @file{fstab} defines five macros to describe the
447possible values:
448
449@vtable @code
450@item FSTAB_RW
61547c48 451The filesystem gets mounted with read and write enabled.
37742e84 452@item FSTAB_RQ
61547c48 453The filesystem gets mounted with read and write enabled. Write access
37742e84
UD
454is restricted by quotas.
455@item FSTAB_RO
c756c71c 456The filesystem gets mounted read-only.
37742e84 457@item FSTAB_SW
c756c71c 458This is not a real filesystem, it is a swap device.
37742e84
UD
459@item FSTAB_XX
460This entry from the @file{fstab} file is totally ignored.
461@end vtable
462
61547c48 463Testing for equality with these values must happen using @code{strcmp}
c756c71c 464since these are all strings. Comparing the pointer will probably always
37742e84
UD
465fail.
466
467@item int fs_freq
468This element describes the dump frequency in days.
469
470@item int fs_passno
471This element describes the pass number on parallel dumps. It is closely
c756c71c 472related to the @code{dump} utility used on Unix systems.
37742e84
UD
473@end table
474@end deftp
475
476
1f77f049 477To read the entire content of the of the @file{fstab} file @theglibc{}
37742e84
UD
478contains a set of three functions which are designed in the usual way.
479
37742e84 480@deftypefun int setfsent (void)
d08a7e4c 481@standards{BSD, fstab.h}
6af8bab7
AO
482@safety{@prelim{}@mtunsafe{@mtasurace{:fsent}}@asunsafe{@ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
483@c setfsent @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
484@c fstab_init(1) @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
485@c malloc dup @ascuheap @acsmem
486@c rewind dup @asucorrupt @acucorrupt [no @aculock]
487@c setmntent dup @ascuheap @asulock @acsmem @acsfd @aculock
37742e84
UD
488This function makes sure that the internal read pointer for the
489@file{fstab} file is at the beginning of the file. This is done by
490either opening the file or resetting the read pointer.
491
492Since the file handle is internal to the libc this function is not
493thread-safe.
494
495This function returns a non-zero value if the operation was successful
496and the @code{getfs*} functions can be used to read the entries of the
497file.
498@end deftypefun
499
37742e84 500@deftypefun void endfsent (void)
d08a7e4c 501@standards{BSD, fstab.h}
6af8bab7
AO
502@safety{@prelim{}@mtunsafe{@mtasurace{:fsent}}@asunsafe{@ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
503@c endfsent @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
504@c endmntent dup @ascuheap @asulock @aculock @acsmem @acsfd
37742e84 505This function makes sure that all resources acquired by a prior call to
c756c71c 506@code{setfsent} (explicitly or implicitly by calling @code{getfsent}) are
37742e84
UD
507freed.
508@end deftypefun
509
37742e84 510@deftypefun {struct fstab *} getfsent (void)
d08a7e4c 511@standards{BSD, fstab.h}
6af8bab7
AO
512@safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
513@c getfsent @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
514@c fstab_init(0) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
515@c fstab_fetch @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
516@c getmntent_r dup @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
517@c fstab_convert @mtasurace:fsent
518@c hasmntopt dup ok
37742e84
UD
519This function returns the next entry of the @file{fstab} file. If this
520is the first call to any of the functions handling @file{fstab} since
521program start or the last call of @code{endfsent}, the file will be
522opened.
523
04b9968b 524The function returns a pointer to a variable of type @code{struct
37742e84
UD
525fstab}. This variable is shared by all threads and therefore this
526function is not thread-safe. If an error occurred @code{getfsent}
c756c71c 527returns a @code{NULL} pointer.
37742e84
UD
528@end deftypefun
529
37742e84 530@deftypefun {struct fstab *} getfsspec (const char *@var{name})
d08a7e4c 531@standards{BSD, fstab.h}
6af8bab7
AO
532@safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
533@c getffsspec @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
534@c fstab_init(1) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
535@c fstab_fetch dup @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
536@c strcmp dup ok
537@c fstab_convert dup @mtasurace:fsent
37742e84
UD
538This function returns the next entry of the @file{fstab} file which has
539a string equal to @var{name} pointed to by the @code{fs_spec} element.
540Since there is normally exactly one entry for each special device it
541makes no sense to call this function more than once for the same
542argument. If this is the first call to any of the functions handling
543@file{fstab} since program start or the last call of @code{endfsent},
544the file will be opened.
545
04b9968b 546The function returns a pointer to a variable of type @code{struct
37742e84
UD
547fstab}. This variable is shared by all threads and therefore this
548function is not thread-safe. If an error occurred @code{getfsent}
c756c71c 549returns a @code{NULL} pointer.
37742e84
UD
550@end deftypefun
551
37742e84 552@deftypefun {struct fstab *} getfsfile (const char *@var{name})
d08a7e4c 553@standards{BSD, fstab.h}
6af8bab7
AO
554@safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
555@c getffsfile @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
556@c fstab_init(1) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
557@c fstab_fetch dup @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
558@c strcmp dup ok
559@c fstab_convert dup @mtasurace:fsent
37742e84
UD
560This function returns the next entry of the @file{fstab} file which has
561a string equal to @var{name} pointed to by the @code{fs_file} element.
562Since there is normally exactly one entry for each mount point it
563makes no sense to call this function more than once for the same
564argument. If this is the first call to any of the functions handling
565@file{fstab} since program start or the last call of @code{endfsent},
566the file will be opened.
567
04b9968b 568The function returns a pointer to a variable of type @code{struct
37742e84
UD
569fstab}. This variable is shared by all threads and therefore this
570function is not thread-safe. If an error occurred @code{getfsent}
c756c71c 571returns a @code{NULL} pointer.
37742e84
UD
572@end deftypefun
573
37742e84 574
faf2289f 575@node mtab
f126ef67 576@subsubsection The @file{mtab} file
faf2289f 577The following functions and data structure access the @file{mtab} file.
37742e84 578
37742e84 579@deftp {Data Type} {struct mntent}
d08a7e4c 580@standards{BSD, mntent.h}
61547c48 581This structure is used with the @code{getmntent}, @code{getmntent_r},
37742e84
UD
582@code{addmntent}, and @code{hasmntopt} functions.
583
584@table @code
585@item char *mnt_fsname
586This element contains a pointer to a string describing the name of the
587special device from which the filesystem is mounted. It corresponds to
588the @code{fs_spec} element in @code{struct fstab}.
589
590@item char *mnt_dir
591This element points to a string describing the mount point of the
592filesystem. It corresponds to the @code{fs_file} element in
593@code{struct fstab}.
594
595@item char *mnt_type
596@code{mnt_type} describes the filesystem type and is therefore
597equivalent to @code{fs_vfstype} in @code{struct fstab}. @file{mntent.h}
04b9968b
UD
598defines a few symbolic names for some of the values this string can have.
599But since the kernel can support arbitrary filesystems it does not
37742e84
UD
600make much sense to give them symbolic names. If one knows the symbol
601name one also knows the filesystem name. Nevertheless here follows the
04b9968b 602list of the symbols provided in @file{mntent.h}.
37742e84
UD
603
604@vtable @code
605@item MNTTYPE_IGNORE
61547c48 606This symbol expands to @code{"ignore"}. The value is sometimes used in
37742e84
UD
607@file{fstab} files to make sure entries are not used without removing them.
608@item MNTTYPE_NFS
609Expands to @code{"nfs"}. Using this macro sometimes could make sense
610since it names the default NFS implementation, in case both version 2
611and 3 are supported.
612@item MNTTYPE_SWAP
613This symbol expands to @code{"swap"}. It names the special @file{fstab}
614entry which names one of the possibly multiple swap partitions.
615@end vtable
616
617@item char *mnt_opts
618The element contains a string describing the options used while mounting
619the filesystem. As for the equivalent element @code{fs_mntops} of
620@code{struct fstab} it is best to use the function @code{getsubopt}
621(@pxref{Suboptions}) to access the parts of this string.
622
623The @file{mntent.h} file defines a number of macros with string values
624which correspond to some of the options understood by the kernel. There
04b9968b 625might be many more options which are possible so it doesn't make much sense
37742e84
UD
626to rely on these macros but to be consistent here is the list:
627
628@vtable @code
629@item MNTOPT_DEFAULTS
630Expands to @code{"defaults"}. This option should be used alone since it
49c091e5 631indicates all values for the customizable values are chosen to be the
37742e84
UD
632default.
633@item MNTOPT_RO
fed8f7f7 634Expands to @code{"ro"}. See the @code{FSTAB_RO} value, it means the
37742e84
UD
635filesystem is mounted read-only.
636@item MNTOPT_RW
61547c48 637Expands to @code{"rw"}. See the @code{FSTAB_RW} value, it means the
37742e84
UD
638filesystem is mounted with read and write permissions.
639@item MNTOPT_SUID
640Expands to @code{"suid"}. This means that the SUID bit (@pxref{How
641Change Persona}) is respected when a program from the filesystem is
642started.
643@item MNTOPT_NOSUID
644Expands to @code{"nosuid"}. This is the opposite of @code{MNTOPT_SUID},
c756c71c 645the SUID bit for all files from the filesystem is ignored.
37742e84
UD
646@item MNTOPT_NOAUTO
647Expands to @code{"noauto"}. At startup time the @code{mount} program
648will ignore this entry if it is started with the @code{-a} option to
649mount all filesystems mentioned in the @file{fstab} file.
650@end vtable
651
652As for the @code{FSTAB_*} entries introduced above it is important to
653use @code{strcmp} to check for equality.
654
655@item mnt_freq
656This elements corresponds to @code{fs_freq} and also specifies the
657frequency in days in which dumps are made.
658
659@item mnt_passno
660This element is equivalent to @code{fs_passno} with the same meaning
661which is uninteresting for all programs beside @code{dump}.
662@end table
663@end deftp
664
665For accessing the @file{mtab} file there is again a set of three
666functions to access all entries in a row. Unlike the functions to
667handle @file{fstab} these functions do not access a fixed file and there
61547c48 668is even a thread safe variant of the get function. Besides this @theglibc{}
1f77f049 669contains functions to alter the file and test for specific options.
37742e84 670
37742e84 671@deftypefun {FILE *} setmntent (const char *@var{file}, const char *@var{mode})
d08a7e4c 672@standards{BSD, mntent.h}
6af8bab7
AO
673@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
674@c setmntent @ascuheap @asulock @acsmem @acsfd @aculock
675@c strlen dup ok
676@c mempcpy dup ok
677@c memcpy dup ok
678@c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
679@c fsetlocking dup ok [no @mtasurace:stream @asulock: exclusive stream]
37742e84
UD
680The @code{setmntent} function prepares the file named @var{FILE} which
681must be in the format of a @file{fstab} and @file{mtab} file for the
682upcoming processing through the other functions of the family. The
683@var{mode} parameter can be chosen in the way the @var{opentype}
684parameter for @code{fopen} (@pxref{Opening Streams}) can be chosen. If
685the file is opened for writing the file is also allowed to be empty.
686
687If the file was successfully opened @code{setmntent} returns a file
61547c48 688handle for future use. Otherwise the return value is @code{NULL}
37742e84
UD
689and @code{errno} is set accordingly.
690@end deftypefun
691
37742e84 692@deftypefun int endmntent (FILE *@var{stream})
d08a7e4c 693@standards{BSD, mntent.h}
6af8bab7
AO
694@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
695@c endmntent @ascuheap @asulock @aculock @acsmem @acsfd
696@c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
37742e84
UD
697This function takes for the @var{stream} parameter a file handle which
698previously was returned from the @code{setmntent} call.
699@code{endmntent} closes the stream and frees all resources.
700
c756c71c
UD
701The return value is @math{1} unless an error occurred in which case it
702is @math{0}.
37742e84
UD
703@end deftypefun
704
37742e84 705@deftypefun {struct mntent *} getmntent (FILE *@var{stream})
d08a7e4c 706@standards{BSD, mntent.h}
6af8bab7
AO
707@safety{@prelim{}@mtunsafe{@mtasurace{:mntentbuf} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asuinit{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsmem{}}}
708@c getmntent @mtasurace:mntentbuf @mtslocale @asucorrupt @ascuheap @asuinit @acuinit @acucorrupt @aculock @acsmem
709@c libc_once @ascuheap @asuinit @acuinit @acsmem
710@c allocate @ascuheap @acsmem
711@c malloc dup @ascuheap @acsmem
712@c getmntent_r dup @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
37742e84 713The @code{getmntent} function takes as the parameter a file handle
61547c48 714previously returned by a successful call to @code{setmntent}. It returns
37742e84
UD
715a pointer to a static variable of type @code{struct mntent} which is
716filled with the information from the next entry from the file currently
717read.
718
c7f7281e 719The file format used prescribes the use of spaces or tab characters to
61547c48 720separate the fields. This makes it harder to use names containing one
37369d1c
UD
721of these characters (e.g., mount points using spaces). Therefore
722these characters are encoded in the files and the @code{getmntent}
723function takes care of the decoding while reading the entries back in.
724@code{'\040'} is used to encode a space character, @code{'\011'} to
725encode a tab character, @code{'\012'} to encode a newline character,
726and @code{'\\'} to encode a backslash.
c7f7281e 727
37742e84
UD
728If there was an error or the end of the file is reached the return value
729is @code{NULL}.
730
731This function is not thread-safe since all calls to this function return
732a pointer to the same static variable. @code{getmntent_r} should be
c756c71c 733used in situations where multiple threads access the file.
37742e84
UD
734@end deftypefun
735
8ded91fb 736@deftypefun {struct mntent *} getmntent_r (FILE *@var{stream}, struct mntent *@var{result}, char *@var{buffer}, int @var{bufsize})
d08a7e4c 737@standards{BSD, mntent.h}
6af8bab7
AO
738@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
739@c getmntent_r @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
740@c flockfile dup @aculock
741@c fgets_unlocked dup @asucorrupt @acucorrupt [locked, so no @mtsrace:stream]
742@c funlockfile dup @aculock
743@c strchr dup ok
744@c strspn dup ok
745@c strsep dup ok
746@c decode_name ok
747@c sscanf dup @mtslocale @ascuheap @acsmem
37742e84
UD
748The @code{getmntent_r} function is the reentrant variant of
749@code{getmntent}. It also returns the next entry from the file and
750returns a pointer. The actual variable the values are stored in is not
751static, though. Instead the function stores the values in the variable
752pointed to by the @var{result} parameter. Additional information (e.g.,
753the strings pointed to by the elements of the result) are kept in the
754buffer of size @var{bufsize} pointed to by @var{buffer}.
755
c7f7281e
UD
756Escaped characters (space, tab, backslash) are converted back in the
757same way as it happens for @code{getmentent}.
758
c756c71c 759The function returns a @code{NULL} pointer in error cases. Errors could be:
37742e84
UD
760@itemize @bullet
761@item
762error while reading the file,
763@item
764end of file reached,
765@item
766@var{bufsize} is too small for reading a complete new entry.
767@end itemize
768@end deftypefun
769
37742e84 770@deftypefun int addmntent (FILE *@var{stream}, const struct mntent *@var{mnt})
d08a7e4c 771@standards{BSD, mntent.h}
81959214 772@safety{@prelim{}@mtsafe{@mtsrace{:stream} @mtslocale{}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
6af8bab7
AO
773@c addmntent @mtasurace:stream @mtslocale @asucorrupt @acucorrupt
774@c fseek dup @asucorrupt @acucorrupt [no @aculock]
775@c encode_name ok
776@c fprintf dup @mtslocale @asucorrupt @acucorrupt [no @ascuheap @acsmem, no @aculock]
777@c fflush dup @asucorrupt @acucorrupt [no @aculock]
04b9968b 778The @code{addmntent} function allows adding a new entry to the file
37742e84
UD
779previously opened with @code{setmntent}. The new entries are always
780appended. I.e., even if the position of the file descriptor is not at
c756c71c 781the end of the file this function does not overwrite an existing entry
37742e84
UD
782following the current position.
783
784The implication of this is that to remove an entry from a file one has
785to create a new file while leaving out the entry to be removed and after
786closing the file remove the old one and rename the new file to the
787chosen name.
788
c7f7281e
UD
789This function takes care of spaces and tab characters in the names to be
790written to the file. It converts them and the backslash character into
61547c48 791the format described in the @code{getmntent} description above.
c7f7281e 792
c756c71c
UD
793This function returns @math{0} in case the operation was successful.
794Otherwise the return value is @math{1} and @code{errno} is set
37742e84
UD
795appropriately.
796@end deftypefun
797
37742e84 798@deftypefun {char *} hasmntopt (const struct mntent *@var{mnt}, const char *@var{opt})
d08a7e4c 799@standards{BSD, mntent.h}
6af8bab7
AO
800@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
801@c hasmntopt ok
802@c strlen dup ok
803@c strstr dup ok
804@c strchr dup ok
37742e84
UD
805This function can be used to check whether the string pointed to by the
806@code{mnt_opts} element of the variable pointed to by @var{mnt} contains
807the option @var{opt}. If this is true a pointer to the beginning of the
808option in the @code{mnt_opts} element is returned. If no such option
c756c71c 809exists the function returns @code{NULL}.
37742e84
UD
810
811This function is useful to test whether a specific option is present but
812when all options have to be processed one is better off with using the
813@code{getsubopt} function to iterate over all options in the string.
814@end deftypefun
faf2289f
UD
815
816@node Other Mount Information
f126ef67 817@subsubsection Other (Non-libc) Sources of Mount Information
faf2289f
UD
818
819On a system with a Linux kernel and the @code{proc} filesystem, you can
820get information on currently mounted filesystems from the file
821@file{mounts} in the @code{proc} filesystem. Its format is similar to
822that of the @file{mtab} file, but represents what is truly mounted
823without relying on facilities outside the kernel to keep @file{mtab} up
824to date.
825
826
99a20616 827@node Mount-Unmount-Remount, , Mount Information, Filesystem Handling
f126ef67 828@subsection Mount, Unmount, Remount
faf2289f
UD
829
830This section describes the functions for mounting, unmounting, and
831remounting filesystems.
832
833Only the superuser can mount, unmount, or remount a filesystem.
834
835These functions do not access the @file{fstab} and @file{mtab} files. You
836should maintain and use these separately. @xref{Mount Information}.
837
838The symbols in this section are declared in @file{sys/mount.h}.
839
faf2289f 840@deftypefun {int} mount (const char *@var{special_file}, const char *@var{dir}, const char *@var{fstype}, unsigned long int @var{options}, const void *@var{data})
d08a7e4c
RJ
841@standards{SVID, sys/mount.h}
842@standards{BSD, sys/mount.h}
6af8bab7
AO
843@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
844@c Direct syscall.
faf2289f 845
dbacafe5 846@code{mount} mounts or remounts a filesystem. The two operations are
0bc93a2f 847quite different and are merged rather unnaturally into this one function.
dbacafe5 848The @code{MS_REMOUNT} option, explained below, determines whether
faf2289f
UD
849@code{mount} mounts or remounts.
850
851For a mount, the filesystem on the block device represented by the
852device special file named @var{special_file} gets mounted over the mount
853point @var{dir}. This means that the directory @var{dir} (along with any
854files in it) is no longer visible; in its place (and still with the name
855@var{dir}) is the root directory of the filesystem on the device.
856
857As an exception, if the filesystem type (see below) is one which is not
858based on a device (e.g. ``proc''), @code{mount} instantiates a
859filesystem and mounts it over @var{dir} and ignores @var{special_file}.
860
861For a remount, @var{dir} specifies the mount point where the filesystem
862to be remounted is (and remains) mounted and @var{special_file} is
863ignored. Remounting a filesystem means changing the options that control
864operations on the filesystem while it is mounted. It does not mean
865unmounting and mounting again.
866
61547c48 867For a mount, you must identify the type of the filesystem with
faf2289f
UD
868@var{fstype}. This type tells the kernel how to access the filesystem
869and can be thought of as the name of a filesystem driver. The
870acceptable values are system dependent. On a system with a Linux kernel
871and the @code{proc} filesystem, the list of possible values is in the
872file @file{filesystems} in the @code{proc} filesystem (e.g. type
873@kbd{cat /proc/filesystems} to see the list). With a Linux kernel, the
874types of filesystems that @code{mount} can mount, and their type names,
875depends on what filesystem drivers are configured into the kernel or
876loaded as loadable kernel modules. An example of a common value for
877@var{fstype} is @code{ext2}.
878
879For a remount, @code{mount} ignores @var{fstype}.
880
881@c This is traditionally called "rwflag" for historical reasons.
882@c No point in confusing people today, though.
883@var{options} specifies a variety of options that apply until the
3566d33c 884filesystem is unmounted or remounted. The precise meaning of an option
faf2289f
UD
885depends on the filesystem and with some filesystems, an option may have
886no effect at all. Furthermore, for some filesystems, some of these
887options (but never @code{MS_RDONLY}) can be overridden for individual
888file accesses via @code{ioctl}.
889
890@var{options} is a bit string with bit fields defined using the
891following mask and masked value macros:
892
2fe82ca6 893@vtable @code
faf2289f
UD
894@item MS_MGC_MASK
895This multibit field contains a magic number. If it does not have the value
896@code{MS_MGC_VAL}, @code{mount} assumes all the following bits are zero and
897the @var{data} argument is a null string, regardless of their actual values.
898
899@item MS_REMOUNT
900This bit on means to remount the filesystem. Off means to mount it.
901@c There is a mask MS_RMT_MASK in mount.h that says only two of the options
4b9a6d7c 902@c can be reset by remount. But the Linux kernel has its own version of
faf2289f
UD
903@c MS_RMT_MASK that says they all can be reset. As far as I can tell,
904@c libc just passes the arguments straight through to the kernel.
905
906@item MS_RDONLY
907This bit on specifies that no writing to the filesystem shall be allowed
dbacafe5 908while it is mounted. This cannot be overridden by @code{ioctl}. This
faf2289f
UD
909option is available on nearly all filesystems.
910
faf2289f
UD
911@item MS_NOSUID
912This bit on specifies that Setuid and Setgid permissions on files in the
913filesystem shall be ignored while it is mounted.
914
915@item MS_NOEXEC
916This bit on specifies that no files in the filesystem shall be executed
917while the filesystem is mounted.
918
919@item MS_NODEV
dbacafe5 920This bit on specifies that no device special files in the filesystem
faf2289f
UD
921shall be accessible while the filesystem is mounted.
922
923@item MS_SYNCHRONOUS
dbacafe5 924This bit on specifies that all writes to the filesystem while it is
11bf311e 925mounted shall be synchronous; i.e., data shall be synced before each
faf2289f
UD
926write completes rather than held in the buffer cache.
927
928@item MS_MANDLOCK
929This bit on specifies that mandatory locks on files shall be permitted while
930the filesystem is mounted.
931
932@item MS_NOATIME
933This bit on specifies that access times of files shall not be updated when
934the files are accessed while the filesystem is mounted.
935
936@item MS_NODIRATIME
937This bit on specifies that access times of directories shall not be updated
938when the directories are accessed while the filesystem in mounted.
939
940@c there is also S_QUOTA Linux fs.h (mount.h still uses its former name
941@c S_WRITE), but I can't see what it does. Turns on quotas, I guess.
942
2fe82ca6 943@end vtable
faf2289f
UD
944
945Any bits not covered by the above masks should be set off; otherwise,
946results are undefined.
947
948The meaning of @var{data} depends on the filesystem type and is controlled
949entirely by the filesystem driver in the kernel.
950
951Example:
952
953@smallexample
954@group
955#include <sys/mount.h>
956
56ef6ab0 957mount("/dev/hdb", "/cdrom", "iso9660", MS_MGC_VAL | MS_RDONLY | MS_NOSUID, "");
faf2289f 958
56ef6ab0 959mount("/dev/hda2", "/mnt", "", MS_MGC_VAL | MS_REMOUNT, "");
faf2289f
UD
960
961@end group
962@end smallexample
963
964Appropriate arguments for @code{mount} are conventionally recorded in
965the @file{fstab} table. @xref{Mount Information}.
966
967The return value is zero if the mount or remount is successful. Otherwise,
dbacafe5 968it is @code{-1} and @code{errno} is set appropriately. The values of
faf2289f
UD
969@code{errno} are filesystem dependent, but here is a general list:
970
971@table @code
972@item EPERM
973The process is not superuser.
974@item ENODEV
975The file system type @var{fstype} is not known to the kernel.
976@item ENOTBLK
977The file @var{dev} is not a block device special file.
978@item EBUSY
979
68979757 980@itemize @bullet
faf2289f 981
dbacafe5 982@item
faf2289f
UD
983The device is already mounted.
984
985@item
986The mount point is busy. (E.g. it is some process' working directory or
987has a filesystem mounted on it already).
988
989@item
61547c48 990The request is to remount read-only, but there are files open for writing.
faf2289f
UD
991@end itemize
992
993@item EINVAL
68979757 994@itemize @bullet
faf2289f
UD
995
996@item
997A remount was attempted, but there is no filesystem mounted over the
998specified mount point.
999
dbacafe5 1000@item
faf2289f
UD
1001The supposed filesystem has an invalid superblock.
1002
1003@end itemize
1004
0bc93a2f 1005@item EACCES
68979757 1006@itemize @bullet
faf2289f
UD
1007
1008@item
dbacafe5 1009The filesystem is inherently read-only (possibly due to a switch on the
faf2289f
UD
1010device) and the process attempted to mount it read/write (by setting the
1011@code{MS_RDONLY} bit off).
1012
1013@item
1014@var{special_file} or @var{dir} is not accessible due to file permissions.
1015
1016@item
1017@var{special_file} is not accessible because it is in a filesystem that is
1018mounted with the @code{MS_NODEV} option.
1019
1020@end itemize
1021
1022@item EM_FILE
1023The table of dummy devices is full. @code{mount} needs to create a
1024dummy device (aka ``unnamed'' device) if the filesystem being mounted is
1025not one that uses a device.
1026
1027@end table
1028
1029@end deftypefun
1030
1031
faf2289f 1032@deftypefun {int} umount2 (const char *@var{file}, int @var{flags})
d08a7e4c 1033@standards{GNU, sys/mount.h}
6af8bab7
AO
1034@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1035@c Direct syscall.
faf2289f
UD
1036
1037@code{umount2} unmounts a filesystem.
1038
1039You can identify the filesystem to unmount either by the device special
1040file that contains the filesystem or by the mount point. The effect is
1041the same. Specify either as the string @var{file}.
1042
dbacafe5 1043@var{flags} contains the one-bit field identified by the following
faf2289f
UD
1044mask macro:
1045
2fe82ca6 1046@vtable @code
faf2289f
UD
1047
1048@item MNT_FORCE
1049This bit on means to force the unmounting even if the filesystem is
1050busy, by making it unbusy first. If the bit is off and the filesystem is
1051busy, @code{umount2} fails with @code{errno} = @code{EBUSY}. Depending
1052on the filesystem, this may override all, some, or no busy conditions.
1053
2fe82ca6 1054@end vtable
faf2289f
UD
1055
1056All other bits in @var{flags} should be set to zero; otherwise, the result
1057is undefined.
1058
1059Example:
1060
1061@smallexample
1062@group
1063#include <sys/mount.h>
1064
1065umount2("/mnt", MNT_FORCE);
1066
1067umount2("/dev/hdd1", 0);
1068
1069@end group
1070@end smallexample
1071
1072After the filesystem is unmounted, the directory that was the mount point
1073is visible, as are any files in it.
1074
1075As part of unmounting, @code{umount2} syncs the filesystem.
1076
1077If the unmounting is successful, the return value is zero. Otherwise, it
1078is @code{-1} and @code{errno} is set accordingly:
1079
1080@table @code
1081@item EPERM
1082The process is not superuser.
1083@item EBUSY
1084The filesystem cannot be unmounted because it is busy. E.g. it contains
1085a directory that is some process's working directory or a file that some
1086process has open. With some filesystems in some cases, you can avoid
1087this failure with the @code{MNT_FORCE} option.
1088
1089@item EINVAL
dbacafe5 1090@var{file} validly refers to a file, but that file is neither a mount
faf2289f
UD
1091point nor a device special file of a currently mounted filesystem.
1092
1093@end table
dbacafe5
UD
1094
1095This function is not available on all systems.
faf2289f
UD
1096@end deftypefun
1097
faf2289f 1098@deftypefun {int} umount (const char *@var{file})
d08a7e4c
RJ
1099@standards{SVID, sys/mount.h}
1100@standards{GNU, sys/mount.h}
6af8bab7
AO
1101@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1102@c Direct syscall or wrapper for umount2.
faf2289f 1103
dbacafe5
UD
1104@code{umount} does the same thing as @code{umount2} with @var{flags} set
1105to zeroes. It is more widely available than @code{umount2} but since it
1106lacks the possibility to forcefully unmount a filesystem is deprecated
1107when @code{umount2} is also available.
faf2289f 1108@end deftypefun