File: cmd_debug_execution.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (114 lines) | stat: -rw-r--r-- 3,236 bytes parent folder | download | duplicates (2)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2024 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 (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/jessevdk/go-flags"
	"github.com/snapcore/snapd/sandbox/apparmor"
	"github.com/snapcore/snapd/snapdtool"
)

type cmdDebugExec struct{}

func init() {
	cmd := addDebugCommand("execution",
		"Obtain information about execution aspects of snap toolchain commands",
		`Display debugging information about aspects of snap toolchain execution, such
as reexecution, tools location etc.`,
		func() flags.Commander { return &cmdDebugExec{} },
		nil,
		nil,
	)
	cmd.extra = func(c *flags.Command) {
		c.AddCommand("apparmor", "Show apparmor", "", &cmdDebugExecAppArmor{})
		c.AddCommand("snap", "Show snap execution info", "", &cmdDebugExecSnap{})
		c.AddCommand("internal-tool", "Show internal tool execution info", "", &cmdDebugExecInternalTool{})
	}
}

func (x *cmdDebugExec) Execute(args []string) error {
	return flag.ErrHelp
}

type cmdDebugExecAppArmor struct{}

func (x *cmdDebugExecAppArmor) Execute(args []string) error {
	cmd, internal, err := apparmor.AppArmorParser()
	if err != nil {
		fmt.Fprintf(Stdout, "apparmor-parser: error:%s\n", err.Error())
		fmt.Fprint(Stdout, "internal: false\n")
		return err
	} else {
		fmt.Fprintf(Stdout, "apparmor-parser: %s\n", cmd.Path)
		fmt.Fprintf(Stdout, "apparmor-parser-command: %s\n", strings.Join(cmd.Args, " "))
		fmt.Fprintf(Stdout, "internal: %v\n", internal)
	}
	return nil
}

type cmdDebugExecSnap struct{}

func (x *cmdDebugExecSnap) Execute(args []string) error {
	fmt.Fprintf(Stdout, "distro-supports-reexec: %v\n", snapdtool.DistroSupportsReExec())
	fmt.Fprintf(Stdout, "is-reexec-enabled: %v\n", snapdtool.IsReexecEnabled())
	fmt.Fprintf(Stdout, "is-reexec-explicitly-enabled: %v\n", snapdtool.IsReexecExplicitlyEnabled())

	isReexecd, err := snapdtool.IsReexecd()
	if err == nil {
		fmt.Fprintf(Stdout, "is-reexecd: %v\n", isReexecd)
	} else {
		fmt.Fprintf(Stdout, "is-reexecd: error:%v\n", err)
	}

	exe, err := os.Readlink("/proc/self/exe")
	if err != nil {
		return err
	}

	fmt.Fprintf(Stdout, "self-exe: %v\n", exe)

	return nil
}

type cmdDebugExecInternalTool struct {
	Positionals struct {
		Tool string `required:"yes" positional-arg-name:"<tool>" description:"internal tool name"`
	} `positional-args:"true"`
}

func (x *cmdDebugExecInternalTool) Execute(args []string) error {
	if x.Positionals.Tool == "" {
		return fmt.Errorf("tool name is missing")
	}

	tool, err := snapdtool.InternalToolPath(x.Positionals.Tool)
	if err != nil {
		return err
	}

	fmt.Fprintf(Stdout, "%s: %s\n", x.Positionals.Tool, tool)

	return nil
}