xref: /openbsd-src/lib/libc/stdlib/system.3 (revision 3abee6c94ac0eb72fc712d2547c03ef6b4ef95d5)
1df930be7Sderaadt.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2df930be7Sderaadt.\" All rights reserved.
3df930be7Sderaadt.\"
4df930be7Sderaadt.\" This code is derived from software contributed to Berkeley by
5df930be7Sderaadt.\" the American National Standards Committee X3, on Information
6df930be7Sderaadt.\" Processing Systems.
7df930be7Sderaadt.\"
8df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt.\" modification, are permitted provided that the following conditions
10df930be7Sderaadt.\" are met:
11df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt.\"    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt.\"    documentation and/or other materials provided with the distribution.
166580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt.\"    may be used to endorse or promote products derived from this software
18df930be7Sderaadt.\"    without specific prior written permission.
19df930be7Sderaadt.\"
20df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt.\" SUCH DAMAGE.
31df930be7Sderaadt.\"
32*3abee6c9Sespie.\"     $OpenBSD: system.3,v 1.14 2016/02/05 18:09:19 espie Exp $
33df930be7Sderaadt.\"
34*3abee6c9Sespie.Dd $Mdocdate: February 5 2016 $
35df930be7Sderaadt.Dt SYSTEM 3
36df930be7Sderaadt.Os
37df930be7Sderaadt.Sh NAME
38df930be7Sderaadt.Nm system
39df930be7Sderaadt.Nd pass a command to the shell
40df930be7Sderaadt.Sh SYNOPSIS
4164d4e987Stedu.In stdlib.h
42df930be7Sderaadt.Ft int
43df930be7Sderaadt.Fn system "const char *string"
44df930be7Sderaadt.Sh DESCRIPTION
45df930be7SderaadtThe
46df930be7Sderaadt.Fn system
47b43b9088Saaronfunction hands the argument
48df930be7Sderaadt.Fa string
49df930be7Sderaadtto the command interpreter
50df930be7Sderaadt.Xr sh 1 .
51df930be7SderaadtThe calling process waits for the shell to finish executing the command,
52df930be7Sderaadtignoring
53df930be7Sderaadt.Dv SIGINT
54df930be7Sderaadtand
55df930be7Sderaadt.Dv SIGQUIT ,
56df930be7Sderaadtand blocking
57df930be7Sderaadt.Dv SIGCHLD .
58df930be7Sderaadt.Pp
59df930be7SderaadtIf
60df930be7Sderaadt.Fa string
61b43b9088Saaronis
62b43b9088Saaron.Dv NULL ,
63df930be7Sderaadt.Fn system
64df930be7Sderaadtwill return non-zero.
65df930be7SderaadtOtherwise,
66df930be7Sderaadt.Fn system
67df930be7Sderaadtreturns the termination status of the shell in the format specified by
68c983ea29Sderaadt.Xr waitpid 2 .
69cb2f56c4Skurt.Pp
70cb2f56c4SkurtNote that fork handlers established using
71cb2f56c4Skurt.Xr pthread_atfork 3
72cb2f56c4Skurtare not called when a multithreaded program calls
73cb2f56c4Skurt.Fn system .
74df930be7Sderaadt.Sh RETURN VALUES
75df930be7SderaadtIf a child process cannot be created, or the termination status of
76df930be7Sderaadtthe shell cannot be obtained,
77df930be7Sderaadt.Fn system
78c5a79f6dSaaronreturns \-1 and sets
79df930be7Sderaadt.Va errno
80df930be7Sderaadtto indicate the error.
81df930be7SderaadtIf execution of the shell fails,
82df930be7Sderaadt.Fn system
83df930be7Sderaadtreturns the termination status for a program that terminates with a call of
84df930be7Sderaadt.Fn exit 127 .
85df930be7Sderaadt.Sh SEE ALSO
86df930be7Sderaadt.Xr sh 1 ,
87df930be7Sderaadt.Xr execve 2 ,
883fbbad10Sniklas.Xr waitpid 2 ,
893fbbad10Sniklas.Xr popen 3
90df930be7Sderaadt.Sh STANDARDS
91df930be7SderaadtThe
92df930be7Sderaadt.Fn system
93b43b9088Saaronfunction conforms to
94df930be7Sderaadt.St -ansiC
95df930be7Sderaadtand
96937e75d5Stholo.St -p1003.2-92 .
97a51c8c4aSschwarze.Sh HISTORY
98a51c8c4aSschwarzeThe
99a51c8c4aSschwarze.Fn system
100a51c8c4aSschwarzefunction first appeared in
101a51c8c4aSschwarze.At v6 .
102f2c1a4e1Saaron.Sh CAVEATS
103f2c1a4e1SaaronNever supply the
104f2c1a4e1Saaron.Fn system
105f2c1a4e1Saaronfunction with a command containing any part of an unsanitized user-supplied
106f2c1a4e1Saaronstring.
107f2c1a4e1SaaronShell meta-characters present will be honored by the
108f2c1a4e1Saaron.Xr sh 1
109f2c1a4e1Saaroncommand interpreter.
110*3abee6c9Sespie.Pp
111*3abee6c9SespieIt is often simpler to bypass the shell and run an external command using
112*3abee6c9Sespie.Xr fork 2 ,
113*3abee6c9Sespie.Xr execlp 3 ,
114*3abee6c9Sespieand
115*3abee6c9Sespie.Xr waitpid 2
116*3abee6c9Sespiedirectly instead of having to sanitize a string for shell consumption.
117