startup.texi 43 KB

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