This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
/
main.go
144 lines (115 loc) · 3.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"syscall"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/rkt/rkt/common"
rktlog "github.com/rkt/rkt/pkg/log"
stage1commontypes "github.com/rkt/rkt/stage1/common/types"
stage1initcommon "github.com/rkt/rkt/stage1/init/common"
"github.com/rkt/rkt/stage1_fly"
)
var (
debug bool
podPid string
appName string
log *rktlog.Logger
diag *rktlog.Logger
)
func init() {
flag.BoolVar(&debug, "debug", false, "Run in debug mode")
flag.StringVar(&podPid, "pid", "", "Pod PID")
flag.StringVar(&appName, "appname", "", "Application name")
log, diag, _ = rktlog.NewLogSet("fly-enter", false)
}
func getRootDir(pid string) (string, error) {
rootLink := fmt.Sprintf("/proc/%s/root", pid)
return os.Readlink(rootLink)
}
func getRuntimeApp(pm *schema.PodManifest) (*schema.RuntimeApp, error) {
if len(pm.Apps) != 1 {
return nil, fmt.Errorf("fly only supports 1 application per Pod for now")
}
return &pm.Apps[0], nil
}
func execArgs(envv []string) error {
argv0 := flag.Arg(0)
argv := flag.Args()
return syscall.Exec(argv0, argv, envv)
}
func main() {
flag.Parse()
log.SetDebug(debug)
diag.SetDebug(debug)
if !debug {
diag.SetOutput(ioutil.Discard)
}
// lock the current goroutine to its current OS thread.
// This will force the subsequent syscalls *made by this goroutine only*
// to be executed in the same OS thread as Setresuid, and Setresgid,
// see https://github.com/golang/go/issues/1435#issuecomment-66054163.
runtime.LockOSThread()
root, err := getRootDir(podPid)
if err != nil {
log.FatalE("Failed to get pod root", err)
}
pm, err := stage1commontypes.LoadPodManifest(".")
if err != nil {
log.FatalE("Failed to load pod manifest", err)
}
ra, err := getRuntimeApp(pm)
if err != nil {
log.FatalE("Failed to get app", err)
}
// mock up a pod so we can call LookupProcessCredentials
pod := &stage1commontypes.Pod{
Root: root,
}
credentials, err := stage1_fly.LookupProcessCredentials(pod, ra, root)
if err != nil {
log.FatalE("failed to lookup process credentials", err)
}
cwd, err := os.Getwd()
if err != nil {
log.FatalE("Failed to get cwd", err)
}
env, err := common.ReadEnvFileRaw(stage1initcommon.EnvFilePath(cwd, types.ACName(appName)))
if err != nil {
log.FatalE("Failed to read app env", err)
}
if err := os.Chdir(root); err != nil {
log.FatalE("Failed to change to new root", err)
}
if err := syscall.Chroot(root); err != nil {
log.FatalE("Failed to chroot", err)
}
diag.Printf("setting credentials: %+v", credentials)
if err := stage1_fly.SetProcessCredentials(credentials); err != nil {
log.FatalE("can't set process credentials", err)
}
diag.Println("PID:", podPid)
diag.Println("APP:", appName)
diag.Println("ARGS:", flag.Args())
if err := execArgs(env); err != nil {
log.PrintE("exec failed", err)
}
os.Exit(254)
}