| 1 |
// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
//go:build freebsd |
| 16 |
// +build freebsd |
| 17 |
|
| 18 |
// Package limits provides routines to check and enforce certain resource |
| 19 |
// limits on the Cloud SQL client proxy process. |
| 20 |
package limits |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"syscall" |
| 25 |
|
| 26 |
"github.com/GoogleCloudPlatform/cloudsql-proxy/logging" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
// For overriding in unittests. |
| 31 |
syscallGetrlimit = syscall.Getrlimit |
| 32 |
syscallSetrlimit = syscall.Setrlimit |
| 33 |
) |
| 34 |
|
| 35 |
// Each connection handled by the proxy requires two file descriptors, one |
| 36 |
// for the local end of the connection and one for the remote. So, the proxy |
| 37 |
// process should be able to open at least 8K file descriptors if it is to |
| 38 |
// handle 4K connections to one instance. |
| 39 |
const ExpectedFDs = 8500 |
| 40 |
|
| 41 |
// SetupFDLimits ensures that the process running the Cloud SQL proxy can have |
| 42 |
// at least wantFDs number of open file descriptors. It returns an error if it |
| 43 |
// cannot ensure the same. |
| 44 |
func SetupFDLimits(wantFDs uint64) error { |
| 45 |
rlim := &syscall.Rlimit{} |
| 46 |
if err := syscallGetrlimit(syscall.RLIMIT_NOFILE, rlim); err != nil { |
| 47 |
return fmt.Errorf("failed to read rlimit for max file descriptors: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
if uint64(rlim.Cur) >= wantFDs { |
| 51 |
logging.Verbosef("current FDs rlimit set to %d, wanted limit is %d. Nothing to do here.", rlim.Cur, wantFDs) |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
// Linux man page: |
| 56 |
// The soft limit is the value that the kernel enforces for the correā |
| 57 |
// sponding resource. The hard limit acts as a ceiling for the soft limit: |
| 58 |
// an unprivileged process may set only its soft limit to a value in the |
| 59 |
// range from 0 up to the hard limit, and (irreversibly) lower its hard |
| 60 |
// limit. A privileged process (under Linux: one with the CAP_SYS_RESOURCE |
| 61 |
// capability in the initial user namespace) may make arbitrary changes to |
| 62 |
// either limit value. |
| 63 |
if uint64(rlim.Max) < wantFDs { |
| 64 |
// When the hard limit is less than what is requested, let's just give it a |
| 65 |
// shot, and if we fail, we fallback and try just setting the softlimit. |
| 66 |
rlim2 := &syscall.Rlimit{} |
| 67 |
rlim2.Max = int64(wantFDs) |
| 68 |
rlim2.Cur = int64(wantFDs) |
| 69 |
if err := syscallSetrlimit(syscall.RLIMIT_NOFILE, rlim2); err == nil { |
| 70 |
logging.Verbosef("Rlimits for file descriptors set to {%v}", rlim2) |
| 71 |
return nil |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
rlim.Cur = int64(wantFDs) |
| 76 |
if err := syscallSetrlimit(syscall.RLIMIT_NOFILE, rlim); err != nil { |
| 77 |
return fmt.Errorf("failed to set rlimit {%v} for max file descriptors: %v", rlim, err) |
| 78 |
} |
| 79 |
|
| 80 |
logging.Verbosef("Rlimits for file descriptors set to {%v}", rlim) |
| 81 |
return nil |
| 82 |
} |