@@ -7,9 +7,11 @@ import (
7
7
_ "embed"
8
8
"fmt"
9
9
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
10
+ "io"
10
11
"log/slog"
11
12
"os"
12
13
"os/exec"
14
+ "path"
13
15
"path/filepath"
14
16
"time"
15
17
)
@@ -18,11 +20,17 @@ const LocalBuildProcessorName = "local"
18
20
19
21
type LocalBuildProcessor struct {
20
22
timeout int
23
+ useDKMS bool
24
+ srcDir string
25
+ envMap map [string ]string
21
26
}
22
27
23
- func NewLocalBuildProcessor (timeout int ) * LocalBuildProcessor {
28
+ func NewLocalBuildProcessor (timeout int , useDKMS bool , srcDir string , envMap map [ string ] string ) * LocalBuildProcessor {
24
29
return & LocalBuildProcessor {
25
30
timeout : timeout ,
31
+ useDKMS : useDKMS ,
32
+ srcDir : srcDir ,
33
+ envMap : envMap ,
26
34
}
27
35
}
28
36
@@ -104,6 +112,11 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
104
112
105
113
// Cannot fail
106
114
vv , _ := v .(* builder.LocalBuilder )
115
+ vv .SrcDir = lbp .srcDir
116
+ vv .UseDKMS = lbp .useDKMS
117
+
118
+ modulePath := vv .GetModuleFullPath (c , kr )
119
+ probePath := path .Join (vv .GetDriverBuildDir (), "bpf" , builder .ProbeFileName )
107
120
for _ , gcc := range gccs {
108
121
vv .GccPath = gcc
109
122
@@ -115,6 +128,10 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
115
128
ctx , cancelFunc := context .WithTimeout (context .Background (), time .Duration (lbp .timeout )* time .Second )
116
129
defer cancelFunc ()
117
130
cmd := exec .CommandContext (ctx , "/bin/bash" , "-c" , driverkitScript )
131
+ // Append requested env variables to the command env
132
+ for key , val := range lbp .envMap {
133
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , key , val ))
134
+ }
118
135
stdout , err := cmd .StdoutPipe ()
119
136
if err != nil {
120
137
slog .Warn ("Failed to pipe output. Trying without piping." , "err" , err )
@@ -139,28 +156,52 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
139
156
}
140
157
// If we received an error, perhaps we must just rebuilt the kmod.
141
158
// Check if we were able to build anything.
142
- if _ , err = os . Stat ( builder . ModuleFullPath ); ! os . IsNotExist ( err ) {
143
- // we built the kmod; there is no need to loop again.
159
+ koFiles , err := filepath . Glob ( modulePath )
160
+ if err == nil && len ( koFiles ) > 0 {
144
161
break
145
162
}
146
- if _ , err = os .Stat (builder . ProbeFullPath ); ! os .IsNotExist (err ) {
163
+ if _ , err = os .Stat (probePath ); ! os .IsNotExist (err ) {
147
164
c .ProbeFilePath = ""
148
165
}
149
166
}
150
167
151
168
if len (b .ModuleFilePath ) > 0 {
152
- if err = os .Rename (builder .ModuleFullPath , b .ModuleFilePath ); err != nil {
169
+ // If we received an error, perhaps we must just rebuilt the kmod.
170
+ // Check if we were able to build anything.
171
+ koFiles , err := filepath .Glob (modulePath )
172
+ if err != nil || len (koFiles ) == 0 {
173
+ return fmt .Errorf ("failed to find kernel module .ko file: %s" , modulePath )
174
+ }
175
+ if err = copyDataToLocalPath (koFiles [0 ], b .ModuleFilePath ); err != nil {
153
176
return err
154
177
}
155
178
slog .With ("path" , b .ModuleFilePath ).Info ("kernel module available" )
156
179
}
157
180
158
181
if len (b .ProbeFilePath ) > 0 {
159
- if err = os . Rename ( builder . ProbeFullPath , b .ProbeFilePath ); err != nil {
182
+ if err = copyDataToLocalPath ( probePath , b .ProbeFilePath ); err != nil {
160
183
return err
161
184
}
162
185
slog .With ("path" , b .ProbeFilePath ).Info ("eBPF probe available" )
163
186
}
164
187
165
188
return nil
166
189
}
190
+
191
+ func copyDataToLocalPath (src , dest string ) error {
192
+ in , err := os .Open (filepath .Clean (src ))
193
+ if err != nil {
194
+ return err
195
+ }
196
+ defer in .Close ()
197
+ err = os .MkdirAll (filepath .Dir (dest ), 0o755 )
198
+ if err != nil {
199
+ return err
200
+ }
201
+ out , err := os .OpenFile (filepath .Clean (dest ), os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o755 )
202
+ if err == nil {
203
+ defer out .Close ()
204
+ _ , err = io .Copy (out , in )
205
+ }
206
+ return err
207
+ }
0 commit comments