-
Notifications
You must be signed in to change notification settings - Fork 562
/
core18.go
764 lines (678 loc) · 22.7 KB
/
core18.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018 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 wrappers
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"syscall"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/systemd"
)
// catches units that run /usr/bin/snap (with args), or things in /usr/lib/snapd/
var execStartRe = regexp.MustCompile(`(?m)^ExecStart=(/usr/bin/snap\s+.*|/usr/lib/snapd/.*)$`)
// snapdToolingMountUnit is the name of the mount unit that provides the snapd tooling
const SnapdToolingMountUnit = "usr-lib-snapd.mount"
func snapdSkipStart(content []byte) bool {
return bytes.Contains(content, []byte("X-Snapd-Snap: do-not-start"))
}
// snapdUnitSkipStart returns true for units that should not be started
// automatically
func snapdUnitSkipStart(unitPath string) (skip bool, err error) {
content, err := os.ReadFile(unitPath)
if err != nil {
if os.IsNotExist(err) {
// no point in starting units that do not exist
return true, nil
}
return false, err
}
return snapdSkipStart(content), nil
}
func writeSnapdToolingMountUnit(sysd systemd.Systemd, prefix string, opts *AddSnapdSnapServicesOptions) error {
// TODO: the following comment is wrong, we don't need RequiredBy=snapd here?
// Not using EnsureMountUnitFile() because we need
// "RequiredBy=snapd.service"
content := []byte(fmt.Sprintf(`[Unit]
Description=Make the snapd snap tooling available for the system
Before=snapd.service
Before=systemd-udevd.service
[Mount]
What=%s/usr/lib/snapd
Where=/usr/lib/snapd
Type=none
Options=bind
[Install]
WantedBy=snapd.service
`, prefix))
fullPath := filepath.Join(dirs.SnapServicesDir, SnapdToolingMountUnit)
err := osutil.EnsureFileState(fullPath,
&osutil.MemoryFileState{
Content: content,
Mode: 0644,
},
)
if err == osutil.ErrSameState {
return nil
}
if err != nil {
return err
}
if err := sysd.DaemonReload(); err != nil {
return err
}
units := []string{SnapdToolingMountUnit}
if err := sysd.EnableNoReload(units); err != nil {
return err
}
// meh this is killing snap services that use Requires=<this-unit> because
// it doesn't use verbatim systemctl restart, it instead does it with
// a systemctl stop and then a systemctl start, which triggers LP #1924805
if err := sysd.Restart(units); err != nil {
return err
}
return nil
}
func undoSnapdToolingMountUnit(sysd systemd.Systemd) error {
mountUnit := "usr-lib-snapd.mount"
mountUnitPath := filepath.Join(dirs.SnapServicesDir, mountUnit)
if !osutil.FileExists(mountUnitPath) {
return nil
}
units := []string{mountUnit}
if err := sysd.DisableNoReload(units); err != nil {
return err
}
// XXX: it is ok to stop the mount unit, the failover handler
// executes snapd directly from the previous revision of snapd snap or
// the core snap, the handler is running directly from the mounted snapd snap
if err := sysd.Stop(units); err != nil {
return err
}
return os.Remove(mountUnitPath)
}
type AddSnapdSnapServicesOptions struct {
// Preseeding is whether the system is currently being preseeded, in which
// case there is not a running systemd for EnsureSnapServicesOptions to
// issue commands like systemctl daemon-reload to.
Preseeding bool
}
// SnapdRestart keeps state of services that need a restart after
// current symlink of snapd snap (/snap/snapd/current) has been
// updated. Call Restart method then.
// Currently, the list of services is static and the absence of a
// SnapdRestart object (nil), means no service requires a restart.
type SnapdRestart interface {
Restart() error
}
type snapdRestartImpl struct {
Sysd systemd.Systemd
}
// Restart restarts systemd service units. Call this method after
// symlink /snap/snapd/current has been updated.
func (r *snapdRestartImpl) Restart() error {
if err := r.Sysd.StartNoBlock([]string{"snapd.apparmor.service"}); err != nil {
return err
}
// and finally start snapd.service (it will stop by itself and gets
// started by systemd then)
// Because of the file lock held on the snapstate by the Overlord, the new
// snapd will block there until we release it. For this reason, we cannot
// start the unit in blocking mode.
// TODO: move/share this responsibility with daemon so that we can make the
// start blocking again
if err := r.Sysd.StartNoBlock([]string{"snapd.service"}); err != nil {
return err
}
if err := r.Sysd.StartNoBlock([]string{"snapd.seeded.service"}); err != nil {
return err
}
// we cannot start snapd.autoimport in blocking mode because
// it has a "After=snapd.seeded.service" which means that on
// seeding a "systemctl start" that blocks would hang forever
// and we deadlock.
if err := r.Sysd.StartNoBlock([]string{"snapd.autoimport.service"}); err != nil {
return err
}
return nil
}
// AddSnapdSnapServices sets up the services based on a given snapd snap in the
// system.
func AddSnapdSnapServices(s *snap.Info, opts *AddSnapdSnapServicesOptions, inter Interacter) (SnapdRestart, error) {
if snapType := s.Type(); snapType != snap.TypeSnapd {
return nil, fmt.Errorf("internal error: adding explicit snapd services for snap %q type %q is unexpected", s.InstanceName(), snapType)
}
// we never write snapd services on classic
if release.OnClassic {
return nil, nil
}
if opts == nil {
opts = &AddSnapdSnapServicesOptions{}
}
var sysd systemd.Systemd
if !opts.Preseeding {
sysd = systemd.New(systemd.SystemMode, inter)
} else {
sysd = systemd.NewEmulationMode("")
}
if err := writeSnapdToolingMountUnit(sysd, s.MountDir(), opts); err != nil {
return nil, err
}
serviceUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.service"))
if err != nil {
return nil, err
}
socketUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.socket"))
if err != nil {
return nil, err
}
timerUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.timer"))
if err != nil {
return nil, err
}
targetUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.target"))
if err != nil {
return nil, err
}
units := append(socketUnits, serviceUnits...)
units = append(units, timerUnits...)
units = append(units, targetUnits...)
snapdUnits := make(map[string]osutil.FileState, len(units)+1)
for _, unit := range units {
st, err := os.Stat(unit)
if err != nil {
return nil, err
}
content, err := os.ReadFile(unit)
if err != nil {
return nil, err
}
if execStartRe.Match(content) {
content = execStartRe.ReplaceAll(content, []byte(fmt.Sprintf("ExecStart=%s$1", s.MountDir())))
// when the service executes a command from the snapd snap, make
// sure the exec path points to the mount dir, and that the
// mount happens before the unit is started
content = append(content, []byte(fmt.Sprintf("\n[Unit]\nRequiresMountsFor=%s\n", s.MountDir()))...)
}
snapdUnits[filepath.Base(unit)] = &osutil.MemoryFileState{
Content: content,
Mode: st.Mode(),
}
}
globs := []string{"snapd.service", "snapd.socket", "snapd.*.service", "snapd.*.timer", "snapd.*.target"}
changed, removed, err := osutil.EnsureDirStateGlobs(dirs.SnapServicesDir, globs, snapdUnits)
if err != nil {
// TODO: uhhhh, what do we do in this case?
return nil, err
}
if (len(changed) + len(removed)) == 0 {
// nothing to do
return nil, nil
}
// stop all removed units first
for _, unit := range removed {
serviceUnits := []string{unit}
if err := sysd.Stop(serviceUnits); err != nil {
logger.Noticef("failed to stop %q: %v", unit, err)
}
if err := sysd.DisableNoReload(serviceUnits); err != nil {
logger.Noticef("failed to disable %q: %v", unit, err)
}
}
// daemon-reload so that we get the new services
if len(changed) > 0 {
if err := sysd.DaemonReload(); err != nil {
return nil, err
}
}
// enable/start all the new services
for _, unit := range changed {
// systemd looks at the logical units, even if 'enabled' service
// symlink points to /lib/systemd/system location, dropping an
// identically named service in /etc overrides the other unit,
// therefore it is sufficient to enable the new units only
//
// Calling sysd.Enable() unconditionally may fail depending on
// systemd version, where older versions (eg 229 in 16.04) would
// error out unless --force is passed, while new ones remove the
// symlink and create a new one.
if !opts.Preseeding {
enabled, err := sysd.IsEnabled(unit)
if err != nil {
return nil, err
}
if enabled {
continue
}
}
if err := sysd.EnableNoReload([]string{unit}); err != nil {
return nil, err
}
}
if !opts.Preseeding {
for _, unit := range changed {
// Some units (like the snapd.system-shutdown.service) cannot
// be started. Others like "snapd.seeded.service" are started
// as dependencies of snapd.service.
if snapdSkipStart(snapdUnits[unit].(*osutil.MemoryFileState).Content) {
continue
}
// Ensure to only restart if the unit was previously
// active. This ensures we DTRT on firstboot and do
// not stop e.g. snapd.socket because doing that
// would mean that the snapd.seeded.service is also
// stopped (independently of snapd.socket being
// active) which confuses the boot order (the unit
// exists before we are fully seeded).
isActive, err := sysd.IsActive(unit)
if err != nil {
return nil, err
}
serviceUnits := []string{unit}
if isActive {
// we can never restart the snapd.socket because
// this will also bring down snapd itself
if unit != "snapd.socket" {
if err := sysd.Restart(serviceUnits); err != nil {
return nil, err
}
}
} else {
if err := sysd.Start(serviceUnits); err != nil {
return nil, err
}
}
}
}
// Handle the user services
if err := writeSnapdUserServicesOnCore(s, opts, inter); err != nil {
return nil, err
}
// Handle D-Bus configuration
if err := writeSnapdDbusConfigOnCore(s); err != nil {
return nil, err
}
if err := writeSnapdDbusActivationOnCore(s); err != nil {
return nil, err
}
if err := writeSnapdDesktopFilesOnCore(s); err != nil {
return nil, err
}
return &snapdRestartImpl{Sysd: sysd}, nil
}
// undoSnapdUserServicesOnCore attempts to remove services that were deployed in
// the filesystem as part of snapd snap installation. This should only be
// executed as part of a controlled undo path.
func undoSnapdServicesOnCore(s *snap.Info, sysd systemd.Systemd) error {
// list service, socket and timer units present in the snapd snap
serviceUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.service"))
if err != nil {
return err
}
socketUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.socket"))
if err != nil {
return err
}
timerUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.timer"))
if err != nil {
return err
}
targetUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "lib/systemd/system/*.target"))
if err != nil {
return err
}
units := append(socketUnits, serviceUnits...)
units = append(units, timerUnits...)
units = append(units, targetUnits...)
for _, snapdUnit := range units {
sysdUnit := filepath.Base(snapdUnit)
coreUnit := filepath.Join(dirs.GlobalRootDir, "lib/systemd/system", sysdUnit)
writtenUnitPath := filepath.Join(dirs.SnapServicesDir, sysdUnit)
if !osutil.FileExists(writtenUnitPath) {
continue
}
existsInCore := osutil.FileExists(coreUnit)
unit := []string{sysdUnit}
if !existsInCore {
// new unit that did not exist on core, disable and stop
if err := sysd.DisableNoReload(unit); err != nil {
logger.Noticef("failed to disable %q: %v", unit, err)
}
if err := sysd.Stop(unit); err != nil {
return err
}
}
if err := os.Remove(writtenUnitPath); err != nil {
return err
}
if !existsInCore {
// nothing more to do here
continue
}
isEnabled, err := sysd.IsEnabled(sysdUnit)
if err != nil {
return err
}
if !isEnabled {
if err := sysd.EnableNoReload(unit); err != nil {
return err
}
}
if sysdUnit == "snapd.socket" {
// do not start the socket, snap failover handler will
// restart it
continue
}
skipStart, err := snapdUnitSkipStart(coreUnit)
if err != nil {
return err
}
if !skipStart {
// TODO: consider using sys.Restart() instead of is-active check
isActive, err := sysd.IsActive(sysdUnit)
if err != nil {
return err
}
if isActive {
if err := sysd.Restart(unit); err != nil {
return err
}
} else {
if err := sysd.Start(unit); err != nil {
return err
}
}
}
}
return nil
}
func writeSnapdUserServicesOnCore(s *snap.Info, opts *AddSnapdSnapServicesOptions, inter Interacter) error {
// Ensure /etc/systemd/user exists
if err := os.MkdirAll(dirs.SnapUserServicesDir, 0755); err != nil {
return err
}
// TODO: use EmulationMode when preseeding (teach EmulationMode about user services)?
sysd := systemd.New(systemd.GlobalUserMode, inter)
serviceUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/lib/systemd/user/*.service"))
if err != nil {
return err
}
socketUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/lib/systemd/user/*.socket"))
if err != nil {
return err
}
units := append(serviceUnits, socketUnits...)
snapdUnits := make(map[string]osutil.FileState, len(units)+1)
for _, unit := range units {
st, err := os.Stat(unit)
if err != nil {
return err
}
content, err := os.ReadFile(unit)
if err != nil {
return err
}
if execStartRe.Match(content) {
content = execStartRe.ReplaceAll(content, []byte(fmt.Sprintf("ExecStart=%s$1", s.MountDir())))
// when the service executes a command from the snapd snap, make
// sure the exec path points to the mount dir, and that the
// mount happens before the unit is started
content = append(content, []byte(fmt.Sprintf("\n[Unit]\nRequiresMountsFor=%s\n", s.MountDir()))...)
}
snapdUnits[filepath.Base(unit)] = &osutil.MemoryFileState{
Content: content,
Mode: st.Mode(),
}
}
changed, removed, err := osutil.EnsureDirStateGlobs(dirs.SnapUserServicesDir, []string{"snapd.*.service", "snapd.*.socket"}, snapdUnits)
if err != nil {
// TODO: uhhhh, what do we do in this case?
return err
}
if (len(changed) + len(removed)) == 0 {
// nothing to do
return nil
}
// disable all removed units first
for _, unit := range removed {
if err := sysd.DisableNoReload([]string{unit}); err != nil {
logger.Noticef("failed to disable %q: %v", unit, err)
}
}
// enable/start all the new services
for _, unit := range changed {
units := []string{unit}
if err := sysd.DisableNoReload(units); err != nil {
logger.Noticef("failed to disable %q: %v", unit, err)
}
if err := sysd.EnableNoReload(units); err != nil {
return err
}
}
if !opts.Preseeding {
if err := userDaemonReload(); err != nil {
logger.Noticef("failed to reload user systemd instances: %v", err)
}
}
return nil
}
// undoSnapdUserServicesOnCore attempts to remove user services that were
// deployed in the filesystem as part of snapd snap installation. This should
// only be executed as part of a controlled undo path.
func undoSnapdUserServicesOnCore(s *snap.Info, inter Interacter) error {
sysd := systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.GlobalUserMode, inter)
// list user service and socket units present in the snapd snap
serviceUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/lib/systemd/user/*.service"))
if err != nil {
return err
}
socketUnits, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/lib/systemd/user/*.socket"))
if err != nil {
return err
}
units := append(serviceUnits, socketUnits...)
for _, srcUnit := range units {
unit := filepath.Base(srcUnit)
writtenUnitPath := filepath.Join(dirs.SnapUserServicesDir, unit)
if !osutil.FileExists(writtenUnitPath) {
continue
}
coreUnit := filepath.Join(dirs.GlobalRootDir, "usr/lib/systemd/user", unit)
existsInCore := osutil.FileExists(coreUnit)
if err := sysd.DisableNoReload([]string{unit}); err != nil {
logger.Noticef("failed to disable %q: %v", unit, err)
}
if err := os.Remove(writtenUnitPath); err != nil {
return err
}
if !existsInCore {
// new unit that did not exist on core
continue
}
if err := sysd.EnableNoReload([]string{unit}); err != nil {
return err
}
}
return nil
}
func DeriveSnapdDBusConfig(s *snap.Info) (sessionContent, systemContent map[string]osutil.FileState, err error) {
sessionConfigs, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/share/dbus-1/session.d/snapd.*.conf"))
if err != nil {
return nil, nil, err
}
sessionContent = make(map[string]osutil.FileState, len(sessionConfigs)+1)
for _, config := range sessionConfigs {
sessionContent[filepath.Base(config)] = &osutil.FileReference{
Path: config,
}
}
systemConfigs, err := filepath.Glob(filepath.Join(s.MountDir(), "usr/share/dbus-1/system.d/snapd.*.conf"))
if err != nil {
return nil, nil, err
}
systemContent = make(map[string]osutil.FileState, len(systemConfigs)+1)
for _, config := range systemConfigs {
systemContent[filepath.Base(config)] = &osutil.FileReference{
Path: config,
}
}
return sessionContent, systemContent, nil
}
func isReadOnlyFsError(err error) bool {
if err == nil {
return false
}
if e, ok := err.(*os.PathError); ok {
err = e.Err
}
if e, ok := err.(syscall.Errno); ok {
return e == syscall.EROFS
}
return false
}
var ensureDirState = osutil.EnsureDirState
func writeSnapdDbusConfigOnCore(s *snap.Info) error {
sessionContent, systemContent, err := DeriveSnapdDBusConfig(s)
if err != nil {
return err
}
_, _, err = ensureDirState(dirs.SnapDBusSessionPolicyDir, "snapd.*.conf", sessionContent)
if err != nil {
if isReadOnlyFsError(err) {
// If /etc/dbus-1/session.d is read-only (which may be the case on very old core18), then
// err is os.PathError with syscall.Errno underneath. Hitting this prevents snapd refresh,
// so log the error but carry on. This fixes LP: 1899664.
// XXX: ideally we should regenerate session files elsewhere if we fail here (otherwise
// this will only happen on future snapd refresh), but realistically this
// is not relevant on core18 devices.
logger.Noticef("%s appears to be read-only, could not write snapd dbus config files", dirs.SnapDBusSessionPolicyDir)
} else {
return err
}
}
_, _, err = osutil.EnsureDirState(dirs.SnapDBusSystemPolicyDir, "snapd.*.conf", systemContent)
if err != nil {
return err
}
return nil
}
func undoSnapdDbusConfigOnCore() error {
_, _, err := osutil.EnsureDirState(dirs.SnapDBusSystemPolicyDir, "snapd.*.conf", nil)
if err != nil {
return err
}
_, _, err = osutil.EnsureDirState(dirs.SnapDBusSessionPolicyDir, "snapd.*.conf", nil)
return err
}
// Service files that have been written by snapd.
// Only ever append this list, if a file is no longer installed it needs to
// remain here so a newer version of snapd can remove it.
var dbusSessionServices = []string{
"io.snapcraft.Launcher.service",
"io.snapcraft.Prompt.service",
"io.snapcraft.Settings.service",
"io.snapcraft.SessionAgent.service",
}
func writeSnapdDbusActivationOnCore(s *snap.Info) error {
if err := os.MkdirAll(dirs.SnapDBusSessionServicesDir, 0755); err != nil {
return err
}
content := make(map[string]osutil.FileState, len(dbusSessionServices)+1)
for _, service := range dbusSessionServices {
filePathInSnap := filepath.Join(s.MountDir(), "usr/share/dbus-1/services", service)
if !osutil.FileExists(filePathInSnap) {
continue
}
content[service] = &osutil.FileReference{
Path: filePathInSnap,
}
}
_, _, err := osutil.EnsureDirStateGlobs(dirs.SnapDBusSessionServicesDir, dbusSessionServices, content)
return err
}
func undoSnapdDbusActivationOnCore() error {
_, _, err := osutil.EnsureDirStateGlobs(dirs.SnapDBusSessionServicesDir, dbusSessionServices, nil)
return err
}
// Desktop files that have been written by snapd.
// Only ever append this list, if a file is no longer installed it needs to
// remain here so a newer version of snapd can remove it.
var snapdDesktopFileNames = []string{
"io.snapcraft.SessionAgent.desktop",
"snap-handle-link.desktop",
}
func writeSnapdDesktopFilesOnCore(s *snap.Info) error {
// Ensure /var/lib/snapd/desktop/applications exists
if err := os.MkdirAll(dirs.SnapDesktopFilesDir, 0755); err != nil {
return err
}
desktopFiles := make(map[string]osutil.FileState, len(snapdDesktopFileNames))
for _, fileName := range snapdDesktopFileNames {
filePathInSnap := filepath.Join(s.MountDir(), "usr/share/applications", fileName)
if !osutil.FileExists(filePathInSnap) {
continue
}
desktopFiles[fileName] = &osutil.FileReference{Path: filePathInSnap}
}
_, _, err := osutil.EnsureDirStateGlobs(dirs.SnapDesktopFilesDir, snapdDesktopFileNames, desktopFiles)
return err
}
func undoSnapdDesktopFilesOnCore(s *snap.Info) error {
_, _, err := osutil.EnsureDirStateGlobs(dirs.SnapDesktopFilesDir, snapdDesktopFileNames, nil)
return err
}
// RemoveSnapdSnapServicesOnCore removes the snapd services generated by a prior
// call to AddSnapdSnapServices. The core snap is used as the reference for
// restoring the system state, making this undo helper suitable for use when
// reverting the first installation of the snapd snap on a core device.
func RemoveSnapdSnapServicesOnCore(s *snap.Info, inter Interacter) error {
if snapType := s.Type(); snapType != snap.TypeSnapd {
return fmt.Errorf("internal error: removing explicit snapd services for snap %q type %q is unexpected", s.InstanceName(), snapType)
}
// snapd services are never written on classic, nothing to remove
if release.OnClassic {
return nil
}
sysd := systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.SystemMode, inter)
if err := undoSnapdDesktopFilesOnCore(s); err != nil {
return err
}
if err := undoSnapdDbusActivationOnCore(); err != nil {
return err
}
if err := undoSnapdDbusConfigOnCore(); err != nil {
return err
}
if err := undoSnapdServicesOnCore(s, sysd); err != nil {
return err
}
if err := undoSnapdUserServicesOnCore(s, inter); err != nil {
return err
}
if err := undoSnapdToolingMountUnit(sysd); err != nil {
return err
}
// XXX: reload after all operations?
return nil
}