some changes

This commit is contained in:
bfu 2021-12-01 18:56:14 -05:00
parent 69eb4d4642
commit 3025c43c1e
No known key found for this signature in database
GPG Key ID: FD1D952871D22043
5 changed files with 90 additions and 22 deletions

0
go.sum Normal file
View File

View File

@ -1,13 +1,13 @@
package leech
import "sync"
import (
"sync"
)
// The bug is a bare-bones interface
// describing basic methods that a Leech
// should exhibit.
type bug interface {
// SetPid will set the process id.
SetPid(pid int)
// Pid will return a process id.
Pid() int
// Process will return a pointer
@ -18,6 +18,10 @@ type bug interface {
ProcStatus() Status
// Meta will return the process metadata.
Meta() *Meta
// Start will start the process.
Start() bool
// TODO: (@bfu4) Can we pipe into a terminal?
// Attach(*term.Terminal)
}
type Leech struct {
@ -29,6 +33,30 @@ type Leech struct {
mu *sync.Mutex
}
// SetProcData will set a leech's current process data.
func (l *Leech) SetProcData(data *ProcessData) {
if l.meta.status > Dead {
// Seriously, dude.
return
}
// Let's not allow access to ~anyone~ during this operation.
l.mu.Lock()
l.meta.process.data = data
l.mu.Unlock()
}
// Start will start a leech's process.
func (l *Leech) Start() bool {
l.mu.Lock()
defer l.mu.Unlock()
l.meta.Process().Daemonize()
if l.meta.process.status > Zombie {
l.meta.pid = l.meta.Process().internal.Pid
l.pid = l.meta.pid
}
return l.meta.Process().IsDaemon()
}
// Meta returns the metadata.
func (l *Leech) Meta() *Meta {
return l.meta
@ -38,18 +66,3 @@ func (l *Leech) Meta() *Meta {
func (l *Leech) Pid() int {
return l.pid
}
// SetPid will set the process id of the leech
// if and only if the leech is not already a daemon
// and nothing is accessing the leech.
func (l *Leech) SetPid(pid int) {
// Lock the mutex.
l.mu.Lock()
if !l.meta.process.IsDaemon() {
// TODO(@bfu): Handle process as well.
l.pid = pid
l.meta.pid = pid
}
// Unlock the synchronization mutex.
defer l.mu.Unlock()
}

View File

@ -1,5 +1,7 @@
package leech
// PermData
// TODO:(@bfu4) We have to actually use this.
type PermData struct {
owner int
access int

View File

@ -6,23 +6,49 @@ import (
)
var (
// cwd is the current working directory of an executable.
cwd, _ = os.Executable()
)
// The Process struct is a structure
// defining a basic process that a leech will
// execute and that may be eventually hooked in to.
type Process struct {
// The status of the process, whether it is a daemon or not.
isDaemon bool
mu *sync.Mutex
wd string
status Status
// The synchronization mutex.
mu *sync.Mutex
// The execution working directory. If it is not specified
// at struct creation, the wd becomes the current working directory (cwd).
wd string
// The actual status of the process.
// TODO: (@bfu4) we need a hook to handle process lifecycle events.
status Status
// The data used in the creation of the process.
data *ProcessData
// The internal golang process.
internal *os.Process
}
// ProcessData is a simple structure
// defining data needed to create
// the actual process executed.
type ProcessData struct {
// The name of the executable.
Name string
// The arguments.
Argv []string
// Other information.
Attr *os.ProcAttr
}
func At(wd string) *Leech {
proc := &Process{
isDaemon: false,
mu: &sync.Mutex{},
wd: wd,
status: Inactive,
data: nil,
}
meta := &Meta{
pid: -1,
@ -32,17 +58,42 @@ func At(wd string) *Leech {
return &Leech{meta: meta, pid: meta.pid}
}
func AtOf(wd string, data *ProcessData) (leech *Leech) {
leech = At(wd)
leech.meta.process.data = data
return
}
func Of(data *ProcessData) (leech *Leech) {
leech = New()
leech.meta.process.data = data
return
}
func New() *Leech {
return At(cwd)
}
func (p *Process) start() bool {
if p.status > Zombie {
return false
}
p.mu.Lock()
defer p.mu.Unlock()
var err error
p.internal, err = os.StartProcess(p.data.Name, p.data.Argv, p.data.Attr)
if err != nil {
p.status = Inactive
}
p.status = Alive
return p.status > Dead
}
// Daemonize will daemonize the process.
func (p *Process) Daemonize() bool {
p.mu.Lock()
defer p.mu.Unlock()
p.start()
p.isDaemon = true
return p.isDaemon
}

View File

@ -2,4 +2,6 @@ leech
---------------------
a process attachment library, with the goal of allowing each instance
to run as a daemon, detached process
to run as a daemon, detached process
also, none if this is tested as of last commit.