procgroup

package module
v0.0.0-...-aff12aa Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

go-procgroup

Group your child processes and kill them at once.

Usage

The object created by group.NewCmd() can be used as a drop-in of exec.Cmd.

package main

import (
	"github.com/jamesits/go-procgroup"
	"os"
	"path/filepath"
	"time"
)

func main() {
	// create a new process group
	g, err := procgroup.NewGroup()
	if err != nil {
		panic(err)
	}
	
	// create a new process in the group and launch it
	c, err := g.NewCmd()
	if err != nil {
		panic(err)
	}
	c.Path = filepath.Join(os.Getenv("WINDIR"), "system32", "cmd.exe")
	c.Args = []string{"cmd.exe", "/c", "notepad.exe"} // instruct CMD to create a subprocess
	c.Stdin = os.Stdin
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	err = c.Start()
	if err != nil {
		panic(err)
	}
	defer c.Wait() // you still need to `Wait` for it so that Golang runtime does not leak memory

	// wait a while
	time.Sleep(3 * time.Second)
	
	// kill all the processes and their child processes in this group
	err = g.Terminate(0)
	if err != nil {
		panic(err)
	}
}

Caveats

This library does not offer any level of security, and process grouping is not a security boundary. There is no guarantee that a malicious process cannot break away from its process group. The library only offers a better abstraction for managing long-running service processes and their own child processes.

To ensure a clean cleanup, the child processes you start will be killed automatically if the parent process dies.

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	exec.Cmd
	// contains filtered or unexported fields
}

func (*Cmd) Run

func (c *Cmd) Run() error

func (*Cmd) Start

func (c *Cmd) Start() (err error)

type Group

type Group struct {
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup() (ret *Group, err error)

func (*Group) NewCmd

func (g *Group) NewCmd() (*Cmd, error)

func (*Group) Terminate

func (g *Group) Terminate(exitCode uint32) error

Jump to

Keyboard shortcuts

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