github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap-seccomp/versioninfo.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"crypto/sha256"
    24  	"fmt"
    25  	"os"
    26  	"strings"
    27  
    28  	"github.com/mvo5/libseccomp-golang"
    29  
    30  	"github.com/snapcore/snapd/cmd/snap-seccomp/syscalls"
    31  	"github.com/snapcore/snapd/osutil"
    32  )
    33  
    34  var seccompSyscalls = syscalls.SeccompSyscalls
    35  
    36  func versionInfo() (string, error) {
    37  	myBuildID, err := osutil.MyBuildID()
    38  	if err != nil {
    39  		return "", fmt.Errorf("cannot get build-id of snap-seccomp: %v", err)
    40  	}
    41  	// Calculate the checksum of all syscall names supported by libseccomp
    42  	// library. We add that to the version info to cover the case when
    43  	// libseccomp version does not change, but the set of supported syscalls
    44  	// does due to distro patches.
    45  	sh := sha256.New()
    46  	newline := []byte("\n")
    47  	for _, syscallName := range seccompSyscalls {
    48  		if _, err := seccomp.GetSyscallFromName(syscallName); err != nil {
    49  			// syscall is unsupported by this version of libseccomp
    50  			continue
    51  		}
    52  		sh.Write([]byte(syscallName))
    53  		sh.Write(newline)
    54  	}
    55  
    56  	major, minor, micro := seccomp.GetLibraryVersion()
    57  	features := goSeccompFeatures()
    58  
    59  	return fmt.Sprintf("%s %d.%d.%d %x %s", myBuildID, major, minor, micro, sh.Sum(nil), features), nil
    60  }
    61  
    62  func showVersionInfo() error {
    63  	vi, err := versionInfo()
    64  	if err != nil {
    65  		return err
    66  	}
    67  	fmt.Fprintln(os.Stdout, vi)
    68  	return nil
    69  }
    70  
    71  func goSeccompFeatures() string {
    72  	var features []string
    73  	if actLogSupported() {
    74  		features = append(features, "bpf-actlog")
    75  	}
    76  
    77  	if len(features) == 0 {
    78  		return "-"
    79  	}
    80  	return strings.Join(features, ":")
    81  }