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