Blame manual/startup.texi

Packit Service 82fcde
@node Program Basics, Processes, Signal Handling, Top
Packit Service 82fcde
@c %MENU% Writing the beginning and end of your program
Packit Service 82fcde
@chapter The Basic Program/System Interface
Packit Service 82fcde
Packit Service 82fcde
@cindex process
Packit Service 82fcde
@cindex program
Packit Service 82fcde
@cindex address space
Packit Service 82fcde
@cindex thread of control
Packit Service 82fcde
@dfn{Processes} are the primitive units for allocation of system
Packit Service 82fcde
resources.  Each process has its own address space and (usually) one
Packit Service 82fcde
thread of control.  A process executes a program; you can have multiple
Packit Service 82fcde
processes executing the same program, but each process has its own copy
Packit Service 82fcde
of the program within its own address space and executes it
Packit Service 82fcde
independently of the other copies.  Though it may have multiple threads
Packit Service 82fcde
of control within the same program and a program may be composed of
Packit Service 82fcde
multiple logically separate modules, a process always executes exactly
Packit Service 82fcde
one program.
Packit Service 82fcde
Packit Service 82fcde
Note that we are using a specific definition of ``program'' for the
Packit Service 82fcde
purposes of this manual, which corresponds to a common definition in the
Packit Service 82fcde
context of Unix systems.  In popular usage, ``program'' enjoys a much
Packit Service 82fcde
broader definition; it can refer for example to a system's kernel, an
Packit Service 82fcde
editor macro, a complex package of software, or a discrete section of
Packit Service 82fcde
code executing within a process.
Packit Service 82fcde
Packit Service 82fcde
Writing the program is what this manual is all about.  This chapter
Packit Service 82fcde
explains the most basic interface between your program and the system
Packit Service 82fcde
that runs, or calls, it.  This includes passing of parameters (arguments
Packit Service 82fcde
and environment) from the system, requesting basic services from the
Packit Service 82fcde
system, and telling the system the program is done.
Packit Service 82fcde
Packit Service 82fcde
A program starts another program with the @code{exec} family of system calls.
Packit Service 82fcde
This chapter looks at program startup from the execee's point of view.  To
Packit Service 82fcde
see the event from the execor's point of view, see @ref{Executing a File}.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Program Arguments::           Parsing your program's command-line arguments
Packit Service 82fcde
* Environment Variables::       Less direct parameters affecting your program
Packit Service 82fcde
* Auxiliary Vector::            Least direct parameters affecting your program
Packit Service 82fcde
* System Calls::                Requesting service from the system
Packit Service 82fcde
* Program Termination::         Telling the system you're done; return status
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Program Arguments, Environment Variables, , Program Basics
Packit Service 82fcde
@section Program Arguments
Packit Service 82fcde
@cindex program arguments
Packit Service 82fcde
@cindex command line arguments
Packit Service 82fcde
@cindex arguments, to program
Packit Service 82fcde
Packit Service 82fcde
@cindex program startup
Packit Service 82fcde
@cindex startup of program
Packit Service 82fcde
@cindex invocation of program
Packit Service 82fcde
@cindex @code{main} function
Packit Service 82fcde
@findex main
Packit Service 82fcde
The system starts a C program by calling the function @code{main}.  It
Packit Service 82fcde
is up to you to write a function named @code{main}---otherwise, you
Packit Service 82fcde
won't even be able to link your program without errors.
Packit Service 82fcde
Packit Service 82fcde
In @w{ISO C} you can define @code{main} either to take no arguments, or to
Packit Service 82fcde
take two arguments that represent the command line arguments to the
Packit Service 82fcde
program, like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int main (int @var{argc}, char *@var{argv}[])
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@cindex argc (program argument count)
Packit Service 82fcde
@cindex argv (program argument vector)
Packit Service 82fcde
The command line arguments are the whitespace-separated tokens given in
Packit Service 82fcde
the shell command used to invoke the program; thus, in @samp{cat foo
Packit Service 82fcde
bar}, the arguments are @samp{foo} and @samp{bar}.  The only way a
Packit Service 82fcde
program can look at its command line arguments is via the arguments of
Packit Service 82fcde
@code{main}.  If @code{main} doesn't take arguments, then you cannot get
Packit Service 82fcde
at the command line.
Packit Service 82fcde
Packit Service 82fcde
The value of the @var{argc} argument is the number of command line
Packit Service 82fcde
arguments.  The @var{argv} argument is a vector of C strings; its
Packit Service 82fcde
elements are the individual command line argument strings.  The file
Packit Service 82fcde
name of the program being run is also included in the vector as the
Packit Service 82fcde
first element; the value of @var{argc} counts this element.  A null
Packit Service 82fcde
pointer always follows the last element: @code{@var{argv}[@var{argc}]}
Packit Service 82fcde
is this null pointer.
Packit Service 82fcde
Packit Service 82fcde
For the command @samp{cat foo bar}, @var{argc} is 3 and @var{argv} has
Packit Service 82fcde
three elements, @code{"cat"}, @code{"foo"} and @code{"bar"}.
Packit Service 82fcde
Packit Service 82fcde
In Unix systems you can define @code{main} a third way, using three arguments:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int main (int @var{argc}, char *@var{argv}[], char *@var{envp}[])
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The first two arguments are just the same.  The third argument
Packit Service 82fcde
@var{envp} gives the program's environment; it is the same as the value
Packit Service 82fcde
of @code{environ}.  @xref{Environment Variables}.  POSIX.1 does not
Packit Service 82fcde
allow this three-argument form, so to be portable it is best to write
Packit Service 82fcde
@code{main} to take two arguments, and use the value of @code{environ}.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Argument Syntax::             By convention, options start with a hyphen.
Packit Service 82fcde
* Parsing Program Arguments::   Ways to parse program options and arguments.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Argument Syntax, Parsing Program Arguments, , Program Arguments
Packit Service 82fcde
@subsection Program Argument Syntax Conventions
Packit Service 82fcde
@cindex program argument syntax
Packit Service 82fcde
@cindex syntax, for program arguments
Packit Service 82fcde
@cindex command argument syntax
Packit Service 82fcde
Packit Service 82fcde
POSIX recommends these conventions for command line arguments.
Packit Service 82fcde
@code{getopt} (@pxref{Getopt}) and @code{argp_parse} (@pxref{Argp}) make
Packit Service 82fcde
it easy to implement them.
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
Arguments are options if they begin with a hyphen delimiter (@samp{-}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Multiple options may follow a hyphen delimiter in a single token if
Packit Service 82fcde
the options do not take arguments.  Thus, @samp{-abc} is equivalent to
Packit Service 82fcde
@samp{-a -b -c}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Option names are single alphanumeric characters (as for @code{isalnum};
Packit Service 82fcde
@pxref{Classification of Characters}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Certain options require an argument.  For example, the @samp{-o} command
Packit Service 82fcde
of the @code{ld} command requires an argument---an output file name.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
An option and its argument may or may not appear as separate tokens.  (In
Packit Service 82fcde
other words, the whitespace separating them is optional.)  Thus,
Packit Service 82fcde
@w{@samp{-o foo}} and @samp{-ofoo} are equivalent.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Options typically precede other non-option arguments.
Packit Service 82fcde
Packit Service 82fcde
The implementations of @code{getopt} and @code{argp_parse} in @theglibc{}
Packit Service 82fcde
normally make it appear as if all the option arguments were
Packit Service 82fcde
specified before all the non-option arguments for the purposes of
Packit Service 82fcde
parsing, even if the user of your program intermixed option and
Packit Service 82fcde
non-option arguments.  They do this by reordering the elements of the
Packit Service 82fcde
@var{argv} array.  This behavior is nonstandard; if you want to suppress
Packit Service 82fcde
it, define the @code{_POSIX_OPTION_ORDER} environment variable.
Packit Service 82fcde
@xref{Standard Environment}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
The argument @samp{--} terminates all options; any following arguments
Packit Service 82fcde
are treated as non-option arguments, even if they begin with a hyphen.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A token consisting of a single hyphen character is interpreted as an
Packit Service 82fcde
ordinary non-option argument.  By convention, it is used to specify
Packit Service 82fcde
input from or output to the standard input and output streams.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Options may be supplied in any order, or appear multiple times.  The
Packit Service 82fcde
interpretation is left up to the particular application program.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
@cindex long-named options
Packit Service 82fcde
GNU adds @dfn{long options} to these conventions.  Long options consist
Packit Service 82fcde
of @samp{--} followed by a name made of alphanumeric characters and
Packit Service 82fcde
dashes.  Option names are typically one to three words long, with
Packit Service 82fcde
hyphens to separate words.  Users can abbreviate the option names as
Packit Service 82fcde
long as the abbreviations are unique.
Packit Service 82fcde
Packit Service 82fcde
To specify an argument for a long option, write
Packit Service 82fcde
@samp{--@var{name}=@var{value}}.  This syntax enables a long option to
Packit Service 82fcde
accept an argument that is itself optional.
Packit Service 82fcde
Packit Service 82fcde
Eventually, @gnusystems{} will provide completion for long option names
Packit Service 82fcde
in the shell.
Packit Service 82fcde
Packit Service 82fcde
@node Parsing Program Arguments, , Argument Syntax, Program Arguments
Packit Service 82fcde
@subsection Parsing Program Arguments
Packit Service 82fcde
Packit Service 82fcde
@cindex program arguments, parsing
Packit Service 82fcde
@cindex command arguments, parsing
Packit Service 82fcde
@cindex parsing program arguments
Packit Service 82fcde
If the syntax for the command line arguments to your program is simple
Packit Service 82fcde
enough, you can simply pick the arguments off from @var{argv} by hand.
Packit Service 82fcde
But unless your program takes a fixed number of arguments, or all of the
Packit Service 82fcde
arguments are interpreted in the same way (as file names, for example),
Packit Service 82fcde
you are usually better off using @code{getopt} (@pxref{Getopt}) or
Packit Service 82fcde
@code{argp_parse} (@pxref{Argp}) to do the parsing.
Packit Service 82fcde
Packit Service 82fcde
@code{getopt} is more standard (the short-option only version of it is a
Packit Service 82fcde
part of the POSIX standard), but using @code{argp_parse} is often
Packit Service 82fcde
easier, both for very simple and very complex option structures, because
Packit Service 82fcde
it does more of the dirty work for you.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Getopt::                      Parsing program options using @code{getopt}.
Packit Service 82fcde
* Argp::                        Parsing program options using @code{argp_parse}.
Packit Service 82fcde
* Suboptions::                  Some programs need more detailed options.
Packit Service 82fcde
* Suboptions Example::          This shows how it could be done for @code{mount}.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@c Getopt and argp start at the @section level so that there's
Packit Service 82fcde
@c enough room for their internal hierarchy (mostly a problem with
Packit Service 82fcde
@c argp).         -Miles
Packit Service 82fcde
Packit Service 82fcde
@include getopt.texi
Packit Service 82fcde
@include argp.texi
Packit Service 82fcde
Packit Service 82fcde
@node Suboptions, Suboptions Example, Argp, Parsing Program Arguments
Packit Service 82fcde
@c This is a @section so that it's at the same level as getopt and argp
Packit Service 82fcde
@subsubsection Parsing of Suboptions
Packit Service 82fcde
Packit Service 82fcde
Having a single level of options is sometimes not enough.  There might
Packit Service 82fcde
be too many options which have to be available or a set of options is
Packit Service 82fcde
closely related.
Packit Service 82fcde
Packit Service 82fcde
For this case some programs use suboptions.  One of the most prominent
Packit Service 82fcde
programs is certainly @code{mount}(8).  The @code{-o} option take one
Packit Service 82fcde
argument which itself is a comma separated list of options.  To ease the
Packit Service 82fcde
programming of code like this the function @code{getsubopt} is
Packit Service 82fcde
available.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getsubopt (char **@var{optionp}, char *const *@var{tokens}, char **@var{valuep})
Packit Service 82fcde
@standards{???, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c getsubopt ok
Packit Service 82fcde
@c  strchrnul dup ok
Packit Service 82fcde
@c  memchr dup ok
Packit Service 82fcde
@c  strncmp dup ok
Packit Service 82fcde
Packit Service 82fcde
The @var{optionp} parameter must be a pointer to a variable containing
Packit Service 82fcde
the address of the string to process.  When the function returns, the
Packit Service 82fcde
reference is updated to point to the next suboption or to the
Packit Service 82fcde
terminating @samp{\0} character if there are no more suboptions available.
Packit Service 82fcde
Packit Service 82fcde
The @var{tokens} parameter references an array of strings containing the
Packit Service 82fcde
known suboptions.  All strings must be @samp{\0} terminated and to mark
Packit Service 82fcde
the end a null pointer must be stored.  When @code{getsubopt} finds a
Packit Service 82fcde
possible legal suboption it compares it with all strings available in
Packit Service 82fcde
the @var{tokens} array and returns the index in the string as the
Packit Service 82fcde
indicator.
Packit Service 82fcde
Packit Service 82fcde
In case the suboption has an associated value introduced by a @samp{=}
Packit Service 82fcde
character, a pointer to the value is returned in @var{valuep}.  The
Packit Service 82fcde
string is @samp{\0} terminated.  If no argument is available
Packit Service 82fcde
@var{valuep} is set to the null pointer.  By doing this the caller can
Packit Service 82fcde
check whether a necessary value is given or whether no unexpected value
Packit Service 82fcde
is present.
Packit Service 82fcde
Packit Service 82fcde
In case the next suboption in the string is not mentioned in the
Packit Service 82fcde
@var{tokens} array the starting address of the suboption including a
Packit Service 82fcde
possible value is returned in @var{valuep} and the return value of the
Packit Service 82fcde
function is @samp{-1}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Suboptions Example, , Suboptions, Parsing Program Arguments
Packit Service 82fcde
@subsection Parsing of Suboptions Example
Packit Service 82fcde
Packit Service 82fcde
The code which might appear in the @code{mount}(8) program is a perfect
Packit Service 82fcde
example of the use of @code{getsubopt}:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include subopt.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Environment Variables, Auxiliary Vector, Program Arguments, Program Basics
Packit Service 82fcde
@section Environment Variables
Packit Service 82fcde
Packit Service 82fcde
@cindex environment variable
Packit Service 82fcde
When a program is executed, it receives information about the context in
Packit Service 82fcde
which it was invoked in two ways.  The first mechanism uses the
Packit Service 82fcde
@var{argv} and @var{argc} arguments to its @code{main} function, and is
Packit Service 82fcde
discussed in @ref{Program Arguments}.  The second mechanism uses
Packit Service 82fcde
@dfn{environment variables} and is discussed in this section.
Packit Service 82fcde
Packit Service 82fcde
The @var{argv} mechanism is typically used to pass command-line
Packit Service 82fcde
arguments specific to the particular program being invoked.  The
Packit Service 82fcde
environment, on the other hand, keeps track of information that is
Packit Service 82fcde
shared by many programs, changes infrequently, and that is less
Packit Service 82fcde
frequently used.
Packit Service 82fcde
Packit Service 82fcde
The environment variables discussed in this section are the same
Packit Service 82fcde
environment variables that you set using assignments and the
Packit Service 82fcde
@code{export} command in the shell.  Programs executed from the shell
Packit Service 82fcde
inherit all of the environment variables from the shell.
Packit Service 82fcde
@c !!! xref to right part of bash manual when it exists
Packit Service 82fcde
Packit Service 82fcde
@cindex environment
Packit Service 82fcde
Standard environment variables are used for information about the user's
Packit Service 82fcde
home directory, terminal type, current locale, and so on; you can define
Packit Service 82fcde
additional variables for other purposes.  The set of all environment
Packit Service 82fcde
variables that have values is collectively known as the
Packit Service 82fcde
@dfn{environment}.
Packit Service 82fcde
Packit Service 82fcde
Names of environment variables are case-sensitive and must not contain
Packit Service 82fcde
the character @samp{=}.  System-defined environment variables are
Packit Service 82fcde
invariably uppercase.
Packit Service 82fcde
Packit Service 82fcde
The values of environment variables can be anything that can be
Packit Service 82fcde
represented as a string.  A value must not contain an embedded null
Packit Service 82fcde
character, since this is assumed to terminate the string.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Environment Access::          How to get and set the values of
Packit Service 82fcde
				 environment variables.
Packit Service 82fcde
* Standard Environment::        These environment variables have
Packit Service 82fcde
                		 standard interpretations.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Environment Access
Packit Service 82fcde
@subsection Environment Access
Packit Service 82fcde
@cindex environment access
Packit Service 82fcde
@cindex environment representation
Packit Service 82fcde
Packit Service 82fcde
The value of an environment variable can be accessed with the
Packit Service 82fcde
@code{getenv} function.  This is declared in the header file
Packit Service 82fcde
@file{stdlib.h}.
Packit Service 82fcde
@pindex stdlib.h
Packit Service 82fcde
Packit Service 82fcde
Libraries should use @code{secure_getenv} instead of @code{getenv}, so
Packit Service 82fcde
that they do not accidentally use untrusted environment variables.
Packit Service 82fcde
Modifications of environment variables are not allowed in
Packit Service 82fcde
multi-threaded programs.  The @code{getenv} and @code{secure_getenv}
Packit Service 82fcde
functions can be safely used in multi-threaded programs.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} getenv (const char *@var{name})
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Unguarded access to __environ.
Packit Service 82fcde
This function returns a string that is the value of the environment
Packit Service 82fcde
variable @var{name}.  You must not modify this string.  In some non-Unix
Packit Service 82fcde
systems not using @theglibc{}, it might be overwritten by subsequent
Packit Service 82fcde
calls to @code{getenv} (but not by any other library function).  If the
Packit Service 82fcde
environment variable @var{name} is not defined, the value is a null
Packit Service 82fcde
pointer.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} secure_getenv (const char *@var{name})
Packit Service 82fcde
@standards{GNU, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsenv{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Calls getenv unless secure mode is enabled.
Packit Service 82fcde
This function is similar to @code{getenv}, but it returns a null
Packit Service 82fcde
pointer if the environment is untrusted.  This happens when the
Packit Service 82fcde
program file has SUID or SGID bits set.  General-purpose libraries
Packit Service 82fcde
should always prefer this function over @code{getenv} to avoid
Packit Service 82fcde
vulnerabilities if the library is referenced from a SUID/SGID program.
Packit Service 82fcde
Packit Service 82fcde
This function is a GNU extension.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int putenv (char *@var{string})
Packit Service 82fcde
@standards{SVID, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtsenv{}}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
Packit Service 82fcde
@c putenv @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
Packit Service 82fcde
@c  strchr dup ok
Packit Service 82fcde
@c  strndup dup @ascuheap @acsmem
Packit Service 82fcde
@c  add_to_environ dup @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
Packit Service 82fcde
@c  free dup @ascuheap @acsmem
Packit Service 82fcde
@c  unsetenv dup @mtasuconst:@mtsenv @asulock @aculock
Packit Service 82fcde
The @code{putenv} function adds or removes definitions from the environment.
Packit Service 82fcde
If the @var{string} is of the form @samp{@var{name}=@var{value}}, the
Packit Service 82fcde
definition is added to the environment.  Otherwise, the @var{string} is
Packit Service 82fcde
interpreted as the name of an environment variable, and any definition
Packit Service 82fcde
for this variable in the environment is removed.
Packit Service 82fcde
Packit Service 82fcde
If the function is successful it returns @code{0}.  Otherwise the return
Packit Service 82fcde
value is nonzero and @code{errno} is set to indicate the error.
Packit Service 82fcde
Packit Service 82fcde
The difference to the @code{setenv} function is that the exact string
Packit Service 82fcde
given as the parameter @var{string} is put into the environment.  If the
Packit Service 82fcde
user should change the string after the @code{putenv} call this will
Packit Service 82fcde
reflect automatically in the environment.  This also requires that
Packit Service 82fcde
@var{string} not be an automatic variable whose scope is left before the
Packit Service 82fcde
variable is removed from the environment.  The same applies of course to
Packit Service 82fcde
dynamically allocated variables which are freed later.
Packit Service 82fcde
Packit Service 82fcde
This function is part of the extended Unix interface.  You should define
Packit Service 82fcde
@var{_XOPEN_SOURCE} before including any header.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int setenv (const char *@var{name}, const char *@var{value}, int @var{replace})
Packit Service 82fcde
@standards{BSD, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtsenv{}}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
Packit Service 82fcde
@c setenv @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
Packit Service 82fcde
@c  add_to_environ @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
Packit Service 82fcde
@c   strlen dup ok
Packit Service 82fcde
@c   libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c   strncmp dup ok
Packit Service 82fcde
@c   realloc dup @ascuheap @acsmem
Packit Service 82fcde
@c   libc_lock_unlock @aculock
Packit Service 82fcde
@c   malloc dup @ascuheap @acsmem
Packit Service 82fcde
@c   free dup @ascuheap @acsmem
Packit Service 82fcde
@c   mempcpy dup ok
Packit Service 82fcde
@c   memcpy dup ok
Packit Service 82fcde
@c   KNOWN_VALUE ok
Packit Service 82fcde
@c    tfind(strcmp) [no @mtsrace guarded access]
Packit Service 82fcde
@c     strcmp dup ok
Packit Service 82fcde
@c   STORE_VALUE @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c    tsearch(strcmp) @ascuheap @acucorrupt @acsmem [no @mtsrace or @asucorrupt guarded access makes for mtsafe and @asulock]
Packit Service 82fcde
@c     strcmp dup ok
Packit Service 82fcde
The @code{setenv} function can be used to add a new definition to the
Packit Service 82fcde
environment.  The entry with the name @var{name} is replaced by the
Packit Service 82fcde
value @samp{@var{name}=@var{value}}.  Please note that this is also true
Packit Service 82fcde
if @var{value} is the empty string.  To do this a new string is created
Packit Service 82fcde
and the strings @var{name} and @var{value} are copied.  A null pointer
Packit Service 82fcde
for the @var{value} parameter is illegal.  If the environment already
Packit Service 82fcde
contains an entry with key @var{name} the @var{replace} parameter
Packit Service 82fcde
controls the action.  If replace is zero, nothing happens.  Otherwise
Packit Service 82fcde
the old entry is replaced by the new one.
Packit Service 82fcde
Packit Service 82fcde
Please note that you cannot remove an entry completely using this function.
Packit Service 82fcde
Packit Service 82fcde
If the function is successful it returns @code{0}.  Otherwise the
Packit Service 82fcde
environment is unchanged and the return value is @code{-1} and
Packit Service 82fcde
@code{errno} is set.
Packit Service 82fcde
Packit Service 82fcde
This function was originally part of the BSD library but is now part of
Packit Service 82fcde
the Unix standard.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int unsetenv (const char *@var{name})
Packit Service 82fcde
@standards{BSD, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtsenv{}}}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit Service 82fcde
@c unsetenv @mtasuconst:@mtsenv @asulock @aculock
Packit Service 82fcde
@c  strchr dup ok
Packit Service 82fcde
@c  strlen dup ok
Packit Service 82fcde
@c  libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c  strncmp dup ok
Packit Service 82fcde
@c  libc_lock_unlock @aculock
Packit Service 82fcde
Using this function one can remove an entry completely from the
Packit Service 82fcde
environment.  If the environment contains an entry with the key
Packit Service 82fcde
@var{name} this whole entry is removed.  A call to this function is
Packit Service 82fcde
equivalent to a call to @code{putenv} when the @var{value} part of the
Packit Service 82fcde
string is empty.
Packit Service 82fcde
Packit Service 82fcde
The function returns @code{-1} if @var{name} is a null pointer, points to
Packit Service 82fcde
an empty string, or points to a string containing a @code{=} character.
Packit Service 82fcde
It returns @code{0} if the call succeeded.
Packit Service 82fcde
Packit Service 82fcde
This function was originally part of the BSD library but is now part of
Packit Service 82fcde
the Unix standard.  The BSD version had no return value, though.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
There is one more function to modify the whole environment.  This
Packit Service 82fcde
function is said to be used in the POSIX.9 (POSIX bindings for Fortran
Packit Service 82fcde
77) and so one should expect it did made it into POSIX.1.  But this
Packit Service 82fcde
never happened.  But we still provide this function as a GNU extension
Packit Service 82fcde
to enable writing standard compliant Fortran environments.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int clearenv (void)
Packit Service 82fcde
@standards{GNU, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtsenv{}}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
Packit Service 82fcde
@c clearenv @mtasuconst:@mtsenv @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c  libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c  free dup @ascuheap @acsmem
Packit Service 82fcde
@c  libc_lock_unlock @aculock
Packit Service 82fcde
The @code{clearenv} function removes all entries from the environment.
Packit Service 82fcde
Using @code{putenv} and @code{setenv} new entries can be added again
Packit Service 82fcde
later.
Packit Service 82fcde
Packit Service 82fcde
If the function is successful it returns @code{0}.  Otherwise the return
Packit Service 82fcde
value is nonzero.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
You can deal directly with the underlying representation of environment
Packit Service 82fcde
objects to add more variables to the environment (for example, to
Packit Service 82fcde
communicate with another program you are about to execute;
Packit Service 82fcde
@pxref{Executing a File}).
Packit Service 82fcde
Packit Service 82fcde
@deftypevar {char **} environ
Packit Service 82fcde
@standards{POSIX.1, unistd.h}
Packit Service 82fcde
The environment is represented as an array of strings.  Each string is
Packit Service 82fcde
of the format @samp{@var{name}=@var{value}}.  The order in which
Packit Service 82fcde
strings appear in the environment is not significant, but the same
Packit Service 82fcde
@var{name} must not appear more than once.  The last element of the
Packit Service 82fcde
array is a null pointer.
Packit Service 82fcde
Packit Service 82fcde
This variable is declared in the header file @file{unistd.h}.
Packit Service 82fcde
Packit Service 82fcde
If you just want to get the value of an environment variable, use
Packit Service 82fcde
@code{getenv}.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
Unix systems, and @gnusystems{}, pass the initial value of
Packit Service 82fcde
@code{environ} as the third argument to @code{main}.
Packit Service 82fcde
@xref{Program Arguments}.
Packit Service 82fcde
Packit Service 82fcde
@node Standard Environment
Packit Service 82fcde
@subsection Standard Environment Variables
Packit Service 82fcde
@cindex standard environment variables
Packit Service 82fcde
Packit Service 82fcde
These environment variables have standard meanings.  This doesn't mean
Packit Service 82fcde
that they are always present in the environment; but if these variables
Packit Service 82fcde
@emph{are} present, they have these meanings.  You shouldn't try to use
Packit Service 82fcde
these environment variable names for some other purpose.
Packit Service 82fcde
Packit Service 82fcde
@comment Extra blank lines make it look better.
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item HOME
Packit Service 82fcde
@cindex @code{HOME} environment variable
Packit Service 82fcde
@cindex home directory
Packit Service 82fcde
Packit Service 82fcde
This is a string representing the user's @dfn{home directory}, or
Packit Service 82fcde
initial default working directory.
Packit Service 82fcde
Packit Service 82fcde
The user can set @code{HOME} to any value.
Packit Service 82fcde
If you need to make sure to obtain the proper home directory
Packit Service 82fcde
for a particular user, you should not use @code{HOME}; instead,
Packit Service 82fcde
look up the user's name in the user database (@pxref{User Database}).
Packit Service 82fcde
Packit Service 82fcde
For most purposes, it is better to use @code{HOME}, precisely because
Packit Service 82fcde
this lets the user specify the value.
Packit Service 82fcde
Packit Service 82fcde
@c !!! also USER
Packit Service 82fcde
@item LOGNAME
Packit Service 82fcde
@cindex @code{LOGNAME} environment variable
Packit Service 82fcde
Packit Service 82fcde
This is the name that the user used to log in.  Since the value in the
Packit Service 82fcde
environment can be tweaked arbitrarily, this is not a reliable way to
Packit Service 82fcde
identify the user who is running a program; a function like
Packit Service 82fcde
@code{getlogin} (@pxref{Who Logged In}) is better for that purpose.
Packit Service 82fcde
Packit Service 82fcde
For most purposes, it is better to use @code{LOGNAME}, precisely because
Packit Service 82fcde
this lets the user specify the value.
Packit Service 82fcde
Packit Service 82fcde
@item PATH
Packit Service 82fcde
@cindex @code{PATH} environment variable
Packit Service 82fcde
Packit Service 82fcde
A @dfn{path} is a sequence of directory names which is used for
Packit Service 82fcde
searching for a file.  The variable @code{PATH} holds a path used
Packit Service 82fcde
for searching for programs to be run.
Packit Service 82fcde
Packit Service 82fcde
The @code{execlp} and @code{execvp} functions (@pxref{Executing a File})
Packit Service 82fcde
use this environment variable, as do many shells and other utilities
Packit Service 82fcde
which are implemented in terms of those functions.
Packit Service 82fcde
Packit Service 82fcde
The syntax of a path is a sequence of directory names separated by
Packit Service 82fcde
colons.  An empty string instead of a directory name stands for the
Packit Service 82fcde
current directory (@pxref{Working Directory}).
Packit Service 82fcde
Packit Service 82fcde
A typical value for this environment variable might be a string like:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
This means that if the user tries to execute a program named @code{foo},
Packit Service 82fcde
the system will look for files named @file{foo}, @file{/bin/foo},
Packit Service 82fcde
@file{/etc/foo}, and so on.  The first of these files that exists is
Packit Service 82fcde
the one that is executed.
Packit Service 82fcde
Packit Service 82fcde
@c !!! also TERMCAP
Packit Service 82fcde
@item TERM
Packit Service 82fcde
@cindex @code{TERM} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies the kind of terminal that is receiving program output.
Packit Service 82fcde
Some programs can make use of this information to take advantage of
Packit Service 82fcde
special escape sequences or terminal modes supported by particular kinds
Packit Service 82fcde
of terminals.  Many programs which use the termcap library
Packit Service 82fcde
(@pxref{Finding a Terminal Description,Find,,termcap,The Termcap Library
Packit Service 82fcde
Manual}) use the @code{TERM} environment variable, for example.
Packit Service 82fcde
Packit Service 82fcde
@item TZ
Packit Service 82fcde
@cindex @code{TZ} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies the time zone.  @xref{TZ Variable}, for information about
Packit Service 82fcde
the format of this string and how it is used.
Packit Service 82fcde
Packit Service 82fcde
@item LANG
Packit Service 82fcde
@cindex @code{LANG} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies the default locale to use for attribute categories where
Packit Service 82fcde
neither @code{LC_ALL} nor the specific environment variable for that
Packit Service 82fcde
category is set.  @xref{Locales}, for more information about
Packit Service 82fcde
locales.
Packit Service 82fcde
Packit Service 82fcde
@ignore
Packit Service 82fcde
@c I doubt this really exists
Packit Service 82fcde
@item LC_ALL
Packit Service 82fcde
@cindex @code{LC_ALL} environment variable
Packit Service 82fcde
Packit Service 82fcde
This is similar to the @code{LANG} environment variable.  However, its
Packit Service 82fcde
value takes precedence over any values provided for the individual
Packit Service 82fcde
attribute category environment variables, or for the @code{LANG}
Packit Service 82fcde
environment variable.
Packit Service 82fcde
@end ignore
Packit Service 82fcde
Packit Service 82fcde
@item LC_ALL
Packit Service 82fcde
@cindex @code{LC_ALL} environment variable
Packit Service 82fcde
Packit Service 82fcde
If this environment variable is set it overrides the selection for all
Packit Service 82fcde
the locales done using the other @code{LC_*} environment variables.  The
Packit Service 82fcde
value of the other @code{LC_*} environment variables is simply ignored
Packit Service 82fcde
in this case.
Packit Service 82fcde
Packit Service 82fcde
@item LC_COLLATE
Packit Service 82fcde
@cindex @code{LC_COLLATE} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for string sorting.
Packit Service 82fcde
Packit Service 82fcde
@item LC_CTYPE
Packit Service 82fcde
@cindex @code{LC_CTYPE} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for character sets and character
Packit Service 82fcde
classification.
Packit Service 82fcde
Packit Service 82fcde
@item LC_MESSAGES
Packit Service 82fcde
@cindex @code{LC_MESSAGES} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for printing messages and to parse
Packit Service 82fcde
responses.
Packit Service 82fcde
Packit Service 82fcde
@item LC_MONETARY
Packit Service 82fcde
@cindex @code{LC_MONETARY} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for formatting monetary values.
Packit Service 82fcde
Packit Service 82fcde
@item LC_NUMERIC
Packit Service 82fcde
@cindex @code{LC_NUMERIC} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for formatting numbers.
Packit Service 82fcde
Packit Service 82fcde
@item LC_TIME
Packit Service 82fcde
@cindex @code{LC_TIME} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies what locale to use for formatting date/time values.
Packit Service 82fcde
Packit Service 82fcde
@item NLSPATH
Packit Service 82fcde
@cindex @code{NLSPATH} environment variable
Packit Service 82fcde
Packit Service 82fcde
This specifies the directories in which the @code{catopen} function
Packit Service 82fcde
looks for message translation catalogs.
Packit Service 82fcde
Packit Service 82fcde
@item _POSIX_OPTION_ORDER
Packit Service 82fcde
@cindex @code{_POSIX_OPTION_ORDER} environment variable.
Packit Service 82fcde
Packit Service 82fcde
If this environment variable is defined, it suppresses the usual
Packit Service 82fcde
reordering of command line arguments by @code{getopt} and
Packit Service 82fcde
@code{argp_parse}.  @xref{Argument Syntax}.
Packit Service 82fcde
Packit Service 82fcde
@c !!! GNU also has COREFILE, CORESERVER, EXECSERVERS
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@node Auxiliary Vector
Packit Service 82fcde
@section Auxiliary Vector
Packit Service 82fcde
@cindex auxiliary vector
Packit Service 82fcde
Packit Service 82fcde
When a program is executed, it receives information from the operating
Packit Service 82fcde
system about the environment in which it is operating.  The form of this
Packit Service 82fcde
information is a table of key-value pairs, where the keys are from the
Packit Service 82fcde
set of @samp{AT_} values in @file{elf.h}.  Some of the data is provided
Packit Service 82fcde
by the kernel for libc consumption, and may be obtained by ordinary
Packit Service 82fcde
interfaces, such as @code{sysconf}.  However, on a platform-by-platform
Packit Service 82fcde
basis there may be information that is not available any other way.
Packit Service 82fcde
Packit Service 82fcde
@subsection Definition of @code{getauxval}
Packit Service 82fcde
@deftypefun {unsigned long int} getauxval (unsigned long int @var{type})
Packit Service 82fcde
@standards{???, sys/auxv.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Reads from hwcap or iterates over constant auxv.
Packit Service 82fcde
This function is used to inquire about the entries in the auxiliary
Packit Service 82fcde
vector.  The @var{type} argument should be one of the @samp{AT_} symbols
Packit Service 82fcde
defined in @file{elf.h}.  If a matching entry is found, the value is
Packit Service 82fcde
returned; if the entry is not found, zero is returned and @code{errno} is
Packit Service 82fcde
set to @code{ENOENT}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
For some platforms, the key @code{AT_HWCAP} is the easiest way to inquire
Packit Service 82fcde
about any instruction set extensions available at runtime.  In this case,
Packit Service 82fcde
there will (of necessity) be a platform-specific set of @samp{HWCAP_}
Packit Service 82fcde
values masked together that describe the capabilities of the cpu on which
Packit Service 82fcde
the program is being executed.
Packit Service 82fcde
Packit Service 82fcde
@node System Calls
Packit Service 82fcde
@section System Calls
Packit Service 82fcde
Packit Service 82fcde
@cindex system call
Packit Service 82fcde
A system call is a request for service that a program makes of the
Packit Service 82fcde
kernel.  The service is generally something that only the kernel has
Packit Service 82fcde
the privilege to do, such as doing I/O.  Programmers don't normally
Packit Service 82fcde
need to be concerned with system calls because there are functions in
Packit Service 82fcde
@theglibc{} to do virtually everything that system calls do.
Packit Service 82fcde
These functions work by making system calls themselves.  For example,
Packit Service 82fcde
there is a system call that changes the permissions of a file, but
Packit Service 82fcde
you don't need to know about it because you can just use @theglibc{}'s
Packit Service 82fcde
@code{chmod} function.
Packit Service 82fcde
Packit Service 82fcde
@cindex kernel call
Packit Service 82fcde
System calls are sometimes called kernel calls.
Packit Service 82fcde
Packit Service 82fcde
However, there are times when you want to make a system call explicitly,
Packit Service 82fcde
and for that, @theglibc{} provides the @code{syscall} function.
Packit Service 82fcde
@code{syscall} is harder to use and less portable than functions like
Packit Service 82fcde
@code{chmod}, but easier and more portable than coding the system call
Packit Service 82fcde
in assembler instructions.
Packit Service 82fcde
Packit Service 82fcde
@code{syscall} is most useful when you are working with a system call
Packit Service 82fcde
which is special to your system or is newer than @theglibc{} you
Packit Service 82fcde
are using.  @code{syscall} is implemented in an entirely generic way;
Packit Service 82fcde
the function does not know anything about what a particular system
Packit Service 82fcde
call does or even if it is valid.
Packit Service 82fcde
Packit Service 82fcde
The description of @code{syscall} in this section assumes a certain
Packit Service 82fcde
protocol for system calls on the various platforms on which @theglibc{}
Packit Service 82fcde
runs.  That protocol is not defined by any strong authority, but
Packit Service 82fcde
we won't describe it here either because anyone who is coding
Packit Service 82fcde
@code{syscall} probably won't accept anything less than kernel and C
Packit Service 82fcde
library source code as a specification of the interface between them
Packit Service 82fcde
anyway.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@code{syscall} is declared in @file{unistd.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {long int} syscall (long int @var{sysno}, @dots{})
Packit Service 82fcde
@standards{???, unistd.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Packit Service 82fcde
@code{syscall} performs a generic system call.
Packit Service 82fcde
Packit Service 82fcde
@cindex system call number
Packit Service 82fcde
@var{sysno} is the system call number.  Each kind of system call is
Packit Service 82fcde
identified by a number.  Macros for all the possible system call numbers
Packit Service 82fcde
are defined in @file{sys/syscall.h}
Packit Service 82fcde
Packit Service 82fcde
The remaining arguments are the arguments for the system call, in
Packit Service 82fcde
order, and their meanings depend on the kind of system call.  Each kind
Packit Service 82fcde
of system call has a definite number of arguments, from zero to five.
Packit Service 82fcde
If you code more arguments than the system call takes, the extra ones to
Packit Service 82fcde
the right are ignored.
Packit Service 82fcde
Packit Service 82fcde
The return value is the return value from the system call, unless the
Packit Service 82fcde
system call failed.  In that case, @code{syscall} returns @code{-1} and
Packit Service 82fcde
sets @code{errno} to an error code that the system call returned.  Note
Packit Service 82fcde
that system calls do not return @code{-1} when they succeed.
Packit Service 82fcde
@cindex errno
Packit Service 82fcde
Packit Service 82fcde
If you specify an invalid @var{sysno}, @code{syscall} returns @code{-1}
Packit Service 82fcde
with @code{errno} = @code{ENOSYS}.
Packit Service 82fcde
Packit Service 82fcde
Example:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <sys/syscall.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
Packit Service 82fcde
@dots{}
Packit Service 82fcde
Packit Service 82fcde
int rc;
Packit Service 82fcde
Packit Service 82fcde
rc = syscall(SYS_chmod, "/etc/passwd", 0444);
Packit Service 82fcde
Packit Service 82fcde
if (rc == -1)
Packit Service 82fcde
   fprintf(stderr, "chmod failed, errno = %d\n", errno);
Packit Service 82fcde
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
This, if all the compatibility stars are aligned, is equivalent to the
Packit Service 82fcde
following preferable code:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
Packit Service 82fcde
#include <sys/types.h>
Packit Service 82fcde
#include <sys/stat.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
Packit Service 82fcde
@dots{}
Packit Service 82fcde
Packit Service 82fcde
int rc;
Packit Service 82fcde
Packit Service 82fcde
rc = chmod("/etc/passwd", 0444);
Packit Service 82fcde
if (rc == -1)
Packit Service 82fcde
   fprintf(stderr, "chmod failed, errno = %d\n", errno);
Packit Service 82fcde
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Program Termination
Packit Service 82fcde
@section Program Termination
Packit Service 82fcde
@cindex program termination
Packit Service 82fcde
@cindex process termination
Packit Service 82fcde
Packit Service 82fcde
@cindex exit status value
Packit Service 82fcde
The usual way for a program to terminate is simply for its @code{main}
Packit Service 82fcde
function to return.  The @dfn{exit status value} returned from the
Packit Service 82fcde
@code{main} function is used to report information back to the process's
Packit Service 82fcde
parent process or shell.
Packit Service 82fcde
Packit Service 82fcde
A program can also terminate normally by calling the @code{exit}
Packit Service 82fcde
function.
Packit Service 82fcde
Packit Service 82fcde
In addition, programs can be terminated by signals; this is discussed in
Packit Service 82fcde
more detail in @ref{Signal Handling}.  The @code{abort} function causes
Packit Service 82fcde
a signal that kills the program.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Normal Termination::          If a program calls @code{exit}, a
Packit Service 82fcde
                                 process terminates normally.
Packit Service 82fcde
* Exit Status::                 The @code{exit status} provides information
Packit Service 82fcde
                                 about why the process terminated.
Packit Service 82fcde
* Cleanups on Exit::            A process can run its own cleanup
Packit Service 82fcde
                                 functions upon normal termination.
Packit Service 82fcde
* Aborting a Program::          The @code{abort} function causes
Packit Service 82fcde
                                 abnormal program termination.
Packit Service 82fcde
* Termination Internals::       What happens when a process terminates.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Normal Termination
Packit Service 82fcde
@subsection Normal Termination
Packit Service 82fcde
Packit Service 82fcde
A process terminates normally when its program signals it is done by
Packit Service 82fcde
calling @code{exit}.  Returning from @code{main} is equivalent to
Packit Service 82fcde
calling @code{exit}, and the value that @code{main} returns is used as
Packit Service 82fcde
the argument to @code{exit}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void exit (int @var{status})
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:exit}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
Packit Service 82fcde
@c Access to the atexit/on_exit list, the libc_atexit hook and tls dtors
Packit Service 82fcde
@c is not guarded.  Streams must be flushed, and that triggers the usual
Packit Service 82fcde
@c AS and AC issues with streams.
Packit Service 82fcde
The @code{exit} function tells the system that the program is done, which
Packit Service 82fcde
causes it to terminate the process.
Packit Service 82fcde
Packit Service 82fcde
@var{status} is the program's exit status, which becomes part of the
Packit Service 82fcde
process' termination status.  This function does not return.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Normal termination causes the following actions:
Packit Service 82fcde
Packit Service 82fcde
@enumerate
Packit Service 82fcde
@item
Packit Service 82fcde
Functions that were registered with the @code{atexit} or @code{on_exit}
Packit Service 82fcde
functions are called in the reverse order of their registration.  This
Packit Service 82fcde
mechanism allows your application to specify its own ``cleanup'' actions
Packit Service 82fcde
to be performed at program termination.  Typically, this is used to do
Packit Service 82fcde
things like saving program state information in a file, or unlocking
Packit Service 82fcde
locks in shared data bases.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
All open streams are closed, writing out any buffered output data.  See
Packit Service 82fcde
@ref{Closing Streams}.  In addition, temporary files opened
Packit Service 82fcde
with the @code{tmpfile} function are removed; see @ref{Temporary Files}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@code{_exit} is called, terminating the program.  @xref{Termination Internals}.
Packit Service 82fcde
@end enumerate
Packit Service 82fcde
Packit Service 82fcde
@node Exit Status
Packit Service 82fcde
@subsection Exit Status
Packit Service 82fcde
@cindex exit status
Packit Service 82fcde
Packit Service 82fcde
When a program exits, it can return to the parent process a small
Packit Service 82fcde
amount of information about the cause of termination, using the
Packit Service 82fcde
@dfn{exit status}.  This is a value between 0 and 255 that the exiting
Packit Service 82fcde
process passes as an argument to @code{exit}.
Packit Service 82fcde
Packit Service 82fcde
Normally you should use the exit status to report very broad information
Packit Service 82fcde
about success or failure.  You can't provide a lot of detail about the
Packit Service 82fcde
reasons for the failure, and most parent processes would not want much
Packit Service 82fcde
detail anyway.
Packit Service 82fcde
Packit Service 82fcde
There are conventions for what sorts of status values certain programs
Packit Service 82fcde
should return.  The most common convention is simply 0 for success and 1
Packit Service 82fcde
for failure.  Programs that perform comparison use a different
Packit Service 82fcde
convention: they use status 1 to indicate a mismatch, and status 2 to
Packit Service 82fcde
indicate an inability to compare.  Your program should follow an
Packit Service 82fcde
existing convention if an existing convention makes sense for it.
Packit Service 82fcde
Packit Service 82fcde
A general convention reserves status values 128 and up for special
Packit Service 82fcde
purposes.  In particular, the value 128 is used to indicate failure to
Packit Service 82fcde
execute another program in a subprocess.  This convention is not
Packit Service 82fcde
universally obeyed, but it is a good idea to follow it in your programs.
Packit Service 82fcde
Packit Service 82fcde
@strong{Warning:} Don't try to use the number of errors as the exit
Packit Service 82fcde
status.  This is actually not very useful; a parent process would
Packit Service 82fcde
generally not care how many errors occurred.  Worse than that, it does
Packit Service 82fcde
not work, because the status value is truncated to eight bits.
Packit Service 82fcde
Thus, if the program tried to report 256 errors, the parent would
Packit Service 82fcde
receive a report of 0 errors---that is, success.
Packit Service 82fcde
Packit Service 82fcde
For the same reason, it does not work to use the value of @code{errno}
Packit Service 82fcde
as the exit status---these can exceed 255.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability note:} Some non-POSIX systems use different
Packit Service 82fcde
conventions for exit status values.  For greater portability, you can
Packit Service 82fcde
use the macros @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} for the
Packit Service 82fcde
conventional status value for success and failure, respectively.  They
Packit Service 82fcde
are declared in the file @file{stdlib.h}.
Packit Service 82fcde
@pindex stdlib.h
Packit Service 82fcde
Packit Service 82fcde
@deftypevr Macro int EXIT_SUCCESS
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
This macro can be used with the @code{exit} function to indicate
Packit Service 82fcde
successful program completion.
Packit Service 82fcde
Packit Service 82fcde
On POSIX systems, the value of this macro is @code{0}.  On other
Packit Service 82fcde
systems, the value might be some other (possibly non-constant) integer
Packit Service 82fcde
expression.
Packit Service 82fcde
@end deftypevr
Packit Service 82fcde
Packit Service 82fcde
@deftypevr Macro int EXIT_FAILURE
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
This macro can be used with the @code{exit} function to indicate
Packit Service 82fcde
unsuccessful program completion in a general sense.
Packit Service 82fcde
Packit Service 82fcde
On POSIX systems, the value of this macro is @code{1}.  On other
Packit Service 82fcde
systems, the value might be some other (possibly non-constant) integer
Packit Service 82fcde
expression.  Other nonzero status values also indicate failures.  Certain
Packit Service 82fcde
programs use different nonzero status values to indicate particular
Packit Service 82fcde
kinds of "non-success".  For example, @code{diff} uses status value
Packit Service 82fcde
@code{1} to mean that the files are different, and @code{2} or more to
Packit Service 82fcde
mean that there was difficulty in opening the files.
Packit Service 82fcde
@end deftypevr
Packit Service 82fcde
Packit Service 82fcde
Don't confuse a program's exit status with a process' termination status.
Packit Service 82fcde
There are lots of ways a process can terminate besides having its program
Packit Service 82fcde
finish.  In the event that the process termination @emph{is} caused by program
Packit Service 82fcde
termination (i.e., @code{exit}), though, the program's exit status becomes
Packit Service 82fcde
part of the process' termination status.
Packit Service 82fcde
Packit Service 82fcde
@node Cleanups on Exit
Packit Service 82fcde
@subsection Cleanups on Exit
Packit Service 82fcde
Packit Service 82fcde
Your program can arrange to run its own cleanup functions if normal
Packit Service 82fcde
termination happens.  If you are writing a library for use in various
Packit Service 82fcde
application programs, then it is unreliable to insist that all
Packit Service 82fcde
applications call the library's cleanup functions explicitly before
Packit Service 82fcde
exiting.  It is much more robust to make the cleanup invisible to the
Packit Service 82fcde
application, by setting up a cleanup function in the library itself
Packit Service 82fcde
using @code{atexit} or @code{on_exit}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int atexit (void (*@var{function}) (void))
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
Packit Service 82fcde
@c atexit @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c  cxa_atexit @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c   __internal_atexit @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c    __new_exitfn @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c     __libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c     calloc dup @ascuheap @acsmem
Packit Service 82fcde
@c     __libc_lock_unlock @aculock
Packit Service 82fcde
@c    atomic_write_barrier dup ok
Packit Service 82fcde
The @code{atexit} function registers the function @var{function} to be
Packit Service 82fcde
called at normal program termination.  The @var{function} is called with
Packit Service 82fcde
no arguments.
Packit Service 82fcde
Packit Service 82fcde
The return value from @code{atexit} is zero on success and nonzero if
Packit Service 82fcde
the function cannot be registered.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int on_exit (void (*@var{function})(int @var{status}, void *@var{arg}), void *@var{arg})
Packit Service 82fcde
@standards{SunOS, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
Packit Service 82fcde
@c on_exit @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c  new_exitfn dup @ascuheap @asulock @aculock @acsmem
Packit Service 82fcde
@c  atomic_write_barrier dup ok
Packit Service 82fcde
This function is a somewhat more powerful variant of @code{atexit}.  It
Packit Service 82fcde
accepts two arguments, a function @var{function} and an arbitrary
Packit Service 82fcde
pointer @var{arg}.  At normal program termination, the @var{function} is
Packit Service 82fcde
called with two arguments:  the @var{status} value passed to @code{exit},
Packit Service 82fcde
and the @var{arg}.
Packit Service 82fcde
Packit Service 82fcde
This function is included in @theglibc{} only for compatibility
Packit Service 82fcde
for SunOS, and may not be supported by other implementations.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Here's a trivial program that illustrates the use of @code{exit} and
Packit Service 82fcde
@code{atexit}:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include atexit.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
When this program is executed, it just prints the message and exits.
Packit Service 82fcde
Packit Service 82fcde
@node Aborting a Program
Packit Service 82fcde
@subsection Aborting a Program
Packit Service 82fcde
@cindex aborting a program
Packit Service 82fcde
Packit Service 82fcde
You can abort your program using the @code{abort} function.  The prototype
Packit Service 82fcde
for this function is in @file{stdlib.h}.
Packit Service 82fcde
@pindex stdlib.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void abort (void)
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
Packit Service 82fcde
@c The implementation takes a recursive lock and attempts to support
Packit Service 82fcde
@c calls from signal handlers, but if we're in the middle of flushing or
Packit Service 82fcde
@c using streams, we may encounter them in inconsistent states.
Packit Service 82fcde
The @code{abort} function causes abnormal program termination.  This
Packit Service 82fcde
does not execute cleanup functions registered with @code{atexit} or
Packit Service 82fcde
@code{on_exit}.
Packit Service 82fcde
Packit Service 82fcde
This function actually terminates the process by raising a
Packit Service 82fcde
@code{SIGABRT} signal, and your program can include a handler to
Packit Service 82fcde
intercept this signal; see @ref{Signal Handling}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Termination Internals
Packit Service 82fcde
@subsection Termination Internals
Packit Service 82fcde
Packit Service 82fcde
The @code{_exit} function is the primitive used for process termination
Packit Service 82fcde
by @code{exit}.  It is declared in the header file @file{unistd.h}.
Packit Service 82fcde
@pindex unistd.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void _exit (int @var{status})
Packit Service 82fcde
@standards{POSIX.1, unistd.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Direct syscall (exit_group or exit); calls __task_terminate on hurd,
Packit Service 82fcde
@c and abort in the generic posix implementation.
Packit Service 82fcde
The @code{_exit} function is the primitive for causing a process to
Packit Service 82fcde
terminate with status @var{status}.  Calling this function does not
Packit Service 82fcde
execute cleanup functions registered with @code{atexit} or
Packit Service 82fcde
@code{on_exit}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void _Exit (int @var{status})
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Alias for _exit.
Packit Service 82fcde
The @code{_Exit} function is the @w{ISO C} equivalent to @code{_exit}.
Packit Service 82fcde
The @w{ISO C} committee members were not sure whether the definitions of
Packit Service 82fcde
@code{_exit} and @code{_Exit} were compatible so they have not used the
Packit Service 82fcde
POSIX name.
Packit Service 82fcde
Packit Service 82fcde
This function was introduced in @w{ISO C99} and is declared in
Packit Service 82fcde
@file{stdlib.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
When a process terminates for any reason---either because the program
Packit Service 82fcde
terminates, or as a result of a signal---the
Packit Service 82fcde
following things happen:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
All open file descriptors in the process are closed.  @xref{Low-Level I/O}.
Packit Service 82fcde
Note that streams are not flushed automatically when the process
Packit Service 82fcde
terminates; see @ref{I/O on Streams}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A process exit status is saved to be reported back to the parent process
Packit Service 82fcde
via @code{wait} or @code{waitpid}; see @ref{Process Completion}.  If the
Packit Service 82fcde
program exited, this status includes as its low-order 8 bits the program
Packit Service 82fcde
exit status.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Any child processes of the process being terminated are assigned a new
Packit Service 82fcde
parent process.  (On most systems, including GNU, this is the @code{init}
Packit Service 82fcde
process, with process ID 1.)
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A @code{SIGCHLD} signal is sent to the parent process.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
If the process is a session leader that has a controlling terminal, then
Packit Service 82fcde
a @code{SIGHUP} signal is sent to each process in the foreground job,
Packit Service 82fcde
and the controlling terminal is disassociated from that session.
Packit Service 82fcde
@xref{Job Control}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
If termination of a process causes a process group to become orphaned,
Packit Service 82fcde
and any member of that process group is stopped, then a @code{SIGHUP}
Packit Service 82fcde
signal and a @code{SIGCONT} signal are sent to each process in the
Packit Service 82fcde
group.  @xref{Job Control}.
Packit Service 82fcde
@end itemize