diff --git a/cmd/incusd/main_forknet.go b/cmd/incusd/main_forknet.go index 65311298e5f..26a23589816 100644 --- a/cmd/incusd/main_forknet.go +++ b/cmd/incusd/main_forknet.go @@ -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) { @@ -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 } @@ -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. diff --git a/internal/server/instance/drivers/driver_lxc.go b/internal/server/instance/drivers/driver_lxc.go index 50616bd304b..cac4f4f8b51 100644 --- a/internal/server/instance/drivers/driver_lxc.go +++ b/internal/server/instance/drivers/driver_lxc.go @@ -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 {