summaryrefslogtreecommitdiff
path: root/nslcd_systemd/util_test.go
blob: 89cf7cba747c8e0248b49e567614007ac94c75ee (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2017 Luke Shumaker <lukeshu@sbcglobal.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA

package nslcd_systemd_test

import (
	"fmt"
	"net"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/pkg/errors"
	"golang.org/x/sys/unix"
)

func testWithTimeout(t *testing.T, timeout time.Duration, fn func(t *testing.T, s chan<- string)) {
	finished := make(chan bool)
	status := make(chan string)
	cur_status := ""
	go func() {
		t.Run("timed", func(t *testing.T) { fn(t, status) })
		finished <- true
	}()
	for {
		select {
		case cur_status = <-status:
		case <-finished:
			close(status)
			return
		case <-time.After(timeout):
			close(status)
			if cur_status != "" {
				t.Fatal("timed out: " + cur_status)
			} else {
				t.Fatal("timed out")
			}
			return
		}
	}
}

var sdListenFds = uintptr(0)
var sdListenFdNames = []string{}

type filer interface {
	Close() error
	File() (*os.File, error)
}

func sdOpenStream(streampath string) (filer, error) {
	// I should have this change based on type of stream
	listener, err := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: streampath})
	if err != nil {
		return nil, errors.Wrap(err, "net.ListenUnix()")
	}
	listener.SetUnlinkOnClose(false)
	return listener, nil
}

func fdIsDevNull(fd uintptr) error {
	file := os.NewFile(fd, fmt.Sprintf("/dev/fd/%d", fd))
	if file == nil {
		return errors.Errorf("not a valid file descriptor: %d", fd)
	}

	statFd, err := file.Stat()
	if err != nil {
		return err
	}

	statNull, err := os.Stat("/dev/null")
	if err != nil {
		return err
	}

	if !os.SameFile(statFd, statNull) {
		return errors.Errorf("FD %d is not /dev/null", fd)
	}
	return nil
}

func sdActivatedStream(streampath string) error {
	// Set up the file descriptor
	err := func(streampath string) error {
		file, err := func(streampath string) (*os.File, error) {
			listener, err := sdOpenStream(streampath)
			if err != nil {
				return nil, err
			}
			defer listener.Close()
			file, err := listener.File()
			if err != nil {
				return nil, errors.Wrap(err, "listener.File()")
			}
			return file, nil
		}(streampath)
		if err != nil {
			return err
		}
		defer file.Close()

		fd := sdListenFds + 3
		err = fdIsDevNull(fd)
		if err != nil {
			return err
		}
		err = unix.Dup2(int(file.Fd()), int(fd))
		if err != nil {
			return errors.Wrap(err, "Dup2()")
		}
		return nil
	}(streampath)
	if err != nil {
		return err
	}

	sdListenFds++
	sdListenFdNames = append(sdListenFdNames, streampath)

	err = os.Setenv("LISTEN_PID", fmt.Sprintf("%d", os.Getpid()))
	if err != nil {
		return errors.Wrap(err, "os.Setenv()")
	}
	err = os.Setenv("LISTEN_FDS", fmt.Sprintf("%d", sdListenFds))
	if err != nil {
		return errors.Wrap(err, "os.Setenv()")
	}
	err = os.Setenv("LISTEN_FDNAMES", strings.Join(sdListenFdNames, ":"))
	if err != nil {
		return errors.Wrap(err, "os.Setenv()")
	}
	return nil
}

func sdActivatedReset() error {
	devnull, err := os.OpenFile("/dev/null", os.O_RDWR, 0666)
	if err != nil {
		return err
	}
	defer devnull.Close()
	for i := uintptr(0); i < sdListenFds; i++ {
		err = unix.Dup2(int(devnull.Fd()), int(3+i))
		if err != nil {
			return err
		}
	}
	sdListenFds = 0
	sdListenFdNames = []string{}
	return nil
}

func sdNotifyListen(sockname string) (*net.UnixConn, error) {
	err := os.Setenv("NOTIFY_SOCKET", sockname)
	if err != nil {
		return nil, err
	}
	return net.ListenUnixgram("unixgram", &net.UnixAddr{Net: "unixgram", Name: sockname})
}

func sdNotifyHandle(sock *net.UnixConn, fn func(dat []byte, oob []byte) error) error {
	var dat [4096]byte
	oob := make([]byte, unix.CmsgSpace(unix.SizeofUcred)+unix.CmsgSpace(8*768))
	for {
		n, oobn, flags, _, err := sock.ReadMsgUnix(dat[:], oob[:])
		if err != nil {
			return err
		}
		if flags&unix.MSG_TRUNC != 0 {
			// Received notify message exceeded maximum size. Ignoring."
			continue
		}
		err = fn(dat[:n], oob[:oobn])
		if err != nil {
			return err
		}
	}
}

func humanizeU64(n uint64) string {
	str := fmt.Sprintf("%d", n)
	bts := make([]byte, len(str)+(len(str)-1)/3)

	s := 0
	b := 0
	for s < len(str) && b < len(bts) {
		if (s % 3 == 0 && s > 0) {
			bts[len(bts)-1-b] = ','
			b++
		}
		bts[len(bts)-1-b] = str[len(str)-1-s]
		b++
		s++
	}

	return string(bts)
}