| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package process |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/url" |
| 21 |
"os" |
| 22 |
"os/exec" |
| 23 |
) |
| 24 |
|
| 25 |
// NewBinaryCmd returns a Cmd to be used to start a logging binary. |
| 26 |
// The Cmd is generated from the provided uri, and the container ID and |
| 27 |
// namespace are appended to the Cmd environment. |
| 28 |
func NewBinaryCmd(binaryURI *url.URL, id, ns string) *exec.Cmd { |
| 29 |
var args []string |
| 30 |
for k, vs := range binaryURI.Query() { |
| 31 |
args = append(args, k) |
| 32 |
if len(vs) > 0 { |
| 33 |
args = append(args, vs[0]) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
cmd := exec.Command(binaryURI.Path, args...) |
| 38 |
|
| 39 |
cmd.Env = append(cmd.Env, |
| 40 |
"CONTAINER_ID="+id, |
| 41 |
"CONTAINER_NAMESPACE="+ns, |
| 42 |
) |
| 43 |
|
| 44 |
return cmd |
| 45 |
} |
| 46 |
|
| 47 |
// CloseFiles closes any files passed in. |
| 48 |
// It it used for cleanup in the event of unexpected errors. |
| 49 |
func CloseFiles(files ...*os.File) { |
| 50 |
for _, file := range files { |
| 51 |
file.Close() |
| 52 |
} |
| 53 |
} |