Skip to content

Commit 820c995

Browse files
authored
Merge pull request #1430 from stgraber/main
Fix DHCP client keeping container up
2 parents 5865d32 + 9064bdd commit 820c995

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

cmd/incusd/main_forknet.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ static void forkdonetdhcp() {
9090
_exit(1);
9191
}
9292
93-
// Attach to the PID namespace.
94-
snprintf(path, sizeof(path), "/proc/%s/ns/pid", pidstr);
95-
if (dosetns_file(path, "pid") < 0) {
96-
fprintf(stderr, "Failed setns to container PID namespace: %s\n", strerror(errno));
97-
_exit(1);
98-
}
99-
10093
// Run in the background.
10194
pid = fork();
10295
if (pid < 0) {
@@ -142,7 +135,14 @@ static void forkdonetdhcp() {
142135
}
143136
144137
// Set the process title.
145-
(void)setproctitle("[incus DHCP] eth0");
138+
char *workdir = advance_arg(false);
139+
if (workdir != NULL) {
140+
char *title = malloc(sizeof(char)*strlen(workdir)+19);
141+
if (title != NULL) {
142+
sprintf(title, "[incus dhcp] %s eth0", workdir);
143+
(void)setproctitle(title);
144+
}
145+
}
146146
147147
// Jump back to Go for the rest
148148
}
@@ -391,6 +391,13 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
391391
return nil
392392
}
393393

394+
// Create PID file.
395+
err = os.WriteFile(filepath.Join(args[0], "dhcp.pid"), []byte(fmt.Sprintf("%d", os.Getpid())), 0644)
396+
if err != nil {
397+
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't write PID file: %v\n", err)
398+
return nil
399+
}
400+
394401
// Handle DHCP renewal.
395402
for {
396403
// Wait until it's renewal time.

internal/server/instance/drivers/driver_lxc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,6 +3228,17 @@ func (d *lxc) onStop(args map[string]string) error {
32283228
// Clean up devices.
32293229
d.cleanupDevices(false, "")
32303230

3231+
// Stop DHCP client if any.
3232+
if util.PathExists(filepath.Join(d.Path(), "network", "dhcp.pid")) {
3233+
dhcpPIDStr, err := os.ReadFile(filepath.Join(d.Path(), "network", "dhcp.pid"))
3234+
if err == nil {
3235+
dhcpPID, err := strconv.Atoi(strings.TrimSpace(string(dhcpPIDStr)))
3236+
if err == nil {
3237+
_ = unix.Kill(dhcpPID, unix.SIGTERM)
3238+
}
3239+
}
3240+
}
3241+
32313242
// Remove directory ownership (to avoid issue if uidmap is re-used)
32323243
err := os.Chown(d.Path(), 0, 0)
32333244
if err != nil {

0 commit comments

Comments
 (0)