You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
275 lines
8.1 KiB
275 lines
8.1 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package minhttp
|
||
10 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"sync"
|
||
|
|
||
10 years ago
|
"github.com/minio/minio/pkg/probe"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// This package is a fork https://github.com/facebookgo/grace
|
||
10 years ago
|
//
|
||
|
// Re-licensing with Apache License 2.0, with code modifications
|
||
|
|
||
|
// This package provides a family of Listen functions that either open a
|
||
|
// fresh connection or provide an inherited connection from when the process
|
||
|
// was started. This behaves like their counterparts in the net pacakge, but
|
||
|
// transparently provide support for graceful restarts without dropping
|
||
|
// connections. This is provided in a systemd socket activation compatible form
|
||
|
// to allow using socket activation.
|
||
|
//
|
||
|
|
||
10 years ago
|
const (
|
||
|
// Used to indicate a graceful restart in the new process.
|
||
10 years ago
|
envCountKey = "LISTEN_FDS" // similar to systemd SDS_LISTEN_FDS
|
||
10 years ago
|
envCountKeyPrefix = envCountKey + "="
|
||
|
)
|
||
|
|
||
|
// In order to keep the working directory the same as when we started we record
|
||
|
// it at startup.
|
||
|
var originalWD, _ = os.Getwd()
|
||
|
|
||
10 years ago
|
// minNet provides the family of Listen functions and maintains the associated
|
||
|
// state. Typically you will have only once instance of minNet per application.
|
||
|
type minNet struct {
|
||
10 years ago
|
inheritedListeners []net.Listener
|
||
|
activeListeners []net.Listener
|
||
10 years ago
|
connLimit int
|
||
10 years ago
|
mutex sync.Mutex
|
||
|
inheritOnce sync.Once
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// minAddr simple wrapper over net.Addr interface to implement IsEqual()
|
||
|
type minAddr struct {
|
||
10 years ago
|
net.Addr
|
||
|
}
|
||
|
|
||
|
// fileListener simple interface to extract file pointers from different types of net.Listener's
|
||
|
type fileListener interface {
|
||
|
File() (*os.File, error)
|
||
|
}
|
||
|
|
||
|
// getInheritedListeners - look for LISTEN_FDS in environment variables and populate listeners accordingly
|
||
10 years ago
|
func (n *minNet) getInheritedListeners() *probe.Error {
|
||
|
var retErr *probe.Error
|
||
10 years ago
|
n.inheritOnce.Do(func() {
|
||
|
n.mutex.Lock()
|
||
|
defer n.mutex.Unlock()
|
||
|
countStr := os.Getenv(envCountKey)
|
||
|
if countStr == "" {
|
||
|
return
|
||
|
}
|
||
|
count, err := strconv.Atoi(countStr)
|
||
|
if err != nil {
|
||
9 years ago
|
retErr = probe.NewError(fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr))
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
fdStart := 3
|
||
|
for i := fdStart; i < fdStart+count; i++ {
|
||
|
file := os.NewFile(uintptr(i), "listener")
|
||
|
l, err := net.FileListener(file)
|
||
|
if err != nil {
|
||
|
file.Close()
|
||
9 years ago
|
retErr = probe.NewError(err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
if err := file.Close(); err != nil {
|
||
9 years ago
|
retErr = probe.NewError(err)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
n.inheritedListeners = append(n.inheritedListeners, l)
|
||
10 years ago
|
}
|
||
|
})
|
||
10 years ago
|
if retErr != nil {
|
||
9 years ago
|
return retErr.Trace()
|
||
10 years ago
|
}
|
||
|
return nil
|
||
10 years ago
|
}
|
||
|
|
||
|
// Listen announces on the local network address laddr. The network net must be
|
||
|
// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
|
||
|
// returns an inherited net.Listener for the matching network and address, or
|
||
10 years ago
|
// creates a new one using net.Listen()
|
||
10 years ago
|
func (n *minNet) Listen(nett, laddr string) (net.Listener, *probe.Error) {
|
||
10 years ago
|
switch nett {
|
||
|
default:
|
||
9 years ago
|
return nil, probe.NewError(net.UnknownNetworkError(nett))
|
||
10 years ago
|
case "tcp", "tcp4", "tcp6":
|
||
|
addr, err := net.ResolveTCPAddr(nett, laddr)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
return n.ListenTCP(nett, addr)
|
||
10 years ago
|
case "unix", "unixpacket":
|
||
10 years ago
|
addr, err := net.ResolveUnixAddr(nett, laddr)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
return n.ListenUnix(nett, addr)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ListenTCP announces on the local network address laddr. The network net must
|
||
|
// be: "tcp", "tcp4" or "tcp6". It returns an inherited net.Listener for the
|
||
|
// matching network and address, or creates a new one using net.ListenTCP.
|
||
10 years ago
|
func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, *probe.Error) {
|
||
10 years ago
|
if err := n.getInheritedListeners(); err != nil {
|
||
10 years ago
|
return nil, err.Trace()
|
||
10 years ago
|
}
|
||
|
|
||
|
n.mutex.Lock()
|
||
|
defer n.mutex.Unlock()
|
||
|
|
||
|
// look for an inherited listener
|
||
10 years ago
|
for i, l := range n.inheritedListeners {
|
||
10 years ago
|
if l == nil { // we nil used inherited listeners
|
||
|
continue
|
||
|
}
|
||
10 years ago
|
equal := minAddr{l.Addr()}.IsEqual(laddr)
|
||
10 years ago
|
if equal {
|
||
10 years ago
|
n.inheritedListeners[i] = nil
|
||
|
n.activeListeners = append(n.activeListeners, l)
|
||
10 years ago
|
return l.(*net.TCPListener), nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// make a fresh listener
|
||
10 years ago
|
l, err := net.ListenTCP(nett, laddr)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, probe.NewError(err)
|
||
10 years ago
|
}
|
||
10 years ago
|
n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit))
|
||
10 years ago
|
return l, nil
|
||
|
}
|
||
|
|
||
|
// ListenUnix announces on the local network address laddr. The network net
|
||
|
// must be a: "unix" or "unixpacket". It returns an inherited net.Listener for
|
||
|
// the matching network and address, or creates a new one using net.ListenUnix.
|
||
10 years ago
|
func (n *minNet) ListenUnix(nett string, laddr *net.UnixAddr) (net.Listener, *probe.Error) {
|
||
10 years ago
|
if err := n.getInheritedListeners(); err != nil {
|
||
10 years ago
|
return nil, err.Trace()
|
||
10 years ago
|
}
|
||
|
|
||
|
n.mutex.Lock()
|
||
|
defer n.mutex.Unlock()
|
||
|
|
||
|
// look for an inherited listener
|
||
10 years ago
|
for i, l := range n.inheritedListeners {
|
||
10 years ago
|
if l == nil { // we nil used inherited listeners
|
||
|
continue
|
||
|
}
|
||
10 years ago
|
equal := minAddr{l.Addr()}.IsEqual(laddr)
|
||
10 years ago
|
if equal {
|
||
10 years ago
|
n.inheritedListeners[i] = nil
|
||
|
n.activeListeners = append(n.activeListeners, l)
|
||
10 years ago
|
return l.(*net.UnixListener), nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// make a fresh listener
|
||
10 years ago
|
l, err := net.ListenUnix(nett, laddr)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, probe.NewError(err)
|
||
10 years ago
|
}
|
||
10 years ago
|
n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit))
|
||
10 years ago
|
return l, nil
|
||
|
}
|
||
|
|
||
|
// activeListeners returns a snapshot copy of the active listeners.
|
||
10 years ago
|
func (n *minNet) getActiveListeners() []net.Listener {
|
||
10 years ago
|
n.mutex.Lock()
|
||
|
defer n.mutex.Unlock()
|
||
10 years ago
|
ls := make([]net.Listener, len(n.activeListeners))
|
||
|
copy(ls, n.activeListeners)
|
||
10 years ago
|
return ls
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// IsEqual is synonymous with IP.IsEqual() method, here IsEqual matches net.Addr instead of net.IP
|
||
10 years ago
|
func (n1 minAddr) IsEqual(n2 net.Addr) bool {
|
||
10 years ago
|
if n1.Network() != n2.Network() {
|
||
10 years ago
|
return false
|
||
|
}
|
||
10 years ago
|
a1h, a1p, _ := net.SplitHostPort(n1.String())
|
||
|
a2h, a2p, _ := net.SplitHostPort(n2.String())
|
||
|
// Special cases since Addr() from net.Listener will
|
||
|
// add frivolous [::] ipv6 for no ":[PORT]" style addresses
|
||
|
if a1h == "::" && a2h == "" && a1p == a2p {
|
||
10 years ago
|
return true
|
||
|
}
|
||
10 years ago
|
if a2h == "::" && a1h == "" && a1p == a2p {
|
||
|
return true
|
||
|
}
|
||
|
if net.ParseIP(a1h).Equal(net.ParseIP(a2h)) && a1p == a2p {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
10 years ago
|
}
|
||
|
|
||
|
// StartProcess starts a new process passing it the active listeners. It
|
||
|
// doesn't fork, but starts a new process using the same environment and
|
||
|
// arguments as when it was originally started. This allows for a newly
|
||
|
// deployed binary to be started. It returns the pid of the newly started
|
||
|
// process when successful.
|
||
10 years ago
|
func (n *minNet) StartProcess() (int, *probe.Error) {
|
||
|
listeners := n.getActiveListeners()
|
||
10 years ago
|
// Extract the fds from the listeners.
|
||
|
files := make([]*os.File, len(listeners))
|
||
|
for i, l := range listeners {
|
||
10 years ago
|
var err error
|
||
10 years ago
|
files[i], err = l.(fileListener).File()
|
||
|
if err != nil {
|
||
9 years ago
|
return 0, probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
defer files[i].Close()
|
||
|
}
|
||
|
|
||
|
// Use the original binary location. This works with symlinks such that if
|
||
|
// the file it points to has been changed we will use the updated symlink.
|
||
|
argv0, err := exec.LookPath(os.Args[0])
|
||
|
if err != nil {
|
||
9 years ago
|
return 0, probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
|
||
|
// Pass on the environment and replace the old count key with the new one.
|
||
|
var env []string
|
||
|
for _, v := range os.Environ() {
|
||
|
if !strings.HasPrefix(v, envCountKeyPrefix) {
|
||
|
env = append(env, v)
|
||
|
}
|
||
|
}
|
||
|
env = append(env, fmt.Sprintf("%s%d", envCountKeyPrefix, len(listeners)))
|
||
|
|
||
10 years ago
|
allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...)
|
||
|
process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
|
||
|
Dir: originalWD,
|
||
|
Env: env,
|
||
|
Files: allFiles,
|
||
|
})
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return 0, probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
return process.Pid, nil
|
||
|
}
|