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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 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"
"io"
"os"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/usersession/xdgopenproxy"
)
var clientConfig = client.Config{
// snapctl should not try to read $HOME/.snap/auth.json, this will
// result in apparmor denials and configure task failures
// (LP: #1660941)
DisableAuth: true,
// we need the less privileged snap socket in snapctl
Socket: dirs.SnapSocket,
}
func main() {
// check for internal commands
if len(os.Args) > 2 && os.Args[1] == "internal" {
switch os.Args[2] {
case "configure-core":
fmt.Fprintf(os.Stderr, "no internal core configuration anymore")
os.Exit(1)
}
}
if len(os.Args) == 3 && os.Args[1] == "user-open" {
if err := xdgopenproxy.Run(os.Args[2]); err != nil {
fmt.Fprintf(os.Stderr, "user-open error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
var stdin io.Reader
if len(os.Args) > 1 && client.InternalSnapctlCmdNeedsStdin(os.Args[1]) {
stdin = os.Stdin
}
// no internal command, route via snapd
stdout, stderr, err := run(stdin)
if err != nil {
if e, ok := err.(*client.Error); ok {
switch e.Kind {
case client.ErrorKindUnsuccessful:
if errRes, ok := e.Value.(map[string]any); ok {
if stdout, ok := errRes["stdout"].(string); ok {
os.Stdout.Write([]byte(stdout))
}
if stderr, ok := errRes["stderr"].(string); ok {
os.Stderr.Write([]byte(stderr))
}
if errCode, ok := errRes["exit-code"].(float64); ok {
os.Exit(int(errCode))
}
}
}
}
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
if stdout != nil {
os.Stdout.Write(stdout)
}
if stderr != nil {
os.Stderr.Write(stderr)
}
}
func run(stdin io.Reader) (stdout, stderr []byte, err error) {
cli := client.New(&clientConfig)
cookie := os.Getenv("SNAP_COOKIE")
// for compatibility, if re-exec is not enabled and facing older snapd.
if cookie == "" {
cookie = os.Getenv("SNAP_CONTEXT")
}
return cli.RunSnapctl(&client.SnapCtlOptions{
ContextID: cookie,
Args: os.Args[1:],
}, stdin)
}
|