Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Windows: Initial compilation enablement
Browse files Browse the repository at this point in the history
Signed-off-by: John Howard <[email protected]>
  • Loading branch information
John Howard committed May 15, 2015
1 parent a37b2a4 commit c712fa0
Show file tree
Hide file tree
Showing 46 changed files with 281 additions and 146 deletions.
2 changes: 2 additions & 0 deletions apparmor/gen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package apparmor

import (
Expand Down
2 changes: 2 additions & 0 deletions apparmor/setup.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package apparmor

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/cgroups.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package cgroups

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/cgroups_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package cgroups

import (
Expand Down
3 changes: 3 additions & 0 deletions cgroups/cgroups_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build !linux

package cgroups
2 changes: 2 additions & 0 deletions cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/blkio_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/cpu_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/cpuset_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/devices.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/devices_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/freezer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
3 changes: 3 additions & 0 deletions cgroups/fs/fs_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build !linux

package fs
2 changes: 2 additions & 0 deletions cgroups/fs/hugetlb.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/memory.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/perf_event.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/stats_util_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/util_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

/*
Utility for testing cgroup operations.
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/fs/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package fs

import (
Expand Down
2 changes: 2 additions & 0 deletions cgroups/stats.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package cgroups

type ThrottlingData struct {
Expand Down
2 changes: 2 additions & 0 deletions cgroups/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package cgroups

import (
Expand Down
3 changes: 3 additions & 0 deletions configs/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const (
Thawed FreezerState = "THAWED"
)

// TODO Windows: This can be factored out in the future as Cgroups are not
// supported on the Windows platform.

type Cgroup struct {
Name string `json:"name"`

Expand Down
51 changes: 3 additions & 48 deletions configs/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package configs

import "fmt"

type Rlimit struct {
Type int `json:"type"`
Hard uint64 `json:"hard"`
Expand All @@ -15,6 +13,9 @@ type IDMap struct {
Size int `json:"size"`
}

// TODO Windows. Many of these fields should be factored out into those parts
// which are common across platforms, and those which are platform specific.

// Config defines configuration options for executing a process inside a contained environment.
type Config struct {
// NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs
Expand Down Expand Up @@ -104,49 +105,3 @@ type Config struct {
// sysctl -w my.property.name value in Linux.
SystemProperties map[string]string `json:"system_properties"`
}

// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
func (c Config) HostUID() (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no user mappings found.")
}
id, found := c.hostIDFromMapping(0, c.UidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no root user mapping found.")
}
return id, nil
}
// Return default root uid 0
return 0, nil
}

// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
func (c Config) HostGID() (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.GidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no gid mappings found.")
}
id, found := c.hostIDFromMapping(0, c.GidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no root user mapping found.")
}
return id, nil
}
// Return default root uid 0
return 0, nil
}

// Utility function that gets a host ID for a container ID from user namespace map
// if that ID is present in the map.
func (c Config) hostIDFromMapping(containerID int, uMap []IDMap) (int, bool) {
for _, m := range uMap {
if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
hostID := m.HostID + (containerID - m.ContainerID)
return hostID, true
}
}
return -1, false
}
49 changes: 49 additions & 0 deletions configs/config_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package configs

import "fmt"

// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
func (c Config) HostUID() (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no user mappings found.")
}
id, found := c.hostIDFromMapping(0, c.UidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no root user mapping found.")
}
return id, nil
}
// Return default root uid 0
return 0, nil
}

// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
func (c Config) HostGID() (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.GidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no gid mappings found.")
}
id, found := c.hostIDFromMapping(0, c.GidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no root user mapping found.")
}
return id, nil
}
// Return default root uid 0
return 0, nil
}

// Utility function that gets a host ID for a container ID from user namespace map
// if that ID is present in the map.
func (c Config) hostIDFromMapping(containerID int, uMap []IDMap) (int, bool) {
for _, m := range uMap {
if (containerID >= m.ContainerID) && (containerID <= (m.ContainerID + m.Size - 1)) {
hostID := m.HostID + (containerID - m.ContainerID)
return hostID, true
}
}
return -1, false
}
2 changes: 2 additions & 0 deletions configs/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
Wildcard = -1
)

// TODO Windows: This can be factored out in the future

type Device struct {
// Device type, block, char, etc.
Type rune `json:"type"`
Expand Down
2 changes: 2 additions & 0 deletions configs/device_defaults.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

package configs

var (
Expand Down
86 changes: 0 additions & 86 deletions configs/namespaces.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,5 @@
package configs

import "fmt"

type NamespaceType string

const (
NEWNET NamespaceType = "NEWNET"
NEWPID NamespaceType = "NEWPID"
NEWNS NamespaceType = "NEWNS"
NEWUTS NamespaceType = "NEWUTS"
NEWIPC NamespaceType = "NEWIPC"
NEWUSER NamespaceType = "NEWUSER"
)

func NamespaceTypes() []NamespaceType {
return []NamespaceType{
NEWNET,
NEWPID,
NEWNS,
NEWUTS,
NEWIPC,
NEWUSER,
}
}

// Namespace defines configuration for each namespace. It specifies an
// alternate path that is able to be joined via setns.
type Namespace struct {
Type NamespaceType `json:"type"`
Path string `json:"path"`
}

func (n *Namespace) GetPath(pid int) string {
if n.Path != "" {
return n.Path
}
return fmt.Sprintf("/proc/%d/ns/%s", pid, n.file())
}

func (n *Namespace) file() string {
file := ""
switch n.Type {
case NEWNET:
file = "net"
case NEWNS:
file = "mnt"
case NEWPID:
file = "pid"
case NEWIPC:
file = "ipc"
case NEWUSER:
file = "user"
case NEWUTS:
file = "uts"
}
return file
}

type Namespaces []Namespace

func (n *Namespaces) Remove(t NamespaceType) bool {
i := n.index(t)
if i == -1 {
return false
}
*n = append((*n)[:i], (*n)[i+1:]...)
return true
}

func (n *Namespaces) Add(t NamespaceType, path string) {
i := n.index(t)
if i == -1 {
*n = append(*n, Namespace{Type: t, Path: path})
return
}
(*n)[i].Path = path
}

func (n *Namespaces) index(t NamespaceType) int {
for i, ns := range *n {
if ns.Type == t {
return i
}
}
return -1
}

func (n *Namespaces) Contains(t NamespaceType) bool {
return n.index(t) != -1
}
Loading

0 comments on commit c712fa0

Please sign in to comment.