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