forked from snapcore/snapd
/
cmd_run.go
208 lines (175 loc) · 5.38 KB
/
cmd_run.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2016 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 main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapenv"
)
var (
syscallExec = syscall.Exec
userCurrent = user.Current
)
type cmdRun struct {
Command string `long:"command" description:"alternative command to run" hidden:"yes"`
Hook string `long:"hook" description:"hook to run" hidden:"yes"`
Revision string `short:"r" description:"use a specific snap revision when running hook" hidden:"yes"`
Shell bool `long:"shell" description:"run a shell instead of the command (useful for debugging)"`
}
func init() {
addCommand("run",
i18n.G("Run the given snap command"),
i18n.G("Run the given snap command with the right confinement and environment"),
func() flags.Commander {
return &cmdRun{}
})
}
func (x *cmdRun) Execute(args []string) error {
if len(args) == 0 {
return fmt.Errorf("need the application to run as argument")
}
snapApp := args[0]
args = args[1:]
// Catch some invalid parameter combinations, provide helpful errors
if x.Hook != "" && x.Command != "" {
return fmt.Errorf("cannot use --hook and --command together")
}
if x.Revision != "" && x.Hook == "" {
return fmt.Errorf("-r can only be used with --hook")
}
if x.Hook != "" && len(args) > 0 {
return fmt.Errorf("too many arguments for hook %q: %s", x.Hook, strings.Join(args, " "))
}
// Now actually handle the dispatching
if x.Hook != "" {
return snapRunHook(snapApp, x.Hook, x.Revision)
}
// pass shell as a special command to snap-exec
if x.Shell {
x.Command = "shell"
}
return snapRunApp(snapApp, x.Command, args)
}
func getSnapInfo(snapName string, snapRevision string) (*snap.Info, error) {
var revision snap.Revision
if snapRevision != "" {
// User supplied a revision.
var err error
revision, err = snap.ParseRevision(snapRevision)
if err != nil {
return nil, err
}
} else {
// User didn't supply a revision, so we need to get it via the snapd API
// here because once we're inside the confinement it may be unavailable.
snaps, err := Client().List([]string{snapName})
if err != nil {
return nil, err
}
if len(snaps) == 0 {
return nil, fmt.Errorf("cannot find snap %q", snapName)
}
if len(snaps) > 1 {
return nil, fmt.Errorf("multiple snaps for %q: %d", snapName, len(snaps))
}
revision = snaps[0].Revision
}
info, err := snap.ReadInfo(snapName, &snap.SideInfo{
Revision: revision,
})
if err != nil {
return nil, err
}
return info, nil
}
// returns the environment that is important for the later stages of execution
// (like SNAP_REVISION that snap-exec requires to work)
func snapExecEnv(info *snap.Info) []string {
env := snapenv.Basic(info)
env = append(env, snapenv.User(info, os.Getenv("HOME"))...)
return env
}
func createUserDataDirs(info *snap.Info) error {
usr, err := userCurrent()
if err != nil {
return fmt.Errorf("cannot get the current user: %s", err)
}
// see snapenv.User
userData := filepath.Join(usr.HomeDir, info.MountDir())
commonUserData := filepath.Join(userData, "..", "common")
for _, d := range []string{userData, commonUserData} {
if err := os.MkdirAll(d, 0755); err != nil {
return fmt.Errorf("cannot create %q: %s", d, err)
}
}
return nil
}
func snapRunApp(snapApp, command string, args []string) error {
snapName, appName := snap.SplitSnapApp(snapApp)
info, err := getSnapInfo(snapName, "")
if err != nil {
return err
}
app := info.Apps[appName]
if app == nil {
return fmt.Errorf("cannot find app %q in %q", appName, snapName)
}
return runSnapConfine(info, app.SecurityTag(), snapApp, command, "", args)
}
func snapRunHook(snapName, hookName, revision string) error {
info, err := getSnapInfo(snapName, revision)
if err != nil {
return err
}
hook := info.Hooks[hookName]
// Make sure this hook is valid for this snap. If not, don't run it. This
// isn't an error, e.g. it will happen if a snap doesn't ship a system hook.
if hook == nil {
return nil
}
return runSnapConfine(info, hook.SecurityTag(), snapName, "", hook.Name, nil)
}
func runSnapConfine(info *snap.Info, securityTag, snapApp, command, hook string, args []string) error {
if err := createUserDataDirs(info); err != nil {
logger.Noticef("WARNING: cannot create user data directory: %s", err)
}
cmd := []string{
"/usr/bin/ubuntu-core-launcher",
securityTag,
securityTag,
"/usr/lib/snapd/snap-exec",
snapApp,
}
if command != "" {
cmd = append(cmd, "--command="+command)
}
if hook != "" {
cmd = append(cmd, "--hook="+hook)
}
cmd = append(cmd, args...)
env := append(os.Environ(), snapExecEnv(info)...)
return syscallExec(cmd[0], cmd, env)
}