Merge pull request #205 from chanwit/dev

Cleanup: separate unix-related codes to make Swarm buildable on Windows
This commit is contained in:
Andrea Luzzardi 2014-12-30 14:24:46 +01:00
commit 644cacace9
3 changed files with 49 additions and 28 deletions

View File

@ -5,9 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"os"
"strings" "strings"
"syscall"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster" "github.com/docker/swarm/cluster"
@ -16,32 +14,6 @@ import (
const DefaultDockerPort = ":2375" const DefaultDockerPort = ":2375"
func newUnixListener(addr string, tlsConfig *tls.Config) (net.Listener, error) {
if err := syscall.Unlink(addr); err != nil && !os.IsNotExist(err) {
return nil, err
}
// there is no way to specify the unix rights to use when
// creating the socket with net.Listener, so we use umask
// to create the file without rights and then we chmod
// to the desired unix rights. This prevent unwanted
// connections between the creation and the chmod
mask := syscall.Umask(0777)
defer syscall.Umask(mask)
l, err := newListener("unix", addr, tlsConfig)
if err != nil {
return nil, err
}
// only usable by the user who started swarm
if err := os.Chmod(addr, 0600); err != nil {
return nil, err
}
return l, nil
}
func newListener(proto, addr string, tlsConfig *tls.Config) (net.Listener, error) { func newListener(proto, addr string, tlsConfig *tls.Config) (net.Listener, error) {
l, err := net.Listen(proto, addr) l, err := net.Listen(proto, addr)
if err != nil { if err != nil {

36
api/server_unix.go Normal file
View File

@ -0,0 +1,36 @@
// +build !windows
package api
import (
"crypto/tls"
"net"
"os"
"syscall"
)
func newUnixListener(addr string, tlsConfig *tls.Config) (net.Listener, error) {
if err := syscall.Unlink(addr); err != nil && !os.IsNotExist(err) {
return nil, err
}
// there is no way to specify the unix rights to use when
// creating the socket with net.Listener, so we use umask
// to create the file without rights and then we chmod
// to the desired unix rights. This prevent unwanted
// connections between the creation and the chmod
mask := syscall.Umask(0777)
defer syscall.Umask(mask)
l, err := newListener("unix", addr, tlsConfig)
if err != nil {
return nil, err
}
// only usable by the user who started swarm
if err := os.Chmod(addr, 0600); err != nil {
return nil, err
}
return l, nil
}

13
api/server_windows.go Normal file
View File

@ -0,0 +1,13 @@
// +build windows
package api
import (
"crypto/tls"
"fmt"
"net"
)
func newUnixListener(addr string, tlsConfig *tls.Config) (net.Listener, error) {
return nil, fmt.Errorf("Windows platform does not support a unix socket")
}