github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/stage0/enter.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //+build linux
    16  
    17  package stage0
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"syscall"
    25  
    26  	"github.com/appc/spec/schema/types"
    27  	"github.com/hashicorp/errwrap"
    28  )
    29  
    30  // Enter enters the pod/app by exec()ing the stage1's /enter similar to /init
    31  // /enter can expect to have its CWD set to the app root.
    32  // appName and command are supplied to /enter on argv followed by any arguments.
    33  // stage1Path is the path of the stage1 rootfs
    34  func Enter(cdir string, podPID int, appName types.ACName, stage1Path string, cmdline []string) error {
    35  	if err := os.Chdir(cdir); err != nil {
    36  		return errwrap.Wrap(errors.New("error changing to dir"), err)
    37  	}
    38  
    39  	ep, err := getStage1Entrypoint(cdir, enterEntrypoint)
    40  	if err != nil {
    41  		return errwrap.Wrap(errors.New("error determining 'enter' entrypoint"), err)
    42  	}
    43  
    44  	argv := []string{filepath.Join(stage1Path, ep)}
    45  	argv = append(argv, fmt.Sprintf("--pid=%d", podPID))
    46  	argv = append(argv, fmt.Sprintf("--appname=%s", appName.String()))
    47  	argv = append(argv, "--")
    48  	argv = append(argv, cmdline...)
    49  	if err := syscall.Exec(argv[0], argv, os.Environ()); err != nil {
    50  		return errwrap.Wrap(errors.New("error execing enter"), err)
    51  	}
    52  
    53  	// never reached
    54  	return nil
    55  }