diff options
author | Andrew G. Morgan <morgan@kernel.org> | 2019-05-19 14:57:20 -0700 |
---|---|---|
committer | Andrew G. Morgan <morgan@kernel.org> | 2019-05-19 14:57:20 -0700 |
commit | 0615d996379dceedefcd65a114f93fefd81c208f (patch) | |
tree | 590edde235e563c88b62b913a449cc43884d1be2 /go/mknames.go | |
parent | ac1ef3125f50594289a2d9a4de3b5a22d2882ea4 (diff) | |
download | libcap2-0615d996379dceedefcd65a114f93fefd81c208f.tar.gz |
A Go (golang) implementation of libcap: import "libcap/cap".
The API for this "libcap/cap" package is very similar to libcap.
I've included a substantial interoperability test that validate
libcap(c) and libcap/cap(go) have import/export text and binary
format compatibility.
My motivation for implementing a standalone Go package was for a
cross-compilation issue I ran into (Go is much more friendly for
cross-compilation by default, unless you need to use cgo).
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
Diffstat (limited to 'go/mknames.go')
-rw-r--r-- | go/mknames.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/go/mknames.go b/go/mknames.go new file mode 100644 index 0000000..d430815 --- /dev/null +++ b/go/mknames.go @@ -0,0 +1,85 @@ +// Program mknames parses the cap_names.h file and creates an equivalent names.go file. +package main + +import ( + "bytes" + "flag" + "fmt" + "io" + "io/ioutil" + "log" + "strings" +) + +var ( + header = flag.String("header", "", "name of header file") +) + +func main() { + flag.Parse() + + if *header == "" { + log.Fatal("usage: mknames --header=.../cap_names.h") + } + d, err := ioutil.ReadFile(*header) + if err != nil { + log.Fatal("reading:", err) + } + + b := bytes.NewBuffer(d) + + var list []string + for { + line, err := b.ReadString('\n') + if err == io.EOF { + break + } + if !strings.Contains(line, `"`) { + continue + } + i := strings.Index(line, `"`) + line = line[i+1:] + i = strings.Index(line, `"`) + line = line[:i] + list = append(list, line) + } + + // generate package file names.go + fmt.Print(`package cap + +/* ** DO NOT EDIT THIS FILE. IT WAS AUTO-GENERATED BY LIBCAP'S GO BUILDER (mknames.go) ** */ + +// NamedCount holds the number of capabilities with official names. +const NamedCount = `, len(list), ` + +// CHOWN etc., are the named capability bits on this system. The +// canonical source for each name is the "uapi/linux/capabilities.h" +// file, which is hard-coded into this package. +const ( +`) + bits := make(map[string]string) + for i, name := range list { + v := strings.ToUpper(strings.TrimPrefix(name, "cap_")) + bits[name] = v + if i == 0 { + fmt.Println(v, " Value = iota") + } else { + fmt.Println(v) + } + } + fmt.Print(`) + +var names = map[Value]string{ +`) + for _, name := range list { + fmt.Printf("%s: %q,\n", bits[name], name) + } + fmt.Print(`} + +var bits = map[string]Value { +`) + for _, name := range list { + fmt.Printf("%q: %s,\n", name, bits[name]) + } + fmt.Println(`}`) +} |