]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/startup.texi
.
[thirdparty/glibc.git] / manual / startup.texi
CommitLineData
17c389fc 1@node Program Basics, Processes, Signal Handling, Top
7a68c94a 2@c %MENU% Writing the beginning and end of your program
17c389fc 3@chapter The Basic Program/System Interface
28f540f4
RM
4
5@cindex process
17c389fc
UD
6@cindex program
7@cindex address space
8@cindex thread of control
28f540f4
RM
9@dfn{Processes} are the primitive units for allocation of system
10resources. Each process has its own address space and (usually) one
11thread of control. A process executes a program; you can have multiple
12processes executing the same program, but each process has its own copy
13of the program within its own address space and executes it
17c389fc
UD
14independently of the other copies. Though it may have multiple threads
15of control within the same program and a program may be composed of
16multiple logically separate modules, a process always executes exactly
17one program.
18
19Note that we are using a specific definition of ``program'' for the
20purposes of this manual, which corresponds to a common definition in the
21context of Unix system. In popular usage, ``program'' enjoys a much
22broader definition; it can refer for example to a system's kernel, an
23editor macro, a complex package of software, or a discrete section of
24code executing within a process.
25
26Writing the program is what this manual is all about. This chapter
27explains the most basic interface between your program and the system
28that runs, or calls, it. This includes passing of parameters (arguments
29and environment) from the system, requesting basic services from the
30system, and telling the system the program is done.
31
32A program starts another program with the @code{exec} family of system calls.
33This chapter looks at program startup from the execee's point of view. To
34see the event from the execor's point of view, @xref{Executing a File}.
28f540f4
RM
35
36@menu
37* Program Arguments:: Parsing your program's command-line arguments.
17c389fc
UD
38* Environment Variables:: Less direct parameters affecting your program
39* System Calls:: Requesting service from the system
40* Program Termination:: Telling the system you're done; return status
28f540f4
RM
41@end menu
42
43@node Program Arguments
44@section Program Arguments
45@cindex program arguments
46@cindex command line arguments
47@cindex arguments, to program
48
49@cindex program startup
50@cindex startup of program
51@cindex invocation of program
52@cindex @code{main} function
53@findex main
54The system starts a C program by calling the function @code{main}. It
55is up to you to write a function named @code{main}---otherwise, you
56won't even be able to link your program without errors.
57
f65fd747 58In @w{ISO C} you can define @code{main} either to take no arguments, or to
28f540f4
RM
59take two arguments that represent the command line arguments to the
60program, like this:
61
62@smallexample
63int main (int @var{argc}, char *@var{argv}[])
64@end smallexample
65
66@cindex argc (program argument count)
67@cindex argv (program argument vector)
68The command line arguments are the whitespace-separated tokens given in
69the shell command used to invoke the program; thus, in @samp{cat foo
70bar}, the arguments are @samp{foo} and @samp{bar}. The only way a
71program can look at its command line arguments is via the arguments of
72@code{main}. If @code{main} doesn't take arguments, then you cannot get
73at the command line.
74
75The value of the @var{argc} argument is the number of command line
76arguments. The @var{argv} argument is a vector of C strings; its
77elements are the individual command line argument strings. The file
78name of the program being run is also included in the vector as the
79first element; the value of @var{argc} counts this element. A null
80pointer always follows the last element: @code{@var{argv}[@var{argc}]}
81is this null pointer.
82
83For the command @samp{cat foo bar}, @var{argc} is 3 and @var{argv} has
84three elements, @code{"cat"}, @code{"foo"} and @code{"bar"}.
85
28f540f4
RM
86In Unix systems you can define @code{main} a third way, using three arguments:
87
88@smallexample
85857f93 89int main (int @var{argc}, char *@var{argv}[], char *@var{envp}[])
28f540f4
RM
90@end smallexample
91
92The first two arguments are just the same. The third argument
17c389fc 93@var{envp} gives the program's environment; it is the same as the value
28f540f4
RM
94of @code{environ}. @xref{Environment Variables}. POSIX.1 does not
95allow this three-argument form, so to be portable it is best to write
96@code{main} to take two arguments, and use the value of @code{environ}.
97
98@menu
2064087b 99* Argument Syntax:: By convention, options start with a hyphen.
b0de3e9e 100* Parsing Program Arguments:: Ways to parse program options and arguments.
28f540f4
RM
101@end menu
102
052b6a6c 103@node Argument Syntax, Parsing Program Arguments, , Program Arguments
28f540f4
RM
104@subsection Program Argument Syntax Conventions
105@cindex program argument syntax
106@cindex syntax, for program arguments
107@cindex command argument syntax
108
109POSIX recommends these conventions for command line arguments.
b0de3e9e
UD
110@code{getopt} (@pxref{Getopt}) and @code{argp_parse} (@pxref{Argp}) make
111it easy to implement them.
28f540f4
RM
112
113@itemize @bullet
114@item
115Arguments are options if they begin with a hyphen delimiter (@samp{-}).
116
117@item
118Multiple options may follow a hyphen delimiter in a single token if
119the options do not take arguments. Thus, @samp{-abc} is equivalent to
120@samp{-a -b -c}.
121
122@item
123Option names are single alphanumeric characters (as for @code{isalnum};
8b7fb588 124@pxref{Classification of Characters}).
28f540f4
RM
125
126@item
127Certain options require an argument. For example, the @samp{-o} command
128of the @code{ld} command requires an argument---an output file name.
129
130@item
131An option and its argument may or may not appear as separate tokens. (In
132other words, the whitespace separating them is optional.) Thus,
133@w{@samp{-o foo}} and @samp{-ofoo} are equivalent.
134
135@item
136Options typically precede other non-option arguments.
137
b0de3e9e
UD
138The implementations of @code{getopt} and @code{argp_parse} in the GNU C
139library normally make it appear as if all the option arguments were
140specified before all the non-option arguments for the purposes of
141parsing, even if the user of your program intermixed option and
142non-option arguments. They do this by reordering the elements of the
143@var{argv} array. This behavior is nonstandard; if you want to suppress
144it, define the @code{_POSIX_OPTION_ORDER} environment variable.
145@xref{Standard Environment}.
28f540f4
RM
146
147@item
148The argument @samp{--} terminates all options; any following arguments
149are treated as non-option arguments, even if they begin with a hyphen.
150
151@item
152A token consisting of a single hyphen character is interpreted as an
153ordinary non-option argument. By convention, it is used to specify
154input from or output to the standard input and output streams.
155
156@item
157Options may be supplied in any order, or appear multiple times. The
158interpretation is left up to the particular application program.
159@end itemize
160
161@cindex long-named options
162GNU adds @dfn{long options} to these conventions. Long options consist
163of @samp{--} followed by a name made of alphanumeric characters and
164dashes. Option names are typically one to three words long, with
165hyphens to separate words. Users can abbreviate the option names as
166long as the abbreviations are unique.
167
168To specify an argument for a long option, write
169@samp{--@var{name}=@var{value}}. This syntax enables a long option to
170accept an argument that is itself optional.
171
172Eventually, the GNU system will provide completion for long option names
173in the shell.
174
052b6a6c 175@node Parsing Program Arguments, , Argument Syntax, Program Arguments
b0de3e9e
UD
176@subsection Parsing Program Arguments
177
28f540f4
RM
178@cindex program arguments, parsing
179@cindex command arguments, parsing
180@cindex parsing program arguments
b0de3e9e
UD
181If the syntax for the command line arguments to your program is simple
182enough, you can simply pick the arguments off from @var{argv} by hand.
183But unless your program takes a fixed number of arguments, or all of the
184arguments are interpreted in the same way (as file names, for example),
185you are usually better off using @code{getopt} (@pxref{Getopt}) or
186@code{argp_parse} (@pxref{Argp}) to do the parsing.
28f540f4 187
b0de3e9e
UD
188@code{getopt} is more standard (the short-option only version of it is a
189part of the POSIX standard), but using @code{argp_parse} is often
190easier, both for very simple and very complex option structures, because
191it does more of the dirty work for you.
28f540f4 192
b0de3e9e
UD
193@menu
194* Getopt:: Parsing program options using @code{getopt}.
195* Argp:: Parsing program options using @code{argp_parse}.
196* Suboptions:: Some programs need more detailed options.
197* Suboptions Example:: This shows how it could be done for @code{mount}.
198@end menu
28f540f4 199
b0de3e9e
UD
200@c Getopt and argp start at the @section level so that there's
201@c enough room for their internal hierarchy (mostly a problem with
202@c argp). -Miles
28f540f4 203
b0de3e9e
UD
204@include getopt.texi
205@include argp.texi
28f540f4 206
b0de3e9e
UD
207@node Suboptions, Suboptions Example, Argp, Parsing Program Arguments
208@c This is a @section so that it's at the same level as getopt and argp
052b6a6c 209@subsubsection Parsing of Suboptions
2064087b
RM
210
211Having a single level of options is sometimes not enough. There might
212be too many options which have to be available or a set of options is
213closely related.
214
215For this case some programs use suboptions. One of the most prominent
216programs is certainly @code{mount}(8). The @code{-o} option take one
217argument which itself is a comma separated list of options. To ease the
218programming of code like this the function @code{getsubopt} is
219available.
220
221@comment stdlib.h
222@deftypefun int getsubopt (char **@var{optionp}, const char* const *@var{tokens}, char **@var{valuep})
223
224The @var{optionp} parameter must be a pointer to a variable containing
225the address of the string to process. When the function returns the
226reference is updated to point to the next suboption or to the
227terminating @samp{\0} character if there is no more suboption available.
228
229The @var{tokens} parameter references an array of strings containing the
230known suboptions. All strings must be @samp{\0} terminated and to mark
231the end a null pointer must be stored. When @code{getsubopt} finds a
232possible legal suboption it compares it with all strings available in
233the @var{tokens} array and returns the index in the string as the
234indicator.
235
236In case the suboption has an associated value introduced by a @samp{=}
237character, a pointer to the value is returned in @var{valuep}. The
238string is @samp{\0} terminated. If no argument is available
239@var{valuep} is set to the null pointer. By doing this the caller can
240check whether a necessary value is given or whether no unexpected value
241is present.
242
243In case the next suboption in the string is not mentioned in the
244@var{tokens} array the starting address of the suboption including a
245possible value is returned in @var{valuep} and the return value of the
246function is @samp{-1}.
247@end deftypefun
248
b0de3e9e 249@node Suboptions Example, , Suboptions, Parsing Program Arguments
2064087b
RM
250@subsection Parsing of Suboptions Example
251
252The code which might appear in the @code{mount}(8) program is a perfect
253example of the use of @code{getsubopt}:
254
255@smallexample
256@include subopt.c.texi
257@end smallexample
258
259
28f540f4
RM
260@node Environment Variables
261@section Environment Variables
262
263@cindex environment variable
264When a program is executed, it receives information about the context in
265which it was invoked in two ways. The first mechanism uses the
266@var{argv} and @var{argc} arguments to its @code{main} function, and is
267discussed in @ref{Program Arguments}. The second mechanism uses
268@dfn{environment variables} and is discussed in this section.
269
270The @var{argv} mechanism is typically used to pass command-line
271arguments specific to the particular program being invoked. The
272environment, on the other hand, keeps track of information that is
273shared by many programs, changes infrequently, and that is less
274frequently used.
275
276The environment variables discussed in this section are the same
277environment variables that you set using assignments and the
278@code{export} command in the shell. Programs executed from the shell
279inherit all of the environment variables from the shell.
280@c !!! xref to right part of bash manual when it exists
281
282@cindex environment
283Standard environment variables are used for information about the user's
284home directory, terminal type, current locale, and so on; you can define
285additional variables for other purposes. The set of all environment
286variables that have values is collectively known as the
287@dfn{environment}.
288
289Names of environment variables are case-sensitive and must not contain
290the character @samp{=}. System-defined environment variables are
291invariably uppercase.
292
293The values of environment variables can be anything that can be
294represented as a string. A value must not contain an embedded null
295character, since this is assumed to terminate the string.
296
297
298@menu
2064087b 299* Environment Access:: How to get and set the values of
40a55d20 300 environment variables.
2064087b 301* Standard Environment:: These environment variables have
40a55d20 302 standard interpretations.
28f540f4
RM
303@end menu
304
305@node Environment Access
306@subsection Environment Access
307@cindex environment access
308@cindex environment representation
309
310The value of an environment variable can be accessed with the
311@code{getenv} function. This is declared in the header file
40a55d20
UD
312@file{stdlib.h}. All of the following functions can be safely used in
313multi-threaded programs. It is made sure that concurrent modifications
314to the environment do not lead to errors.
28f540f4
RM
315@pindex stdlib.h
316
317@comment stdlib.h
f65fd747 318@comment ISO
28f540f4
RM
319@deftypefun {char *} getenv (const char *@var{name})
320This function returns a string that is the value of the environment
321variable @var{name}. You must not modify this string. In some non-Unix
322systems not using the GNU library, it might be overwritten by subsequent
323calls to @code{getenv} (but not by any other library function). If the
324environment variable @var{name} is not defined, the value is a null
325pointer.
326@end deftypefun
327
328
329@comment stdlib.h
330@comment SVID
14d9bd50 331@deftypefun int putenv (char *@var{string})
28f540f4
RM
332The @code{putenv} function adds or removes definitions from the environment.
333If the @var{string} is of the form @samp{@var{name}=@var{value}}, the
334definition is added to the environment. Otherwise, the @var{string} is
335interpreted as the name of an environment variable, and any definition
336for this variable in the environment is removed.
337
66f8fa9b
UD
338The difference to the @code{setenv} function is that the exact string
339given as the parameter @var{string} is put into the environment. If the
340user should change the string after the @code{putenv} call this will
341reflect in automatically in the environment. This also requires that
342@var{string} is no automatic variable which scope is left before the
d364e525
UD
343variable is removed from the environment. The same applies of course to
344dynamically allocated variables which are freed later.
66f8fa9b 345
40a55d20
UD
346This function is part of the extended Unix interface. Since it was also
347available in old SVID libraries you should define either
348@var{_XOPEN_SOURCE} or @var{_SVID_SOURCE} before including any header.
349@end deftypefun
350
351
352@comment stdlib.h
353@comment BSD
354@deftypefun int setenv (const char *@var{name}, const char *@var{value}, int @var{replace})
355The @code{setenv} function can be used to add a new definition to the
356environment. The entry with the name @var{name} is replaced by the
357value @samp{@var{name}=@var{value}}. Please note that this is also true
66f8fa9b
UD
358if @var{value} is the empty string. To do this a new string is created
359and the strings @var{name} and @var{value} are copied. A null pointer
360for the @var{value} parameter is illegal. If the environment already
361contains an entry with key @var{name} the @var{replace} parameter
362controls the action. If replace is zero, nothing happens. Otherwise
363the old entry is replaced by the new one.
40a55d20
UD
364
365Please note that you cannot remove an entry completely using this function.
366
0423ee17
UD
367This function was originally part of the BSD library but is now part of
368the Unix standard.
40a55d20
UD
369@end deftypefun
370
371@comment stdlib.h
372@comment BSD
0423ee17 373@deftypefun int unsetenv (const char *@var{name})
40a55d20
UD
374Using this function one can remove an entry completely from the
375environment. If the environment contains an entry with the key
376@var{name} this whole entry is removed. A call to this function is
377equivalent to a call to @code{putenv} when the @var{value} part of the
378string is empty.
379
0423ee17
UD
380The function return @code{-1} if @var{name} is a null pointer, points to
381an empty string, or points to a string containing a @code{=} character.
382It returns @code{0} if the call succeeded.
383
b912ca11 384This function was originally part of the BSD library but is now part of
0423ee17 385the Unix standard. The BSD version had no return value, though.
40a55d20
UD
386@end deftypefun
387
388There is one more function to modify the whole environment. This
389function is said to be used in the POSIX.9 (POSIX bindings for Fortran
39077) and so one should expect it did made it into POSIX.1. But this
391never happened. But we still provide this function as a GNU extension
392to enable writing standard compliant Fortran environments.
393
394@comment stdlib.h
395@comment GNU
396@deftypefun int clearenv (void)
397The @code{clearenv} function removes all entries from the environment.
398Using @code{putenv} and @code{setenv} new entries can be added again
399later.
400
401If the function is successful it returns @code{0}. Otherwise the return
402value is nonzero.
28f540f4
RM
403@end deftypefun
404
28f540f4
RM
405
406You can deal directly with the underlying representation of environment
407objects to add more variables to the environment (for example, to
8b7fb588
UD
408communicate with another program you are about to execute;
409@pxref{Executing a File}).
28f540f4
RM
410
411@comment unistd.h
412@comment POSIX.1
413@deftypevar {char **} environ
414The environment is represented as an array of strings. Each string is
415of the format @samp{@var{name}=@var{value}}. The order in which
416strings appear in the environment is not significant, but the same
417@var{name} must not appear more than once. The last element of the
418array is a null pointer.
419
420This variable is declared in the header file @file{unistd.h}.
421
422If you just want to get the value of an environment variable, use
423@code{getenv}.
424@end deftypevar
425
426Unix systems, and the GNU system, pass the initial value of
427@code{environ} as the third argument to @code{main}.
428@xref{Program Arguments}.
429
430@node Standard Environment
431@subsection Standard Environment Variables
432@cindex standard environment variables
433
434These environment variables have standard meanings. This doesn't mean
435that they are always present in the environment; but if these variables
436@emph{are} present, they have these meanings. You shouldn't try to use
437these environment variable names for some other purpose.
438
439@comment Extra blank lines make it look better.
440@table @code
441@item HOME
838e5ffe 442@cindex @code{HOME} environment variable
28f540f4
RM
443@cindex home directory
444
445This is a string representing the user's @dfn{home directory}, or
446initial default working directory.
447
448The user can set @code{HOME} to any value.
449If you need to make sure to obtain the proper home directory
450for a particular user, you should not use @code{HOME}; instead,
451look up the user's name in the user database (@pxref{User Database}).
452
453For most purposes, it is better to use @code{HOME}, precisely because
454this lets the user specify the value.
455
456@c !!! also USER
457@item LOGNAME
838e5ffe 458@cindex @code{LOGNAME} environment variable
28f540f4
RM
459
460This is the name that the user used to log in. Since the value in the
461environment can be tweaked arbitrarily, this is not a reliable way to
17c389fc 462identify the user who is running a program; a function like
28f540f4
RM
463@code{getlogin} (@pxref{Who Logged In}) is better for that purpose.
464
465For most purposes, it is better to use @code{LOGNAME}, precisely because
466this lets the user specify the value.
467
468@item PATH
838e5ffe 469@cindex @code{PATH} environment variable
28f540f4
RM
470
471A @dfn{path} is a sequence of directory names which is used for
472searching for a file. The variable @code{PATH} holds a path used
473for searching for programs to be run.
474
475The @code{execlp} and @code{execvp} functions (@pxref{Executing a File})
476use this environment variable, as do many shells and other utilities
477which are implemented in terms of those functions.
478
479The syntax of a path is a sequence of directory names separated by
1b82a4a8 480colons. An empty string instead of a directory name stands for the
28f540f4
RM
481current directory (@pxref{Working Directory}).
482
483A typical value for this environment variable might be a string like:
484
485@smallexample
486:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin
487@end smallexample
488
489This means that if the user tries to execute a program named @code{foo},
490the system will look for files named @file{foo}, @file{/bin/foo},
491@file{/etc/foo}, and so on. The first of these files that exists is
492the one that is executed.
493
494@c !!! also TERMCAP
495@item TERM
838e5ffe 496@cindex @code{TERM} environment variable
28f540f4
RM
497
498This specifies the kind of terminal that is receiving program output.
499Some programs can make use of this information to take advantage of
500special escape sequences or terminal modes supported by particular kinds
501of terminals. Many programs which use the termcap library
502(@pxref{Finding a Terminal Description,Find,,termcap,The Termcap Library
503Manual}) use the @code{TERM} environment variable, for example.
504
505@item TZ
838e5ffe 506@cindex @code{TZ} environment variable
28f540f4
RM
507
508This specifies the time zone. @xref{TZ Variable}, for information about
509the format of this string and how it is used.
510
511@item LANG
838e5ffe 512@cindex @code{LANG} environment variable
28f540f4
RM
513
514This specifies the default locale to use for attribute categories where
515neither @code{LC_ALL} nor the specific environment variable for that
516category is set. @xref{Locales}, for more information about
517locales.
518
519@ignore
520@c I doubt this really exists
521@item LC_ALL
838e5ffe 522@cindex @code{LC_ALL} environment variable
28f540f4
RM
523
524This is similar to the @code{LANG} environment variable. However, its
525value takes precedence over any values provided for the individual
526attribute category environment variables, or for the @code{LANG}
527environment variable.
528@end ignore
529
40a55d20 530@item LC_ALL
838e5ffe 531@cindex @code{LC_ALL} environment variable
40a55d20
UD
532
533If this environment variable is set it overrides the selection for all
534the locales done using the other @code{LC_*} environment variables. The
535value of the other @code{LC_*} environment variables is simply ignored
536in this case.
537
28f540f4 538@item LC_COLLATE
838e5ffe 539@cindex @code{LC_COLLATE} environment variable
28f540f4
RM
540
541This specifies what locale to use for string sorting.
542
543@item LC_CTYPE
838e5ffe 544@cindex @code{LC_CTYPE} environment variable
28f540f4
RM
545
546This specifies what locale to use for character sets and character
547classification.
548
40a55d20 549@item LC_MESSAGES
838e5ffe 550@cindex @code{LC_MESSAGES} environment variable
40a55d20
UD
551
552This specifies what locale to use for printing messages and to parse
f2ea0f5b 553responses.
40a55d20 554
28f540f4 555@item LC_MONETARY
838e5ffe 556@cindex @code{LC_MONETARY} environment variable
28f540f4
RM
557
558This specifies what locale to use for formatting monetary values.
559
560@item LC_NUMERIC
838e5ffe 561@cindex @code{LC_NUMERIC} environment variable
28f540f4
RM
562
563This specifies what locale to use for formatting numbers.
564
565@item LC_TIME
838e5ffe 566@cindex @code{LC_TIME} environment variable
28f540f4
RM
567
568This specifies what locale to use for formatting date/time values.
569
40a55d20 570@item NLSPATH
838e5ffe 571@cindex @code{NLSPATH} environment variable
40a55d20
UD
572
573This specifies the directories in which the @code{catopen} function
574looks for message translation catalogs.
575
28f540f4 576@item _POSIX_OPTION_ORDER
838e5ffe 577@cindex @code{_POSIX_OPTION_ORDER} environment variable.
28f540f4
RM
578
579If this environment variable is defined, it suppresses the usual
b0de3e9e
UD
580reordering of command line arguments by @code{getopt} and
581@code{argp_parse}. @xref{Argument Syntax}.
28f540f4
RM
582
583@c !!! GNU also has COREFILE, CORESERVER, EXECSERVERS
584@end table
585
17c389fc
UD
586@node System Calls
587@section System Calls
588
589@cindex system call
590A system call is a request for service that a program makes of the
591kernel. The service is generally something that only the kernel has
592the privilege to do, such as doing I/O. Programmers don't normally
593need to be concerned with system calls because there are functions in
594the GNU C library to do virtually everything that system calls do.
595These functions work by making system calls themselves. For example,
d364e525 596there is a system call that changes the permissions of a file, but
17c389fc
UD
597you don't need to know about it because you can just use the GNU C
598library's @code{chmod} function.
599
600@cindex kernel call
601System calls are sometimes called kernel calls.
602
603However, there are times when you want to make a system call explicitly,
604and for that, the GNU C library provides the @code{syscall} function.
605@code{syscall} is harder to use and less portable than functions like
606@code{chmod}, but easier and more portable than coding the system call
607in assembler instructions.
608
609@code{syscall} is most useful when you are working with a system call
610which is special to your system or is newer than the GNU C library you
611are using. @code{syscall} is implemented in an entirely generic way;
612the function does not know anything about what a particular system
613call does or even if it is valid.
614
615The description of @code{syscall} in this section assumes a certain
616protocol for system calls on the various platforms on which the GNU C
617library runs. That protocol is not defined by any strong authority, but
618we won't describe it here either because anyone who is coding
619@code{syscall} probably won't accept anything less than kernel and C
620library source code as a specification of the interface between them
621anyway.
622
623
624@code{syscall} is declared in @file{unistd.h}.
625
626@comment unistd.h
627@comment ???
4c78249d 628@deftypefun {long int} syscall (long int @var{sysno}, ...)
17c389fc
UD
629
630@code{syscall} performs a generic system call.
631
632@cindex system call number
633@var{sysno} is the system call number. Each kind of system call is
634identified by a number. Macros for all the possible system call numbers
635are defined in @file{sys/syscall.h}
636
637The remaining arguments are the arguments for the system call, in
638order, and their meanings depend on the kind of system call. Each kind
639of system call has a definite number of arguments, from zero to five.
640If you code more arguments than the system call takes, the extra ones to
641the right are ignored.
642
643The return value is the return value from the system call, unless the
644system call failed. In that case, @code{syscall} returns @code{-1} and
645sets @code{errno} to an error code that the system call returned. Note
646that system calls do not return @code{-1} when they succeed.
647@cindex errno
648
649If you specify an invalid @var{sysno}, @code{syscall} returns @code{-1}
650with @code{errno} = @code{ENOSYS}.
651
652Example:
653
654@smallexample
655
656#include <unistd.h>
657#include <sys/syscall.h>
658#include <errno.h>
659
95fdc6a0 660@dots{}
17c389fc
UD
661
662int rc;
663
664rc = syscall(SYS_chmod, "/etc/passwd", 0444);
665
d364e525 666if (rc == -1)
17c389fc
UD
667 fprintf(stderr, "chmod failed, errno = %d\n", errno);
668
669@end smallexample
670
671This, if all the compatibility stars are aligned, is equivalent to the
672following preferable code:
673
674@smallexample
675
676#include <sys/types.h>
677#include <sys/stat.h>
678#include <errno.h>
679
95fdc6a0 680@dots{}
17c389fc
UD
681
682int rc;
683
684rc = chmod("/etc/passwd", 0444);
685if (rc == -1)
686 fprintf(stderr, "chmod failed, errno = %d\n", errno);
687
688@end smallexample
689
690@end deftypefun
691
692
28f540f4
RM
693@node Program Termination
694@section Program Termination
695@cindex program termination
696@cindex process termination
697
698@cindex exit status value
699The usual way for a program to terminate is simply for its @code{main}
700function to return. The @dfn{exit status value} returned from the
701@code{main} function is used to report information back to the process's
702parent process or shell.
703
704A program can also terminate normally by calling the @code{exit}
705function.
706
707In addition, programs can be terminated by signals; this is discussed in
708more detail in @ref{Signal Handling}. The @code{abort} function causes
709a signal that kills the program.
710
711@menu
712* Normal Termination:: If a program calls @code{exit}, a
713 process terminates normally.
1b82a4a8
RM
714* Exit Status:: The @code{exit status} provides information
715 about why the process terminated.
28f540f4 716* Cleanups on Exit:: A process can run its own cleanup
1b82a4a8 717 functions upon normal termination.
28f540f4 718* Aborting a Program:: The @code{abort} function causes
1b82a4a8 719 abnormal program termination.
28f540f4
RM
720* Termination Internals:: What happens when a process terminates.
721@end menu
722
723@node Normal Termination
724@subsection Normal Termination
725
17c389fc
UD
726A process terminates normally when its program signals it is done by
727calling @code{exit}. Returning from @code{main} is equivalent to
728calling @code{exit}, and the value that @code{main} returns is used as
729the argument to @code{exit}.
28f540f4
RM
730
731@comment stdlib.h
f65fd747 732@comment ISO
28f540f4 733@deftypefun void exit (int @var{status})
17c389fc
UD
734The @code{exit} function tells the system that the program is done, which
735causes it to terminate the process.
736
737@var{status} is the program's exit status, which becomes part of the
738process' termination status. This function does not return.
28f540f4
RM
739@end deftypefun
740
741Normal termination causes the following actions:
742
743@enumerate
1b82a4a8 744@item
28f540f4
RM
745Functions that were registered with the @code{atexit} or @code{on_exit}
746functions are called in the reverse order of their registration. This
747mechanism allows your application to specify its own ``cleanup'' actions
748to be performed at program termination. Typically, this is used to do
749things like saving program state information in a file, or unlocking
750locks in shared data bases.
751
1b82a4a8 752@item
28f540f4
RM
753All open streams are closed, writing out any buffered output data. See
754@ref{Closing Streams}. In addition, temporary files opened
755with the @code{tmpfile} function are removed; see @ref{Temporary Files}.
756
1b82a4a8 757@item
28f540f4
RM
758@code{_exit} is called, terminating the program. @xref{Termination Internals}.
759@end enumerate
760
761@node Exit Status
762@subsection Exit Status
763@cindex exit status
764
765When a program exits, it can return to the parent process a small
766amount of information about the cause of termination, using the
767@dfn{exit status}. This is a value between 0 and 255 that the exiting
768process passes as an argument to @code{exit}.
769
770Normally you should use the exit status to report very broad information
771about success or failure. You can't provide a lot of detail about the
772reasons for the failure, and most parent processes would not want much
773detail anyway.
774
775There are conventions for what sorts of status values certain programs
776should return. The most common convention is simply 0 for success and 1
777for failure. Programs that perform comparison use a different
778convention: they use status 1 to indicate a mismatch, and status 2 to
779indicate an inability to compare. Your program should follow an
780existing convention if an existing convention makes sense for it.
781
782A general convention reserves status values 128 and up for special
783purposes. In particular, the value 128 is used to indicate failure to
784execute another program in a subprocess. This convention is not
785universally obeyed, but it is a good idea to follow it in your programs.
786
787@strong{Warning:} Don't try to use the number of errors as the exit
788status. This is actually not very useful; a parent process would
789generally not care how many errors occurred. Worse than that, it does
790not work, because the status value is truncated to eight bits.
791Thus, if the program tried to report 256 errors, the parent would
792receive a report of 0 errors---that is, success.
793
794For the same reason, it does not work to use the value of @code{errno}
795as the exit status---these can exceed 255.
796
797@strong{Portability note:} Some non-POSIX systems use different
798conventions for exit status values. For greater portability, you can
799use the macros @code{EXIT_SUCCESS} and @code{EXIT_FAILURE} for the
800conventional status value for success and failure, respectively. They
801are declared in the file @file{stdlib.h}.
802@pindex stdlib.h
803
804@comment stdlib.h
f65fd747 805@comment ISO
28f540f4
RM
806@deftypevr Macro int EXIT_SUCCESS
807This macro can be used with the @code{exit} function to indicate
808successful program completion.
809
810On POSIX systems, the value of this macro is @code{0}. On other
811systems, the value might be some other (possibly non-constant) integer
812expression.
813@end deftypevr
814
815@comment stdlib.h
f65fd747 816@comment ISO
28f540f4
RM
817@deftypevr Macro int EXIT_FAILURE
818This macro can be used with the @code{exit} function to indicate
819unsuccessful program completion in a general sense.
820
821On POSIX systems, the value of this macro is @code{1}. On other
822systems, the value might be some other (possibly non-constant) integer
f166d865 823expression. Other nonzero status values also indicate failures. Certain
28f540f4
RM
824programs use different nonzero status values to indicate particular
825kinds of "non-success". For example, @code{diff} uses status value
826@code{1} to mean that the files are different, and @code{2} or more to
827mean that there was difficulty in opening the files.
828@end deftypevr
829
17c389fc
UD
830Don't confuse a program's exit status with a process' termination status.
831There are lots of ways a process can terminate besides having it's program
832finish. In the event that the process termination @emph{is} caused by program
32c075e1 833termination (i.e. @code{exit}), though, the program's exit status becomes
17c389fc
UD
834part of the process' termination status.
835
28f540f4
RM
836@node Cleanups on Exit
837@subsection Cleanups on Exit
838
839Your program can arrange to run its own cleanup functions if normal
840termination happens. If you are writing a library for use in various
841application programs, then it is unreliable to insist that all
842applications call the library's cleanup functions explicitly before
843exiting. It is much more robust to make the cleanup invisible to the
844application, by setting up a cleanup function in the library itself
845using @code{atexit} or @code{on_exit}.
846
847@comment stdlib.h
f65fd747 848@comment ISO
28f540f4
RM
849@deftypefun int atexit (void (*@var{function}) (void))
850The @code{atexit} function registers the function @var{function} to be
851called at normal program termination. The @var{function} is called with
852no arguments.
853
854The return value from @code{atexit} is zero on success and nonzero if
1b82a4a8 855the function cannot be registered.
28f540f4
RM
856@end deftypefun
857
858@comment stdlib.h
859@comment SunOS
860@deftypefun int on_exit (void (*@var{function})(int @var{status}, void *@var{arg}), void *@var{arg})
861This function is a somewhat more powerful variant of @code{atexit}. It
862accepts two arguments, a function @var{function} and an arbitrary
863pointer @var{arg}. At normal program termination, the @var{function} is
864called with two arguments: the @var{status} value passed to @code{exit},
865and the @var{arg}.
866
867This function is included in the GNU C library only for compatibility
868for SunOS, and may not be supported by other implementations.
869@end deftypefun
870
871Here's a trivial program that illustrates the use of @code{exit} and
872@code{atexit}:
873
874@smallexample
875@include atexit.c.texi
876@end smallexample
877
878@noindent
879When this program is executed, it just prints the message and exits.
880
881@node Aborting a Program
882@subsection Aborting a Program
883@cindex aborting a program
884
885You can abort your program using the @code{abort} function. The prototype
886for this function is in @file{stdlib.h}.
887@pindex stdlib.h
888
889@comment stdlib.h
f65fd747 890@comment ISO
28f540f4
RM
891@deftypefun void abort (void)
892The @code{abort} function causes abnormal program termination. This
893does not execute cleanup functions registered with @code{atexit} or
894@code{on_exit}.
895
896This function actually terminates the process by raising a
897@code{SIGABRT} signal, and your program can include a handler to
898intercept this signal; see @ref{Signal Handling}.
899@end deftypefun
900
901@c Put in by rms. Don't remove.
902@cartouche
903@strong{Future Change Warning:} Proposed Federal censorship regulations
904may prohibit us from giving you information about the possibility of
905calling this function. We would be required to say that this is not an
906acceptable way of terminating a program.
907@end cartouche
908
909@node Termination Internals
910@subsection Termination Internals
911
912The @code{_exit} function is the primitive used for process termination
913by @code{exit}. It is declared in the header file @file{unistd.h}.
914@pindex unistd.h
915
916@comment unistd.h
917@comment POSIX.1
918@deftypefun void _exit (int @var{status})
919The @code{_exit} function is the primitive for causing a process to
920terminate with status @var{status}. Calling this function does not
921execute cleanup functions registered with @code{atexit} or
922@code{on_exit}.
923@end deftypefun
924
e518937a
UD
925@comment stdlib.h
926@comment ISO
927@deftypefun void _Exit (int @var{status})
928The @code{_Exit} function is the @w{ISO C} equivalent to @code{_exit}.
929The @w{ISO C} committee members were not sure whether the definitions of
930@code{_exit} and @code{_Exit} were compatible so they have not used the
931POSIX name.
932
ec751a23 933This function was introduced in @w{ISO C99} and is declared in
e518937a
UD
934@file{stdlib.h}.
935@end deftypefun
936
17c389fc
UD
937When a process terminates for any reason---either because the program
938terminates, or as a result of a signal---the
28f540f4
RM
939following things happen:
940
941@itemize @bullet
942@item
943All open file descriptors in the process are closed. @xref{Low-Level I/O}.
944Note that streams are not flushed automatically when the process
8b7fb588 945terminates; see @ref{I/O on Streams}.
28f540f4
RM
946
947@item
17c389fc
UD
948A process exit status is saved to be reported back to the parent process
949via @code{wait} or @code{waitpid}; see @ref{Process Completion}. If the
950program exited, this status includes as its low-order 8 bits the program
951exit status.
952
28f540f4
RM
953
954@item
955Any child processes of the process being terminated are assigned a new
956parent process. (On most systems, including GNU, this is the @code{init}
957process, with process ID 1.)
958
959@item
960A @code{SIGCHLD} signal is sent to the parent process.
961
962@item
963If the process is a session leader that has a controlling terminal, then
964a @code{SIGHUP} signal is sent to each process in the foreground job,
965and the controlling terminal is disassociated from that session.
966@xref{Job Control}.
967
968@item
969If termination of a process causes a process group to become orphaned,
970and any member of that process group is stopped, then a @code{SIGHUP}
971signal and a @code{SIGCONT} signal are sent to each process in the
972group. @xref{Job Control}.
973@end itemize