File: dev_incus.go

package info (click to toggle)
incus 6.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,428 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (42 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download | duplicates (5)
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
//go:build linux && cgo

package endpoints

import (
	"net"
	"path/filepath"
)

// Create a new net.Listener bound to the unix socket of the devIncus endpoint.
func createDevIncuslListener(dir string) (net.Listener, error) {
	path := filepath.Join(dir, "guestapi", "sock")

	// If this socket exists, that means a previous daemon died and
	// didn't clean up. We assume that such a daemon is actually dead
	// if we get this far, since localCreateListener() tries to connect to
	// the actual socket to make sure that it is actually dead. So, it
	// is safe to remove it here without any checks.
	//
	// Also, it would be nice to SO_REUSEADDR here so we don't have to
	// delete the socket, but we can't:
	//   http://stackoverflow.com/questions/15716302/so-reuseaddr-and-af-unix
	//
	// Note that this will force clients to reconnect on restart.
	err := socketUnixRemoveStale(path)
	if err != nil {
		return nil, err
	}

	listener, err := socketUnixListen(path)
	if err != nil {
		return nil, err
	}

	err = socketUnixSetPermissions(path, 0o666)
	if err != nil {
		_ = listener.Close()
		return nil, err
	}

	return listener, nil
}