about summary refs log tree commit diff
path: root/listener.go
blob: a4fd692a33dc1ed92bc32cc52e4356d2c0dac153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-FileCopyrightText: V <v@anomalous.eu>
// SPDX-FileCopyrightText: edef <edef@edef.eu>
// SPDX-License-Identifier: OSL-3.0

package main

import (
	"fmt"
	"net"

	"github.com/coreos/go-systemd/v22/activation"
)

type listenAddress string

func (a listenAddress) Listeners() ([]net.Listener, error) {
	ln, err := net.Listen("tcp", string(a))
	if err != nil {
		return nil, err
	}
	return []net.Listener{ln}, nil
}

func (a listenAddress) Get() interface{} {
	return string(a)
}

func (a *listenAddress) Set(v string) error {
	*a = listenAddress(v)
	return nil
}

func (a listenAddress) String() string {
	return string(a)
}

type activationSocket struct{}

func (activationSocket) Listeners() (lns []net.Listener, err error) {
	files := activation.Files(true)
	lns = make([]net.Listener, len(files))
	for i, f := range files {
		lns[i], err = net.FileListener(f)
		if err != nil {
			return nil, err
		}
		f.Close()
	}
	return
}

func (activationSocket) String() string {
	return "activation socket"
}

func (activationSocket) Set(string) error {
	return fmt.Errorf("incompatible with socket activation")
}