Skip to content

Commit 0030412

Browse files
authored
Merge pull request #2106 from gwenya/inherit-init-pid-fd
incusd/instance/lxc: Refactor inheritInitPidFd
2 parents a0335a1 + 39eea30 commit 0030412

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

internal/server/instance/drivers/driver_lxc.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,26 +1803,36 @@ func (d *lxc) DeviceEventHandler(runConf *deviceConfig.RunConfig) error {
18031803

18041804
// Generate uevent inside container if requested.
18051805
if len(runConf.Uevents) > 0 {
1806-
pidFdNr, pidFd := d.inheritInitPidFd()
1807-
if pidFdNr >= 0 {
1806+
pidFd := d.inheritInitPidFd()
1807+
pidFdNr := "-1"
1808+
if pidFd != nil {
18081809
defer func() { _ = pidFd.Close() }()
1810+
pidFdNr = "3"
18091811
}
18101812

18111813
for _, eventParts := range runConf.Uevents {
1812-
ueventArray := make([]string, 6)
1813-
ueventArray[0] = "forkuevent"
1814-
ueventArray[1] = "inject"
1815-
ueventArray[2] = "--"
1816-
ueventArray[3] = fmt.Sprintf("%d", d.InitPID())
1817-
ueventArray[4] = fmt.Sprintf("%d", pidFdNr)
18181814
length := 0
18191815
for _, part := range eventParts {
18201816
length = length + len(part) + 1
18211817
}
18221818

1823-
ueventArray[5] = fmt.Sprintf("%d", length)
1824-
ueventArray = append(ueventArray, eventParts...)
1825-
_, _, err := subprocess.RunCommandSplit(context.TODO(), nil, []*os.File{pidFd}, d.state.OS.ExecPath, ueventArray...)
1819+
args := []string{
1820+
"forkuevent",
1821+
"inject",
1822+
"--",
1823+
fmt.Sprintf("%d", d.InitPID()),
1824+
pidFdNr,
1825+
fmt.Sprintf("%d", length),
1826+
}
1827+
1828+
args = append(args, eventParts...)
1829+
1830+
_, _, err := subprocess.RunCommandSplit(
1831+
context.TODO(),
1832+
nil,
1833+
[]*os.File{pidFd},
1834+
d.state.OS.ExecPath,
1835+
args...)
18261836
if err != nil {
18271837
return err
18281838
}
@@ -7221,17 +7231,17 @@ func (d *lxc) templateApplyNow(trigger instance.TemplateTrigger) error {
72217231
return nil
72227232
}
72237233

7224-
func (d *lxc) inheritInitPidFd() (int, *os.File) {
7234+
func (d *lxc) inheritInitPidFd() *os.File {
72257235
if d.state.OS.PidFds {
72267236
pidFdFile, err := d.InitPidFd()
72277237
if err != nil {
7228-
return -1, nil
7238+
return nil
72297239
}
72307240

7231-
return 3, pidFdFile
7241+
return pidFdFile
72327242
}
72337243

7234-
return -1, nil
7244+
return nil
72357245
}
72367246

72377247
// FileSFTPConn returns a connection to the forkfile handler.
@@ -7356,8 +7366,8 @@ func (d *lxc) FileSFTPConn() (net.Conn, error) {
73567366
extraFiles = append(extraFiles, rootfsFile)
73577367

73587368
// Get the pidfd.
7359-
pidFdNr, pidFd := d.inheritInitPidFd()
7360-
if pidFdNr >= 0 {
7369+
pidFd := d.inheritInitPidFd()
7370+
if pidFd != nil {
73617371
defer func() { _ = pidFd.Close() }()
73627372
args = append(args, "5")
73637373
extraFiles = append(extraFiles, pidFd)
@@ -7900,9 +7910,11 @@ func (d *lxc) networkState(hostInterfaces []net.Interface) map[string]api.Instan
79007910
}
79017911

79027912
if !couldUseNetnsGetifaddrs {
7903-
pidFdNr, pidFd := d.inheritInitPidFd()
7904-
if pidFdNr >= 0 {
7913+
pidFd := d.inheritInitPidFd()
7914+
pidFdNr := "-1"
7915+
if pidFd != nil {
79057916
defer func() { _ = pidFd.Close() }()
7917+
pidFdNr = "3"
79067918
}
79077919

79087920
// Get the network state from the container
@@ -7915,7 +7927,7 @@ func (d *lxc) networkState(hostInterfaces []net.Interface) map[string]api.Instan
79157927
"info",
79167928
"--",
79177929
fmt.Sprintf("%d", pid),
7918-
fmt.Sprintf("%d", pidFdNr))
7930+
pidFdNr)
79197931
// Process forkgetnet response
79207932
if err != nil {
79217933
d.logger.Error("Error calling 'forknet", logger.Ctx{"err": err, "pid": pid})
@@ -8267,9 +8279,11 @@ func (d *lxc) removeMount(mount string) error {
82678279
}
82688280
} else {
82698281
// Remove the mount from the container
8270-
pidFdNr, pidFd := d.inheritInitPidFd()
8271-
if pidFdNr >= 0 {
8282+
pidFd := d.inheritInitPidFd()
8283+
pidFdNr := "-1"
8284+
if pidFd != nil {
82728285
defer func() { _ = pidFd.Close() }()
8286+
pidFdNr = "3"
82738287
}
82748288

82758289
_, err := subprocess.RunCommandInheritFds(
@@ -8280,7 +8294,7 @@ func (d *lxc) removeMount(mount string) error {
82808294
"go-umount",
82818295
"--",
82828296
fmt.Sprintf("%d", pid),
8283-
fmt.Sprintf("%d", pidFdNr),
8297+
pidFdNr,
82848298
mount)
82858299
if err != nil {
82868300
return err

0 commit comments

Comments
 (0)