9 // Ulimit is a human friendly version of Rlimit.
16 // Rlimit specifies the resource limits, such as max open files.
18 Type int `json:"type,omitempty"`
19 Hard uint64 `json:"hard,omitempty"`
20 Soft uint64 `json:"soft,omitempty"`
24 // magic numbers for making the syscall
25 // some of these are defined in the syscall package, but not all.
26 // Also since Windows client doesn't get access to the syscall package, need to
46 var ulimitNameMapping = map[string]int{
47 //"as": rlimitAs, // Disabled since this doesn't seem usable with the way Docker inits a container.
53 "memlock": rlimitMemlock,
54 "msgqueue": rlimitMsgqueue,
56 "nofile": rlimitNofile,
59 "rtprio": rlimitRtprio,
60 "rttime": rlimitRttime,
61 "sigpending": rlimitSigpending,
65 // ParseUlimit parses and returns a Ulimit from the specified string.
66 func ParseUlimit(val string) (*Ulimit, error) {
67 parts := strings.SplitN(val, "=", 2)
69 return nil, fmt.Errorf("invalid ulimit argument: %s", val)
72 if _, exists := ulimitNameMapping[parts[0]]; !exists {
73 return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
78 hard = &soft // default to soft in case no hard was set
82 switch limitVals := strings.Split(parts[1], ":"); len(limitVals) {
84 temp, err = strconv.ParseInt(limitVals[1], 10, 64)
91 soft, err = strconv.ParseInt(limitVals[0], 10, 64)
96 return nil, fmt.Errorf("too many limit value arguments - %s, can only have up to two, `soft[:hard]`", parts[1])
100 return nil, fmt.Errorf("ulimit soft limit must be less than or equal to hard limit: %d > %d", soft, *hard)
103 return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
106 // GetRlimit returns the RLimit corresponding to Ulimit.
107 func (u *Ulimit) GetRlimit() (*Rlimit, error) {
108 t, exists := ulimitNameMapping[u.Name]
110 return nil, fmt.Errorf("invalid ulimit name %s", u.Name)
113 return &Rlimit{Type: t, Soft: uint64(u.Soft), Hard: uint64(u.Hard)}, nil
116 func (u *Ulimit) String() string {
117 return fmt.Sprintf("%s=%d:%d", u.Name, u.Soft, u.Hard)