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