mirror of
https://github.com/go-gitea/gitea
synced 2024-11-15 06:34:25 +00:00
12a1f914f4
* update github.com/alecthomas/chroma v0.8.0 -> v0.8.1 * github.com/blevesearch/bleve v1.0.10 -> v1.0.12 * editorconfig-core-go v2.1.1 -> v2.3.7 * github.com/gliderlabs/ssh v0.2.2 -> v0.3.1 * migrate editorconfig.ParseBytes to Parse * github.com/shurcooL/vfsgen to 0d455de96546 * github.com/go-git/go-git/v5 v5.1.0 -> v5.2.0 * github.com/google/uuid v1.1.1 -> v1.1.2 * github.com/huandu/xstrings v1.3.0 -> v1.3.2 * github.com/klauspost/compress v1.10.11 -> v1.11.1 * github.com/markbates/goth v1.61.2 -> v1.65.0 * github.com/mattn/go-sqlite3 v1.14.0 -> v1.14.4 * github.com/mholt/archiver v3.3.0 -> v3.3.2 * github.com/microcosm-cc/bluemonday 4f7140c49acb -> v1.0.4 * github.com/minio/minio-go v7.0.4 -> v7.0.5 * github.com/olivere/elastic v7.0.9 -> v7.0.20 * github.com/urfave/cli v1.20.0 -> v1.22.4 * github.com/prometheus/client_golang v1.1.0 -> v1.8.0 * github.com/xanzy/go-gitlab v0.37.0 -> v0.38.1 * mvdan.cc/xurls v2.1.0 -> v2.2.0 Co-authored-by: Lauris BH <lauris@nix.lv>
210 lines
4.5 KiB
Go
Vendored
210 lines
4.5 KiB
Go
Vendored
// Copyright 2019 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
package procfs
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// ProcMapPermissions contains permission settings read from /proc/[pid]/maps
|
|
type ProcMapPermissions struct {
|
|
// mapping has the [R]ead flag set
|
|
Read bool
|
|
// mapping has the [W]rite flag set
|
|
Write bool
|
|
// mapping has the [X]ecutable flag set
|
|
Execute bool
|
|
// mapping has the [S]hared flag set
|
|
Shared bool
|
|
// mapping is marked as [P]rivate (copy on write)
|
|
Private bool
|
|
}
|
|
|
|
// ProcMap contains the process memory-mappings of the process,
|
|
// read from /proc/[pid]/maps
|
|
type ProcMap struct {
|
|
// The start address of current mapping.
|
|
StartAddr uintptr
|
|
// The end address of the current mapping
|
|
EndAddr uintptr
|
|
// The permissions for this mapping
|
|
Perms *ProcMapPermissions
|
|
// The current offset into the file/fd (e.g., shared libs)
|
|
Offset int64
|
|
// Device owner of this mapping (major:minor) in Mkdev format.
|
|
Dev uint64
|
|
// The inode of the device above
|
|
Inode uint64
|
|
// The file or psuedofile (or empty==anonymous)
|
|
Pathname string
|
|
}
|
|
|
|
// parseDevice parses the device token of a line and converts it to a dev_t
|
|
// (mkdev) like structure.
|
|
func parseDevice(s string) (uint64, error) {
|
|
toks := strings.Split(s, ":")
|
|
if len(toks) < 2 {
|
|
return 0, fmt.Errorf("unexpected number of fields")
|
|
}
|
|
|
|
major, err := strconv.ParseUint(toks[0], 16, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
minor, err := strconv.ParseUint(toks[1], 16, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return unix.Mkdev(uint32(major), uint32(minor)), nil
|
|
}
|
|
|
|
// parseAddress just converts a hex-string to a uintptr
|
|
func parseAddress(s string) (uintptr, error) {
|
|
a, err := strconv.ParseUint(s, 16, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return uintptr(a), nil
|
|
}
|
|
|
|
// parseAddresses parses the start-end address
|
|
func parseAddresses(s string) (uintptr, uintptr, error) {
|
|
toks := strings.Split(s, "-")
|
|
if len(toks) < 2 {
|
|
return 0, 0, fmt.Errorf("invalid address")
|
|
}
|
|
|
|
saddr, err := parseAddress(toks[0])
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
eaddr, err := parseAddress(toks[1])
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
return saddr, eaddr, nil
|
|
}
|
|
|
|
// parsePermissions parses a token and returns any that are set.
|
|
func parsePermissions(s string) (*ProcMapPermissions, error) {
|
|
if len(s) < 4 {
|
|
return nil, fmt.Errorf("invalid permissions token")
|
|
}
|
|
|
|
perms := ProcMapPermissions{}
|
|
for _, ch := range s {
|
|
switch ch {
|
|
case 'r':
|
|
perms.Read = true
|
|
case 'w':
|
|
perms.Write = true
|
|
case 'x':
|
|
perms.Execute = true
|
|
case 'p':
|
|
perms.Private = true
|
|
case 's':
|
|
perms.Shared = true
|
|
}
|
|
}
|
|
|
|
return &perms, nil
|
|
}
|
|
|
|
// parseProcMap will attempt to parse a single line within a proc/[pid]/maps
|
|
// buffer.
|
|
func parseProcMap(text string) (*ProcMap, error) {
|
|
fields := strings.Fields(text)
|
|
if len(fields) < 5 {
|
|
return nil, fmt.Errorf("truncated procmap entry")
|
|
}
|
|
|
|
saddr, eaddr, err := parseAddresses(fields[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
perms, err := parsePermissions(fields[1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
offset, err := strconv.ParseInt(fields[2], 16, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
device, err := parseDevice(fields[3])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
inode, err := strconv.ParseUint(fields[4], 10, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pathname := ""
|
|
|
|
if len(fields) >= 5 {
|
|
pathname = strings.Join(fields[5:], " ")
|
|
}
|
|
|
|
return &ProcMap{
|
|
StartAddr: saddr,
|
|
EndAddr: eaddr,
|
|
Perms: perms,
|
|
Offset: offset,
|
|
Dev: device,
|
|
Inode: inode,
|
|
Pathname: pathname,
|
|
}, nil
|
|
}
|
|
|
|
// ProcMaps reads from /proc/[pid]/maps to get the memory-mappings of the
|
|
// process.
|
|
func (p Proc) ProcMaps() ([]*ProcMap, error) {
|
|
file, err := os.Open(p.path("maps"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
maps := []*ProcMap{}
|
|
scan := bufio.NewScanner(file)
|
|
|
|
for scan.Scan() {
|
|
m, err := parseProcMap(scan.Text())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
maps = append(maps, m)
|
|
}
|
|
|
|
return maps, nil
|
|
}
|