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

Fix DHCP client keeping container up #1430

Merged
merged 3 commits into from
Nov 27, 2024
Merged
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
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