mirror of https://github.com/docker/cli.git
cli/connhelper: remove dependency on pkg/process
This package will not be included in the api or client modules, and we're currently only using a single function of it, and only the unix implementation, so let's fork it for now (although the package may be moved to moby/sys). This removes the last dependency on github.com/docker/docker. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
fcfaa8daeb
commit
2abcbf842f
|
|
@ -4,12 +4,17 @@ package commandconn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/process"
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
@ -51,16 +56,16 @@ func TestCloseRunningCommand(t *testing.T) {
|
||||||
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
cmdConn := c.(*commandConn)
|
cmdConn := c.(*commandConn)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
n, err := c.Write([]byte("hello"))
|
n, err := c.Write([]byte("hello"))
|
||||||
assert.Check(t, is.Equal(len("hello"), n))
|
assert.Check(t, is.Equal(len("hello"), n))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
err = cmdConn.Close()
|
err = cmdConn.Close()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -79,7 +84,7 @@ func TestCloseTwice(t *testing.T) {
|
||||||
c, err := New(ctx, "sh", "-c", "echo hello; sleep 1; exit 0")
|
c, err := New(ctx, "sh", "-c", "echo hello; sleep 1; exit 0")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
cmdConn := c.(*commandConn)
|
cmdConn := c.(*commandConn)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
n, err := c.Read(b)
|
n, err := c.Read(b)
|
||||||
|
|
@ -88,11 +93,11 @@ func TestCloseTwice(t *testing.T) {
|
||||||
|
|
||||||
err = cmdConn.Close()
|
err = cmdConn.Close()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
err = cmdConn.Close()
|
err = cmdConn.Close()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -111,7 +116,7 @@ func TestEOFTimeout(t *testing.T) {
|
||||||
c, err := New(ctx, "sh", "-c", "sleep 20")
|
c, err := New(ctx, "sh", "-c", "sleep 20")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
cmdConn := c.(*commandConn)
|
cmdConn := c.(*commandConn)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
cmdConn.stdout = mockStdoutEOF{}
|
cmdConn.stdout = mockStdoutEOF{}
|
||||||
|
|
||||||
|
|
@ -148,7 +153,7 @@ func TestCloseWhileWriting(t *testing.T) {
|
||||||
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
cmdConn := c.(*commandConn)
|
cmdConn := c.(*commandConn)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
writeErrC := make(chan error)
|
writeErrC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -164,7 +169,7 @@ func TestCloseWhileWriting(t *testing.T) {
|
||||||
|
|
||||||
err = c.Close()
|
err = c.Close()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
writeErr := <-writeErrC
|
writeErr := <-writeErrC
|
||||||
assert.ErrorContains(t, writeErr, "file already closed")
|
assert.ErrorContains(t, writeErr, "file already closed")
|
||||||
|
|
@ -176,7 +181,7 @@ func TestCloseWhileReading(t *testing.T) {
|
||||||
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
cmdConn := c.(*commandConn)
|
cmdConn := c.(*commandConn)
|
||||||
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
readErrC := make(chan error)
|
readErrC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -193,8 +198,37 @@ func TestCloseWhileReading(t *testing.T) {
|
||||||
|
|
||||||
err = cmdConn.Close()
|
err = cmdConn.Close()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
|
||||||
|
|
||||||
readErr := <-readErrC
|
readErr := <-readErrC
|
||||||
assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
|
assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processAlive returns true if a process with a given pid is running. It only considers
|
||||||
|
// positive PIDs; 0 (all processes in the current process group), -1 (all processes
|
||||||
|
// with a PID larger than 1), and negative (-n, all processes in process group
|
||||||
|
// "n") values for pid are never considered to be alive.
|
||||||
|
//
|
||||||
|
// It was forked from https://github.com/moby/moby/blob/v28.3.3/pkg/process/process_unix.go#L17-L42
|
||||||
|
func processAlive(pid int) bool {
|
||||||
|
if pid < 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin":
|
||||||
|
// OS X does not have a proc filesystem. Use kill -0 pid to judge if the
|
||||||
|
// process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1
|
||||||
|
//
|
||||||
|
// Sig may be one of the signals specified in sigaction(2) or it may
|
||||||
|
// be 0, in which case error checking is performed but no signal is
|
||||||
|
// actually sent. This can be used to check the validity of pid.
|
||||||
|
err := syscall.Kill(pid, 0)
|
||||||
|
|
||||||
|
// Either the PID was found (no error), or we get an EPERM, which means
|
||||||
|
// the PID exists, but we don't have permissions to signal it.
|
||||||
|
return err == nil || errors.Is(err, syscall.EPERM)
|
||||||
|
default:
|
||||||
|
_, err := os.Stat(filepath.Join("/proc", strconv.Itoa(pid)))
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ require (
|
||||||
github.com/distribution/reference v0.6.0
|
github.com/distribution/reference v0.6.0
|
||||||
github.com/docker/cli-docs-tool v0.10.0
|
github.com/docker/cli-docs-tool v0.10.0
|
||||||
github.com/docker/distribution v2.8.3+incompatible
|
github.com/docker/distribution v2.8.3+incompatible
|
||||||
github.com/docker/docker v28.2.3-0.20250731152656-4faedf2bec36+incompatible // master (v29.0-dev)
|
|
||||||
github.com/docker/docker-credential-helpers v0.9.3
|
github.com/docker/docker-credential-helpers v0.9.3
|
||||||
github.com/docker/go-connections v0.5.0
|
github.com/docker/go-connections v0.5.0
|
||||||
github.com/docker/go-units v0.5.0
|
github.com/docker/go-units v0.5.0
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,6 @@ github.com/docker/cli-docs-tool v0.10.0/go.mod h1:5EM5zPnT2E7yCLERZmrDA234Vwn09f
|
||||||
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
||||||
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
|
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
|
||||||
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
||||||
github.com/docker/docker v28.2.3-0.20250731152656-4faedf2bec36+incompatible h1:V1yo9808DrQaHES6Q3hW5Rtd44+mrc4oKulmzPgRDAM=
|
|
||||||
github.com/docker/docker v28.2.3-0.20250731152656-4faedf2bec36+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
|
||||||
github.com/docker/docker-credential-helpers v0.9.3 h1:gAm/VtF9wgqJMoxzT3Gj5p4AqIjCBS4wrsOh9yRqcz8=
|
github.com/docker/docker-credential-helpers v0.9.3 h1:gAm/VtF9wgqJMoxzT3Gj5p4AqIjCBS4wrsOh9yRqcz8=
|
||||||
github.com/docker/docker-credential-helpers v0.9.3/go.mod h1:x+4Gbw9aGmChi3qTLZj8Dfn0TD20M/fuWy0E5+WDeCo=
|
github.com/docker/docker-credential-helpers v0.9.3/go.mod h1:x+4Gbw9aGmChi3qTLZj8Dfn0TD20M/fuWy0E5+WDeCo=
|
||||||
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c h1:lzqkGL9b3znc+ZUgi7FlLnqjQhcXxkNM/quxIjBVMD0=
|
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c h1:lzqkGL9b3znc+ZUgi7FlLnqjQhcXxkNM/quxIjBVMD0=
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,191 +0,0 @@
|
||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
https://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
Copyright 2013-2018 Docker, Inc.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
Docker
|
|
||||||
Copyright 2012-2017 Docker, Inc.
|
|
||||||
|
|
||||||
This product includes software developed at Docker, Inc. (https://www.docker.com).
|
|
||||||
|
|
||||||
This product contains software (https://github.com/creack/pty) developed
|
|
||||||
by Keith Rarick, licensed under the MIT License.
|
|
||||||
|
|
||||||
The following is courtesy of our legal counsel:
|
|
||||||
|
|
||||||
|
|
||||||
Use and transfer of Docker may be subject to certain restrictions by the
|
|
||||||
United States and other governments.
|
|
||||||
It is your responsibility to ensure that your use and/or transfer does not
|
|
||||||
violate applicable laws.
|
|
||||||
|
|
||||||
For more information, please see https://www.bis.doc.gov
|
|
||||||
|
|
||||||
See also https://www.apache.org/dev/crypto.html and/or seek legal counsel.
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
// Package process provides a set of basic functions to manage individual
|
|
||||||
// processes.
|
|
||||||
package process
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
package process
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Alive returns true if process with a given pid is running.
|
|
||||||
//
|
|
||||||
// It only considers positive PIDs; 0 (all processes in the current process
|
|
||||||
// group), -1 (all processes with a PID larger than 1), and negative (-n,
|
|
||||||
// all processes in process group "n") values for pid are never considered
|
|
||||||
// to be alive.
|
|
||||||
func Alive(pid int) bool {
|
|
||||||
if pid < 1 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return alive(pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Kill force-stops a process. It only allows positive PIDs; 0 (all processes
|
|
||||||
// in the current process group), -1 (all processes with a PID larger than 1),
|
|
||||||
// and negative (-n, all processes in process group "n") values for pid producs
|
|
||||||
// an error. Refer to [KILL(2)] for details.
|
|
||||||
//
|
|
||||||
// [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html
|
|
||||||
func Kill(pid int) error {
|
|
||||||
if pid < 1 {
|
|
||||||
return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid)
|
|
||||||
}
|
|
||||||
return kill(pid)
|
|
||||||
}
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
package process
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func zombie(pid int) (bool, error) {
|
|
||||||
if pid < 1 {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
|
|
||||||
if err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
//go:build !linux
|
|
||||||
|
|
||||||
package process
|
|
||||||
|
|
||||||
func zombie(pid int) (bool, error) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
//go:build !windows
|
|
||||||
|
|
||||||
package process
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func alive(pid int) bool {
|
|
||||||
switch runtime.GOOS {
|
|
||||||
case "darwin":
|
|
||||||
// macOS does not have a proc filesystem. Use kill -0 pid to judge if the
|
|
||||||
// process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1
|
|
||||||
//
|
|
||||||
// Sig may be one of the signals specified in sigaction(2) or it may
|
|
||||||
// be 0, in which case error checking is performed but no signal is
|
|
||||||
// actually sent. This can be used to check the validity of pid.
|
|
||||||
err := unix.Kill(pid, 0)
|
|
||||||
|
|
||||||
// Either the PID was found (no error) or we get an EPERM, which means
|
|
||||||
// the PID exists, but we don't have permissions to signal it.
|
|
||||||
return err == nil || errors.Is(err, unix.EPERM)
|
|
||||||
default:
|
|
||||||
_, err := os.Stat(filepath.Join("/proc", strconv.Itoa(pid)))
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func kill(pid int) error {
|
|
||||||
err := unix.Kill(pid, unix.SIGKILL)
|
|
||||||
if err != nil && !errors.Is(err, unix.ESRCH) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zombie return true if process has a state with "Z". It only considers positive
|
|
||||||
// PIDs; 0 (all processes in the current process group), -1 (all processes with
|
|
||||||
// a PID larger than 1), and negative (-n, all processes in process group "n")
|
|
||||||
// values for pid are ignored. Refer to [PROC(5)] for details.
|
|
||||||
//
|
|
||||||
// Zombie is only implemented on Linux, and returns false on all other platforms.
|
|
||||||
//
|
|
||||||
// [PROC(5)]: https://man7.org/linux/man-pages/man5/proc.5.html
|
|
||||||
func Zombie(pid int) (bool, error) {
|
|
||||||
return zombie(pid)
|
|
||||||
}
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
package process
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"golang.org/x/sys/windows"
|
|
||||||
)
|
|
||||||
|
|
||||||
func alive(pid int) bool {
|
|
||||||
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid))
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
var c uint32
|
|
||||||
err = windows.GetExitCodeProcess(h, &c)
|
|
||||||
_ = windows.CloseHandle(h)
|
|
||||||
if err != nil {
|
|
||||||
// From the GetExitCodeProcess function (processthreadsapi.h) API docs:
|
|
||||||
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess
|
|
||||||
//
|
|
||||||
// The GetExitCodeProcess function returns a valid error code defined by the
|
|
||||||
// application only after the thread terminates. Therefore, an application should
|
|
||||||
// not use STILL_ACTIVE (259) as an error code (STILL_ACTIVE is a macro for
|
|
||||||
// STATUS_PENDING (minwinbase.h)). If a thread returns STILL_ACTIVE (259) as
|
|
||||||
// an error code, then applications that test for that value could interpret it
|
|
||||||
// to mean that the thread is still running, and continue to test for the
|
|
||||||
// completion of the thread after the thread has terminated, which could put
|
|
||||||
// the application into an infinite loop.
|
|
||||||
return c == uint32(windows.STATUS_PENDING)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func kill(pid int) error {
|
|
||||||
p, err := os.FindProcess(pid)
|
|
||||||
if err == nil {
|
|
||||||
err = p.Kill()
|
|
||||||
if err != nil && err != os.ErrProcessDone {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
@ -65,9 +65,6 @@ github.com/docker/distribution/registry/client/transport
|
||||||
github.com/docker/distribution/registry/storage/cache
|
github.com/docker/distribution/registry/storage/cache
|
||||||
github.com/docker/distribution/registry/storage/cache/memory
|
github.com/docker/distribution/registry/storage/cache/memory
|
||||||
github.com/docker/distribution/uuid
|
github.com/docker/distribution/uuid
|
||||||
# github.com/docker/docker v28.2.3-0.20250731152656-4faedf2bec36+incompatible
|
|
||||||
## explicit
|
|
||||||
github.com/docker/docker/pkg/process
|
|
||||||
# github.com/docker/docker-credential-helpers v0.9.3
|
# github.com/docker/docker-credential-helpers v0.9.3
|
||||||
## explicit; go 1.21
|
## explicit; go 1.21
|
||||||
github.com/docker/docker-credential-helpers/client
|
github.com/docker/docker-credential-helpers/client
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue