mirror of
https://github.com/containers/podman.git
synced 2025-10-19 12:12:36 +08:00
systemd socker activation: check listener to prevent panic
Commit 5fa6f686db added a regression which was fixed in eb71712626f9. Apply the same fix again to prevent a panic and return a proper error instead. To not regress again I added a e2e test which makes sure we do not panic. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
@ -46,6 +46,10 @@ func restService(flags *pflag.FlagSet, cfg *entities.PodmanConfig, opts entities
|
|||||||
return fmt.Errorf("wrong number of file descriptors for socket activation protocol (%d != 1)", len(listeners))
|
return fmt.Errorf("wrong number of file descriptors for socket activation protocol (%d != 1)", len(listeners))
|
||||||
}
|
}
|
||||||
listener = listeners[0]
|
listener = listeners[0]
|
||||||
|
// note that activation.Listeners() returns nil when it cannot listen on the fd (i.e. udp connection)
|
||||||
|
if listener == nil {
|
||||||
|
return fmt.Errorf("unexpected fd received from systemd: cannot listen on it")
|
||||||
|
}
|
||||||
libpodRuntime.SetRemoteURI(listeners[0].Addr().String())
|
libpodRuntime.SetRemoteURI(listeners[0].Addr().String())
|
||||||
} else {
|
} else {
|
||||||
uri, err := url.Parse(opts.URI)
|
uri, err := url.Parse(opts.URI)
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -21,6 +22,7 @@ var _ = Describe("Systemd activate", func() {
|
|||||||
var tempDir string
|
var tempDir string
|
||||||
var err error
|
var err error
|
||||||
var podmanTest *PodmanTestIntegration
|
var podmanTest *PodmanTestIntegration
|
||||||
|
var activate string
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
tempDir, err = testUtils.CreateTempDirInTempDir()
|
tempDir, err = testUtils.CreateTempDirInTempDir()
|
||||||
@ -31,17 +33,10 @@ var _ = Describe("Systemd activate", func() {
|
|||||||
|
|
||||||
podmanTest = PodmanTestCreate(tempDir)
|
podmanTest = PodmanTestCreate(tempDir)
|
||||||
podmanTest.Setup()
|
podmanTest.Setup()
|
||||||
})
|
|
||||||
|
|
||||||
AfterEach(func() {
|
|
||||||
podmanTest.Cleanup()
|
|
||||||
processTestResult(CurrentGinkgoTestDescription())
|
|
||||||
})
|
|
||||||
|
|
||||||
It("stop podman.service", func() {
|
|
||||||
SkipIfRemote("Testing stopped service requires both podman and podman-remote binaries")
|
SkipIfRemote("Testing stopped service requires both podman and podman-remote binaries")
|
||||||
|
|
||||||
activate, err := exec.LookPath("systemd-socket-activate")
|
activate, err = exec.LookPath("systemd-socket-activate")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
activate = "/usr/bin/systemd-socket-activate"
|
activate = "/usr/bin/systemd-socket-activate"
|
||||||
}
|
}
|
||||||
@ -54,7 +49,14 @@ var _ = Describe("Systemd activate", func() {
|
|||||||
case err != nil:
|
case err != nil:
|
||||||
Skip(err.Error())
|
Skip(err.Error())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
AfterEach(func() {
|
||||||
|
podmanTest.Cleanup()
|
||||||
|
processTestResult(CurrentGinkgoTestDescription())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("stop podman.service", func() {
|
||||||
// systemd-socket-activate does not support DNS lookups
|
// systemd-socket-activate does not support DNS lookups
|
||||||
host := "127.0.0.1"
|
host := "127.0.0.1"
|
||||||
port, err := podmanUtils.GetRandomPort()
|
port, err := podmanUtils.GetRandomPort()
|
||||||
@ -103,4 +105,37 @@ var _ = Describe("Systemd activate", func() {
|
|||||||
Expect(abiSession).To(Exit(0))
|
Expect(abiSession).To(Exit(0))
|
||||||
Expect(abiSession.OutputToString()).To(Equal("true"))
|
Expect(abiSession.OutputToString()).To(Equal("true"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("invalid systemd file descriptor", func() {
|
||||||
|
host := "127.0.0.1"
|
||||||
|
port, err := podmanUtils.GetRandomPort()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
addr := fmt.Sprintf("%s:%d", host, port)
|
||||||
|
|
||||||
|
// start systemd activation with datagram socket
|
||||||
|
activateSession := testUtils.StartSystemExec(activate, []string{
|
||||||
|
"--datagram", "--listen", addr,
|
||||||
|
podmanTest.PodmanBinary,
|
||||||
|
"--root=" + filepath.Join(tempDir, "server_root"),
|
||||||
|
"system", "service",
|
||||||
|
"--time=0",
|
||||||
|
})
|
||||||
|
Expect(activateSession.Exited).ShouldNot(Receive(), "Failed to start podman service")
|
||||||
|
|
||||||
|
// we have to wait for systemd-socket-activate to become ready
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
// now dial the socket to start podman
|
||||||
|
conn, err := net.Dial("udp", addr)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer conn.Close()
|
||||||
|
_, err = conn.Write([]byte("test"))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
// wait for podman to exit
|
||||||
|
activateSession.Wait(10)
|
||||||
|
Expect(activateSession).To(Exit(125))
|
||||||
|
Expect(activateSession.ErrorToString()).To(ContainSubstring("Error: unexpected fd received from systemd: cannot listen on it"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user