]> sourceware.org Git - glibc.git/blame - manual/intro.texi
io: Fix destructive nature of tst-fchmod-errors
[glibc.git] / manual / intro.texi
CommitLineData
28f540f4
RM
1@node Introduction, Error Reporting, Top, Top
2@chapter Introduction
7a68c94a 3@c %MENU% Purpose of the GNU C Library
28f540f4
RM
4
5The C language provides no built-in facilities for performing such
6common operations as input/output, memory management, string
7manipulation, and the like. Instead, these facilities are defined
8in a standard @dfn{library}, which you compile and link with your
9programs.
10@cindex library
11
1f77f049 12@Theglibc{}, described in this document, defines all of the
f65fd747 13library functions that are specified by the @w{ISO C} standard, as well as
28f540f4 14additional features specific to POSIX and other derivatives of the Unix
a7a93d50 15operating system, and extensions specific to @gnusystems{}.
28f540f4
RM
16
17The purpose of this manual is to tell you how to use the facilities
1f77f049 18of @theglibc{}. We have mentioned which features belong to which
f2ea0f5b 19standards to help you identify things that are potentially non-portable
28f540f4
RM
20to other systems. But the emphasis in this manual is not on strict
21portability.
22
23@menu
24* Getting Started:: What this manual is for and how to use it.
25* Standards and Portability:: Standards and sources upon which the GNU
26 C library is based.
27* Using the Library:: Some practical uses for the library.
28* Roadmap to the Manual:: Overview of the remaining chapters in
29 this manual.
30@end menu
31
32@node Getting Started, Standards and Portability, , Introduction
33@section Getting Started
34
35This manual is written with the assumption that you are at least
36somewhat familiar with the C programming language and basic programming
f65fd747
UD
37concepts. Specifically, familiarity with ISO standard C
38(@pxref{ISO C}), rather than ``traditional'' pre-ISO C dialects, is
28f540f4
RM
39assumed.
40
1f77f049 41@Theglibc{} includes several @dfn{header files}, each of which
28f540f4
RM
42provides definitions and declarations for a group of related facilities;
43this information is used by the C compiler when processing your program.
44For example, the header file @file{stdio.h} declares facilities for
45performing input and output, and the header file @file{string.h}
46declares string processing utilities. The organization of this manual
47generally follows the same division as the header files.
48
49If you are reading this manual for the first time, you should read all
50of the introductory material and skim the remaining chapters. There are
1f77f049 51a @emph{lot} of functions in @theglibc{} and it's not realistic to
28f540f4
RM
52expect that you will be able to remember exactly @emph{how} to use each
53and every one of them. It's more important to become generally familiar
54with the kinds of facilities that the library provides, so that when you
55are writing your programs you can recognize @emph{when} to make use of
56library functions, and @emph{where} in this manual you can find more
57specific information about them.
58
59
60@node Standards and Portability, Using the Library, Getting Started, Introduction
61@section Standards and Portability
62@cindex standards
63
1f77f049
JM
64This section discusses the various standards and other sources that @theglibc{}
65is based upon. These sources include the @w{ISO C} and
28f540f4
RM
66POSIX standards, and the System V and Berkeley Unix implementations.
67
68The primary focus of this manual is to tell you how to make effective
1f77f049 69use of the @glibcadj{} facilities. But if you are concerned about
28f540f4
RM
70making your programs compatible with these standards, or portable to
71operating systems other than GNU, this can affect how you use the
72library. This section gives you an overview of these standards, so that
73you will know what they are when they are mentioned in other parts of
74the manual.
75
76@xref{Library Summary}, for an alphabetical list of the functions and
77other symbols provided by the library. This list also states which
78standards each function or symbol comes from.
79
80@menu
b13927da 81* ISO C:: The international standard for the C
f65fd747
UD
82 programming language.
83* POSIX:: The ISO/IEC 9945 (aka IEEE 1003) standards
84 for operating systems.
28f540f4 85* Berkeley Unix:: BSD and SunOS.
2c6fe0bd 86* SVID:: The System V Interface Description.
b13927da 87* XPG:: The X/Open Portability Guide.
6c0be743 88* Linux Kernel:: The Linux kernel.
28f540f4
RM
89@end menu
90
f65fd747
UD
91@node ISO C, POSIX, , Standards and Portability
92@subsection ISO C
93@cindex ISO C
28f540f4 94
1f77f049 95@Theglibc{} is compatible with the C standard adopted by the
28f540f4 96American National Standards Institute (ANSI):
f65fd747 97@cite{American National Standard X3.159-1989---``ANSI C''} and later
6d52618b 98by the International Standardization Organization (ISO):
f65fd747
UD
99@cite{ISO/IEC 9899:1990, ``Programming languages---C''}.
100We here refer to the standard as @w{ISO C} since this is the more
101general standard in respect of ratification.
1f77f049 102The header files and library facilities that make up @theglibc{} are
0005e54f 103a superset of those specified by the @w{ISO C} standard.
28f540f4
RM
104
105@pindex gcc
f65fd747 106If you are concerned about strict adherence to the @w{ISO C} standard, you
28f540f4 107should use the @samp{-ansi} option when you compile your programs with
f65fd747 108the GNU C compiler. This tells the compiler to define @emph{only} ISO
28f540f4
RM
109standard features from the library header files, unless you explicitly
110ask for additional features. @xref{Feature Test Macros}, for
111information on how to do this.
112
f65fd747
UD
113Being able to restrict the library to include only @w{ISO C} features is
114important because @w{ISO C} puts limitations on what names can be defined
28f540f4
RM
115by the library implementation, and the GNU extensions don't fit these
116limitations. @xref{Reserved Names}, for more information about these
117restrictions.
118
119This manual does not attempt to give you complete details on the
f65fd747 120differences between @w{ISO C} and older dialects. It gives advice on how
28f540f4
RM
121to write programs to work portably under multiple C dialects, but does
122not aim for completeness.
123
b13927da 124
f65fd747 125@node POSIX, Berkeley Unix, ISO C, Standards and Portability
28f540f4
RM
126@subsection POSIX (The Portable Operating System Interface)
127@cindex POSIX
128@cindex POSIX.1
129@cindex IEEE Std 1003.1
b13927da 130@cindex ISO/IEC 9945-1
28f540f4
RM
131@cindex POSIX.2
132@cindex IEEE Std 1003.2
b13927da 133@cindex ISO/IEC 9945-2
28f540f4 134
1f77f049 135@Theglibc{} is also compatible with the ISO @dfn{POSIX} family of
28f540f4 136standards, known more formally as the @dfn{Portable Operating System
b13927da
UD
137Interface for Computer Environments} (ISO/IEC 9945). They were also
138published as ANSI/IEEE Std 1003. POSIX is derived mostly from various
139versions of the Unix operating system.
28f540f4
RM
140
141The library facilities specified by the POSIX standards are a superset
f65fd747
UD
142of those required by @w{ISO C}; POSIX specifies additional features for
143@w{ISO C} functions, as well as specifying new additional functions. In
28f540f4
RM
144general, the additional requirements and functionality defined by the
145POSIX standards are aimed at providing lower-level support for a
146particular kind of operating system environment, rather than general
147programming language support which can run in many diverse operating
0005e54f 148system environments.
28f540f4 149
1f77f049 150@Theglibc{} implements all of the functions specified in
b13927da 151@cite{ISO/IEC 9945-1:1996, the POSIX System Application Program
28f540f4 152Interface}, commonly referred to as POSIX.1. The primary extensions to
f65fd747 153the @w{ISO C} facilities specified by this standard include file system
28f540f4
RM
154interface primitives (@pxref{File System Interface}), device-specific
155terminal control functions (@pxref{Low-Level Terminal Interface}), and
156process control functions (@pxref{Processes}).
157
b13927da 158Some facilities from @cite{ISO/IEC 9945-2:1993, the POSIX Shell and
1f77f049 159Utilities standard} (POSIX.2) are also implemented in @theglibc{}.
28f540f4
RM
160These include utilities for dealing with regular expressions and other
161pattern matching facilities (@pxref{Pattern Matching}).
162
0a57b83e
AO
163@menu
164* POSIX Safety Concepts:: Safety concepts from POSIX.
165* Unsafe Features:: Features that make functions unsafe.
166* Conditionally Safe Features:: Features that make functions unsafe
167 in the absence of workarounds.
168* Other Safety Remarks:: Additional safety features and remarks.
169@end menu
170
28f540f4
RM
171@comment Roland sez:
172@comment The GNU C library as it stands conforms to 1003.2 draft 11, which
173@comment specifies:
2c6fe0bd 174@comment
28f540f4
RM
175@comment Several new macros in <limits.h>.
176@comment popen, pclose
177@comment <regex.h> (which is not yet fully implemented--wait on this)
178@comment fnmatch
179@comment getopt
180@comment <glob.h>
181@comment <wordexp.h> (not yet implemented)
182@comment confstr
183
0a57b83e
AO
184@node POSIX Safety Concepts, Unsafe Features, , POSIX
185@subsubsection POSIX Safety Concepts
186@cindex POSIX Safety Concepts
187
188This manual documents various safety properties of @glibcadj{}
189functions, in lines that follow their prototypes and look like:
190
191@sampsafety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
192
193The properties are assessed according to the criteria set forth in the
194POSIX standard for such safety contexts as Thread-, Async-Signal- and
195Async-Cancel- -Safety. Intuitive definitions of these properties,
196attempting to capture the meaning of the standard definitions, follow.
197
198@itemize @bullet
199
200@item
201@cindex MT-Safe
202@cindex Thread-Safe
203@code{MT-Safe} or Thread-Safe functions are safe to call in the presence
204of other threads. MT, in MT-Safe, stands for Multi Thread.
205
206Being MT-Safe does not imply a function is atomic, nor that it uses any
207of the memory synchronization mechanisms POSIX exposes to users. It is
208even possible that calling MT-Safe functions in sequence does not yield
209an MT-Safe combination. For example, having a thread call two MT-Safe
210functions one right after the other does not guarantee behavior
211equivalent to atomic execution of a combination of both functions, since
212concurrent calls in other threads may interfere in a destructive way.
213
214Whole-program optimizations that could inline functions across library
215interfaces may expose unsafe reordering, and so performing inlining
216across the @glibcadj{} interface is not recommended. The documented
217MT-Safety status is not guaranteed under whole-program optimization.
218However, functions defined in user-visible headers are designed to be
219safe for inlining.
220
221
222@item
223@cindex AS-Safe
224@cindex Async-Signal-Safe
225@code{AS-Safe} or Async-Signal-Safe functions are safe to call from
226asynchronous signal handlers. AS, in AS-Safe, stands for Asynchronous
227Signal.
228
229Many functions that are AS-Safe may set @code{errno}, or modify the
230floating-point environment, because their doing so does not make them
231unsuitable for use in signal handlers. However, programs could
232misbehave should asynchronous signal handlers modify this thread-local
233state, and the signal handling machinery cannot be counted on to
234preserve it. Therefore, signal handlers that call functions that may
235set @code{errno} or modify the floating-point environment @emph{must}
236save their original values, and restore them before returning.
237
238
239@item
240@cindex AC-Safe
241@cindex Async-Cancel-Safe
242@code{AC-Safe} or Async-Cancel-Safe functions are safe to call when
243asynchronous cancellation is enabled. AC in AC-Safe stands for
244Asynchronous Cancellation.
245
246The POSIX standard defines only three functions to be AC-Safe, namely
247@code{pthread_cancel}, @code{pthread_setcancelstate}, and
248@code{pthread_setcanceltype}. At present @theglibc{} provides no
249guarantees beyond these three functions, but does document which
250functions are presently AC-Safe. This documentation is provided for use
251by @theglibc{} developers.
252
253Just like signal handlers, cancellation cleanup routines must configure
254the floating point environment they require. The routines cannot assume
255a floating point environment, particularly when asynchronous
256cancellation is enabled. If the configuration of the floating point
257environment cannot be performed atomically then it is also possible that
258the environment encountered is internally inconsistent.
259
260
261@item
262@cindex MT-Unsafe
263@cindex Thread-Unsafe
264@cindex AS-Unsafe
265@cindex Async-Signal-Unsafe
266@cindex AC-Unsafe
267@cindex Async-Cancel-Unsafe
268@code{MT-Unsafe}, @code{AS-Unsafe}, @code{AC-Unsafe} functions are not
269safe to call within the safety contexts described above. Calling them
270within such contexts invokes undefined behavior.
271
272Functions not explicitly documented as safe in a safety context should
273be regarded as Unsafe.
274
275
276@item
277@cindex Preliminary
278@code{Preliminary} safety properties are documented, indicating these
279properties may @emph{not} be counted on in future releases of
280@theglibc{}.
281
282Such preliminary properties are the result of an assessment of the
283properties of our current implementation, rather than of what is
284mandated and permitted by current and future standards.
285
286Although we strive to abide by the standards, in some cases our
287implementation is safe even when the standard does not demand safety,
288and in other cases our implementation does not meet the standard safety
289requirements. The latter are most likely bugs; the former, when marked
290as @code{Preliminary}, should not be counted on: future standards may
291require changes that are not compatible with the additional safety
292properties afforded by the current implementation.
293
294Furthermore, the POSIX standard does not offer a detailed definition of
295safety. We assume that, by ``safe to call'', POSIX means that, as long
296as the program does not invoke undefined behavior, the ``safe to call''
297function behaves as specified, and does not cause other functions to
298deviate from their specified behavior. We have chosen to use its loose
299definitions of safety, not because they are the best definitions to use,
300but because choosing them harmonizes this manual with POSIX.
301
302Please keep in mind that these are preliminary definitions and
303annotations, and certain aspects of the definitions are still under
304discussion and might be subject to clarification or change.
305
306Over time, we envision evolving the preliminary safety notes into stable
307commitments, as stable as those of our interfaces. As we do, we will
308remove the @code{Preliminary} keyword from safety notes. As long as the
309keyword remains, however, they are not to be regarded as a promise of
310future behavior.
311
312
313@end itemize
314
315Other keywords that appear in safety notes are defined in subsequent
316sections.
317
318
319@node Unsafe Features, Conditionally Safe Features, POSIX Safety Concepts, POSIX
320@subsubsection Unsafe Features
321@cindex Unsafe Features
322
323Functions that are unsafe to call in certain contexts are annotated with
324keywords that document their features that make them unsafe to call.
325AS-Unsafe features in this section indicate the functions are never safe
326to call when asynchronous signals are enabled. AC-Unsafe features
327indicate they are never safe to call when asynchronous cancellation is
328enabled. There are no MT-Unsafe marks in this section.
329
330@itemize @bullet
331
332@item @code{lock}
333@cindex lock
334
335Functions marked with @code{lock} as an AS-Unsafe feature may be
336interrupted by a signal while holding a non-recursive lock. If the
337signal handler calls another such function that takes the same lock, the
338result is a deadlock.
339
340Functions annotated with @code{lock} as an AC-Unsafe feature may, if
341cancelled asynchronously, fail to release a lock that would have been
342released if their execution had not been interrupted by asynchronous
343thread cancellation. Once a lock is left taken, attempts to take that
344lock will block indefinitely.
345
346
347@item @code{corrupt}
348@cindex corrupt
349
350Functions marked with @code{corrupt} as an AS-Unsafe feature may corrupt
351data structures and misbehave when they interrupt, or are interrupted
352by, another such function. Unlike functions marked with @code{lock},
353these take recursive locks to avoid MT-Safety problems, but this is not
354enough to stop a signal handler from observing a partially-updated data
355structure. Further corruption may arise from the interrupted function's
356failure to notice updates made by signal handlers.
357
358Functions marked with @code{corrupt} as an AC-Unsafe feature may leave
359data structures in a corrupt, partially updated state. Subsequent uses
360of the data structure may misbehave.
361
362@c A special case, probably not worth documenting separately, involves
363@c reallocing, or even freeing pointers. Any case involving free could
364@c be easily turned into an ac-safe leak by resetting the pointer before
365@c releasing it; I don't think we have any case that calls for this sort
366@c of fixing. Fixing the realloc cases would require a new interface:
367@c instead of @code{ptr=realloc(ptr,size)} we'd have to introduce
368@c @code{acsafe_realloc(&ptr,size)} that would modify ptr before
369@c releasing the old memory. The ac-unsafe realloc could be implemented
370@c in terms of an internal interface with this semantics (say
371@c __acsafe_realloc), but since realloc can be overridden, the function
372@c we call to implement realloc should not be this internal interface,
373@c but another internal interface that calls __acsafe_realloc if realloc
374@c was not overridden, and calls the overridden realloc with async
375@c cancel disabled. --lxoliva
376
377
378@item @code{heap}
379@cindex heap
380
381Functions marked with @code{heap} may call heap memory management
382functions from the @code{malloc}/@code{free} family of functions and are
383only as safe as those functions. This note is thus equivalent to:
384
385@sampsafety{@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
386
387
388@c Check for cases that should have used plugin instead of or in
389@c addition to this. Then, after rechecking gettext, adjust i18n if
390@c needed.
391@item @code{dlopen}
392@cindex dlopen
393
394Functions marked with @code{dlopen} use the dynamic loader to load
395shared libraries into the current execution image. This involves
396opening files, mapping them into memory, allocating additional memory,
397resolving symbols, applying relocations and more, all of this while
398holding internal dynamic loader locks.
399
400The locks are enough for these functions to be AS- and AC-Unsafe, but
401other issues may arise. At present this is a placeholder for all
402potential safety issues raised by @code{dlopen}.
403
404@c dlopen runs init and fini sections of the module; does this mean
405@c dlopen always implies plugin?
406
407
408@item @code{plugin}
409@cindex plugin
410
411Functions annotated with @code{plugin} may run code from plugins that
412may be external to @theglibc{}. Such plugin functions are assumed to be
413MT-Safe, AS-Unsafe and AC-Unsafe. Examples of such plugins are stack
414@cindex NSS
415unwinding libraries, name service switch (NSS) and character set
416@cindex iconv
417conversion (iconv) back-ends.
418
419Although the plugins mentioned as examples are all brought in by means
420of dlopen, the @code{plugin} keyword does not imply any direct
421involvement of the dynamic loader or the @code{libdl} interfaces, those
422are covered by @code{dlopen}. For example, if one function loads a
423module and finds the addresses of some of its functions, while another
424just calls those already-resolved functions, the former will be marked
425with @code{dlopen}, whereas the latter will get the @code{plugin}. When
426a single function takes all of these actions, then it gets both marks.
427
428
429@item @code{i18n}
430@cindex i18n
431
432Functions marked with @code{i18n} may call internationalization
433functions of the @code{gettext} family and will be only as safe as those
434functions. This note is thus equivalent to:
435
436@sampsafety{@mtsafe{@mtsenv{}}@asunsafe{@asucorrupt{} @ascuheap{} @ascudlopen{}}@acunsafe{@acucorrupt{}}}
437
438
439@item @code{timer}
440@cindex timer
441
442Functions marked with @code{timer} use the @code{alarm} function or
443similar to set a time-out for a system call or a long-running operation.
444In a multi-threaded program, there is a risk that the time-out signal
445will be delivered to a different thread, thus failing to interrupt the
446intended thread. Besides being MT-Unsafe, such functions are always
447AS-Unsafe, because calling them in signal handlers may interfere with
448timers set in the interrupted code, and AC-Unsafe, because there is no
449safe way to guarantee an earlier timer will be reset in case of
450asynchronous cancellation.
451
452@end itemize
453
454
455@node Conditionally Safe Features, Other Safety Remarks, Unsafe Features, POSIX
456@subsubsection Conditionally Safe Features
457@cindex Conditionally Safe Features
458
459For some features that make functions unsafe to call in certain
460contexts, there are known ways to avoid the safety problem other than
461refraining from calling the function altogether. The keywords that
462follow refer to such features, and each of their definitions indicate
463how the whole program needs to be constrained in order to remove the
464safety problem indicated by the keyword. Only when all the reasons that
465make a function unsafe are observed and addressed, by applying the
466documented constraints, does the function become safe to call in a
467context.
468
469@itemize @bullet
470
471@item @code{init}
472@cindex init
473
474Functions marked with @code{init} as an MT-Unsafe feature perform
475MT-Unsafe initialization when they are first called.
476
477Calling such a function at least once in single-threaded mode removes
478this specific cause for the function to be regarded as MT-Unsafe. If no
479other cause for that remains, the function can then be safely called
480after other threads are started.
481
482Functions marked with @code{init} as an AS- or AC-Unsafe feature use the
483internal @code{libc_once} machinery or similar to initialize internal
484data structures.
485
486If a signal handler interrupts such an initializer, and calls any
487function that also performs @code{libc_once} initialization, it will
488deadlock if the thread library has been loaded.
489
490Furthermore, if an initializer is partially complete before it is
491canceled or interrupted by a signal whose handler requires the same
492initialization, some or all of the initialization may be performed more
493than once, leaking resources or even resulting in corrupt internal data.
494
495Applications that need to call functions marked with @code{init} as an
496AS- or AC-Unsafe feature should ensure the initialization is performed
497before configuring signal handlers or enabling cancellation, so that the
498AS- and AC-Safety issues related with @code{libc_once} do not arise.
499
500@c We may have to extend the annotations to cover conditions in which
501@c initialization may or may not occur, since an initial call in a safe
502@c context is no use if the initialization doesn't take place at that
503@c time: it doesn't remove the risk for later calls.
504
505
506@item @code{race}
507@cindex race
508
509Functions annotated with @code{race} as an MT-Safety issue operate on
510objects in ways that may cause data races or similar forms of
511destructive interference out of concurrent execution. In some cases,
512the objects are passed to the functions by users; in others, they are
513used by the functions to return values to users; in others, they are not
514even exposed to users.
515
516We consider access to objects passed as (indirect) arguments to
517functions to be data race free. The assurance of data race free objects
518is the caller's responsibility. We will not mark a function as
519MT-Unsafe or AS-Unsafe if it misbehaves when users fail to take the
520measures required by POSIX to avoid data races when dealing with such
521objects. As a general rule, if a function is documented as reading from
522an object passed (by reference) to it, or modifying it, users ought to
523use memory synchronization primitives to avoid data races just as they
524would should they perform the accesses themselves rather than by calling
525the library function. @code{FILE} streams are the exception to the
526general rule, in that POSIX mandates the library to guard against data
527races in many functions that manipulate objects of this specific opaque
528type. We regard this as a convenience provided to users, rather than as
529a general requirement whose expectations should extend to other types.
530
531In order to remind users that guarding certain arguments is their
532responsibility, we will annotate functions that take objects of certain
533types as arguments. We draw the line for objects passed by users as
534follows: objects whose types are exposed to users, and that users are
535expected to access directly, such as memory buffers, strings, and
536various user-visible @code{struct} types, do @emph{not} give reason for
537functions to be annotated with @code{race}. It would be noisy and
538redundant with the general requirement, and not many would be surprised
539by the library's lack of internal guards when accessing objects that can
540be accessed directly by users.
541
542As for objects that are opaque or opaque-like, in that they are to be
543manipulated only by passing them to library functions (e.g.,
544@code{FILE}, @code{DIR}, @code{obstack}, @code{iconv_t}), there might be
545additional expectations as to internal coordination of access by the
546library. We will annotate, with @code{race} followed by a colon and the
547argument name, functions that take such objects but that do not take
548care of synchronizing access to them by default. For example,
549@code{FILE} stream @code{unlocked} functions will be annotated, but
550those that perform implicit locking on @code{FILE} streams by default
551will not, even though the implicit locking may be disabled on a
552per-stream basis.
553
554In either case, we will not regard as MT-Unsafe functions that may
555access user-supplied objects in unsafe ways should users fail to ensure
556the accesses are well defined. The notion prevails that users are
557expected to safeguard against data races any user-supplied objects that
558the library accesses on their behalf.
559
560@c The above describes @mtsrace; @mtasurace is described below.
561
562This user responsibility does not apply, however, to objects controlled
563by the library itself, such as internal objects and static buffers used
564to return values from certain calls. When the library doesn't guard
565them against concurrent uses, these cases are regarded as MT-Unsafe and
566AS-Unsafe (although the @code{race} mark under AS-Unsafe will be omitted
567as redundant with the one under MT-Unsafe). As in the case of
568user-exposed objects, the mark may be followed by a colon and an
569identifier. The identifier groups all functions that operate on a
570certain unguarded object; users may avoid the MT-Safety issues related
571with unguarded concurrent access to such internal objects by creating a
572non-recursive mutex related with the identifier, and always holding the
573mutex when calling any function marked as racy on that identifier, as
574they would have to should the identifier be an object under user
575control. The non-recursive mutex avoids the MT-Safety issue, but it
576trades one AS-Safety issue for another, so use in asynchronous signals
577remains undefined.
578
579When the identifier relates to a static buffer used to hold return
580values, the mutex must be held for as long as the buffer remains in use
581by the caller. Many functions that return pointers to static buffers
582offer reentrant variants that store return values in caller-supplied
583buffers instead. In some cases, such as @code{tmpname}, the variant is
584chosen not by calling an alternate entry point, but by passing a
585non-@code{NULL} pointer to the buffer in which the returned values are
586to be stored. These variants are generally preferable in multi-threaded
587programs, although some of them are not MT-Safe because of other
588internal buffers, also documented with @code{race} notes.
589
590
591@item @code{const}
592@cindex const
593
594Functions marked with @code{const} as an MT-Safety issue non-atomically
595modify internal objects that are better regarded as constant, because a
596substantial portion of @theglibc{} accesses them without
597synchronization. Unlike @code{race}, that causes both readers and
598writers of internal objects to be regarded as MT-Unsafe and AS-Unsafe,
599this mark is applied to writers only. Writers remain equally MT- and
600AS-Unsafe to call, but the then-mandatory constness of objects they
601modify enables readers to be regarded as MT-Safe and AS-Safe (as long as
602no other reasons for them to be unsafe remain), since the lack of
603synchronization is not a problem when the objects are effectively
604constant.
605
606The identifier that follows the @code{const} mark will appear by itself
607as a safety note in readers. Programs that wish to work around this
608safety issue, so as to call writers, may use a non-recursve
609@code{rwlock} associated with the identifier, and guard @emph{all} calls
610to functions marked with @code{const} followed by the identifier with a
611write lock, and @emph{all} calls to functions marked with the identifier
612by itself with a read lock. The non-recursive locking removes the
613MT-Safety problem, but it trades one AS-Safety problem for another, so
614use in asynchronous signals remains undefined.
615
616@c But what if, instead of marking modifiers with const:id and readers
617@c with just id, we marked writers with race:id and readers with ro:id?
618@c Instead of having to define each instance of “id”, we'd have a
619@c general pattern governing all such “id”s, wherein race:id would
620@c suggest the need for an exclusive/write lock to make the function
621@c safe, whereas ro:id would indicate “id” is expected to be read-only,
622@c but if any modifiers are called (while holding an exclusive lock),
623@c then ro:id-marked functions ought to be guarded with a read lock for
624@c safe operation. ro:env or ro:locale, for example, seems to convey
625@c more clearly the expectations and the meaning, than just env or
626@c locale.
627
628
629@item @code{sig}
630@cindex sig
631
632Functions marked with @code{sig} as a MT-Safety issue (that implies an
633identical AS-Safety issue, omitted for brevity) may temporarily install
634a signal handler for internal purposes, which may interfere with other
635uses of the signal, identified after a colon.
636
637This safety problem can be worked around by ensuring that no other uses
638of the signal will take place for the duration of the call. Holding a
639non-recursive mutex while calling all functions that use the same
640temporary signal; blocking that signal before the call and resetting its
641handler afterwards is recommended.
642
643There is no safe way to guarantee the original signal handler is
644restored in case of asynchronous cancellation, therefore so-marked
645functions are also AC-Unsafe.
646
647@c fixme: at least deferred cancellation should get it right, and would
648@c obviate the restoring bit below, and the qualifier above.
649
650Besides the measures recommended to work around the MT- and AS-Safety
651problem, in order to avert the cancellation problem, disabling
652asynchronous cancellation @emph{and} installing a cleanup handler to
653restore the signal to the desired state and to release the mutex are
654recommended.
655
656
657@item @code{term}
658@cindex term
659
660Functions marked with @code{term} as an MT-Safety issue may change the
661terminal settings in the recommended way, namely: call @code{tcgetattr},
662modify some flags, and then call @code{tcsetattr}; this creates a window
663in which changes made by other threads are lost. Thus, functions marked
664with @code{term} are MT-Unsafe. The same window enables changes made by
665asynchronous signals to be lost. These functions are also AS-Unsafe,
666but the corresponding mark is omitted as redundant.
667
668It is thus advisable for applications using the terminal to avoid
669concurrent and reentrant interactions with it, by not using it in signal
670handlers or blocking signals that might use it, and holding a lock while
671calling these functions and interacting with the terminal. This lock
672should also be used for mutual exclusion with functions marked with
06e90b14
AO
673@code{@mtasurace{:tcattr(fd)}}, where @var{fd} is a file descriptor for
674the controlling terminal. The caller may use a single mutex for
675simplicity, or use one mutex per terminal, even if referenced by
676different file descriptors.
0a57b83e
AO
677
678Functions marked with @code{term} as an AC-Safety issue are supposed to
679restore terminal settings to their original state, after temporarily
680changing them, but they may fail to do so if cancelled.
681
682@c fixme: at least deferred cancellation should get it right, and would
683@c obviate the restoring bit below, and the qualifier above.
684
685Besides the measures recommended to work around the MT- and AS-Safety
686problem, in order to avert the cancellation problem, disabling
687asynchronous cancellation @emph{and} installing a cleanup handler to
688restore the terminal settings to the original state and to release the
689mutex are recommended.
690
691
692@end itemize
693
694
695@node Other Safety Remarks, , Conditionally Safe Features, POSIX
696@subsubsection Other Safety Remarks
697@cindex Other Safety Remarks
698
699Additional keywords may be attached to functions, indicating features
700that do not make a function unsafe to call, but that may need to be
701taken into account in certain classes of programs:
702
703@itemize @bullet
704
0a57b83e
AO
705@item @code{locale}
706@cindex locale
707
708Functions annotated with @code{locale} as an MT-Safety issue read from
709the locale object without any form of synchronization. Functions
710annotated with @code{locale} called concurrently with locale changes may
711behave in ways that do not correspond to any of the locales active
712during their execution, but an unpredictable mix thereof.
713
714We do not mark these functions as MT- or AS-Unsafe, however, because
715functions that modify the locale object are marked with
716@code{const:locale} and regarded as unsafe. Being unsafe, the latter
717are not to be called when multiple threads are running or asynchronous
718signals are enabled, and so the locale can be considered effectively
719constant in these contexts, which makes the former safe.
720
721@c Should the locking strategy suggested under @code{const} be used,
722@c failure to guard locale uses is not as fatal as data races in
723@c general: unguarded uses will @emph{not} follow dangling pointers or
724@c access uninitialized, unmapped or recycled memory. Each access will
725@c read from a consistent locale object that is or was active at some
726@c point during its execution. Without synchronization, however, it
727@c cannot even be assumed that, after a change in locale, earlier
728@c locales will no longer be used, even after the newly-chosen one is
729@c used in the thread. Nevertheless, even though unguarded reads from
730@c the locale will not violate type safety, functions that access the
731@c locale multiple times may invoke all sorts of undefined behavior
732@c because of the unexpected locale changes.
733
734
0a57b83e
AO
735@item @code{env}
736@cindex env
737
738Functions marked with @code{env} as an MT-Safety issue access the
739environment with @code{getenv} or similar, without any guards to ensure
740safety in the presence of concurrent modifications.
741
742We do not mark these functions as MT- or AS-Unsafe, however, because
743functions that modify the environment are all marked with
744@code{const:env} and regarded as unsafe. Being unsafe, the latter are
745not to be called when multiple threads are running or asynchronous
746signals are enabled, and so the environment can be considered
747effectively constant in these contexts, which makes the former safe.
748
749
750@item @code{hostid}
751@cindex hostid
752
753The function marked with @code{hostid} as an MT-Safety issue reads from
754the system-wide data structures that hold the ``host ID'' of the
755machine. These data structures cannot generally be modified atomically.
756Since it is expected that the ``host ID'' will not normally change, the
757function that reads from it (@code{gethostid}) is regarded as safe,
758whereas the function that modifies it (@code{sethostid}) is marked with
759@code{@mtasuconst{:@mtshostid{}}}, indicating it may require special
760care if it is to be called. In this specific case, the special care
761amounts to system-wide (not merely intra-process) coordination.
762
763
764@item @code{sigintr}
765@cindex sigintr
766
767Functions marked with @code{sigintr} as an MT-Safety issue access the
768@code{_sigintr} internal data structure without any guards to ensure
769safety in the presence of concurrent modifications.
770
771We do not mark these functions as MT- or AS-Unsafe, however, because
772functions that modify the this data structure are all marked with
773@code{const:sigintr} and regarded as unsafe. Being unsafe, the latter
774are not to be called when multiple threads are running or asynchronous
775signals are enabled, and so the data structure can be considered
776effectively constant in these contexts, which makes the former safe.
777
778
779@item @code{fd}
780@cindex fd
781
782Functions annotated with @code{fd} as an AC-Safety issue may leak file
783descriptors if asynchronous thread cancellation interrupts their
784execution.
785
786Functions that allocate or deallocate file descriptors will generally be
787marked as such. Even if they attempted to protect the file descriptor
788allocation and deallocation with cleanup regions, allocating a new
789descriptor and storing its number where the cleanup region could release
790it cannot be performed as a single atomic operation. Similarly,
791releasing the descriptor and taking it out of the data structure
792normally responsible for releasing it cannot be performed atomically.
793There will always be a window in which the descriptor cannot be released
794because it was not stored in the cleanup handler argument yet, or it was
795already taken out before releasing it. It cannot be taken out after
796release: an open descriptor could mean either that the descriptor still
797has to be closed, or that it already did so but the descriptor was
798reallocated by another thread or signal handler.
799
800Such leaks could be internally avoided, with some performance penalty,
801by temporarily disabling asynchronous thread cancellation. However,
802since callers of allocation or deallocation functions would have to do
803this themselves, to avoid the same sort of leak in their own layer, it
804makes more sense for the library to assume they are taking care of it
805than to impose a performance penalty that is redundant when the problem
806is solved in upper layers, and insufficient when it is not.
807
808This remark by itself does not cause a function to be regarded as
809AC-Unsafe. However, cumulative effects of such leaks may pose a
810problem for some programs. If this is the case, suspending asynchronous
811cancellation for the duration of calls to such functions is recommended.
812
813
814@item @code{mem}
815@cindex mem
816
817Functions annotated with @code{mem} as an AC-Safety issue may leak
818memory if asynchronous thread cancellation interrupts their execution.
819
820The problem is similar to that of file descriptors: there is no atomic
821interface to allocate memory and store its address in the argument to a
822cleanup handler, or to release it and remove its address from that
823argument, without at least temporarily disabling asynchronous
824cancellation, which these functions do not do.
825
826This remark does not by itself cause a function to be regarded as
827generally AC-Unsafe. However, cumulative effects of such leaks may be
828severe enough for some programs that disabling asynchronous cancellation
829for the duration of calls to such functions may be required.
830
831
832@item @code{cwd}
833@cindex cwd
834
835Functions marked with @code{cwd} as an MT-Safety issue may temporarily
836change the current working directory during their execution, which may
837cause relative pathnames to be resolved in unexpected ways in other
838threads or within asynchronous signal or cancellation handlers.
839
840This is not enough of a reason to mark so-marked functions as MT- or
841AS-Unsafe, but when this behavior is optional (e.g., @code{nftw} with
842@code{FTW_CHDIR}), avoiding the option may be a good alternative to
843using full pathnames or file descriptor-relative (e.g. @code{openat})
844system calls.
845
846
847@item @code{!posix}
848@cindex !posix
849
850This remark, as an MT-, AS- or AC-Safety note to a function, indicates
851the safety status of the function is known to differ from the specified
852status in the POSIX standard. For example, POSIX does not require a
853function to be Safe, but our implementation is, or vice-versa.
854
855For the time being, the absence of this remark does not imply the safety
856properties we documented are identical to those mandated by POSIX for
857the corresponding functions.
858
859
06e90b14
AO
860@item @code{:identifier}
861@cindex :identifier
862
863Annotations may sometimes be followed by identifiers, intended to group
864several functions that e.g. access the data structures in an unsafe way,
865as in @code{race} and @code{const}, or to provide more specific
866information, such as naming a signal in a function marked with
867@code{sig}. It is envisioned that it may be applied to @code{lock} and
868@code{corrupt} as well in the future.
869
870In most cases, the identifier will name a set of functions, but it may
871name global objects or function arguments, or identifiable properties or
872logical components associated with them, with a notation such as
873e.g. @code{:buf(arg)} to denote a buffer associated with the argument
874@var{arg}, or @code{:tcattr(fd)} to denote the terminal attributes of a
875file descriptor @var{fd}.
876
877The most common use for identifiers is to provide logical groups of
878functions and arguments that need to be protected by the same
879synchronization primitive in order to ensure safe operation in a given
880context.
881
882
883@item @code{/condition}
884@cindex /condition
885
886Some safety annotations may be conditional, in that they only apply if a
887boolean expression involving arguments, global variables or even the
5a1021e8 888underlying kernel evaluates to true. Such conditions as
06e90b14
AO
889@code{/hurd} or @code{/!linux!bsd} indicate the preceding marker only
890applies when the underlying kernel is the HURD, or when it is neither
891Linux nor a BSD kernel, respectively. @code{/!ps} and
892@code{/one_per_line} indicate the preceding marker only applies when
893argument @var{ps} is NULL, or global variable @var{one_per_line} is
894nonzero.
895
896When all marks that render a function unsafe are adorned with such
897conditions, and none of the named conditions hold, then the function can
898be regarded as safe.
899
900
0a57b83e
AO
901@end itemize
902
28f540f4
RM
903
904@node Berkeley Unix, SVID, POSIX, Standards and Portability
905@subsection Berkeley Unix
906@cindex BSD Unix
907@cindex 4.@var{n} BSD Unix
908@cindex Berkeley Unix
909@cindex SunOS
910@cindex Unix, Berkeley
911
1f77f049 912@Theglibc{} defines facilities from some versions of Unix which
28f540f4
RM
913are not formally standardized, specifically from the 4.2 BSD, 4.3 BSD,
914and 4.4 BSD Unix systems (also known as @dfn{Berkeley Unix}) and from
915@dfn{SunOS} (a popular 4.2 BSD derivative that includes some Unix System
f65fd747 916V functionality). These systems support most of the @w{ISO C} and POSIX
28f540f4
RM
917facilities, and 4.4 BSD and newer releases of SunOS in fact support them all.
918
919The BSD facilities include symbolic links (@pxref{Symbolic Links}), the
920@code{select} function (@pxref{Waiting for I/O}), the BSD signal
921functions (@pxref{BSD Signal Handling}), and sockets (@pxref{Sockets}).
922
b13927da 923@node SVID, XPG, Berkeley Unix, Standards and Portability
28f540f4
RM
924@subsection SVID (The System V Interface Description)
925@cindex SVID
926@cindex System V Unix
927@cindex Unix, System V
928
929The @dfn{System V Interface Description} (SVID) is a document describing
930the AT&T Unix System V operating system. It is to some extent a
931superset of the POSIX standard (@pxref{POSIX}).
932
1f77f049 933@Theglibc{} defines most of the facilities required by the SVID
f65fd747 934that are not also required by the @w{ISO C} or POSIX standards, for
28f540f4
RM
935compatibility with System V Unix and other Unix systems (such as
936SunOS) which include these facilities. However, many of the more
937obscure and less generally useful facilities required by the SVID are
938not included. (In fact, Unix System V itself does not provide them all.)
939
b13927da
UD
940The supported facilities from System V include the methods for
941inter-process communication and shared memory, the @code{hsearch} and
942@code{drand48} families of functions, @code{fmtmsg} and several of the
943mathematical functions.
944
6c0be743 945@node XPG, Linux Kernel, SVID, Standards and Portability
b13927da
UD
946@subsection XPG (The X/Open Portability Guide)
947
948The X/Open Portability Guide, published by the X/Open Company, Ltd., is
949a more general standard than POSIX. X/Open owns the Unix copyright and
950the XPG specifies the requirements for systems which are intended to be
951a Unix system.
952
1f77f049 953@Theglibc{} complies to the X/Open Portability Guide, Issue 4.2,
714a562f 954with all extensions common to XSI (X/Open System Interface)
b13927da
UD
955compliant systems and also all X/Open UNIX extensions.
956
957The additions on top of POSIX are mainly derived from functionality
958available in @w{System V} and BSD systems. Some of the really bad
959mistakes in @w{System V} systems were corrected, though. Since
960fulfilling the XPG standard with the Unix extensions is a
961precondition for getting the Unix brand chances are good that the
962functionality is available on commercial systems.
28f540f4 963
6c0be743
DD
964@node Linux Kernel, , XPG, Standards and Portability
965@subsection Linux (The Linux Kernel)
966
967@Theglibc{} includes by reference the Linux man-pages
968@value{man_pages_version} documentation to document the listed
969syscalls for the Linux kernel. For reference purposes only the latest
970@uref{https://www.kernel.org/doc/man-pages/,Linux man-pages Project}
971documentation can be accessed from the
972@uref{https://www.kernel.org,Linux kernel} website. Where the syscall
973has more specific documentation in this manual that more specific
974documentation is considered authoritative.
975
976Additional details on the Linux system call interface can be found in
977@xref{System Calls}.
28f540f4
RM
978
979@node Using the Library, Roadmap to the Manual, Standards and Portability, Introduction
980@section Using the Library
981
982This section describes some of the practical issues involved in using
1f77f049 983@theglibc{}.
28f540f4
RM
984
985@menu
986* Header Files:: How to include the header files in your
2c6fe0bd 987 programs.
28f540f4
RM
988* Macro Definitions:: Some functions in the library may really
989 be implemented as macros.
990* Reserved Names:: The C standard reserves some names for
991 the library, and some for users.
992* Feature Test Macros:: How to control what names are defined.
993@end menu
994
995@node Header Files, Macro Definitions, , Using the Library
996@subsection Header Files
997@cindex header files
998
999Libraries for use by C programs really consist of two parts: @dfn{header
1000files} that define types and macros and declare variables and
1001functions; and the actual library or @dfn{archive} that contains the
1002definitions of the variables and functions.
1003
1004(Recall that in C, a @dfn{declaration} merely provides information that
1005a function or variable exists and gives its type. For a function
1006declaration, information about the types of its arguments might be
1007provided as well. The purpose of declarations is to allow the compiler
1008to correctly process references to the declared variables and functions.
1009A @dfn{definition}, on the other hand, actually allocates storage for a
1010variable or says what a function does.)
1011@cindex definition (compared to declaration)
1012@cindex declaration (compared to definition)
1013
1f77f049 1014In order to use the facilities in @theglibc{}, you should be sure
28f540f4
RM
1015that your program source files include the appropriate header files.
1016This is so that the compiler has declarations of these facilities
1017available and can correctly process references to them. Once your
1018program has been compiled, the linker resolves these references to
1019the actual definitions provided in the archive file.
1020
1021Header files are included into a program source file by the
1022@samp{#include} preprocessor directive. The C language supports two
1023forms of this directive; the first,
1024
1025@smallexample
1026#include "@var{header}"
1027@end smallexample
1028
1029@noindent
1030is typically used to include a header file @var{header} that you write
1031yourself; this would contain definitions and declarations describing the
1032interfaces between the different parts of your particular application.
1033By contrast,
1034
1035@smallexample
1036#include <file.h>
1037@end smallexample
1038
1039@noindent
1040is typically used to include a header file @file{file.h} that contains
1041definitions and declarations for a standard library. This file would
1042normally be installed in a standard place by your system administrator.
1043You should use this second form for the C library header files.
1044
1045Typically, @samp{#include} directives are placed at the top of the C
1046source file, before any other code. If you begin your source files with
1047some comments explaining what the code in the file does (a good idea),
1048put the @samp{#include} directives immediately afterwards, following the
1049feature test macro definition (@pxref{Feature Test Macros}).
1050
1051For more information about the use of header files and @samp{#include}
1052directives, @pxref{Header Files,,, cpp.info, The GNU C Preprocessor
0005e54f 1053Manual}.
28f540f4 1054
1f77f049 1055@Theglibc{} provides several header files, each of which contains
28f540f4
RM
1056the type and macro definitions and variable and function declarations
1057for a group of related facilities. This means that your programs may
1058need to include several header files, depending on exactly which
1059facilities you are using.
1060
1061Some library header files include other library header files
1062automatically. However, as a matter of programming style, you should
1063not rely on this; it is better to explicitly include all the header
1f77f049
JM
1064files required for the library facilities you are using. The @glibcadj{}
1065header files have been written in such a way that it doesn't
28f540f4
RM
1066matter if a header file is accidentally included more than once;
1067including a header file a second time has no effect. Likewise, if your
1068program needs to include multiple header files, the order in which they
1069are included doesn't matter.
1070
1071@strong{Compatibility Note:} Inclusion of standard header files in any
f65fd747 1072order and any number of times works in any @w{ISO C} implementation.
28f540f4
RM
1073However, this has traditionally not been the case in many older C
1074implementations.
1075
1076Strictly speaking, you don't @emph{have to} include a header file to use
1077a function it declares; you could declare the function explicitly
1078yourself, according to the specifications in this manual. But it is
1079usually better to include the header file because it may define types
1080and macros that are not otherwise available and because it may define
1081more efficient macro replacements for some functions. It is also a sure
1082way to have the correct declaration.
1083
1084@node Macro Definitions, Reserved Names, Header Files, Using the Library
1085@subsection Macro Definitions of Functions
1086@cindex shadowing functions with macros
1087@cindex removing macros that shadow functions
1088@cindex undefining macros that shadow functions
1089
1090If we describe something as a function in this manual, it may have a
1091macro definition as well. This normally has no effect on how your
1092program runs---the macro definition does the same thing as the function
1093would. In particular, macro equivalents for library functions evaluate
1094arguments exactly once, in the same way that a function call would. The
1095main reason for these macro definitions is that sometimes they can
1096produce an inline expansion that is considerably faster than an actual
1097function call.
1098
1099Taking the address of a library function works even if it is also
1100defined as a macro. This is because, in this context, the name of the
1101function isn't followed by the left parenthesis that is syntactically
1102necessary to recognize a macro call.
1103
1104You might occasionally want to avoid using the macro definition of a
1105function---perhaps to make your program easier to debug. There are
1106two ways you can do this:
1107
1108@itemize @bullet
2c6fe0bd 1109@item
28f540f4
RM
1110You can avoid a macro definition in a specific use by enclosing the name
1111of the function in parentheses. This works because the name of the
1112function doesn't appear in a syntactic context where it is recognizable
1113as a macro call.
1114
1115@item
1116You can suppress any macro definition for a whole source file by using
1117the @samp{#undef} preprocessor directive, unless otherwise stated
1118explicitly in the description of that facility.
1119@end itemize
1120
1121For example, suppose the header file @file{stdlib.h} declares a function
1122named @code{abs} with
1123
1124@smallexample
1125extern int abs (int);
1126@end smallexample
1127
1128@noindent
1129and also provides a macro definition for @code{abs}. Then, in:
1130
1131@smallexample
1132#include <stdlib.h>
b13927da 1133int f (int *i) @{ return abs (++*i); @}
28f540f4
RM
1134@end smallexample
1135
1136@noindent
1137the reference to @code{abs} might refer to either a macro or a function.
1138On the other hand, in each of the following examples the reference is
1139to a function and not a macro.
1140
1141@smallexample
1142#include <stdlib.h>
b13927da 1143int g (int *i) @{ return (abs) (++*i); @}
28f540f4
RM
1144
1145#undef abs
b13927da 1146int h (int *i) @{ return abs (++*i); @}
28f540f4
RM
1147@end smallexample
1148
1149Since macro definitions that double for a function behave in
1150exactly the same way as the actual function version, there is usually no
1151need for any of these methods. In fact, removing macro definitions usually
1152just makes your program slower.
1153
1154
1155@node Reserved Names, Feature Test Macros, Macro Definitions, Using the Library
1156@subsection Reserved Names
1157@cindex reserved names
1158@cindex name space
1159
1160The names of all library types, macros, variables and functions that
f65fd747 1161come from the @w{ISO C} standard are reserved unconditionally; your program
28f540f4
RM
1162@strong{may not} redefine these names. All other library names are
1163reserved if your program explicitly includes the header file that
1164defines or declares them. There are several reasons for these
1165restrictions:
1166
1167@itemize @bullet
1168@item
1169Other people reading your code could get very confused if you were using
1170a function named @code{exit} to do something completely different from
1171what the standard @code{exit} function does, for example. Preventing
1172this situation helps to make your programs easier to understand and
1173contributes to modularity and maintainability.
1174
1175@item
1176It avoids the possibility of a user accidentally redefining a library
1177function that is called by other library functions. If redefinition
1178were allowed, those other functions would not work properly.
1179
1180@item
1181It allows the compiler to do whatever special optimizations it pleases
1182on calls to these functions, without the possibility that they may have
1183been redefined by the user. Some library facilities, such as those for
1184dealing with variadic arguments (@pxref{Variadic Functions})
1185and non-local exits (@pxref{Non-Local Exits}), actually require a
1186considerable amount of cooperation on the part of the C compiler, and
0bc93a2f
AJ
1187with respect to the implementation, it might be easier for the compiler
1188to treat these as built-in parts of the language.
28f540f4
RM
1189@end itemize
1190
1191In addition to the names documented in this manual, reserved names
1192include all external identifiers (global functions and variables) that
1193begin with an underscore (@samp{_}) and all identifiers regardless of
1194use that begin with either two underscores or an underscore followed by
1195a capital letter are reserved names. This is so that the library and
1196header files can define functions, variables, and macros for internal
1197purposes without risk of conflict with names in user programs.
1198
1199Some additional classes of identifier names are reserved for future
1200extensions to the C language or the POSIX.1 environment. While using these
1201names for your own purposes right now might not cause a problem, they do
1202raise the possibility of conflict with future versions of the C
1203or POSIX standards, so you should avoid these names.
1204
1205@itemize @bullet
2c6fe0bd 1206@item
28f540f4
RM
1207Names beginning with a capital @samp{E} followed a digit or uppercase
1208letter may be used for additional error code names. @xref{Error
1209Reporting}.
1210
1211@item
1212Names that begin with either @samp{is} or @samp{to} followed by a
1213lowercase letter may be used for additional character testing and
1214conversion functions. @xref{Character Handling}.
1215
1216@item
1217Names that begin with @samp{LC_} followed by an uppercase letter may be
1218used for additional macros specifying locale attributes.
1219@xref{Locales}.
1220
1221@item
1222Names of all existing mathematics functions (@pxref{Mathematics})
1223suffixed with @samp{f} or @samp{l} are reserved for corresponding
1224functions that operate on @code{float} and @code{long double} arguments,
1225respectively.
1226
1227@item
1228Names that begin with @samp{SIG} followed by an uppercase letter are
1229reserved for additional signal names. @xref{Standard Signals}.
1230
1231@item
1232Names that begin with @samp{SIG_} followed by an uppercase letter are
1233reserved for additional signal actions. @xref{Basic Signal Handling}.
1234
1235@item
1236Names beginning with @samp{str}, @samp{mem}, or @samp{wcs} followed by a
1237lowercase letter are reserved for additional string and array functions.
1238@xref{String and Array Utilities}.
1239
1240@item
1241Names that end with @samp{_t} are reserved for additional type names.
1242@end itemize
1243
1244In addition, some individual header files reserve names beyond
1245those that they actually define. You only need to worry about these
1246restrictions if your program includes that particular header file.
1247
1248@itemize @bullet
1249@item
1250The header file @file{dirent.h} reserves names prefixed with
1251@samp{d_}.
1252@pindex dirent.h
1253
1254@item
1255The header file @file{fcntl.h} reserves names prefixed with
1256@samp{l_}, @samp{F_}, @samp{O_}, and @samp{S_}.
1257@pindex fcntl.h
1258
1259@item
1260The header file @file{grp.h} reserves names prefixed with @samp{gr_}.
1261@pindex grp.h
1262
1263@item
1264The header file @file{limits.h} reserves names suffixed with @samp{_MAX}.
1265@pindex limits.h
1266
1267@item
1268The header file @file{pwd.h} reserves names prefixed with @samp{pw_}.
1269@pindex pwd.h
1270
1271@item
1272The header file @file{signal.h} reserves names prefixed with @samp{sa_}
1273and @samp{SA_}.
1274@pindex signal.h
1275
1276@item
1277The header file @file{sys/stat.h} reserves names prefixed with @samp{st_}
1278and @samp{S_}.
1279@pindex sys/stat.h
1280
1281@item
1282The header file @file{sys/times.h} reserves names prefixed with @samp{tms_}.
1283@pindex sys/times.h
1284
1285@item
1286The header file @file{termios.h} reserves names prefixed with @samp{c_},
1287@samp{V}, @samp{I}, @samp{O}, and @samp{TC}; and names prefixed with
1288@samp{B} followed by a digit.
1289@pindex termios.h
1290@end itemize
1291
1292@comment Include the section on Creature Nest Macros.
28f540f4
RM
1293@include creature.texi
1294
1295@node Roadmap to the Manual, , Using the Library, Introduction
1296@section Roadmap to the Manual
1297
1298Here is an overview of the contents of the remaining chapters of
1299this manual.
1300
c54e5cf7
CD
1301@c The chapter overview ordering is:
1302@c Error Reporting (2)
1303@c Virtual Memory Allocation and Paging (3)
1304@c Character Handling (4)
1305@c Strings and Array Utilities (5)
1306@c Character Set Handling (6)
1307@c Locales and Internationalization (7)
1308@c Searching and Sorting (9)
1309@c Pattern Matching (10)
1310@c Input/Output Overview (11)
1311@c Input/Output on Streams (12)
64d9ebae 1312@c Low-level Input/Output (13)
c54e5cf7
CD
1313@c File System Interface (14)
1314@c Pipes and FIFOs (15)
1315@c Sockets (16)
1316@c Low-Level Terminal Interface (17)
1317@c Syslog (18)
1318@c Mathematics (19)
64d9ebae 1319@c Arithmetic Functions (20)
c54e5cf7
CD
1320@c Date and Time (21)
1321@c Non-Local Exist (23)
1322@c Signal Handling (24)
1323@c The Basic Program/System Interface (25)
1324@c Processes (26)
1325@c Job Control (28)
1326@c System Databases and Name Service Switch (29)
1327@c Users and Groups (30) -- References `User Database' and `Group Database'
1328@c System Management (31)
1329@c System Configuration Parameters (32)
1330@c C Language Facilities in the Library (AA)
1331@c Summary of Library Facilities (AB)
1332@c Installing (AC)
1333@c Library Maintenance (AD)
1334
1335@c The following chapters need overview text to be added:
1336@c Message Translation (8)
1337@c Resource Usage And Limitations (22)
1338@c Inter-Process Communication (27)
c54e5cf7
CD
1339@c Debugging support (34)
1340@c POSIX Threads (35)
1341@c Internal Probes (36)
1342@c Platform-specific facilities (AE)
1343@c Contributors to (AF)
1344@c Free Software Needs Free Documentation (AG)
1345@c GNU Lesser General Public License (AH)
1346@c GNU Free Documentation License (AI)
1347
28f540f4 1348@itemize @bullet
2c6fe0bd 1349@item
28f540f4
RM
1350@ref{Error Reporting}, describes how errors detected by the library
1351are reported.
1352
28f540f4 1353
2c6fe0bd 1354@item
1f77f049 1355@ref{Memory}, describes @theglibc{}'s facilities for managing and
99a20616
UD
1356using virtual and real memory, including dynamic allocation of virtual
1357memory. If you do not know in advance how much memory your program
1358needs, you can allocate it dynamically instead, and manipulate it via
1359pointers.
28f540f4 1360
2c6fe0bd 1361@item
28f540f4
RM
1362@ref{Character Handling}, contains information about character
1363classification functions (such as @code{isspace}) and functions for
1364performing case conversion.
1365
2c6fe0bd 1366@item
28f540f4
RM
1367@ref{String and Array Utilities}, has descriptions of functions for
1368manipulating strings (null-terminated character arrays) and general
1369byte arrays, including operations such as copying and comparison.
1370
c54e5cf7
CD
1371@item
1372@ref{Character Set Handling}, contains information about manipulating
1373characters and strings using character sets larger than will fit in
1374the usual @code{char} data type.
1375
1376@item
1377@ref{Locales}, describes how selecting a particular country
1378or language affects the behavior of the library. For example, the locale
1379affects collation sequences for strings and how monetary values are
1380formatted.
1381
1382@item
1383@ref{Searching and Sorting}, contains information about functions
1384for searching and sorting arrays. You can use these functions on any
1385kind of array by providing an appropriate comparison function.
1386
1387@item
1388@ref{Pattern Matching}, presents functions for matching regular expressions
1389and shell file name patterns, and for expanding words as the shell does.
1390
28f540f4
RM
1391@item
1392@ref{I/O Overview}, gives an overall look at the input and output
1393facilities in the library, and contains information about basic concepts
1394such as file names.
1395
1396@item
1397@ref{I/O on Streams}, describes I/O operations involving streams (or
1398@w{@code{FILE *}} objects). These are the normal C library functions
1399from @file{stdio.h}.
1400
1401@item
1402@ref{Low-Level I/O}, contains information about I/O operations
1403on file descriptors. File descriptors are a lower-level mechanism
1404specific to the Unix family of operating systems.
1405
1406@item
1407@ref{File System Interface}, has descriptions of operations on entire
1408files, such as functions for deleting and renaming them and for creating
1409new directories. This chapter also contains information about how you
1410can access the attributes of a file, such as its owner and file protection
1411modes.
1412
1413@item
1414@ref{Pipes and FIFOs}, contains information about simple interprocess
1415communication mechanisms. Pipes allow communication between two related
1416processes (such as between a parent and child), while FIFOs allow
1417communication between processes sharing a common file system on the same
1418machine.
1419
1420@item
1421@ref{Sockets}, describes a more complicated interprocess communication
1422mechanism that allows processes running on different machines to
1423communicate over a network. This chapter also contains information about
1424Internet host addressing and how to use the system network databases.
1425
1426@item
1427@ref{Low-Level Terminal Interface}, describes how you can change the
1428attributes of a terminal device. If you want to disable echo of
1429characters typed by the user, for example, read this chapter.
1430
2c6fe0bd 1431@item
28f540f4
RM
1432@ref{Mathematics}, contains information about the math library
1433functions. These include things like random-number generators and
1434remainder functions on integers as well as the usual trigonometric and
1435exponential functions on floating-point numbers.
1436
1437@item
1438@ref{Arithmetic,, Low-Level Arithmetic Functions}, describes functions
1439for simple arithmetic, analysis of floating-point values, and reading
1440numbers from strings.
1441
28f540f4
RM
1442@item
1443@ref{Date and Time}, describes functions for measuring both calendar time
1444and CPU time, as well as functions for setting alarms and timers.
1445
28f540f4
RM
1446@item
1447@ref{Non-Local Exits}, contains descriptions of the @code{setjmp} and
1448@code{longjmp} functions. These functions provide a facility for
1449@code{goto}-like jumps which can jump from one function to another.
1450
1451@item
1452@ref{Signal Handling}, tells you all about signals---what they are,
1453how to establish a handler that is called when a particular kind of
1454signal is delivered, and how to prevent signals from arriving during
1455critical sections of your program.
1456
1457@item
17c389fc 1458@ref{Program Basics}, tells how your programs can access their
28f540f4
RM
1459command-line arguments and environment variables.
1460
2c6fe0bd 1461@item
28f540f4
RM
1462@ref{Processes}, contains information about how to start new processes
1463and run programs.
1464
1465@item
1466@ref{Job Control}, describes functions for manipulating process groups
1467and the controlling terminal. This material is probably only of
1468interest if you are writing a shell or other program which handles job
1469control specially.
1470
2c6fe0bd
UD
1471@item
1472@ref{Name Service Switch}, describes the services which are available
1473for looking up names in the system databases, how to determine which
1474service is used for which database, and how these services are
1475implemented so that contributors can design their own services.
1476
28f540f4
RM
1477@item
1478@ref{User Database}, and @ref{Group Database}, tell you how to access
1479the system user and group databases.
1480
1481@item
faf2289f
UD
1482@ref{System Management}, describes functions for controlling and getting
1483information about the hardware and software configuration your program
1484is executing under.
28f540f4 1485
2c6fe0bd 1486@item
28f540f4
RM
1487@ref{System Configuration}, tells you how you can get information about
1488various operating system limits. Most of these parameters are provided for
1489compatibility with POSIX.
1490
c54e5cf7
CD
1491@item
1492@ref{Language Features}, contains information about library support for
1493standard parts of the C language, including things like the @code{sizeof}
1494operator and the symbolic constant @code{NULL}, how to write functions
1495accepting variable numbers of arguments, and constants describing the
1496ranges and other properties of the numerical types. There is also a simple
1497debugging mechanism which allows you to put assertions in your code, and
1498have diagnostic messages printed if the tests fail.
1499
28f540f4
RM
1500@item
1501@ref{Library Summary}, gives a summary of all the functions, variables, and
1502macros in the library, with complete data types and function prototypes,
1503and says what standard or system each is derived from.
1504
1505@item
1f77f049 1506@ref{Installation}, explains how to build and install @theglibc{} on
5551a7b6
AJ
1507your system, and how to report any bugs you might find.
1508
1509@item
1510@ref{Maintenance}, explains how to add new functions or port the
1511library to a new system.
28f540f4
RM
1512@end itemize
1513
1514If you already know the name of the facility you are interested in, you
1515can look it up in @ref{Library Summary}. This gives you a summary of
1516its syntax and a pointer to where you can find a more detailed
1517description. This appendix is particularly useful if you just want to
1518verify the order and type of arguments to a function, for example. It
1519also tells you what standard or system each function, variable, or macro
1520is derived from.
This page took 0.715133 seconds and 6 git commands to generate.