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

Commit

Permalink
Merge pull request #280 from cpuguy83/add_rlimit_support
Browse files Browse the repository at this point in the history
Add support for setting rlimit for contianer
  • Loading branch information
crosbymichael committed Nov 26, 2014
2 parents 7294213 + e6cc8fc commit 7ce34f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.go
Expand Up @@ -68,6 +68,10 @@ type Config struct {
// RestrictSys will remount /proc/sys, /sys, and mask over sysrq-trigger as well as /proc/irq and
// /proc/bus
RestrictSys bool `json:"restrict_sys,omitempty"`

// Rlimits specifies the resource limits, such as max open files, to set in the container
// If Rlimits are not set, the container will inherit rlimits from the parent process
Rlimits []Rlimit `json:"rlimits,omitempty"`
}

// Routes can be specified to create entries in the route table as the container is started
Expand All @@ -90,3 +94,9 @@ type Route struct {
// The device to set this route up for, for example: eth0
InterfaceName string `json:"interface_name,omitempty"`
}

type Rlimit struct {
Type int `json:"type,omitempty"`
Hard uint64 `json:"hard,omitempty"`
Soft uint64 `json:"soft,omitempty"`
}
21 changes: 21 additions & 0 deletions integration/exec_test.go
Expand Up @@ -156,3 +156,24 @@ func TestIPCBadPath(t *testing.T) {
t.Fatal("container succeded with bad ipc path")
}
}

func TestRlimit(t *testing.T) {
if testing.Short() {
return
}

rootfs, err := newRootFs()
if err != nil {
t.Fatal(err)
}
defer remove(rootfs)

config := newTemplateConfig(rootfs)
out, _, err := runContainer(config, "", "/bin/sh", "-c", "ulimit -n")
if err != nil {
t.Fatal(err)
}
if limit := strings.TrimSpace(out.Stdout.String()); limit != "1024" {
t.Fatalf("expected rlimit to be 1024, got %s", limit)
}
}
9 changes: 9 additions & 0 deletions integration/template_test.go
@@ -1,6 +1,8 @@
package integration

import (
"syscall"

"github.com/docker/libcontainer"
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/devices"
Expand Down Expand Up @@ -60,5 +62,12 @@ func newTemplateConfig(rootfs string) *libcontainer.Config {
Gateway: "localhost",
},
},
Rlimits: []libcontainer.Rlimit{
{
Type: syscall.RLIMIT_NOFILE,
Hard: uint64(1024),
Soft: uint64(1024),
},
},
}
}
14 changes: 14 additions & 0 deletions namespaces/init.go
Expand Up @@ -89,6 +89,10 @@ func Init(container *libcontainer.Config, uncleanRootfs, consolePath string, pip
return fmt.Errorf("setup route %s", err)
}

if err := setupRlimits(container); err != nil {
return fmt.Errorf("setup rlimits %s", err)
}

label.Init()

if err := mount.InitializeMountNamespace(rootfs,
Expand Down Expand Up @@ -238,6 +242,16 @@ func setupRoute(container *libcontainer.Config) error {
return nil
}

func setupRlimits(container *libcontainer.Config) error {
for _, rlimit := range container.Rlimits {
l := &syscall.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft}
if err := syscall.Setrlimit(rlimit.Type, l); err != nil {
return fmt.Errorf("error setting rlimit type %v: %v", rlimit.Type, err)
}
}
return nil
}

// FinalizeNamespace drops the caps, sets the correct user
// and working dir, and closes any leaky file descriptors
// before execing the command inside the namespace
Expand Down

0 comments on commit 7ce34f5

Please sign in to comment.