2013-10-10 04:53:38 -04:00
|
|
|
package sysinit
|
2013-02-13 17:01:44 -05:00
|
|
|
|
|
|
|
import (
|
2013-08-14 20:14:30 -04:00
|
|
|
"encoding/json"
|
2013-02-13 20:24:35 -05:00
|
|
|
"flag"
|
2013-02-13 17:01:44 -05:00
|
|
|
"fmt"
|
2013-09-17 09:02:12 -04:00
|
|
|
"github.com/dotcloud/docker/netlink"
|
2013-07-01 19:48:59 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-08-13 18:40:23 -04:00
|
|
|
"io/ioutil"
|
2013-02-13 17:01:44 -05:00
|
|
|
"log"
|
2013-09-17 09:02:12 -04:00
|
|
|
"net"
|
2013-02-13 17:01:44 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2013-02-13 20:24:35 -05:00
|
|
|
"strconv"
|
2013-03-23 22:11:00 -04:00
|
|
|
"strings"
|
2013-02-13 17:01:44 -05:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2013-02-20 20:45:46 -05:00
|
|
|
// Setup networking
|
|
|
|
func setupNetworking(gw string) {
|
|
|
|
if gw == "" {
|
|
|
|
return
|
|
|
|
}
|
2013-09-17 09:02:12 -04:00
|
|
|
|
|
|
|
ip := net.ParseIP(gw)
|
|
|
|
if ip == nil {
|
|
|
|
log.Fatalf("Unable to set up networking, %s is not a valid IP", gw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := netlink.AddDefaultGw(ip); err != nil {
|
2013-02-20 20:45:46 -05:00
|
|
|
log.Fatalf("Unable to set up networking: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:38:17 -04:00
|
|
|
// Setup working directory
|
|
|
|
func setupWorkingDirectory(workdir string) {
|
|
|
|
if workdir == "" {
|
|
|
|
return
|
|
|
|
}
|
2013-10-14 03:27:38 -04:00
|
|
|
if err := syscall.Chdir(workdir); err != nil {
|
|
|
|
log.Fatalf("Unable to change dir to %v: %v", workdir, err)
|
|
|
|
}
|
2013-08-15 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
// Takes care of dropping privileges to the desired user
|
|
|
|
func changeUser(u string) {
|
|
|
|
if u == "" {
|
|
|
|
return
|
|
|
|
}
|
2013-07-01 19:48:59 -04:00
|
|
|
userent, err := utils.UserLookup(u)
|
2013-02-13 20:24:35 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to find user %v: %v", u, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(userent.Uid)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid uid: %v", userent.Uid)
|
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(userent.Gid)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid gid: %v", userent.Gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Setgid(gid); err != nil {
|
|
|
|
log.Fatalf("setgid failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := syscall.Setuid(uid); err != nil {
|
|
|
|
log.Fatalf("setuid failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 22:11:00 -04:00
|
|
|
// Clear environment pollution introduced by lxc-start
|
2013-08-13 18:40:23 -04:00
|
|
|
func cleanupEnv() {
|
2013-03-23 22:11:00 -04:00
|
|
|
os.Clearenv()
|
2013-08-14 20:14:30 -04:00
|
|
|
var lines []string
|
2013-08-13 18:40:23 -04:00
|
|
|
content, err := ioutil.ReadFile("/.dockerenv")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to load environment variables: %v", err)
|
|
|
|
}
|
2013-08-14 20:14:30 -04:00
|
|
|
err = json.Unmarshal(content, &lines)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to unmarshal environment variables: %v", err)
|
|
|
|
}
|
2013-08-13 18:40:23 -04:00
|
|
|
for _, kv := range lines {
|
2013-03-23 22:11:00 -04:00
|
|
|
parts := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(parts) == 1 {
|
|
|
|
parts = append(parts, "")
|
|
|
|
}
|
|
|
|
os.Setenv(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
func executeProgram(name string, args []string) {
|
|
|
|
path, err := exec.LookPath(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to locate %v", name)
|
|
|
|
os.Exit(127)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Exec(path, args, os.Environ()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 17:01:44 -05:00
|
|
|
// Sys Init code
|
|
|
|
// This code is run INSIDE the container and is responsible for setting
|
|
|
|
// up the environment before running the actual process
|
|
|
|
func SysInit() {
|
|
|
|
if len(os.Args) <= 1 {
|
2013-10-18 01:39:57 -04:00
|
|
|
fmt.Println("You should not invoke dockerinit manually")
|
2013-02-13 17:01:44 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-02-13 20:24:35 -05:00
|
|
|
var u = flag.String("u", "", "username or uid")
|
2013-02-20 20:45:46 -05:00
|
|
|
var gw = flag.String("g", "", "gateway address")
|
2013-08-15 14:38:17 -04:00
|
|
|
var workdir = flag.String("w", "", "workdir")
|
2013-02-13 17:01:44 -05:00
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
flag.Parse()
|
2013-02-20 20:45:46 -05:00
|
|
|
|
2013-08-13 18:40:23 -04:00
|
|
|
cleanupEnv()
|
2013-02-20 20:45:46 -05:00
|
|
|
setupNetworking(*gw)
|
2013-08-15 14:38:17 -04:00
|
|
|
setupWorkingDirectory(*workdir)
|
2013-02-13 20:24:35 -05:00
|
|
|
changeUser(*u)
|
|
|
|
executeProgram(flag.Arg(0), flag.Args())
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|