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