2019-01-12 04:58:27 +00:00
|
|
|
// +build selinux,linux
|
|
|
|
|
|
|
|
package selinux
|
|
|
|
|
|
|
|
import (
|
2020-07-24 21:23:56 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Returns a []byte slice if the xattr is set and nil otherwise
|
|
|
|
// Requires path and its attribute as arguments
|
|
|
|
func lgetxattr(path string, attr string) ([]byte, error) {
|
|
|
|
// Start with a 128 length byte array
|
2020-07-24 21:23:56 +00:00
|
|
|
dest := make([]byte, 128)
|
|
|
|
sz, errno := unix.Lgetxattr(path, attr, dest)
|
|
|
|
for errno == unix.ERANGE {
|
|
|
|
// Buffer too small, use zero-sized buffer to get the actual size
|
|
|
|
sz, errno = unix.Lgetxattr(path, attr, []byte{})
|
|
|
|
if errno != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil, errno
|
|
|
|
}
|
2020-07-24 21:23:56 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
dest = make([]byte, sz)
|
2020-07-24 21:23:56 +00:00
|
|
|
sz, errno = unix.Lgetxattr(path, attr, dest)
|
|
|
|
}
|
|
|
|
if errno != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:23:56 +00:00
|
|
|
return dest[:sz], nil
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|