Skip to content

Commit

Permalink
Syscall functions are kept in helpers.go file for more redability and…
Browse files Browse the repository at this point in the history
… reusability

Signed-off-by: GLVS Kiriti <[email protected]>
  • Loading branch information
GLVSKiriti committed Jun 17, 2024
1 parent c09117e commit fe51d56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
37 changes: 37 additions & 0 deletions pkg/declarative/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2024 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package declarative

import (
"fmt"

"golang.org/x/sys/unix"
)

func WriteSyscall(filepath string, content string) error {
// Open the file using unix.Open syscall
fd, err := unix.Open(filepath, unix.O_WRONLY|unix.O_CREAT, 0644)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer unix.Close(fd)

// Write to the file using unix.Write
_, err = unix.Write(fd, []byte(content))
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
}
return nil
}
18 changes: 2 additions & 16 deletions pkg/declarative/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package declarative
import (
"fmt"
"os/exec"

"golang.org/x/sys/unix"
)

// Common runner interface for runners like hostrunner, container-runner etc..
Expand All @@ -42,20 +40,8 @@ func (r *Hostrunner) Setup(beforeScript string) error {
func (r *Hostrunner) ExecuteStep(step SyscallStep) error {
switch step.Syscall {
case "write":
filepath := step.Args["filepath"]
content := step.Args["content"]

// Open the file using unix.Open syscall
fd, err := unix.Open(filepath, unix.O_WRONLY|unix.O_CREAT, 0644)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer unix.Close(fd)

// Write to the file using unix.Write
_, err = unix.Write(fd, []byte(content))
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
if err := WriteSyscall(step.Args["filepath"], step.Args["content"]); err != nil {
return fmt.Errorf("write syscall failed with error: %v", err)
}
default:
return fmt.Errorf("unsupported syscall: %s", step.Syscall)
Expand Down

0 comments on commit fe51d56

Please sign in to comment.