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