37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
jailer.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-05-23 08:58 . Fix broken jail
package jailer
import (
"context"
"fmt"
"os"
"os/exec"
"os/user"
"path"
"strconv"
"sync"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const BaseJailPath = "/opt/jail"
var lock = sync.Mutex{}
// CreateJail sets up the named directory for use with chroot
func CreateJail(name string) error {
lock.Lock()
defer lock.Unlock()
jailPath := path.Join(BaseJailPath, name)
// Check for the done file, if that exists the jail is ready to be used
_, err := os.Stat(path.Join(jailPath, "done"))
if err == nil {
return nil
}
// If the base dir exists without the done file rebuild the directory
_, err = os.Stat(jailPath)
if err == nil {
if err := os.RemoveAll(jailPath); err != nil {
return err
}
}
logrus.Debugf("Creating jail for %v", name)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/usr/bin/jailer.sh", name)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("error running the jail command: %v", string(out)))
}
logrus.Debugf("Output from create jail command %v", string(out))
return nil
}
// GetUserCred looks up the user and provides it in syscall.Credential
func GetUserCred() (*syscall.Credential, error) {
u, err := user.Current()
if err != nil {
uID := os.Getuid()
u, err = user.LookupId(strconv.Itoa(uID))
if err != nil {
return nil, err
}
}
i, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return nil, err
}
uid := uint32(i)
i, err = strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return nil, err
}
gid := uint32(i)
return &syscall.Credential{Uid: uid, Gid: gid}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc23

搜索帮助

0d507c66 1850385 C8b1a773 1850385