forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifacestate.go
541 lines (464 loc) · 18.1 KB
/
ifacestate.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Package ifacestate implements the manager and state aspects
// responsible for the maintenance of interfaces the system.
package ifacestate
import (
"fmt"
"sync"
"time"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/policy"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
)
var connectRetryTimeout = time.Second * 5
// ErrAlreadyConnected describes the error that occurs when attempting to connect already connected interface.
type ErrAlreadyConnected struct {
Connection interfaces.ConnRef
}
const (
ConnectTaskEdge = state.TaskSetEdge("connect-task")
AfterConnectHooksEdge = state.TaskSetEdge("after-connect-hooks")
)
func (e ErrAlreadyConnected) Error() string {
return fmt.Sprintf("already connected: %q", e.Connection.ID())
}
// findSymmetricAutoconnectTask checks if there is another auto-connect task affecting same snap because of plug/slot.
func findSymmetricAutoconnectTask(st *state.State, plugSnap, slotSnap string, installTask *state.Task) (bool, error) {
snapsup, err := snapstate.TaskSnapSetup(installTask)
if err != nil {
return false, fmt.Errorf("internal error: cannot obtain snap setup from task: %s", installTask.Summary())
}
installedSnap := snapsup.InstanceName()
// if we find any auto-connect task that's not ready and is affecting our snap, return true to indicate that
// it should be ignored (we shouldn't create connect tasks for it)
for _, task := range st.Tasks() {
if !task.Status().Ready() && task.ID() != installTask.ID() && task.Kind() == "auto-connect" {
snapsup, err := snapstate.TaskSnapSetup(task)
if err != nil {
return false, fmt.Errorf("internal error: cannot obtain snap setup from task: %s", task.Summary())
}
otherSnap := snapsup.InstanceName()
if (otherSnap == plugSnap && installedSnap == slotSnap) || (otherSnap == slotSnap && installedSnap == plugSnap) {
return true, nil
}
}
}
return false, nil
}
type connectOpts struct {
ByGadget bool
AutoConnect bool
DelayedSetupProfiles bool
}
// Connect returns a set of tasks for connecting an interface.
//
func Connect(st *state.State, plugSnap, plugName, slotSnap, slotName string) (*state.TaskSet, error) {
if err := snapstate.CheckChangeConflictMany(st, []string{plugSnap, slotSnap}, ""); err != nil {
return nil, err
}
return connect(st, plugSnap, plugName, slotSnap, slotName, connectOpts{})
}
func connect(st *state.State, plugSnap, plugName, slotSnap, slotName string, flags connectOpts) (*state.TaskSet, error) {
// TODO: Store the intent-to-connect in the state so that we automatically
// try to reconnect on reboot (reconnection can fail or can connect with
// different parameters so we cannot store the actual connection details).
// Create a series of tasks:
// - prepare-plug-<plug> hook
// - prepare-slot-<slot> hook
// - connect task
// - connect-slot-<slot> hook
// - connect-plug-<plug> hook
// The tasks run in sequence (are serialized by WaitFor). The hooks are optional
// and their tasks are created when hook exists or is declared in the snap.
// The prepare- hooks collect attributes via snapctl set.
// 'snapctl set' can only modify own attributes (plug's attributes in the *-plug-* hook and
// slot's attributes in the *-slot-* hook).
// 'snapctl get' can read both slot's and plug's attributes.
// check if the connection already exists
conns, err := getConns(st)
if err != nil {
return nil, err
}
connRef := interfaces.ConnRef{PlugRef: interfaces.PlugRef{Snap: plugSnap, Name: plugName}, SlotRef: interfaces.SlotRef{Snap: slotSnap, Name: slotName}}
if conn, ok := conns[connRef.ID()]; ok && conn.Undesired == false && conn.HotplugGone == false {
return nil, &ErrAlreadyConnected{Connection: connRef}
}
var plugSnapst, slotSnapst snapstate.SnapState
if err = snapstate.Get(st, plugSnap, &plugSnapst); err != nil {
return nil, err
}
if err = snapstate.Get(st, slotSnap, &slotSnapst); err != nil {
return nil, err
}
plugSnapInfo, err := plugSnapst.CurrentInfo()
if err != nil {
return nil, err
}
slotSnapInfo, err := slotSnapst.CurrentInfo()
if err != nil {
return nil, err
}
plugStatic, slotStatic, err := initialConnectAttributes(st, plugSnapInfo, plugSnap, plugName, slotSnapInfo, slotSnap, slotName)
if err != nil {
return nil, err
}
connectInterface := st.NewTask("connect", fmt.Sprintf(i18n.G("Connect %s:%s to %s:%s"), plugSnap, plugName, slotSnap, slotName))
initialContext := make(map[string]interface{})
initialContext["attrs-task"] = connectInterface.ID()
tasks := state.NewTaskSet()
var prev *state.Task
addTask := func(t *state.Task) {
if prev != nil {
t.WaitFor(prev)
}
tasks.AddTask(t)
}
preparePlugHookName := fmt.Sprintf("prepare-plug-%s", plugName)
if plugSnapInfo.Hooks[preparePlugHookName] != nil {
plugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: preparePlugHookName,
Optional: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), plugHookSetup.Hook, plugHookSetup.Snap)
undoPrepPlugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: "unprepare-plug-" + plugName,
Optional: true,
IgnoreError: true,
}
preparePlugConnection := hookstate.HookTaskWithUndo(st, summary, plugHookSetup, undoPrepPlugHookSetup, initialContext)
addTask(preparePlugConnection)
prev = preparePlugConnection
}
prepareSlotHookName := fmt.Sprintf("prepare-slot-%s", slotName)
if slotSnapInfo.Hooks[prepareSlotHookName] != nil {
slotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: prepareSlotHookName,
Optional: true,
}
undoPrepSlotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: "unprepare-slot-" + slotName,
Optional: true,
IgnoreError: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), slotHookSetup.Hook, slotHookSetup.Snap)
prepareSlotConnection := hookstate.HookTaskWithUndo(st, summary, slotHookSetup, undoPrepSlotHookSetup, initialContext)
addTask(prepareSlotConnection)
prev = prepareSlotConnection
}
connectInterface.Set("slot", interfaces.SlotRef{Snap: slotSnap, Name: slotName})
connectInterface.Set("plug", interfaces.PlugRef{Snap: plugSnap, Name: plugName})
if flags.AutoConnect {
connectInterface.Set("auto", true)
}
if flags.ByGadget {
connectInterface.Set("by-gadget", true)
}
if flags.DelayedSetupProfiles {
connectInterface.Set("delayed-setup-profiles", true)
}
// Expose a copy of all plug and slot attributes coming from yaml to interface hooks. The hooks will be able
// to modify them but all attributes will be checked against assertions after the hooks are run.
emptyDynamicAttrs := map[string]interface{}{}
connectInterface.Set("plug-static", plugStatic)
connectInterface.Set("slot-static", slotStatic)
connectInterface.Set("plug-dynamic", emptyDynamicAttrs)
connectInterface.Set("slot-dynamic", emptyDynamicAttrs)
// The main 'connect' task should wait on prepare-slot- hook or on prepare-plug- hook (whichever is present),
// but not on both. While there would be no harm in waiting for both, it's not needed as prepare-slot- will
// wait for prepare-plug- anyway, and a simple one-to-one wait dependency makes testing easier.
addTask(connectInterface)
prev = connectInterface
if flags.DelayedSetupProfiles {
// mark as the last task in connect prepare
tasks.MarkEdge(connectInterface, ConnectTaskEdge)
}
connectSlotHookName := fmt.Sprintf("connect-slot-%s", slotName)
if slotSnapInfo.Hooks[connectSlotHookName] != nil {
connectSlotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: connectSlotHookName,
Optional: true,
}
undoConnectSlotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: "disconnect-slot-" + slotName,
Optional: true,
IgnoreError: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), connectSlotHookSetup.Hook, connectSlotHookSetup.Snap)
connectSlotConnection := hookstate.HookTaskWithUndo(st, summary, connectSlotHookSetup, undoConnectSlotHookSetup, initialContext)
addTask(connectSlotConnection)
prev = connectSlotConnection
if flags.DelayedSetupProfiles {
tasks.MarkEdge(connectSlotConnection, AfterConnectHooksEdge)
}
}
connectPlugHookName := fmt.Sprintf("connect-plug-%s", plugName)
if plugSnapInfo.Hooks[connectPlugHookName] != nil {
connectPlugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: connectPlugHookName,
Optional: true,
}
undoConnectPlugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: "disconnect-plug-" + plugName,
Optional: true,
IgnoreError: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), connectPlugHookSetup.Hook, connectPlugHookSetup.Snap)
connectPlugConnection := hookstate.HookTaskWithUndo(st, summary, connectPlugHookSetup, undoConnectPlugHookSetup, initialContext)
addTask(connectPlugConnection)
if flags.DelayedSetupProfiles {
// only mark AfterConnectHooksEdge if not already set on connect-slot- hook task
if edge, _ := tasks.Edge(AfterConnectHooksEdge); edge == nil {
tasks.MarkEdge(connectPlugConnection, AfterConnectHooksEdge)
}
}
prev = connectPlugConnection
}
return tasks, nil
}
func initialConnectAttributes(st *state.State, plugSnapInfo *snap.Info, plugSnap string, plugName string, slotSnapInfo *snap.Info, slotSnap string, slotName string) (plugStatic, slotStatic map[string]interface{}, err error) {
var plugSnapst snapstate.SnapState
if err = snapstate.Get(st, plugSnap, &plugSnapst); err != nil {
return nil, nil, err
}
plug, ok := plugSnapInfo.Plugs[plugName]
if !ok {
return nil, nil, fmt.Errorf("snap %q has no plug named %q", plugSnap, plugName)
}
var slotSnapst snapstate.SnapState
if err = snapstate.Get(st, slotSnap, &slotSnapst); err != nil {
return nil, nil, err
}
if err := addImplicitSlots(st, slotSnapInfo); err != nil {
return nil, nil, err
}
slot, ok := slotSnapInfo.Slots[slotName]
if !ok {
return nil, nil, fmt.Errorf("snap %q has no slot named %q", slotSnap, slotName)
}
return plug.Attrs, slot.Attrs, nil
}
// Disconnect returns a set of tasks for disconnecting an interface.
func Disconnect(st *state.State, conn *interfaces.Connection) (*state.TaskSet, error) {
plugSnap := conn.Plug.Snap().InstanceName()
slotSnap := conn.Slot.Snap().InstanceName()
if err := snapstate.CheckChangeConflictMany(st, []string{plugSnap, slotSnap}, ""); err != nil {
return nil, err
}
return disconnectTasks(st, conn, disconnectOpts{})
}
// Forget returs a set of tasks for disconnecting and forgetting an interface.
// If the interface is already disconnected, it will be removed from the state
// (forgotten).
func Forget(st *state.State, repo *interfaces.Repository, connRef *interfaces.ConnRef) (*state.TaskSet, error) {
if err := snapstate.CheckChangeConflictMany(st, []string{connRef.PlugRef.Snap, connRef.SlotRef.Snap}, ""); err != nil {
return nil, err
}
if conn, err := repo.Connection(connRef); err == nil {
// connection exists - run regular set of disconnect tasks with forget
// flag.
opts := disconnectOpts{Forget: true}
ts, err := disconnectTasks(st, conn, opts)
return ts, err
}
// connection is not active (and possibly either the plug or slot
// doesn't exist); disconnect tasks don't need hooks as we simply
// want to remove connection from state.
ts := forgetTasks(st, connRef)
return ts, nil
}
type disconnectOpts struct {
AutoDisconnect bool
ByHotplug bool
Forget bool
}
// forgetTasks creates a set of tasks for forgetting an inactive connection
func forgetTasks(st *state.State, connRef *interfaces.ConnRef) *state.TaskSet {
summary := fmt.Sprintf(i18n.G("Forget connection %s:%s from %s:%s"),
connRef.PlugRef.Snap, connRef.PlugRef.Name,
connRef.SlotRef.Snap, connRef.SlotRef.Name)
disconnectTask := st.NewTask("disconnect", summary)
disconnectTask.Set("slot", connRef.SlotRef)
disconnectTask.Set("plug", connRef.PlugRef)
disconnectTask.Set("forget", true)
return state.NewTaskSet(disconnectTask)
}
// disconnectTasks creates a set of tasks for disconnect, including hooks, but does not do any conflict checking.
func disconnectTasks(st *state.State, conn *interfaces.Connection, flags disconnectOpts) (*state.TaskSet, error) {
plugSnap := conn.Plug.Snap().InstanceName()
slotSnap := conn.Slot.Snap().InstanceName()
plugName := conn.Plug.Name()
slotName := conn.Slot.Name()
var plugSnapst, slotSnapst snapstate.SnapState
if err := snapstate.Get(st, slotSnap, &slotSnapst); err != nil {
return nil, err
}
if err := snapstate.Get(st, plugSnap, &plugSnapst); err != nil {
return nil, err
}
summary := fmt.Sprintf(i18n.G("Disconnect %s:%s from %s:%s"),
plugSnap, plugName, slotSnap, slotName)
disconnectTask := st.NewTask("disconnect", summary)
disconnectTask.Set("slot", interfaces.SlotRef{Snap: slotSnap, Name: slotName})
disconnectTask.Set("plug", interfaces.PlugRef{Snap: plugSnap, Name: plugName})
if flags.Forget {
disconnectTask.Set("forget", true)
}
disconnectTask.Set("slot-static", conn.Slot.StaticAttrs())
disconnectTask.Set("slot-dynamic", conn.Slot.DynamicAttrs())
disconnectTask.Set("plug-static", conn.Plug.StaticAttrs())
disconnectTask.Set("plug-dynamic", conn.Plug.DynamicAttrs())
if flags.AutoDisconnect {
disconnectTask.Set("auto-disconnect", true)
}
if flags.ByHotplug {
disconnectTask.Set("by-hotplug", true)
}
ts := state.NewTaskSet()
var prev *state.Task
addTask := func(t *state.Task) {
if prev != nil {
t.WaitFor(prev)
}
ts.AddTask(t)
prev = t
}
initialContext := make(map[string]interface{})
initialContext["attrs-task"] = disconnectTask.ID()
plugSnapInfo, err := plugSnapst.CurrentInfo()
if err != nil {
return nil, err
}
slotSnapInfo, err := slotSnapst.CurrentInfo()
if err != nil {
return nil, err
}
// only run slot hooks if slotSnap is active
if slotSnapst.Active {
hookName := fmt.Sprintf("disconnect-slot-%s", slotName)
if slotSnapInfo.Hooks[hookName] != nil {
disconnectSlotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: hookName,
Optional: true,
}
undoDisconnectSlotHookSetup := &hookstate.HookSetup{
Snap: slotSnap,
Hook: "connect-slot-" + slotName,
Optional: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), disconnectSlotHookSetup.Hook, disconnectSlotHookSetup.Snap)
disconnectSlot := hookstate.HookTaskWithUndo(st, summary, disconnectSlotHookSetup, undoDisconnectSlotHookSetup, initialContext)
addTask(disconnectSlot)
}
}
// only run plug hooks if plugSnap is active
if plugSnapst.Active {
hookName := fmt.Sprintf("disconnect-plug-%s", plugName)
if plugSnapInfo.Hooks[hookName] != nil {
disconnectPlugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: hookName,
Optional: true,
}
undoDisconnectPlugHookSetup := &hookstate.HookSetup{
Snap: plugSnap,
Hook: "connect-plug-" + plugName,
Optional: true,
}
summary := fmt.Sprintf(i18n.G("Run hook %s of snap %q"), disconnectPlugHookSetup.Hook, disconnectPlugHookSetup.Snap)
disconnectPlug := hookstate.HookTaskWithUndo(st, summary, disconnectPlugHookSetup, undoDisconnectPlugHookSetup, initialContext)
addTask(disconnectPlug)
}
}
addTask(disconnectTask)
return ts, nil
}
// CheckInterfaces checks whether plugs and slots of snap are allowed for installation.
func CheckInterfaces(st *state.State, snapInfo *snap.Info, deviceCtx snapstate.DeviceContext) error {
// XXX: addImplicitSlots is really a brittle interface
if err := addImplicitSlots(st, snapInfo); err != nil {
return err
}
modelAs := deviceCtx.Model()
var storeAs *asserts.Store
if modelAs.Store() != "" {
var err error
storeAs, err = assertstate.Store(st, modelAs.Store())
if err != nil && !asserts.IsNotFound(err) {
return err
}
}
baseDecl, err := assertstate.BaseDeclaration(st)
if err != nil {
return fmt.Errorf("internal error: cannot find base declaration: %v", err)
}
if snapInfo.SnapID == "" {
// no SnapID means --dangerous was given, perform a minimal check about the compatibility of the snap type and the interface
ic := policy.InstallCandidateMinimalCheck{
Snap: snapInfo,
BaseDeclaration: baseDecl,
Model: modelAs,
Store: storeAs,
}
return ic.Check()
}
snapDecl, err := assertstate.SnapDeclaration(st, snapInfo.SnapID)
if err != nil {
return fmt.Errorf("cannot find snap declaration for %q: %v", snapInfo.InstanceName(), err)
}
ic := policy.InstallCandidate{
Snap: snapInfo,
SnapDeclaration: snapDecl,
BaseDeclaration: baseDecl,
Model: modelAs,
Store: storeAs,
}
return ic.Check()
}
var once sync.Once
func delayedCrossMgrInit() {
once.Do(func() {
// hook interface checks into snapstate installation logic
snapstate.AddCheckSnapCallback(func(st *state.State, snapInfo, _ *snap.Info, _ snap.Container, _ snapstate.Flags, deviceCtx snapstate.DeviceContext) error {
return CheckInterfaces(st, snapInfo, deviceCtx)
})
// hook into conflict checks mechanisms
snapstate.AddAffectedSnapsByKind("connect", connectDisconnectAffectedSnaps)
snapstate.AddAffectedSnapsByKind("disconnect", connectDisconnectAffectedSnaps)
})
}
func MockConnectRetryTimeout(d time.Duration) (restore func()) {
old := connectRetryTimeout
connectRetryTimeout = d
return func() { connectRetryTimeout = old }
}