File: procfs.go

package info (click to toggle)
rust-pathrs 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,912 kB
  • sloc: python: 1,138; sh: 371; ansic: 259; makefile: 151
file content (185 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download
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
// SPDX-License-Identifier: MPL-2.0
/*
 * libpathrs: safe path resolution on Linux
 * Copyright (C) 2019-2025 Aleksa Sarai <cyphar@cyphar.com>
 * Copyright (C) 2019-2025 SUSE LLC
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package main

import (
	"context"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/urfave/cli/v3"
	"golang.org/x/sys/unix"

	"cyphar.com/go-pathrs/procfs"
)

type procfsOpenFunc func(path string, flags int) (*os.File, error)

var procfsCmd = &cli.Command{
	Name:  "procfs",
	Usage: "ProcfsHandle::* operations",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:  "unmasked",
			Usage: "use unmasked procfs handle",
		},
		&cli.StringFlag{
			Name:  "base",
			Usage: "base path for procfs operations (root, pid=<n>, self, thread-self)",
			Value: "root",
		},
	},
	Commands: []*cli.Command{
		procfsOpenCmd,
		procfsReadlinkCmd,
	},

	Before: func(ctx context.Context, cmd *cli.Command) (_ context.Context, Err error) {
		var opts []procfs.OpenOption
		if cmd.Bool("unmasked") {
			opts = append(opts, procfs.UnmaskedProcRoot)
		}
		proc, err := procfs.Open(opts...)
		if err != nil {
			return nil, err
		}
		ctx = context.WithValue(ctx, "procfs", proc)
		defer func() {
			if Err != nil {
				_ = proc.Close()
			}
		}()

		return ctx, nil
	},

	After: func(ctx context.Context, cmd *cli.Command) error {
		if proc, ok := ctx.Value("procfs").(*procfs.Handle); ok {
			_ = proc.Close()
		}
		return nil
	},
}

var procfsOpenCmd = cmdWithOptions(&cli.Command{
	Name:  "open",
	Usage: "open a path in procfs",
	Flags: []cli.Flag{
		&cli.BoolWithInverseFlag{
			Name:  "follow",
			Usage: "follow trailing symlinks",
			Value: true,
		},
	},
	Arguments: []cli.Argument{
		&cli.StringArg{
			Name: "subpath",
		},
	},

	Action: func(ctx context.Context, cmd *cli.Command) error {
		proc := ctx.Value("procfs").(*procfs.Handle)
		follow := cmd.Bool("follow")
		subpath := cmd.StringArg("subpath")

		oflags := unix.O_RDONLY
		if val := ctx.Value("oflags"); val != nil {
			oflags = val.(int)
		}
		if !follow {
			oflags |= unix.O_NOFOLLOW
		}

		var (
			f   *os.File
			err error
		)

		baseStr := cmd.String("base")
		if pidStr, ok := strings.CutPrefix(baseStr, "pid="); ok {
			var pid int
			pid, err = strconv.Atoi(pidStr)
			if err != nil {
				return fmt.Errorf("failed to parse --base=%q: %w", pidStr, err)
			}
			f, err = proc.OpenPid(pid, subpath, oflags)
		} else {
			switch baseStr {
			case "root":
				f, err = proc.OpenRoot(subpath, oflags)
			case "self":
				f, err = proc.OpenSelf(subpath, oflags)
			case "thread-self":
				var closer procfs.ThreadCloser
				f, closer, err = proc.OpenThreadSelf(subpath, oflags)
				if closer != nil {
					defer closer()
				}
			default:
				return fmt.Errorf("invalid --base value %q", baseStr)
			}
		}
		if err != nil {
			return err
		}
		defer f.Close()

		fmt.Println("FILE-PATH", f.Name())
		// TODO: Input/output file data.
		return nil
	},
}, oflags("oflags", "O_* flags to use when opening the file", unix.O_RDONLY))

var procfsReadlinkCmd = &cli.Command{
	Name:  "readlink",
	Usage: "read the target path of a symbolic link in procfs",
	Arguments: []cli.Argument{
		&cli.StringArg{
			Name: "subpath",
		},
	},

	Action: func(ctx context.Context, cmd *cli.Command) error {
		proc := ctx.Value("procfs").(*procfs.Handle)
		subpath := cmd.StringArg("subpath")

		var base procfs.ProcBase
		baseStr := cmd.String("base")
		if pidStr, ok := strings.CutPrefix(baseStr, "pid="); ok {
			pid, err := strconv.Atoi(pidStr)
			if err != nil {
				return fmt.Errorf("failed to parse --base=%q: %w", pidStr, err)
			}
			base = procfs.ProcPid(pid)
		} else {
			switch baseStr {
			case "root":
				base = procfs.ProcRoot
			case "self":
				base = procfs.ProcSelf
			case "thread-self":
				base = procfs.ProcThreadSelf
			default:
				return fmt.Errorf("invalid --base value %q", baseStr)
			}
		}

		target, err := proc.Readlink(base, subpath)
		if err != nil {
			return err
		}
		fmt.Println("LINK-TARGET", target)
		return nil
	},
}