diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-11-07 23:29:42 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-11-07 23:29:42 -0500 |
commit | 25cc9644b2d8dae449e5a75559a202acb21b49bd (patch) | |
tree | d05c309afc31d884b8e0e55fed691ca65d54b8d9 /src/sd_daemon | |
parent | aa17f05b81357cb3c63bee30b361c682ab12205e (diff) |
use the separate packages from lukeshu.com, clean up the Makefile
Diffstat (limited to 'src/sd_daemon')
-rw-r--r-- | src/sd_daemon/doc.go | 16 | ||||
-rw-r--r-- | src/sd_daemon/listen_fds.go | 59 | ||||
-rw-r--r-- | src/sd_daemon/logger/logger.go | 54 | ||||
-rw-r--r-- | src/sd_daemon/lsb/exit-status.go | 91 | ||||
-rw-r--r-- | src/sd_daemon/notify.go | 63 |
5 files changed, 0 insertions, 283 deletions
diff --git a/src/sd_daemon/doc.go b/src/sd_daemon/doc.go deleted file mode 100644 index 665e25e..0000000 --- a/src/sd_daemon/doc.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2015 Luke Shumaker -// -// 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 sd provides APIs for systemd new-style daemons. -package sd diff --git a/src/sd_daemon/listen_fds.go b/src/sd_daemon/listen_fds.go deleted file mode 100644 index fbd2247..0000000 --- a/src/sd_daemon/listen_fds.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2015 CoreOS, Inc. -// Copyright 2015 Luke Shumaker -// -// 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 sd - -import ( - "os" - "strconv" - "syscall" -) - -//#include <systemd/sd-daemon.h> -import "C" - -// Returns a list of file descriptors passed in by the service manager -// as part of the socket-based activation logic. -// -// If unsetEnv is true, then (regarless of whether the function call -// itself succeeds or not) it will unset the environmental variables -// LISTEN_FDS and LISTEN_PID, which will cause further calls to this -// function to fail. -// -// In the case of an error, this function returns nil. -func ListenFds(unsetEnv bool) []*os.File { - if unsetEnv { - defer os.Unsetenv("LISTEN_PID") - defer os.Unsetenv("LISTEN_FDS") - } - - pid, err := strconv.Atoi(os.Getenv("LISTEN_PID")) - if err != nil || pid != os.Getpid() { - return nil - } - - nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS")) - if err != nil || nfds == 0 { - return nil - } - - files := make([]*os.File, 0, nfds) - for fd := C.SD_LISTEN_FDS_START; fd < C.SD_LISTEN_FDS_START+nfds; fd++ { - syscall.CloseOnExec(fd) - files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_"+strconv.Itoa(fd))) - } - - return files -} diff --git a/src/sd_daemon/logger/logger.go b/src/sd_daemon/logger/logger.go deleted file mode 100644 index c15475a..0000000 --- a/src/sd_daemon/logger/logger.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2015 Luke Shumaker -// -// 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 logger implements a simple stderr-based logger with systemd -// log levels. -package logger - -import ( - "fmt" - "os" -) - -//#include <systemd/sd-daemon.h> -import "C" - -func log(level string, format string, a ...interface{}) { - f := level + format + "\n" - fmt.Fprintf(os.Stderr, f, a...) -} - -// system is unusable -func Emerg( /* */ format string, a ...interface{}) { log(C.SD_EMERG /* */, format, a...) } - -// action must be taken immediately -func Alert( /* */ format string, a ...interface{}) { log(C.SD_ALERT /* */, format, a...) } - -// critical conditions -func Crit( /* */ format string, a ...interface{}) { log(C.SD_CRIT /* */, format, a...) } - -// error conditions -func Err( /* */ format string, a ...interface{}) { log(C.SD_ERR /* */, format, a...) } - -// warning conditions -func Warning( /**/ format string, a ...interface{}) { log(C.SD_WARNING /**/, format, a...) } - -// normal but significant condition -func Notice( /* */ format string, a ...interface{}) { log(C.SD_NOTICE /* */, format, a...) } - -// informational -func Info( /* */ format string, a ...interface{}) { log(C.SD_INFO /* */, format, a...) } - -// debug-level messages -func Debug( /* */ format string, a ...interface{}) { log(C.SD_DEBUG /* */, format, a...) } diff --git a/src/sd_daemon/lsb/exit-status.go b/src/sd_daemon/lsb/exit-status.go deleted file mode 100644 index cc1604d..0000000 --- a/src/sd_daemon/lsb/exit-status.go +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2015 Luke Shumaker -// -// 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 lsb provides constant exit codes specified by the Linux -// Standard Base. -package lsb - -import ( - "os" - "sd_daemon/logger" -) - -// systemd daemon(7) recommends using the exit codes defined in the -// "LSB recomendations for SysV init scripts"[1]. -// -// [1]: http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html -const ( - EXIT_SUCCESS uint8 = 0 - EXIT_FAILURE uint8 = 1 - EXIT_INVALIDARGUMENT uint8 = 2 - EXIT_NOTIMPLEMENTED uint8 = 3 - EXIT_NOPERMISSION uint8 = 4 - EXIT_NOTINSTALLED uint8 = 5 - EXIT_NOTCONFIGURED uint8 = 6 - EXIT_NOTRUNNING uint8 = 7 - // 8- 99 are reserved for future LSB use - // 100-149 are reserved for distribution use - // 150-199 are reserved for application use - // 200-254 are reserved for init system use - - // Therefore, the following are taken from systemd's - // `src/basic/exit-status.h` - EXIT_CHDIR uint8 = 200 - EXIT_NICE uint8 = 201 - EXIT_FDS uint8 = 202 - EXIT_EXEC uint8 = 203 - EXIT_MEMORY uint8 = 204 - EXIT_LIMITS uint8 = 205 - EXIT_OOM_ADJUST uint8 = 206 - EXIT_SIGNAL_MASK uint8 = 207 - EXIT_STDIN uint8 = 208 - EXIT_STDOUT uint8 = 209 - EXIT_CHROOT uint8 = 210 - EXIT_IOPRIO uint8 = 211 - EXIT_TIMERSLACK uint8 = 212 - EXIT_SECUREBITS uint8 = 213 - EXIT_SETSCHEDULER uint8 = 214 - EXIT_CPUAFFINITY uint8 = 215 - EXIT_GROUP uint8 = 216 - EXIT_USER uint8 = 217 - EXIT_CAPABILITIES uint8 = 218 - EXIT_CGROUP uint8 = 219 - EXIT_SETSID uint8 = 220 - EXIT_CONFIRM uint8 = 221 - EXIT_STDERR uint8 = 222 - _EXIT_RESERVED uint8 = 223 // used to be tcpwrap don't reuse! - EXIT_PAM uint8 = 224 - EXIT_NETWORK uint8 = 225 - EXIT_NAMESPACE uint8 = 226 - EXIT_NO_NEW_PRIVILEGES uint8 = 227 - EXIT_SECCOMP uint8 = 228 - EXIT_SELINUX_CONTEXT uint8 = 229 - EXIT_PERSONALITY uint8 = 230 - EXIT_APPARMOR_PROFILE uint8 = 231 - EXIT_ADDRESS_FAMILIES uint8 = 232 - EXIT_RUNTIME_DIRECTORY uint8 = 233 - EXIT_MAKE_STARTER uint8 = 234 - EXIT_CHOWN uint8 = 235 - EXIT_BUS_ENDPOINT uint8 = 236 - EXIT_SMACK_PROCESS_LABEL uint8 = 237 -) - -// This is a utility function to defer at the beginning of a goroutine -// in order to have the correct exit code in the case of a panic. -func Recover() { - if r := recover(); r != nil { - logger.Err("panic: %v", r) - os.Exit(int(EXIT_FAILURE)) - } -} diff --git a/src/sd_daemon/notify.go b/src/sd_daemon/notify.go deleted file mode 100644 index 8fce6da..0000000 --- a/src/sd_daemon/notify.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2013-2015 Docker, Inc. -// Copyright 2014 CoreOS, Inc. -// Copyright 2015 Luke Shumaker -// -// 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 sd - -import ( - "errors" - "net" - "os" -) - -// errNotifyNoSocket is an error returned if no socket was specified. -var errNotifyNoSocket = errors.New("No socket") - -// Notify sends a message to the service manager aobout state -// changes. It is common to ignore the error. -// -// If unsetEnv is true, then (regarless of whether the function call -// itself succeeds or not) it will unset the environmental variable -// NOTIFY_SOCKET, which will cause further calls to this function to -// fail. -// -// The state parameter should countain a newline-separated list of -// variable assignments. -// -// See the documentation for sd_notify(3) for well-known variable -// assignments. -func Notify(unsetEnv bool, state string) error { - if unsetEnv { - defer os.Unsetenv("NOTIFY_SOCKET") - } - - socketAddr := &net.UnixAddr{ - Name: os.Getenv("NOTIFY_SOCKET"), - Net: "unixgram", - } - - if socketAddr.Name == "" { - return errNotifyNoSocket - } - - conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr) - if err != nil { - return err - } - defer conn.Close() - - _, err = conn.Write([]byte(state)) - return err -} |