/
main.go
213 lines (170 loc) · 4.53 KB
/
main.go
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
213
// Copyright (c) 2019, AT&T Intellectual Property.
// All rights reserved.
//
// Copyright (c) 2015-2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only
/* ifmgrd is a daemon that manages interface configuration.
Usage:
-cpuprofile=<filename> Defines a file which to write a cpu
profile that can be parsed with go pprof. When defined, the
daemon will begin recording cpu profile information when it
receives a SIGUSR1 signal. Then on a subsequent SIGUSR1 it
will write the profile information to the defined file.
-socketfile=<filename> When defined configd will write its pid to
the defined file (defualt: /run/ifmgrd/main.sock).
-yangdir=<dir> Directory configd will load YANG files and watch
for updates (default: /usr/share/configd/yang).
-configdsocket=<filename> Specify the location of the configd socket
with which we can proxy requests (default: /run/configd/main.sock).
SIGUSR1 Issuing SIGUSR1 to the daemon will toggle run-time
profiling. Profile data will be written to the file specified
by the cpuprofile option.
*/
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"syscall"
"github.com/coreos/go-systemd/activation"
"github.com/danos/config/schema"
"github.com/danos/config/yangconfig"
"github.com/danos/ifmgrd"
"github.com/danos/yang/compile"
)
var basepath string = "/run/ifmgrd"
var runningprof bool
var cpuproffile os.File
/* Command line options */
var cpuprofile string
var socket string
var yangdir string
var capabilities string
var configdsocket string
func sigstartprof() {
sigch := make(chan os.Signal)
signal.Notify(sigch, syscall.SIGUSR1)
for {
<-sigch
if cpuprofile != "" {
if !runningprof {
cpuproffile, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(cpuproffile)
runningprof = true
} else {
pprof.StopCPUProfile()
cpuproffile.Close()
runningprof = false
}
}
}
}
func fatal(err error) {
if err != nil {
log.Fatal(err)
}
}
func init() {
flag.StringVar(&cpuprofile, "cpuprofile",
basepath+"/profile.pprof",
"Write cpu profile to supplied file on SIGUSR1.")
flag.StringVar(&socket, "socketfile",
basepath+"/main.sock",
"Path to socket used to comminicate with daemon.")
flag.StringVar(&yangdir, "yangdir",
"/usr/share/configd/yang",
"Load YANG from specified directory.")
flag.StringVar(&capabilities, "capabilities",
compile.DefaultCapsLocation,
"File specifying system capabilities")
flag.StringVar(&configdsocket, "configdsocket",
"/run/configd/main.sock",
"Location where the configd socket resides")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
}
const tmppath = "/tmp/configd.org"
const newconfigdsocket = tmppath + "/main.sock"
func jugglemounts() error {
//mkdir -p tmppath
err := os.MkdirAll(tmppath, 0755)
if err != nil {
return err
}
//touch newsocket
f, err := os.Create(newconfigdsocket)
if err != nil {
return err
}
f.Close()
//mkdir -p basepath
err = os.MkdirAll(basepath, 0755)
if err != nil {
return err
}
//mount --bind configdsocket newsocket
err = syscall.Mount(configdsocket,
newconfigdsocket, "", syscall.MS_BIND, "")
if err != nil {
return err
}
//mount --bind basepath $(dirname configdsocket)
err = syscall.Mount(basepath,
filepath.Dir(configdsocket), "", syscall.MS_BIND, "")
if err != nil {
return err
}
return nil
}
func main() {
var err error
flag.Parse()
fatal(jugglemounts())
go sigstartprof()
ycfg := yangconfig.NewConfig().IncludeYangDirs(yangdir).
IncludeFeatures(capabilities).SystemConfig()
st, err := schema.CompileDir(
&compile.Config{
YangLocations: ycfg.YangLocator(),
Features: ycfg.FeaturesChecker(),
Filter: compile.IsConfig},
nil)
fatal(err)
ifmgrd.SchemaTree.Store(st)
listeners, err := activation.Listeners(true)
fatal(err)
if len(listeners) == 0 {
fmt.Println("No systemd listeners")
if !os.IsNotExist(os.Remove(socket)) {
fatal(err)
}
ua, err := net.ResolveUnixAddr("unix", socket)
fatal(err)
l, err := net.ListenUnix("unix", ua)
fatal(err)
err = os.Chmod(socket, 0770)
fatal(err)
listeners = append(listeners, l)
}
l := listeners[0]
config := &ifmgrd.Config{
Yangdir: yangdir,
Socket: socket,
Capabilities: capabilities,
ConfigdSocket: newconfigdsocket,
}
srv := ifmgrd.NewSrv(l.(*net.UnixListener), config)
fatal(srv.Serve())
}