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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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"
"os"
"path/filepath"
"regexp"
"text/template"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
)
func generateDBusActivationFile(app *snap.AppInfo, busName string) ([]byte, error) {
// The D-Bus service activation file format is defined as part
// of the protocol specification:
//
// https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-starting-services
serviceTemplate := `[D-BUS Service]
Name={{.BusName}}
Comment=Bus name for snap application {{.App.Snap.InstanceName}}.{{.App.Name}}
SystemdService={{.App.ServiceName}}
Exec={{.App.LauncherCommand}}
AssumedAppArmorLabel={{.App.SecurityTag}}
{{- if eq .App.DaemonScope "system"}}
User=root
{{- end}}
X-Snap={{.App.Snap.InstanceName}}
`
t := template.Must(template.New("dbus-service").Parse(serviceTemplate))
serviceData := struct {
App *snap.AppInfo
BusName string
}{
App: app,
BusName: busName,
}
var templateOut bytes.Buffer
if err := t.Execute(&templateOut, serviceData); err != nil {
return nil, err
}
return templateOut.Bytes(), nil
}
var snapNameLine = regexp.MustCompile(`(?m)^X-Snap=(.*)$`)
// snapNameFromServiceFile returns the snap name for the D-Bus service activation file.
func snapNameFromServiceFile(filename string) (owner string, err error) {
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
m := snapNameLine.FindSubmatch(content)
if m != nil {
owner = string(m[1])
}
return owner, nil
}
// snapServiceActivationFiles returns the list of service activation files for a snap.
func snapServiceActivationFiles(dir, snapName string) (services []string, err error) {
glob := filepath.Join(dir, "*.service")
matches, err := filepath.Glob(glob)
if err != nil {
return nil, err
}
for _, match := range matches {
serviceSnap, err := snapNameFromServiceFile(match)
if err != nil {
return nil, err
}
if serviceSnap == snapName {
services = append(services, filepath.Base(match))
}
}
return services, nil
}
func AddSnapDBusActivationFiles(s *snap.Info) error {
if err := os.MkdirAll(dirs.SnapDBusSessionServicesDir, 0755); err != nil {
return err
}
if err := os.MkdirAll(dirs.SnapDBusSystemServicesDir, 0755); err != nil {
return err
}
// Make sure we include any service files that claim to have
// been written by the snap.
sessionServices, err := snapServiceActivationFiles(dirs.SnapDBusSessionServicesDir, s.InstanceName())
if err != nil {
return err
}
systemServices, err := snapServiceActivationFiles(dirs.SnapDBusSystemServicesDir, s.InstanceName())
if err != nil {
return err
}
sessionContent := make(map[string]osutil.FileState)
systemContent := make(map[string]osutil.FileState)
for _, app := range s.Apps {
if !app.IsService() {
continue
}
for _, slot := range app.ActivatesOn {
var busName string
if err := slot.Attr("name", &busName); err != nil {
return err
}
content, err := generateDBusActivationFile(app, busName)
if err != nil {
return err
}
filename := busName + ".service"
fileState := &osutil.MemoryFileState{
Content: content,
Mode: 0644,
}
switch app.DaemonScope {
case snap.SystemDaemon:
systemContent[filename] = fileState
systemServices = append(systemServices, filename)
case snap.UserDaemon:
sessionContent[filename] = fileState
sessionServices = append(sessionServices, filename)
}
}
}
if _, _, err = osutil.EnsureDirStateGlobs(dirs.SnapDBusSessionServicesDir, sessionServices, sessionContent); err != nil {
return err
}
if _, _, err = osutil.EnsureDirStateGlobs(dirs.SnapDBusSystemServicesDir, systemServices, systemContent); err != nil {
// On error, remove files installed by first invocation
osutil.EnsureDirStateGlobs(dirs.SnapDBusSessionServicesDir, sessionServices, nil)
return err
}
return nil
}
func RemoveSnapDBusActivationFiles(s *snap.Info) error {
// Select files to delete via X-Snap line to ensure everything
// is cleaned up if "snap try" is used and snap.yaml is
// modified.
for _, servicesDir := range []string{
dirs.SnapDBusSessionServicesDir,
dirs.SnapDBusSystemServicesDir,
} {
toRemove, err := snapServiceActivationFiles(servicesDir, s.InstanceName())
if err != nil {
return err
}
if _, _, err = osutil.EnsureDirStateGlobs(servicesDir, toRemove, nil); err != nil {
return err
}
}
return nil
}
|