This repository has been archived by the owner on May 27, 2021. It is now read-only.
/
actions
140 lines (112 loc) · 3.28 KB
/
actions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
####################################################################
# Prey Core Action Functions - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################
# modules can queue specific actions by calling
# add_action 'module_name' 'function_name'
add_action(){
log " ++ Queueing action for $1 module!"
if [ -z "$2" ]; then
function=1
else
function=$2
fi
actions[${#actions[*]}]="${1}=${function}"
}
# sets the running pid to the action/module name
add_running_action(){
running_actions[${#running_actions[*]}]="${1}=${2}"
}
get_action(){
get_var actions $1
}
# gets the pid of the running process
get_running_action(){
get_var running_actions $1
}
action_callback(){
until read -st 1; do
echo -n ""
done
log " -- Action finished. Running callback!"
eval '$1 $REPLY'
}
# receives the pid name, assigns it to the module name
create_pidfile_for(){
log " -- Creating pidfile for $current_module's $1..."
echo $1 > "$tmpbase/prey-$current_module.pid"
}
unload_action(){
log " -- Unloading $1 from memory..."
local pidfile="$tmpbase/prey-$1.pid"
kill -HUP -- -`cat $pidfile` &> /dev/null
}
remove_pidfile(){
local pidfile="$tmpbase/prey-$1.pid"
if [ -f "$pidfile" ]; then
log ' -- Removing pidfile...'
rm -f "$pidfile" 2> /dev/null
else
log " -- Pidfile doesn't exist!"
fi
}
check_running_actions(){
for pidfile in `ls $tmpbase/prey-*.pid 2> /dev/null`; do
if [ ! -f $pidfile ]; then
continue # just to make sure
fi
local action_name=`basename $pidfile | sed -e 's/prey-//' -e 's/\.pid//'`
log " -- Found running action: $action_name"
if [ -z `get_action $action_name` ]; then # action was removed from options
# log " -- Removing $action_name which was untoggled by user..."
unload_action $action_name
sleep 1
else
add_running_action $action_name `cat $pidfile`
fi
done
}
action_finished(){
if [[ -n "$2" && `get_running_action $2` == $1 ]]; then
wait $1
return_status=$?
log " -- Action $2 exited with status $return_status."
remove_pidfile $2
fi
}
run_pending_actions(){
if [ "${#actions[*]}" -gt 0 ]; then
# -b -> Cause the status of terminated background jobs to be reported immediately,
# rather than before printing the next primary prompt.
# -m job control enabled
set -bm
log -e "\n${bold} -- Running pending actions...${bold_end}\n"
for action in "${actions[@]}"; do
local current_module=`echo $action | sed 's/=.*//'`
local function=`echo $action | sed 's/.*=\(.*\)/\1/'`
if [ ! -z `get_running_action $current_module` ]; then
log " -- $current_module is already running! Skipping..."
continue
fi
initialize_module $current_module
if [ $? == 1 ]; then
continue
fi
if [ "$function" != "1" ]; then # specific function requested
log " -- Running action $function from $current_module module."
eval "$function" &
else # no specific action requested, we'll assume there's a run file
log "\n${bold} >> Running $current_module module!${bold_end}\n"
. "$module_path/core/run" &
fi
pid=$!
create_pidfile_for $pid
add_running_action $current_module $pid
trap 'action_finished $pid $current_module' SIGCHLD
done
wait
log " -- No pending actions left. All done!\n"
fi
}