osutil

package
v0.35.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: Unlicense Imports: 6 Imported by: 5

Documentation

Overview

Package osutil contains utilities for functions requiring system calls and other OS-specific APIs. OS-specific network handling should go to github.com/AdguardTeam/golibs/netutil instead.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsReconfigureSignal added in v0.30.3

func IsReconfigureSignal(sig os.Signal) (ok bool)

IsReconfigureSignal returns true if sig is a reconfigure signal.

NOTE: It always returns false on Windows.

func IsShutdownSignal added in v0.23.2

func IsShutdownSignal(sig os.Signal) (ok bool)

IsShutdownSignal returns true if sig is a shutdown signal.

func NotifyContextShutdownSignal added in v0.35.11

func NotifyContextShutdownSignal(
	n ContextSignalNotifier,
	parent context.Context,
) (ctx context.Context, stop context.CancelFunc)

NotifyContextShutdownSignal returns a copy of the parent context, which will be canceled when the shutdown signal arrives.

func NotifyReconfigureSignal added in v0.30.3

func NotifyReconfigureSignal(n SignalNotifier, c chan<- os.Signal)

NotifyReconfigureSignal notifies c on receiving reconfigure signals using n.

NOTE: It does nothing on Windows.

func NotifyShutdownSignal added in v0.23.2

func NotifyShutdownSignal(n SignalNotifier, c chan<- os.Signal)

NotifyShutdownSignal notifies c on receiving shutdown signals using n.

func RootDirFS

func RootDirFS() (fsys fs.FS)

RootDirFS returns a filesystem rooted at the system's root directory.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/osutil"
)

func main() {
	fsys := osutil.RootDirFS()

	d, err := fsys.Open(".")
	if err != nil {
		fmt.Printf("opening: %v\n", err)

		return
	}

	fi, err := d.Stat()
	if err != nil {
		fmt.Printf("getting info: %v\n", err)

		return
	}

	fmt.Printf("is dir: %t\n", fi.IsDir())

	err = d.Close()
	if err != nil {
		fmt.Printf("closing: %v\n", err)

		return
	}

}
Output:
is dir: true

Types

type ContextSignalNotifier added in v0.35.11

type ContextSignalNotifier interface {
	SignalNotifier

	// NotifyContext returns a copy of the parent context that will be canceled
	// when one of the given signals arrives, when the stop function is called,
	// or when the parent context is marked done.  This depends on which event
	// happens first.
	//
	// See also [signal.NotifyContext].
	NotifyContext(
		parent context.Context,
		sig ...os.Signal,
	) (ctx context.Context, stop context.CancelFunc)
}

ContextSignalNotifier extends the SignalNotifier interface allowing to handle signals with context cancellation.

type DefaultSignalNotifier added in v0.20.0

type DefaultSignalNotifier struct{}

DefaultSignalNotifier is a ContextSignalNotifier that uses signal.Notify, signal.Stop and signal.NotifyContext.

func (DefaultSignalNotifier) Notify added in v0.20.0

func (n DefaultSignalNotifier) Notify(c chan<- os.Signal, sig ...os.Signal)

Notify implements the ContextSignalNotifier interface for DefaultSignalNotifier.

func (DefaultSignalNotifier) NotifyContext added in v0.35.11

func (n DefaultSignalNotifier) NotifyContext(
	parent context.Context,
	sig ...os.Signal,
) (ctx context.Context, stop context.CancelFunc)

NotifyContext implements the ContextSignalNotifier interface for DefaultSignalHandler.

func (DefaultSignalNotifier) Stop added in v0.20.0

func (n DefaultSignalNotifier) Stop(c chan<- os.Signal)

Stop implements the ContextSignalNotifier interface for DefaultSignalNotifier.

type EmptySignalNotifier added in v0.35.11

type EmptySignalNotifier struct{}

EmptySignalNotifier is a ContextSignalNotifier that does nothing.

func (EmptySignalNotifier) Notify added in v0.35.11

func (n EmptySignalNotifier) Notify(c chan<- os.Signal, sig ...os.Signal)

Notify implements the ContextSignalNotifier interface for EmptySignalNotifier.

func (EmptySignalNotifier) NotifyContext added in v0.35.11

func (n EmptySignalNotifier) NotifyContext(
	parent context.Context,
	sig ...os.Signal,
) (ctx context.Context, stop context.CancelFunc)

NotifyContext implements the ContextSignalNotifier interface for EmptySignalNotifier.

func (EmptySignalNotifier) Stop added in v0.35.11

func (n EmptySignalNotifier) Stop(c chan<- os.Signal)

Stop implements the ContextSignalNotifier interface for EmptySignalNotifier.

type ExitCode added in v0.20.0

type ExitCode = int

ExitCode is a semantic alias for int when it's used as an exit code.

const (
	ExitCodeSuccess       ExitCode = 0
	ExitCodeFailure       ExitCode = 1
	ExitCodeArgumentError ExitCode = 2
)

Exit status constants.

type ExitFunc added in v0.35.12

type ExitFunc func(code int)

ExitFunc represents a function that is used to exit the application. Note, that os.Exit has the same signature.

type SignalNotifier added in v0.20.0

type SignalNotifier interface {
	// Notify starts relaying incoming signals to c.  If no signals are
	// provided, all incoming signals are relayed to c.  Otherwise, just the
	// provided signals are.
	//
	// Implementations will not block sending to c: the caller must ensure that
	// c has sufficient buffer space to keep up with the expected signal rate.
	// For a channel used for notification of just one signal value, a buffer of
	// size 1 is sufficient.
	//
	// It is allowed to call Notify multiple times with the same channel: each
	// call expands the set of signals sent to that channel.  The only way to
	// remove signals from the set is to call Stop.
	//
	// It is allowed to call Notify multiple times with different channels and
	// the same signals: each channel receives copies of incoming signals
	// independently.
	//
	// See also [signal.Notify].
	Notify(c chan<- os.Signal, sig ...os.Signal)

	// Stop causes the SignalNotifier to stop relaying incoming signals to c.
	// It undoes the effect of all prior calls to Notify using c.  When Stop
	// returns, it c must not receive any more signals.
	//
	// See also [signal.Stop].
	Stop(c chan<- os.Signal)
}

SignalNotifier is the interface for OS functions that can notify about incoming signals using a channel.

Directories

Path Synopsis
Package executil contains utilities for running OS commands.
Package executil contains utilities for running OS commands.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL