virtiofsd: Fix file descriptors leak and return correct PID

This commit will fix two problems:
- Virtiofsd process ID returned to the caller will always be 0,
   the pid var is never being assigned a value.
- Socket listen fd may leak in case of failure of starting virtiofsd process.
  This is a port of be9ca0d58b

Fixes: #1931

Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
bin 2021-05-27 14:57:29 +08:00
parent 35f297ad50
commit 773deca2f6

View File

@ -75,6 +75,8 @@ func (v *virtiofsd) getSocketFD() (*os.File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// no longer needed since fd is a dup
defer listener.Close() defer listener.Close()
listener.SetUnlinkOnClose(false) listener.SetUnlinkOnClose(false)
@ -98,6 +100,7 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer socketFD.Close()
cmd.ExtraFiles = append(cmd.ExtraFiles, socketFD) cmd.ExtraFiles = append(cmd.ExtraFiles, socketFD)
@ -128,7 +131,7 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) {
v.wait = waitVirtiofsReady v.wait = waitVirtiofsReady
} }
return pid, socketFD.Close() return cmd.Process.Pid, nil
} }
func (v *virtiofsd) Stop(ctx context.Context) error { func (v *virtiofsd) Stop(ctx context.Context) error {