Skip to content

Servicemanager Go-API

import "givc/modules/pkgs/servicemanager"

Copyright 2024 TII (SSRC) and the Ghaf contributors SPDX-License-Identifier: Apache-2.0

The servicemanager package provides functionality to manage systemd services and applications.

const (
NO_WAIT_FOR_MERGE = 0 * time.Second
WAIT_FOR_MERGE = 2 * time.Second
)

const (
ResourceStreamInterval = 400 * time.Millisecond
)

type SystemdControlServer struct {
Controller *SystemdController
givc_systemd.UnimplementedUnitControlServiceServer
}

func NewSystemdControlServer(whitelist []string, applications []types.ApplicationManifest) (*SystemdControlServer, error)

NewSystemdControlServer creates a new instance of SystemdControlServer with the provided service whitelist and applications.

func (s *SystemdControlServer) Close()

func (s *SystemdControlServer) FreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

FreezeUnit handles the gRPC request to freeze (pause) a systemd unit.

func (*SystemdControlServer) GetUnitStatus

Section titled “func (*SystemdControlServer) GetUnitStatus”
func (s *SystemdControlServer) GetUnitStatus(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

GetUnitStatus handles the gRPC request to get the status of a systemd unit.

func (s *SystemdControlServer) KillUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

KillUnit handles the gRPC request to kill a systemd unit.

func (s *SystemdControlServer) MonitorUnit(req *givc_systemd.UnitResourceRequest, stream givc_systemd.UnitControlService_MonitorUnitServer) error

MonitorUnit handles the gRPC request to monitor a systemd unit’s resource usage. This is legacy code and will be removed.

func (s *SystemdControlServer) Name() string

func (*SystemdControlServer) RegisterGrpcService

Section titled “func (*SystemdControlServer) RegisterGrpcService”
func (s *SystemdControlServer) RegisterGrpcService(srv *grpc.Server)

func (*SystemdControlServer) StartApplication

Section titled “func (*SystemdControlServer) StartApplication”
func (s *SystemdControlServer) StartApplication(ctx context.Context, req *givc_systemd.AppUnitRequest) (*givc_systemd.UnitResponse, error)

StartApplication handles the gRPC request to start an application unit.

func (s *SystemdControlServer) StartUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

StartUnit handles the gRPC request to start a systemd unit.

func (s *SystemdControlServer) StopUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

StopUnit handles the gRPC request to stop a systemd unit.

func (s *SystemdControlServer) UnfreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)

UnfreezeUnit handles the gRPC request to unfreeze (unpause) a systemd unit.

func (*SystemdControlServer) getUnitStatus

Section titled “func (*SystemdControlServer) getUnitStatus”
func (s *SystemdControlServer) getUnitStatus(ctx context.Context, name string) (*givc_systemd.UnitStatus, error)

getUnitStatus fetches the status of a systemd unit by its name.

type SystemdController struct {
conn *dbus.Conn
whitelist []string
applications []givc_types.ApplicationManifest
cancelCtx context.CancelFunc
}

func NewController(whitelist []string, applications []givc_types.ApplicationManifest) (*SystemdController, error)

NewController creates a new SystemdController instance, connecting to either the system or session bus, depending on the user (root or other). It further initializes the whitelist to restrict service/application access to only pre-defined units.

func (c *SystemdController) Close()

func (c *SystemdController) FindUnit(name string) ([]dbus.UnitStatus, error)

FindUnit returns the status of all units matching the name. It performs a whitelist check to ensure the unit is allowed to be queried.

func (c *SystemdController) FreezeUnit(ctx context.Context, name string) error

FreezeUnit freezes a unit.

func (*SystemdController) IsUnitWhitelisted

Section titled “func (*SystemdController) IsUnitWhitelisted”
func (c *SystemdController) IsUnitWhitelisted(name string) bool

IsUnitWhitelisted checks if a unit is whitelisted.

func (c *SystemdController) KillUnit(ctx context.Context, name string) error

KillUnit forcefully terminates a unit.

func (*SystemdController) StartApplication

Section titled “func (*SystemdController) StartApplication”
func (c *SystemdController) StartApplication(ctx context.Context, serviceName string, serviceArgs []string) (*dbus.UnitStatus, error)

StartApplication starts an application service with the dynamic arguments. Dynamic arguments are validated in accordance with the application manifest. The application is started as a transient systemd unit, which means it is not persisted. Since applications may be merged into a single unit, this function implements a pre-start analysis and a post-start watch mechanism to determine if the application is merged or not.

func (c *SystemdController) StartUnit(ctx context.Context, name string) error

StartUnit starts any systemd unit.

func (c *SystemdController) StopUnit(ctx context.Context, name string) error

StopUnit stops a unit by name.

func (c *SystemdController) UnfreezeUnit(ctx context.Context, name string) error

UnfreezeUnit unfreezes (thaw) a unit.

func (*SystemdController) getUnitCpuAndMem

Section titled “func (*SystemdController) getUnitCpuAndMem”
func (c *SystemdController) getUnitCpuAndMem(ctx context.Context, pid uint32) (float64, float32, error)

The function getUnitCpuAndMem returns the CPU and memory usage of a unit. No check against unit whitelist is performed and must be enforced by the caller. This functionality is deprecated.

func (*SystemdController) getUnitProperties

Section titled “func (*SystemdController) getUnitProperties”
func (c *SystemdController) getUnitProperties(ctx context.Context, unitName string) (map[string]interface{}, error)

The function getUnitProperties returns all properties of a unit as a map. No check against unit whitelist is performed and must be enforced by the caller.

func (*SystemdController) getUnitPropertyString

Section titled “func (*SystemdController) getUnitPropertyString”
func (c *SystemdController) getUnitPropertyString(ctx context.Context, unitName string, propertyName string) (string, error)

The function getUnitPropertyString returns the value of a specific property of a unit as a string. No check against unit whitelist is performed and must be enforced by the caller.

Generated by gomarkdoc