summaryrefslogtreecommitdiff
path: root/sd_login/type_machine.go
diff options
context:
space:
mode:
Diffstat (limited to 'sd_login/type_machine.go')
-rw-r--r--sd_login/type_machine.go55
1 files changed, 52 insertions, 3 deletions
diff --git a/sd_login/type_machine.go b/sd_login/type_machine.go
index 5892017..d89eaad 100644
--- a/sd_login/type_machine.go
+++ b/sd_login/type_machine.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 Luke Shumaker <lukeshu@sbcglobal.net>
+// Copyright (C) 2016-2017 Luke Shumaker <lukeshu@sbcglobal.net>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,9 +21,44 @@ import (
"golang.org/x/sys/unix"
)
+// A Machine is a name representing a locally running container or
+// virtual machine that is registerd with (systemd-)machined.
type Machine string
-// running VMs/containers
+// isValid returns whether the Machine name is valid.
+func (m Machine) isValid() bool {
+ if len(m) > host_name_max {
+ // Note that Linux HOST_NAME_MAX is 64, but DNS allows
+ // 255, so names from DNS might be invalid.
+ return false
+ }
+
+ n_dots := 0
+ dot := true
+ for _, c := range m {
+ if c == '.' {
+ if dot {
+ return false
+ }
+ dot = true
+ n_dots++
+ } else {
+ if !strings.ContainsRune(letters+digits+"-_.", c) {
+ return false
+ }
+ dot = false
+ }
+ }
+
+ if dot { // trailing dot or empty
+ return false
+ }
+
+ return true
+}
+
+// GetMachines returns a list of currently running containers/virtual
+// machines.
func GetMachines() ([]Machine, error) {
strs, err := get_files_in_directory("/run/systemd/machines/")
if err != nil {
@@ -31,7 +66,8 @@ func GetMachines() ([]Machine, error) {
}
var machines []Machine
for _, str := range strs {
- if strings.HasPrefix(str, "unit:") || !valid_machine_name(str) {
+ machine := Machine(str)
+ if strings.HasPrefix(str, "unit:") || machine.isValid() {
continue
}
machines = append(machines, Machine(str))
@@ -39,7 +75,14 @@ func GetMachines() ([]Machine, error) {
return machines, nil
}
+// GetClass returns the class of a locally running machine. The class
+// is either "vm" or "container", depending on if the machine is an
+// virtual machine or a container.
func (m Machine) GetClass() (string, error) {
+ if !m.isValid() {
+ return "", unix.EINVAL
+ }
+
env, err := parse_env_file("/run/systemd/machines/" + string(m))
if err != nil {
return "", err
@@ -51,7 +94,13 @@ func (m Machine) GetClass() (string, error) {
return class, nil
}
+// GetIfIndices returns the numeric indices of the network interfaces
+// on the host that are pointing toward this machine.
func (m Machine) GetIfIndices() ([]int, error) {
+ if !m.isValid() {
+ return nil, unix.EINVAL
+ }
+
env, err := parse_env_file("/run/systemd/machines/" + string(m))
if err != nil {
return nil, err