mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-28 04:59:58 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
84
vendor/github.com/coreos/go-systemd/daemon/sdnotify.go
generated
vendored
Normal file
84
vendor/github.com/coreos/go-systemd/daemon/sdnotify.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright 2014 Docker, Inc.
|
||||
// Copyright 2015-2018 CoreOS, Inc.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// Package daemon provides a Go implementation of the sd_notify protocol.
|
||||
// It can be used to inform systemd of service start-up completion, watchdog
|
||||
// events, and other status changes.
|
||||
//
|
||||
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
// SdNotifyReady tells the service manager that service startup is finished
|
||||
// or the service finished loading its configuration.
|
||||
SdNotifyReady = "READY=1"
|
||||
|
||||
// SdNotifyStopping tells the service manager that the service is beginning
|
||||
// its shutdown.
|
||||
SdNotifyStopping = "STOPPING=1"
|
||||
|
||||
// SdNotifyReloading tells the service manager that this service is
|
||||
// reloading its configuration. Note that you must call SdNotifyReady when
|
||||
// it completed reloading.
|
||||
SdNotifyReloading = "RELOADING=1"
|
||||
|
||||
// SdNotifyWatchdog tells the service manager to update the watchdog
|
||||
// timestamp for the service.
|
||||
SdNotifyWatchdog = "WATCHDOG=1"
|
||||
)
|
||||
|
||||
// SdNotify sends a message to the init daemon. It is common to ignore the error.
|
||||
// If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
|
||||
// will be unconditionally unset.
|
||||
//
|
||||
// It returns one of the following:
|
||||
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
|
||||
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
|
||||
// (true, nil) - notification supported, data has been sent
|
||||
func SdNotify(unsetEnvironment bool, state string) (bool, error) {
|
||||
socketAddr := &net.UnixAddr{
|
||||
Name: os.Getenv("NOTIFY_SOCKET"),
|
||||
Net: "unixgram",
|
||||
}
|
||||
|
||||
// NOTIFY_SOCKET not set
|
||||
if socketAddr.Name == "" {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if unsetEnvironment {
|
||||
if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
||||
// Error connecting to NOTIFY_SOCKET
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if _, err = conn.Write([]byte(state)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
79
vendor/github.com/coreos/go-systemd/daemon/sdnotify_test.go
generated
vendored
Normal file
79
vendor/github.com/coreos/go-systemd/daemon/sdnotify_test.go
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2016 CoreOS, Inc.
|
||||
//
|
||||
// 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.
|
||||
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSdNotify
|
||||
func TestSdNotify(t *testing.T) {
|
||||
|
||||
testDir, e := ioutil.TempDir("/tmp/", "test-")
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
defer os.RemoveAll(testDir)
|
||||
|
||||
notifySocket := testDir + "/notify-socket.sock"
|
||||
laddr := net.UnixAddr{
|
||||
Name: notifySocket,
|
||||
Net: "unixgram",
|
||||
}
|
||||
_, e = net.ListenUnixgram("unixgram", &laddr)
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
unsetEnv bool
|
||||
envSocket string
|
||||
|
||||
wsent bool
|
||||
werr bool
|
||||
}{
|
||||
// (true, nil) - notification supported, data has been sent
|
||||
{false, notifySocket, true, false},
|
||||
// (false, err) - notification supported, but failure happened
|
||||
{true, testDir + "/missing.sock", false, true},
|
||||
// (false, nil) - notification not supported
|
||||
{true, "", false, false},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
must(os.Unsetenv("NOTIFY_SOCKET"))
|
||||
if tt.envSocket != "" {
|
||||
must(os.Setenv("NOTIFY_SOCKET", tt.envSocket))
|
||||
}
|
||||
sent, err := SdNotify(tt.unsetEnv, fmt.Sprintf("TestSdNotify test message #%d", i))
|
||||
|
||||
if sent != tt.wsent {
|
||||
t.Errorf("#%d: expected send result %t, got %t", i, tt.wsent, sent)
|
||||
}
|
||||
if tt.werr && err == nil {
|
||||
t.Errorf("#%d: want non-nil err, got nil", i)
|
||||
} else if !tt.werr && err != nil {
|
||||
t.Errorf("#%d: want nil err, got %v", i, err)
|
||||
}
|
||||
if tt.unsetEnv && tt.envSocket != "" && os.Getenv("NOTIFY_SOCKET") != "" {
|
||||
t.Errorf("#%d: environment variable not cleaned up", i)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
73
vendor/github.com/coreos/go-systemd/daemon/watchdog.go
generated
vendored
Normal file
73
vendor/github.com/coreos/go-systemd/daemon/watchdog.go
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2016 CoreOS, Inc.
|
||||
//
|
||||
// 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.
|
||||
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SdWatchdogEnabled returns watchdog information for a service.
|
||||
// Processes should call daemon.SdNotify(false, daemon.SdNotifyWatchdog) every
|
||||
// time / 2.
|
||||
// If `unsetEnvironment` is true, the environment variables `WATCHDOG_USEC` and
|
||||
// `WATCHDOG_PID` will be unconditionally unset.
|
||||
//
|
||||
// It returns one of the following:
|
||||
// (0, nil) - watchdog isn't enabled or we aren't the watched PID.
|
||||
// (0, err) - an error happened (e.g. error converting time).
|
||||
// (time, nil) - watchdog is enabled and we can send ping.
|
||||
// time is delay before inactive service will be killed.
|
||||
func SdWatchdogEnabled(unsetEnvironment bool) (time.Duration, error) {
|
||||
wusec := os.Getenv("WATCHDOG_USEC")
|
||||
wpid := os.Getenv("WATCHDOG_PID")
|
||||
if unsetEnvironment {
|
||||
wusecErr := os.Unsetenv("WATCHDOG_USEC")
|
||||
wpidErr := os.Unsetenv("WATCHDOG_PID")
|
||||
if wusecErr != nil {
|
||||
return 0, wusecErr
|
||||
}
|
||||
if wpidErr != nil {
|
||||
return 0, wpidErr
|
||||
}
|
||||
}
|
||||
|
||||
if wusec == "" {
|
||||
return 0, nil
|
||||
}
|
||||
s, err := strconv.Atoi(wusec)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error converting WATCHDOG_USEC: %s", err)
|
||||
}
|
||||
if s <= 0 {
|
||||
return 0, fmt.Errorf("error WATCHDOG_USEC must be a positive number")
|
||||
}
|
||||
interval := time.Duration(s) * time.Microsecond
|
||||
|
||||
if wpid == "" {
|
||||
return interval, nil
|
||||
}
|
||||
p, err := strconv.Atoi(wpid)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error converting WATCHDOG_PID: %s", err)
|
||||
}
|
||||
if os.Getpid() != p {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return interval, nil
|
||||
}
|
85
vendor/github.com/coreos/go-systemd/daemon/watchdog_test.go
generated
vendored
Normal file
85
vendor/github.com/coreos/go-systemd/daemon/watchdog_test.go
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
// Copyright 2016 CoreOS, Inc.
|
||||
//
|
||||
// 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.
|
||||
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSdWatchdogEnabled(t *testing.T) {
|
||||
mypid := strconv.Itoa(os.Getpid())
|
||||
tests := []struct {
|
||||
usec string // empty => unset
|
||||
pid string // empty => unset
|
||||
unsetEnv bool // arbitrarily set across testcases
|
||||
|
||||
werr bool
|
||||
wdelay time.Duration
|
||||
}{
|
||||
// Success cases
|
||||
{"100", mypid, true, false, 100 * time.Microsecond},
|
||||
{"50", mypid, true, false, 50 * time.Microsecond},
|
||||
{"1", mypid, false, false, 1 * time.Microsecond},
|
||||
{"1", "", true, false, 1 * time.Microsecond},
|
||||
|
||||
// No-op cases
|
||||
{"", mypid, true, false, 0}, // WATCHDOG_USEC not set
|
||||
{"1", "0", false, false, 0}, // WATCHDOG_PID doesn't match
|
||||
{"", "", true, false, 0}, // Both not set
|
||||
|
||||
// Failure cases
|
||||
{"-1", mypid, true, true, 0}, // Negative USEC
|
||||
{"string", "1", false, true, 0}, // Non-integer USEC value
|
||||
{"1", "string", true, true, 0}, // Non-integer PID value
|
||||
{"stringa", "stringb", false, true, 0}, // E v e r y t h i n g
|
||||
{"-10239", "-eleventythree", true, true, 0}, // i s w r o n g
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
if tt.usec != "" {
|
||||
must(os.Setenv("WATCHDOG_USEC", tt.usec))
|
||||
} else {
|
||||
must(os.Unsetenv("WATCHDOG_USEC"))
|
||||
}
|
||||
if tt.pid != "" {
|
||||
must(os.Setenv("WATCHDOG_PID", tt.pid))
|
||||
} else {
|
||||
must(os.Unsetenv("WATCHDOG_PID"))
|
||||
}
|
||||
|
||||
delay, err := SdWatchdogEnabled(tt.unsetEnv)
|
||||
|
||||
if tt.werr && err == nil {
|
||||
t.Errorf("#%d: want non-nil err, got nil", i)
|
||||
} else if !tt.werr && err != nil {
|
||||
t.Errorf("#%d: want nil err, got %v", i, err)
|
||||
}
|
||||
if tt.wdelay != delay {
|
||||
t.Errorf("#%d: want delay=%d, got %d", i, tt.wdelay, delay)
|
||||
}
|
||||
if tt.unsetEnv && (os.Getenv("WATCHDOG_PID") != "" || os.Getenv("WATCHDOG_USEC") != "") {
|
||||
t.Errorf("#%d: environment variables not cleaned up", i)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user