Skip to content

Commit

Permalink
Merge pull request #1430 from stgraber/main
Browse files Browse the repository at this point in the history
Fix DHCP client keeping container up
  • Loading branch information
hallyn authored Nov 27, 2024
2 parents 5865d32 + 9064bdd commit 820c995
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cmd/incusd/main_forknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ static void forkdonetdhcp() {
_exit(1);
}
// Attach to the PID namespace.
snprintf(path, sizeof(path), "/proc/%s/ns/pid", pidstr);
if (dosetns_file(path, "pid") < 0) {
fprintf(stderr, "Failed setns to container PID namespace: %s\n", strerror(errno));
_exit(1);
}
// Run in the background.
pid = fork();
if (pid < 0) {
Expand Down Expand Up @@ -142,7 +135,14 @@ static void forkdonetdhcp() {
}
// Set the process title.
(void)setproctitle("[incus DHCP] eth0");
char *workdir = advance_arg(false);
if (workdir != NULL) {
char *title = malloc(sizeof(char)*strlen(workdir)+19);
if (title != NULL) {
sprintf(title, "[incus dhcp] %s eth0", workdir);
(void)setproctitle(title);
}
}
// Jump back to Go for the rest
}
Expand Down Expand Up @@ -391,6 +391,13 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
return nil
}

// Create PID file.
err = os.WriteFile(filepath.Join(args[0], "dhcp.pid"), []byte(fmt.Sprintf("%d", os.Getpid())), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't write PID file: %v\n", err)
return nil
}

// Handle DHCP renewal.
for {
// Wait until it's renewal time.
Expand Down
11 changes: 11 additions & 0 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3228,6 +3228,17 @@ func (d *lxc) onStop(args map[string]string) error {
// Clean up devices.
d.cleanupDevices(false, "")

// Stop DHCP client if any.
if util.PathExists(filepath.Join(d.Path(), "network", "dhcp.pid")) {
dhcpPIDStr, err := os.ReadFile(filepath.Join(d.Path(), "network", "dhcp.pid"))
if err == nil {
dhcpPID, err := strconv.Atoi(strings.TrimSpace(string(dhcpPIDStr)))
if err == nil {
_ = unix.Kill(dhcpPID, unix.SIGTERM)
}
}
}

// Remove directory ownership (to avoid issue if uidmap is re-used)
err := os.Chown(d.Path(), 0, 0)
if err != nil {
Expand Down

0 comments on commit 820c995

Please sign in to comment.