Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxd/device/proxy: Remove unix socket from host upon proxy device removal #15081

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions lxd/device/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,21 +651,36 @@ func (d *proxy) setupProxyProcInfo() (*proxyProcInfo, error) {

func (d *proxy) killProxyProc(pidPath string) error {
// If the pid file doesn't exist, there is no process to kill.
if !shared.PathExists(pidPath) {
return nil
if shared.PathExists(pidPath) {
p, err := subprocess.ImportProcess(pidPath)
if err != nil {
return fmt.Errorf("Could not read pid file: %s", err)
}

err = p.Stop()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This p.Stop() is where the forkproxy is being sent the KILL signal. Based on our discussions, since the socket was opened with O_CLOEXEC, it shouldn't not be killed but instead TERM'ed to get a chance to do the cleanup?

@tomponline using KILL here seems to mimic what's done in the other fork* helpers but maybe this one warrants a different approach? Or it doesn't and it's OK to do the cleanup "manually" rather than relying on O_CLOEXEC.

if err != nil && err != subprocess.ErrNotRunning {
return fmt.Errorf("Unable to kill forkproxy: %s", err)
}

err = os.Remove(pidPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("Failed to remove PID file: %w", err)
}
}

p, err := subprocess.ImportProcess(pidPath)
listenAddr, err := network.ProxyParseAddr(d.config["listen"])
if err != nil {
return fmt.Errorf("Could not read pid file: %s", err)
return err
}

err = p.Stop()
if err != nil && err != subprocess.ErrNotRunning {
return fmt.Errorf("Unable to kill forkproxy: %s", err)
// Remove socket file if needed.
if listenAddr.ConnType == "unix" && !listenAddr.Abstract {
err = os.Remove(listenAddr.Address)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("Failed to remove socket file: %w", err)
}
}

_ = os.Remove(pidPath)
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion test/suites/container_devices_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,12 @@ container_devices_proxy_unix() {
false
fi

rm -f "${HOST_SOCK}"
# Ensure host socket removed upon device removal.
lxc config device remove proxyTester proxyDev
if [ -e "${HOST_SOCK}" ]; then
echo "Host socket was not removed upon device removal"
false
fi

# Cleanup
lxc delete -f proxyTester
Expand Down
Loading