cmd, pkg/utils: Split out the code to get the initialization stamp path

This will prevent any silly bug in getting the initialization stamp path
from breaking the communication protocol between the 'enter' or 'run'
commands on the host and the Toolbx container's entry point process.

https://github.com/containers/toolbox/pull/1633
This commit is contained in:
Debarshi Ray 2025-04-11 17:03:36 +02:00
parent 63309e4666
commit 2956ecacb4
3 changed files with 19 additions and 7 deletions

View File

@ -342,14 +342,12 @@ func initContainer(cmd *cobra.Command, args []string) error {
logrus.Debug("Finished initializing container")
toolboxRuntimeDirectory, err := utils.GetRuntimeDirectory(targetUser)
pid := os.Getpid()
initializedStamp, err := utils.GetInitializedStamp(pid, targetUser)
if err != nil {
return err
}
pid := os.Getpid()
initializedStamp := fmt.Sprintf("%s/container-initialized-%d", toolboxRuntimeDirectory, pid)
logrus.Debugf("Creating initialization stamp %s", initializedStamp)
initializedStampFile, err := os.Create(initializedStamp)

View File

@ -592,13 +592,11 @@ func constructExecArgs(container, preserveFDs string,
}
func ensureContainerIsInitialized(container string, entryPointPID int, timestamp time.Time) error {
toolboxRuntimeDirectory, err := utils.GetRuntimeDirectory(currentUser)
initializedStamp, err := utils.GetInitializedStamp(entryPointPID, currentUser)
if err != nil {
return err
}
initializedStampBase := fmt.Sprintf("container-initialized-%d", entryPointPID)
initializedStamp := filepath.Join(toolboxRuntimeDirectory, initializedStampBase)
logrus.Debugf("Checking if initialization stamp %s exists", initializedStamp)
shouldUsePolling := isUsePollingSet()
@ -648,6 +646,11 @@ func ensureContainerIsInitialized(container string, entryPointPID int, timestamp
if watcherForStamp != nil {
defer watcherForStamp.Close()
toolboxRuntimeDirectory, err := utils.GetRuntimeDirectory(currentUser)
if err != nil {
return err
}
if err := watcherForStamp.Add(toolboxRuntimeDirectory); err != nil {
if errors.Is(err, unix.ENOMEM) || errors.Is(err, unix.ENOSPC) {
logrus.Debugf("Setting up watches for file system events: failed to add path: %s",

View File

@ -503,6 +503,17 @@ func getHostVersionID() (string, error) {
return osRelease["VERSION_ID"], nil
}
func GetInitializedStamp(entryPointPID int, targetUser *user.User) (string, error) {
toolbxRuntimeDirectory, err := GetRuntimeDirectory(targetUser)
if err != nil {
return "", err
}
initializedStampBase := fmt.Sprintf("container-initialized-%d", entryPointPID)
initializedStamp := filepath.Join(toolbxRuntimeDirectory, initializedStampBase)
return initializedStamp, nil
}
// GetMountPoint returns the mount point of a target.
func GetMountPoint(target string) (string, error) {
var stdout strings.Builder