logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

system.3p (14909B)


  1. '\" et
  2. .TH SYSTEM "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. system
  12. \(em issue a command
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdlib.h>
  17. .P
  18. int system(const char *\fIcommand\fP);
  19. .fi
  20. .SH DESCRIPTION
  21. The functionality described on this reference page is aligned with the
  22. ISO\ C standard. Any conflict between the requirements described here and the
  23. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  24. .P
  25. If
  26. .IR command
  27. is a null pointer, the
  28. \fIsystem\fR()
  29. function shall determine whether the host environment has a command
  30. processor. If
  31. .IR command
  32. is not a null pointer, the
  33. \fIsystem\fR()
  34. function shall pass the string pointed to by
  35. .IR command
  36. to that command processor to be executed in an implementation-defined
  37. manner; this might then cause the program calling
  38. \fIsystem\fR()
  39. to behave in a non-conforming manner or to terminate.
  40. .P
  41. The
  42. \fIsystem\fR()
  43. function shall behave as if a child process were created using
  44. \fIfork\fR(),
  45. and the child process invoked the
  46. .IR sh
  47. utility using
  48. \fIexecl\fR()
  49. as follows:
  50. .sp
  51. .RS 4
  52. .nf
  53. execl(<\fIshell path\fP>, "sh", "-c", \fIcommand\fP, (char *)0);
  54. .fi
  55. .P
  56. .RE
  57. .P
  58. where <\fIshell path\fP> is an unspecified pathname for the
  59. .IR sh
  60. utility. It is unspecified whether the handlers registered with
  61. \fIpthread_atfork\fR()
  62. are called as part of the creation of the child process.
  63. .P
  64. The
  65. \fIsystem\fR()
  66. function shall ignore the SIGINT and SIGQUIT signals, and shall
  67. block the SIGCHLD
  68. signal, while waiting for the command to terminate. If this might
  69. cause the application to miss a signal that would have killed it, then
  70. the application should examine the return value from
  71. \fIsystem\fR()
  72. and take whatever action is appropriate to the application if the
  73. command terminated due to receipt of a signal.
  74. .P
  75. The
  76. \fIsystem\fR()
  77. function shall not affect the termination status of any child of the
  78. calling processes other than the process or processes it itself
  79. creates.
  80. .P
  81. The
  82. \fIsystem\fR()
  83. function shall not return until the child process has terminated.
  84. .P
  85. The
  86. \fIsystem\fR()
  87. function need not be thread-safe.
  88. .SH "RETURN VALUE"
  89. If
  90. .IR command
  91. is a null pointer,
  92. \fIsystem\fR()
  93. shall return non-zero to indicate that a command processor is
  94. available, or zero if none is available.
  95. The
  96. \fIsystem\fR()
  97. function shall always return non-zero when
  98. .IR command
  99. is NULL.
  100. .P
  101. If
  102. .IR command
  103. is not a null pointer,
  104. \fIsystem\fR()
  105. shall return the termination status of the command language interpreter
  106. in the format specified by
  107. \fIwaitpid\fR().
  108. The termination status shall be as defined for the
  109. .IR sh
  110. utility; otherwise, the termination status is unspecified. If some
  111. error prevents the command language interpreter from executing after
  112. the child process is created, the return value from
  113. \fIsystem\fR()
  114. shall be as if the command language interpreter had terminated using
  115. .IR exit (127)
  116. or
  117. .IR _exit (127).
  118. If a child process cannot be created, or if the termination status for
  119. the command language interpreter cannot be obtained,
  120. \fIsystem\fR()
  121. shall return \-1 and set
  122. .IR errno
  123. to indicate the error.
  124. .SH ERRORS
  125. The
  126. \fIsystem\fR()
  127. function may set
  128. .IR errno
  129. values as described by
  130. .IR "\fIfork\fR\^(\|)".
  131. .br
  132. .P
  133. In addition,
  134. \fIsystem\fR()
  135. may fail if:
  136. .TP
  137. .BR ECHILD
  138. The status of the child process created by
  139. \fIsystem\fR()
  140. is no longer available.
  141. .LP
  142. .IR "The following sections are informative."
  143. .SH EXAMPLES
  144. None.
  145. .SH "APPLICATION USAGE"
  146. If the return value of
  147. \fIsystem\fR()
  148. is not \-1, its value can be decoded through the use of the macros
  149. described in
  150. .IR <sys/wait.h> .
  151. For convenience, these macros are also provided in
  152. .IR <stdlib.h> .
  153. .P
  154. Note that, while
  155. \fIsystem\fR()
  156. must ignore SIGINT and SIGQUIT and block SIGCHLD while waiting for the
  157. child to terminate, the handling of signals in the executed command is
  158. as specified by
  159. \fIfork\fR()
  160. and
  161. .IR exec .
  162. For example, if SIGINT is being caught or is set to SIG_DFL when
  163. \fIsystem\fR()
  164. is called, then the child is started with SIGINT handling set to
  165. SIG_DFL.
  166. .P
  167. Ignoring SIGINT and SIGQUIT in the parent process prevents coordination
  168. problems (two processes reading from the same terminal, for example)
  169. when the executed command ignores or catches one of the signals. It is
  170. also usually the correct action when the user has given a command to
  171. the application to be executed synchronously (as in the
  172. .BR '!'
  173. command in many interactive applications). In either case, the signal
  174. should be delivered only to the child process, not to the application
  175. itself. There is one situation where ignoring the signals might have
  176. less than the desired effect. This is when the application uses
  177. \fIsystem\fR()
  178. to perform some task invisible to the user. If the user typed the
  179. interrupt character (\c
  180. .BR \(dq\(haC\(dq ,
  181. for example) while
  182. \fIsystem\fR()
  183. is being used in this way, one would expect the application to be
  184. killed, but only the executed command is killed. Applications that use
  185. \fIsystem\fR()
  186. in this way should carefully check the return status from
  187. \fIsystem\fR()
  188. to see if the executed command was successful, and should take
  189. appropriate action when the command fails.
  190. .P
  191. Blocking SIGCHLD while waiting for the child to terminate prevents the
  192. application from catching the signal and obtaining status from
  193. \fIsystem\fR()'s
  194. child process before
  195. \fIsystem\fR()
  196. can get the status itself.
  197. .P
  198. The context in which the utility is ultimately executed may differ from
  199. that in which
  200. \fIsystem\fR()
  201. was called. For example, file descriptors that have the FD_CLOEXEC
  202. flag set are closed, and the process ID and parent process ID are
  203. different. Also, if the executed utility changes its environment
  204. variables or its current working directory, that change is not
  205. reflected in the caller's context.
  206. .P
  207. There is no defined way for an application to find the specific path
  208. for the shell. However,
  209. \fIconfstr\fR()
  210. can provide a value for
  211. .IR PATH
  212. that is guaranteed to find the
  213. .IR sh
  214. utility.
  215. .P
  216. Using the
  217. \fIsystem\fR()
  218. function in more than one thread in a process or when the SIGCHLD
  219. signal is being manipulated by more than one thread in a process may
  220. produce unexpected results.
  221. .SH RATIONALE
  222. The
  223. \fIsystem\fR()
  224. function should not be used by programs that have set user (or group)
  225. ID privileges. The
  226. \fIfork\fR()
  227. and
  228. .IR exec
  229. family of functions (except
  230. \fIexeclp\fR()
  231. and
  232. \fIexecvp\fR()),
  233. should be used instead. This prevents any unforeseen manipulation of
  234. the environment of the user that could cause execution of commands not
  235. anticipated by the calling program.
  236. .P
  237. There are three levels of specification for the
  238. \fIsystem\fR()
  239. function. The ISO\ C standard gives the most basic. It requires that the function
  240. exists, and defines a way for an application to query whether a command
  241. language interpreter exists. It says nothing about the command language
  242. or the environment in which the command is interpreted.
  243. .P
  244. POSIX.1\(hy2008 places additional restrictions on
  245. \fIsystem\fR().
  246. It requires that if there is a command language interpreter, the
  247. environment must be as specified by
  248. \fIfork\fR()
  249. and
  250. .IR exec .
  251. This ensures, for example, that close-on-\c
  252. .IR exec
  253. works, that file locks are not inherited, and that the process ID is
  254. different. It also specifies the return value from
  255. \fIsystem\fR()
  256. when the command line can be run, thus giving the application some
  257. information about the command's completion status.
  258. .P
  259. Finally, POSIX.1\(hy2008 requires the command to be interpreted as in the shell
  260. command language defined in the Shell and Utilities volume of POSIX.1\(hy2017.
  261. .P
  262. Note that,
  263. .IR system (NULL)
  264. is required to return non-zero, indicating that there is a command
  265. language interpreter. At first glance, this would seem to conflict with
  266. the ISO\ C standard which allows
  267. .IR system (NULL)
  268. to return zero. There is no conflict, however. A system must have a
  269. command language interpreter, and is non-conforming if none is present.
  270. It is therefore permissible for the
  271. \fIsystem\fR()
  272. function on such a system to implement the behavior specified by the
  273. ISO\ C standard as long as it is understood that the implementation does not
  274. conform to POSIX.1\(hy2008 if
  275. .IR system (NULL)
  276. returns zero.
  277. .P
  278. It was explicitly decided that when
  279. .IR command
  280. is NULL,
  281. \fIsystem\fR()
  282. should not be required to check to make sure that the command language
  283. interpreter actually exists with the correct mode, that there are
  284. enough processes to execute it, and so on. The call
  285. .IR system (NULL)
  286. could, theoretically, check for such problems as too many existing
  287. child processes, and return zero. However, it would be inappropriate to
  288. return zero due to such a (presumably) transient condition. If some
  289. condition exists that is not under the control of this application and
  290. that would cause any
  291. \fIsystem\fR()
  292. call to fail, that system has been rendered non-conforming.
  293. .P
  294. Early drafts required, or allowed,
  295. \fIsystem\fR()
  296. to return with
  297. .IR errno
  298. set to
  299. .BR [EINTR]
  300. if it was interrupted with a signal. This error return was removed, and
  301. a requirement that
  302. \fIsystem\fR()
  303. not return until the child has terminated was added. This means that if
  304. a
  305. \fIwaitpid\fR()
  306. call in
  307. \fIsystem\fR()
  308. exits with
  309. .IR errno
  310. set to
  311. .BR [EINTR] ,
  312. \fIsystem\fR()
  313. must reissue the
  314. \fIwaitpid\fR().
  315. This change was made for two reasons:
  316. .IP " 1." 4
  317. There is no way for an application to clean up if
  318. \fIsystem\fR()
  319. returns
  320. .BR [EINTR] ,
  321. short of calling
  322. \fIwait\fR(),
  323. and that could have the undesirable effect of returning the status of
  324. children other than the one started by
  325. \fIsystem\fR().
  326. .IP " 2." 4
  327. While it might require a change in some historical implementations,
  328. those implementations already have to be changed because they use
  329. \fIwait\fR()
  330. instead of
  331. \fIwaitpid\fR().
  332. .P
  333. Note that if the application is catching SIGCHLD signals, it will
  334. receive such a signal before a successful
  335. \fIsystem\fR()
  336. call returns.
  337. .P
  338. To conform to POSIX.1\(hy2008,
  339. \fIsystem\fR()
  340. must use
  341. \fIwaitpid\fR(),
  342. or some similar function, instead of
  343. \fIwait\fR().
  344. .P
  345. The following code sample illustrates how
  346. \fIsystem\fR()
  347. might be implemented on an implementation conforming to POSIX.1\(hy2008.
  348. .sp
  349. .RS 4
  350. .nf
  351. #include <signal.h>
  352. int system(const char *cmd)
  353. {
  354. int stat;
  355. pid_t pid;
  356. struct sigaction sa, savintr, savequit;
  357. sigset_t saveblock;
  358. if (cmd == NULL)
  359. return(1);
  360. sa.sa_handler = SIG_IGN;
  361. sigemptyset(&sa.sa_mask);
  362. sa.sa_flags = 0;
  363. sigemptyset(&savintr.sa_mask);
  364. sigemptyset(&savequit.sa_mask);
  365. sigaction(SIGINT, &sa, &savintr);
  366. sigaction(SIGQUIT, &sa, &savequit);
  367. sigaddset(&sa.sa_mask, SIGCHLD);
  368. sigprocmask(SIG_BLOCK, &sa.sa_mask, &saveblock);
  369. if ((pid = fork()) == 0) {
  370. sigaction(SIGINT, &savintr, (struct sigaction *)0);
  371. sigaction(SIGQUIT, &savequit, (struct sigaction *)0);
  372. sigprocmask(SIG_SETMASK, &saveblock, (sigset_t *)0);
  373. execl("/bin/sh", "sh", "-c", cmd, (char *)0);
  374. _exit(127);
  375. }
  376. if (pid == -1) {
  377. stat = -1; /* errno comes from fork() */
  378. } else {
  379. while (waitpid(pid, &stat, 0) == -1) {
  380. if (errno != EINTR){
  381. stat = -1;
  382. break;
  383. }
  384. }
  385. }
  386. sigaction(SIGINT, &savintr, (struct sigaction *)0);
  387. sigaction(SIGQUIT, &savequit, (struct sigaction *)0);
  388. sigprocmask(SIG_SETMASK, &saveblock, (sigset_t *)0);
  389. return(stat);
  390. }
  391. .fi
  392. .P
  393. .RE
  394. .P
  395. Note that, while a particular implementation of
  396. \fIsystem\fR()
  397. (such as the one above) can assume a particular path for the shell,
  398. such a path is not necessarily valid on another system. The above
  399. example is not portable, and is not intended to be.
  400. .P
  401. Note also that the above example implementation is not thread-safe.
  402. Implementations can provide a thread-safe
  403. \fIsystem\fR()
  404. function, but doing so involves complications such as how to restore the
  405. signal dispositions for SIGINT and SIGQUIT correctly if there are
  406. overlapping calls, and how to deal with cancellation. The example
  407. above would not restore the signal dispositions and would leak
  408. a process ID if cancelled. This does not matter for a
  409. non-thread-safe implementation since canceling a non-thread-safe
  410. function results in undefined behavior (see
  411. .IR "Section 2.9.5.2" ", " "Cancellation Points").
  412. To avoid leaking a process ID, a thread-safe implementation would need
  413. to terminate the child process when acting on a cancellation.
  414. .P
  415. One reviewer suggested that an implementation of
  416. \fIsystem\fR()
  417. might want to use an environment variable such as
  418. .IR SHELL
  419. to determine which command interpreter to use. The supposed
  420. implementation would use the default command interpreter if the one
  421. specified by the environment variable was not available. This would
  422. allow a user, when using an application that prompts for command lines
  423. to be processed using
  424. \fIsystem\fR(),
  425. to specify a different command interpreter. Such an implementation is
  426. discouraged. If the alternate command interpreter did not follow the
  427. command line syntax specified in the Shell and Utilities volume of POSIX.1\(hy2017, then changing
  428. .IR SHELL
  429. would render
  430. \fIsystem\fR()
  431. non-conforming. This would affect applications that expected the
  432. specified behavior from
  433. \fIsystem\fR(),
  434. and since the Shell and Utilities volume of POSIX.1\(hy2017 does not mention that
  435. .IR SHELL
  436. affects
  437. \fIsystem\fR(),
  438. the application would not know that it needed to unset
  439. .IR SHELL .
  440. .SH "FUTURE DIRECTIONS"
  441. None.
  442. .SH "SEE ALSO"
  443. .IR "Section 2.9.5.2" ", " "Cancellation Points",
  444. .IR "\fIexec\fR\^",
  445. .IR "\fIpipe\fR\^(\|)",
  446. .IR "\fIpthread_atfork\fR\^(\|)",
  447. .IR "\fIwait\fR\^(\|)"
  448. .P
  449. The Base Definitions volume of POSIX.1\(hy2017,
  450. .IR "\fB<limits.h>\fP",
  451. .IR "\fB<signal.h>\fP",
  452. .IR "\fB<stdlib.h>\fP",
  453. .IR "\fB<sys_wait.h>\fP"
  454. .P
  455. The Shell and Utilities volume of POSIX.1\(hy2017,
  456. .IR "\fIsh\fR\^"
  457. .\"
  458. .SH COPYRIGHT
  459. Portions of this text are reprinted and reproduced in electronic form
  460. from IEEE Std 1003.1-2017, Standard for Information Technology
  461. -- Portable Operating System Interface (POSIX), The Open Group Base
  462. Specifications Issue 7, 2018 Edition,
  463. Copyright (C) 2018 by the Institute of
  464. Electrical and Electronics Engineers, Inc and The Open Group.
  465. In the event of any discrepancy between this version and the original IEEE and
  466. The Open Group Standard, the original IEEE and The Open Group Standard
  467. is the referee document. The original Standard can be obtained online at
  468. http://www.opengroup.org/unix/online.html .
  469. .PP
  470. Any typographical or formatting errors that appear
  471. in this page are most likely
  472. to have been introduced during the conversion of the source files to
  473. man page format. To report such errors, see
  474. https://www.kernel.org/doc/man-pages/reporting_bugs.html .