1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// hacksawd is a privileged daemon that manages the mounts
16package main
17
18import (
19	"net"
20	"net/http"
21	"net/rpc"
22	"os"
23	"strconv"
24
25	"android.googlesource.com/platform/tools/treble.git/hacksaw/bind"
26)
27
28func main() {
29	if os.Getenv("LISTEN_PID") != strconv.Itoa(os.Getpid()) {
30		panic("Unexpected PID")
31	}
32
33	if os.Getenv("LISTEN_FDS") != strconv.Itoa(1) {
34		panic("Unexpected number of socket fds")
35	}
36
37	const socketFD = 3
38	socketFile := os.NewFile(socketFD, "hacksawd.sock")
39
40	listener, err := net.FileListener(socketFile)
41	if err != nil {
42		panic(err)
43	}
44
45	binder := bind.NewLocalPathBinder()
46	server := bind.NewServer(binder)
47	if err = rpc.Register(server); err != nil {
48		panic(err)
49	}
50	rpc.HandleHTTP()
51	http.Serve(listener, nil)
52}
53