aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/system/service_abi.go
blob: d59a45564e3947972a48a6b45a8eab63aa8827cd (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
// +build linux,!remote

package system

import (
	"context"
	"net"
	"os"
	"path/filepath"
	"strings"

	api "github.com/containers/podman/v3/pkg/api/server"
	"github.com/containers/podman/v3/pkg/domain/entities"
	"github.com/containers/podman/v3/pkg/domain/infra"
	"github.com/containers/podman/v3/pkg/util"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/spf13/pflag"
	"golang.org/x/sys/unix"
)

func restService(opts entities.ServiceOptions, flags *pflag.FlagSet, cfg *entities.PodmanConfig) error {
	var (
		listener *net.Listener
		err      error
	)

	if opts.URI != "" {
		fields := strings.Split(opts.URI, ":")
		if len(fields) == 1 {
			return errors.Errorf("%s is an invalid socket destination", opts.URI)
		}
		path := opts.URI
		if fields[0] == "unix" {
			if path, err = filepath.Abs(fields[1]); err != nil {
				return err
			}
		}
		util.SetSocketPath(path)
		if os.Getenv("LISTEN_FDS") != "" {
			// If it is activated by systemd, use the first LISTEN_FD (3)
			// instead of opening the socket file.
			f := os.NewFile(uintptr(3), "podman.sock")
			l, err := net.FileListener(f)
			if err != nil {
				return err
			}
			listener = &l
		} else {
			network := fields[0]
			address := strings.Join(fields[1:], ":")
			l, err := net.Listen(network, address)
			if err != nil {
				return errors.Wrapf(err, "unable to create socket")
			}
			listener = &l
		}
	}

	// Close stdin, so shortnames will not prompt
	devNullfile, err := os.Open(os.DevNull)
	if err != nil {
		return err
	}
	defer devNullfile.Close()
	if err := unix.Dup2(int(devNullfile.Fd()), int(os.Stdin.Fd())); err != nil {
		return err
	}
	rt, err := infra.GetRuntime(context.Background(), flags, cfg)
	if err != nil {
		return err
	}

	infra.StartWatcher(rt)
	server, err := api.NewServerWithSettings(rt, listener, api.Options{Timeout: opts.Timeout, CorsHeaders: opts.CorsHeaders})
	if err != nil {
		return err
	}
	defer func() {
		if err := server.Shutdown(); err != nil {
			logrus.Warnf("Error when stopping API service: %s", err)
		}
	}()

	err = server.Serve()
	if listener != nil {
		_ = (*listener).Close()
	}
	return err
}