Update.
[glibc.git] / manual / sysinfo.texi
blobf35aa8b0c674abfd4f152ba3f647f30caef998fc
1 @node System Information, System Configuration, Users and Groups, Top
2 @chapter System Information
4 This chapter describes functions that return information about the
5 particular machine that is in use---the type of hardware, the type of
6 software, and the individual machine's name.
8 @menu
9 * Host Identification::         Determining the name of the machine.
10 * Hardware/Software Type ID::   Determining the hardware type of the
11                                  machine and what operating system it is
12                                  running.
13 * Filesystem handling::         Which is mounted and/or available?
14 @end menu
17 @node Host Identification
18 @section Host Identification
20 This section explains how to identify the particular machine that your
21 program is running on.  The identification of a machine consists of its
22 Internet host name and Internet address; see @ref{Internet Namespace}.
23 The host name should always be a fully qualified domain name, like
24 @w{@samp{crispy-wheats-n-chicken.ai.mit.edu}}, not a simple name like
25 just @w{@samp{crispy-wheats-n-chicken}}.
27 @pindex hostname
28 @pindex hostid
29 @pindex unistd.h
30 Prototypes for these functions appear in @file{unistd.h}.  The shell
31 commands @code{hostname} and @code{hostid} work by calling them.
33 @comment unistd.h
34 @comment BSD
35 @deftypefun int gethostname (char *@var{name}, size_t @var{size})
36 This function returns the name of the host machine in the array
37 @var{name}.  The @var{size} argument specifies the size of this array,
38 in bytes.
40 The return value is @code{0} on success and @code{-1} on failure.  In
41 the GNU C library, @code{gethostname} fails if @var{size} is not large
42 enough; then you can try again with a larger array.  The following
43 @code{errno} error condition is defined for this function:
45 @table @code
46 @item ENAMETOOLONG
47 The @var{size} argument is less than the size of the host name plus one.
48 @end table
50 @pindex sys/param.h
51 On some systems, there is a symbol for the maximum possible host name
52 length: @code{MAXHOSTNAMELEN}.  It is defined in @file{sys/param.h}.
53 But you can't count on this to exist, so it is cleaner to handle
54 failure and try again.
56 @code{gethostname} stores the beginning of the host name in @var{name}
57 even if the host name won't entirely fit.  For some purposes, a
58 truncated host name is good enough.  If it is, you can ignore the
59 error code.
60 @end deftypefun
62 @comment unistd.h
63 @comment BSD
64 @deftypefun int sethostname (const char *@var{name}, size_t @var{length})
65 The @code{sethostname} function sets the name of the host machine to
66 @var{name}, a string with length @var{length}.  Only privileged
67 processes are allowed to do this.  Usually it happens just once, at
68 system boot time.
70 The return value is @code{0} on success and @code{-1} on failure.
71 The following @code{errno} error condition is defined for this function:
73 @table @code
74 @item EPERM
75 This process cannot set the host name because it is not privileged.
76 @end table
77 @end deftypefun
79 @comment unistd.h
80 @comment BSD
81 @deftypefun {long int} gethostid (void)
82 This function returns the ``host ID'' of the machine the program is
83 running on.  By convention, this is usually the primary Internet address
84 of that machine, converted to a @w{@code{long int}}.  However, some
85 systems it is a meaningless but unique number which is hard-coded for
86 each machine.
87 @end deftypefun
89 @comment unistd.h
90 @comment BSD
91 @deftypefun int sethostid (long int @var{id})
92 The @code{sethostid} function sets the ``host ID'' of the host machine
93 to @var{id}.  Only privileged processes are allowed to do this.  Usually
94 it happens just once, at system boot time.
96 The return value is @code{0} on success and @code{-1} on failure.
97 The following @code{errno} error condition is defined for this function:
99 @table @code
100 @item EPERM
101 This process cannot set the host name because it is not privileged.
103 @item ENOSYS
104 The operating system does not support setting the host ID.  On some
105 systems, the host ID is a meaningless but unique number hard-coded for
106 each machine.
107 @end table
108 @end deftypefun
110 @node Hardware/Software Type ID
111 @section Hardware/Software Type Identification
113 You can use the @code{uname} function to find out some information about
114 the type of computer your program is running on.  This function and the
115 associated data type are declared in the header file
116 @file{sys/utsname.h}.
117 @pindex sys/utsname.h
119 @comment sys/utsname.h
120 @comment POSIX.1
121 @deftp {Data Type} {struct utsname}
122 The @code{utsname} structure is used to hold information returned
123 by the @code{uname} function.  It has the following members:
125 @table @code
126 @item char sysname[]
127 This is the name of the operating system in use.
129 @item char nodename[]
130 This is the network name of this particular computer.  In the GNU
131 library, the value is the same as that returned by @code{gethostname};
132 see @ref{Host Identification}.
134 @item char release[]
135 This is the current release level of the operating system implementation.
137 @item char version[]
138 This is the current version level within the release of the operating
139 system.
141 @item char machine[]
142 This is a description of the type of hardware that is in use.
144 Some systems provide a mechanism to interrogate the kernel directly for
145 this information.  On systems without such a mechanism, the GNU C
146 library fills in this field based on the configuration name that was
147 specified when building and installing the library.
149 GNU uses a three-part name to describe a system configuration; the three
150 parts are @var{cpu}, @var{manufacturer} and @var{system-type}, and they
151 are separated with dashes.  Any possible combination of three names is
152 potentially meaningful, but most such combinations are meaningless in
153 practice and even the meaningful ones are not necessarily supported by
154 any particular GNU program.
156 Since the value in @code{machine} is supposed to describe just the
157 hardware, it consists of the first two parts of the configuration name:
158 @samp{@var{cpu}-@var{manufacturer}}.  For example, it might be one of these:
160 @quotation
161 @code{"sparc-sun"},
162 @code{"i386-@var{anything}"},
163 @code{"m68k-hp"},
164 @code{"m68k-sony"},
165 @code{"m68k-sun"},
166 @code{"mips-dec"}
167 @end quotation
168 @end table
169 @end deftp
171 @comment sys/utsname.h
172 @comment POSIX.1
173 @deftypefun int uname (struct utsname *@var{info})
174 The @code{uname} function fills in the structure pointed to by
175 @var{info} with information about the operating system and host machine.
176 A non-negative value indicates that the data was successfully stored.
178 @code{-1} as the value indicates an error.  The only error possible is
179 @code{EFAULT}, which we normally don't mention as it is always a
180 possibility.
181 @end deftypefun
184 @node Filesystem handling
185 @section Which filesystems are mounted and/or available?
187 The Unix concept of @emph{Everything is a file} is based on the
188 possibility to @dfn{mount} filesystems or other things into the
189 filesystem.  For some programs it is desirable and necessary to access
190 the information whether and, if yes, where a certain filesystem is
191 mounted or simply to get lists of all the available filesystems.  The
192 GNU libc provides some functions to retrieve this information portably.
194 Traditionally Unix systems have a file named @file{/etc/fstab} which
195 describes all possibly mounted filesystems.  The @code{mount} program
196 uses this file to mount at startup time of the system all the necessary
197 filesystems.  The information about all the filesystems actually mounted
198 is normally kept in a file named @file{/etc/mtab}.  Both files share
199 the same syntax and it is crucial that this syntax is followed all the
200 time.  Therefore it is best to never directly write the files.  The
201 functions described in this section can do this and they also provide
202 the functionality to convert the external textual representation to the
203 internal representation.
205 @vindex _PATH_FSTAB
206 @vindex _PATH_MNTTAB
207 @vindex FSTAB
208 @vindex _PATH_MOUNTED
209 The filenames given above should never be used directly.  The portable
210 way to handle these file is to use the macros @code{_PATH_FSTAB},
211 defined in @file{fstab.h} and @code{_PATH_MNTTAB}, defined in
212 @file{mntent.h}, respectively.  There are also two alternate macro names
213 @code{FSTAB} and @code{_PATH_MOUNTED} defined but both names are
214 deprecated and kept only for backward compatibility.  The two former
215 names should always be used.
217 The internal representation for entries of the file is @w{@code{struct
218 fstab}}, defined in @file{fstab.h}.
220 @comment fstab.h
221 @comment BSD
222 @deftp {Data Type} {struct fstab}
223 This structure is used with the @code{getfsent}, @code{getfsspec}, and
224 @code{getfsfile} functions.
226 @table @code
227 @item char *fs_spec
228 This element describes the device from which the filesystem is mounted.
229 Normally this is the name of a special device, such as a hard disk
230 partition, but it could also be a more or less generic string.  For
231 @dfn{NFS} it would be a hostname and directory name combination.
233 Even though the element is not declared @code{const} it shouldn't be
234 modified.  The missing @code{const} has historic reasons, since this
235 function predates @w{ISO C}.  The same is true for the other string
236 elements of this structure.
238 @item char *fs_file
239 This describes the mount point on the local system.  I.e., accessing any
240 file in this filesystem has implicitly or explicitly this string as a
241 prefix.
243 @item char *fs_vfstype
244 This is the type of the filesystem.  Depending on what the underlying
245 kernel understands it can be any string.
247 @item char *fs_mntops
248 This is a string containing options passed to the kernel with the
249 @code{mount} call.  Again, this can be almost anything.  There can be
250 more than one option, separated from the others by a comma.  Each option
251 consists of a name and an optional value part, introduced by an @code{=}
252 character.
254 If the value of this element must be processed it should best happen
255 using the @code{getsubopt} function; see @ref{Suboptions}.
257 @item const char *fs_type
258 This name is poorly chosen.  This element points to a string (possibly
259 in the @code{fs_mntops} string) which describes the modes with which the
260 filesystem is mounted.  @file{fstab} defines five macros to describe the
261 possible values:
263 @vtable @code
264 @item FSTAB_RW
265 The filesystems gets mounted with read and write enabled.
266 @item FSTAB_RQ
267 The filesystems gets mounted with read and write enabled.  Write access
268 is restricted by quotas.
269 @item FSTAB_RO
270 The filesystem gets mounted read-only.
271 @item FSTAB_SW
272 This is not a real filesystem, it is a swap device.
273 @item FSTAB_XX
274 This entry from the @file{fstab} file is totally ignored.
275 @end vtable
277 Testing for equality with these value must happen using @code{strcmp}
278 since these are all strings.  Comparing the pointer will probably always
279 fail.
281 @item int fs_freq
282 This element describes the dump frequency in days.
284 @item int fs_passno
285 This element describes the pass number on parallel dumps.  It is closely
286 related to the @code{dump} utility used on Unix systems.
287 @end table
288 @end deftp
291 To read the entire content of the of the @file{fstab} file the GNU libc
292 contains a set of three functions which are designed in the usual way.
294 @comment fstab.h
295 @comment BSD
296 @deftypefun int setfsent (void)
297 This function makes sure that the internal read pointer for the
298 @file{fstab} file is at the beginning of the file.  This is done by
299 either opening the file or resetting the read pointer.
301 Since the file handle is internal to the libc this function is not
302 thread-safe.
304 This function returns a non-zero value if the operation was successful
305 and the @code{getfs*} functions can be used to read the entries of the
306 file.
307 @end deftypefun
309 @comment fstab.h
310 @comment BSD
311 @deftypefun void endfsent (void)
312 This function makes sure that all resources acquired by a prior call to
313 @code{setfsent} (explicitly or implicitly by calling @code{getfsent}) are
314 freed.
315 @end deftypefun
317 @comment fstab.h
318 @comment BSD
319 @deftypefun {struct fstab *} getfsent (void)
320 This function returns the next entry of the @file{fstab} file.  If this
321 is the first call to any of the functions handling @file{fstab} since
322 program start or the last call of @code{endfsent}, the file will be
323 opened.
325 The function returns a pointer to an variable of type @code{struct
326 fstab}.  This variable is shared by all threads and therefore this
327 function is not thread-safe.  If an error occurred @code{getfsent}
328 returns a @code{NULL} pointer.
329 @end deftypefun
331 @comment fstab.h
332 @comment BSD
333 @deftypefun {struct fstab *} getfsspec (const char *@var{name})
334 This function returns the next entry of the @file{fstab} file which has
335 a string equal to @var{name} pointed to by the @code{fs_spec} element.
336 Since there is normally exactly one entry for each special device it
337 makes no sense to call this function more than once for the same
338 argument.  If this is the first call to any of the functions handling
339 @file{fstab} since program start or the last call of @code{endfsent},
340 the file will be opened.
342 The function returns a pointer to an variable of type @code{struct
343 fstab}.  This variable is shared by all threads and therefore this
344 function is not thread-safe.  If an error occurred @code{getfsent}
345 returns a @code{NULL} pointer.
346 @end deftypefun
348 @comment fstab.h
349 @comment BSD
350 @deftypefun {struct fstab *} getfsfile (const char *@var{name})
351 This function returns the next entry of the @file{fstab} file which has
352 a string equal to @var{name} pointed to by the @code{fs_file} element.
353 Since there is normally exactly one entry for each mount point it
354 makes no sense to call this function more than once for the same
355 argument.  If this is the first call to any of the functions handling
356 @file{fstab} since program start or the last call of @code{endfsent},
357 the file will be opened.
359 The function returns a pointer to an variable of type @code{struct
360 fstab}.  This variable is shared by all threads and therefore this
361 function is not thread-safe.  If an error occurred @code{getfsent}
362 returns a @code{NULL} pointer.
363 @end deftypefun
365 To access the @file{mtab} file there is a different set of functions and
366 also a different structure to describe the results.
369 @comment fstab.h
370 @comment BSD
371 @deftp {Data Type} {struct mntent}
372 This structure is used with the @code{getmntent}, @code{getmntent_t},
373 @code{addmntent}, and @code{hasmntopt} functions.
375 @table @code
376 @item char *mnt_fsname
377 This element contains a pointer to a string describing the name of the
378 special device from which the filesystem is mounted.  It corresponds to
379 the @code{fs_spec} element in @code{struct fstab}.
381 @item char *mnt_dir
382 This element points to a string describing the mount point of the
383 filesystem.  It corresponds to the @code{fs_file} element in
384 @code{struct fstab}.
386 @item char *mnt_type
387 @code{mnt_type} describes the filesystem type and is therefore
388 equivalent to @code{fs_vfstype} in @code{struct fstab}.  @file{mntent.h}
389 defines a few symbolic names for some of the value this string can have.
390 But since the kernel can support an arbitrary filesystems it does not
391 make much sense to give them symbolic names.  If one knows the symbol
392 name one also knows the filesystem name.  Nevertheless here follows the
393 list of the symbol provided in @file{mntent.h}.
395 @vtable @code
396 @item MNTTYPE_IGNORE
397 This symbol expands to @code{"ignore"}.  The value is sometime used in
398 @file{fstab} files to make sure entries are not used without removing them.
399 @item MNTTYPE_NFS
400 Expands to @code{"nfs"}.  Using this macro sometimes could make sense
401 since it names the default NFS implementation, in case both version 2
402 and 3 are supported.
403 @item MNTTYPE_SWAP
404 This symbol expands to @code{"swap"}.  It names the special @file{fstab}
405 entry which names one of the possibly multiple swap partitions.
406 @end vtable
408 @item char *mnt_opts
409 The element contains a string describing the options used while mounting
410 the filesystem.  As for the equivalent element @code{fs_mntops} of
411 @code{struct fstab} it is best to use the function @code{getsubopt}
412 (@pxref{Suboptions}) to access the parts of this string.
414 The @file{mntent.h} file defines a number of macros with string values
415 which correspond to some of the options understood by the kernel.  There
416 might be many more options which are possible so it makes not much sense
417 to rely on these macros but to be consistent here is the list:
419 @vtable @code
420 @item MNTOPT_DEFAULTS
421 Expands to @code{"defaults"}.  This option should be used alone since it
422 indicates all values for the custumizable values are chosen to be the
423 default.
424 @item MNTOPT_RO
425 Expands to @code{"ro"}.  See the @code{FSTAB_RO} value, it means the
426 filesystem is mounted read-only.
427 @item MNTOPT_RW
428 Expand to @code{"rw"}.  See the @code{FSTAB_RW} value, it means the
429 filesystem is mounted with read and write permissions.
430 @item MNTOPT_SUID
431 Expands to @code{"suid"}.  This means that the SUID bit (@pxref{How
432 Change Persona}) is respected when a program from the filesystem is
433 started.
434 @item MNTOPT_NOSUID
435 Expands to @code{"nosuid"}.  This is the opposite of @code{MNTOPT_SUID},
436 the SUID bit for all files from the filesystem is ignored.
437 @item MNTOPT_NOAUTO
438 Expands to @code{"noauto"}.  At startup time the @code{mount} program
439 will ignore this entry if it is started with the @code{-a} option to
440 mount all filesystems mentioned in the @file{fstab} file.
441 @end vtable
443 As for the @code{FSTAB_*} entries introduced above it is important to
444 use @code{strcmp} to check for equality.
446 @item mnt_freq
447 This elements corresponds to @code{fs_freq} and also specifies the
448 frequency in days in which dumps are made.
450 @item mnt_passno
451 This element is equivalent to @code{fs_passno} with the same meaning
452 which is uninteresting for all programs beside @code{dump}.
453 @end table
454 @end deftp
456 For accessing the @file{mtab} file there is again a set of three
457 functions to access all entries in a row.  Unlike the functions to
458 handle @file{fstab} these functions do not access a fixed file and there
459 is even a thread safe variant of the get function.  Beside this the GNU
460 libc contains functions to alter the file and test for specific options.
462 @comment mntent.h
463 @comment BSD
464 @deftypefun {FILE *} setmntent (const char *@var{file}, const char *@var{mode})
465 The @code{setmntent} function prepares the file named @var{FILE} which
466 must be in the format of a @file{fstab} and @file{mtab} file for the
467 upcoming processing through the other functions of the family.  The
468 @var{mode} parameter can be chosen in the way the @var{opentype}
469 parameter for @code{fopen} (@pxref{Opening Streams}) can be chosen.  If
470 the file is opened for writing the file is also allowed to be empty.
472 If the file was successfully opened @code{setmntent} returns a file
473 descriptor for future use.  Otherwise the return value is @code{NULL}
474 and @code{errno} is set accordingly.
475 @end deftypefun
477 @comment mntent.h
478 @comment BSD
479 @deftypefun int endmntent (FILE *@var{stream})
480 This function takes for the @var{stream} parameter a file handle which
481 previously was returned from the @code{setmntent} call.
482 @code{endmntent} closes the stream and frees all resources.
484 The return value is @math{1} unless an error occurred in which case it
485 is @math{0}.
486 @end deftypefun
488 @comment mntent.h
489 @comment BSD
490 @deftypefun {struct mntent *} getmntent (FILE *@var{stream})
491 The @code{getmntent} function takes as the parameter a file handle
492 previously returned by successful call to @code{setmntent}.  It returns
493 a pointer to a static variable of type @code{struct mntent} which is
494 filled with the information from the next entry from the file currently
495 read.
497 If there was an error or the end of the file is reached the return value
498 is @code{NULL}.
500 This function is not thread-safe since all calls to this function return
501 a pointer to the same static variable.  @code{getmntent_r} should be
502 used in situations where multiple threads access the file.
503 @end deftypefun
505 @comment mntent.h
506 @comment BSD
507 @deftypefun {struct mntent *} getmntent_r (FILE *@var{stream}, struct mentent *@var{result}, char *@var{buffer}, int @var{bufsize})
508 The @code{getmntent_r} function is the reentrant variant of
509 @code{getmntent}.  It also returns the next entry from the file and
510 returns a pointer.  The actual variable the values are stored in is not
511 static, though.  Instead the function stores the values in the variable
512 pointed to by the @var{result} parameter.  Additional information (e.g.,
513 the strings pointed to by the elements of the result) are kept in the
514 buffer of size @var{bufsize} pointed to by @var{buffer}.
516 The function returns a @code{NULL} pointer in error cases.  Errors could be:
517 @itemize @bullet
518 @item
519 error while reading the file,
520 @item
521 end of file reached,
522 @item
523 @var{bufsize} is too small for reading a complete new entry.
524 @end itemize
525 @end deftypefun
527 @comment mntent.h
528 @comment BSD
529 @deftypefun int addmntent (FILE *@var{stream}, const struct mntent *@var{mnt})
530 The @code{addmntent} function allows to add a new entry to the file
531 previously opened with @code{setmntent}.  The new entries are always
532 appended.  I.e., even if the position of the file descriptor is not at
533 the end of the file this function does not overwrite an existing entry
534 following the current position.
536 The implication of this is that to remove an entry from a file one has
537 to create a new file while leaving out the entry to be removed and after
538 closing the file remove the old one and rename the new file to the
539 chosen name.
541 This function returns @math{0} in case the operation was successful.
542 Otherwise the return value is @math{1} and @code{errno} is set
543 appropriately.
544 @end deftypefun
546 @comment mntent.h
547 @comment BSD
548 @deftypefun {char *} hasmntopt (const struct mntent *@var{mnt}, const char *@var{opt})
549 This function can be used to check whether the string pointed to by the
550 @code{mnt_opts} element of the variable pointed to by @var{mnt} contains
551 the option @var{opt}.  If this is true a pointer to the beginning of the
552 option in the @code{mnt_opts} element is returned.  If no such option
553 exists the function returns @code{NULL}.
555 This function is useful to test whether a specific option is present but
556 when all options have to be processed one is better off with using the
557 @code{getsubopt} function to iterate over all options in the string.
558 @end deftypefun