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