Skip to content

Commit

Permalink
chore: wip - writing output to file
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Sep 17, 2024
1 parent 19a58fe commit ee715ad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
38 changes: 23 additions & 15 deletions internal/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,39 @@ func Create(opts *CreateOpts, log *zerolog.Logger) error {

cloneFlags = cloneFlags | flag
}

var capabilityFlags []uintptr
// for _, cap := range spec.Process.Capabilities.Ambient {
// capabilityFlags = append(capabilityFlags, uintptr(pkg.Capabilities[cap]))
// }

log.Info().Msg("set sysprocattr")
// apply configuration, e.g. devices, proc, etc...
forkCmd.SysProcAttr = &syscall.SysProcAttr{
AmbientCaps: capabilityFlags,
// TODO: presumably this should be clone flags from namespaces in the config spec??
Cloneflags: cloneFlags,
// Cloneflags: syscall.CLONE_NEWUTS |
// syscall.CLONE_NEWPID |
// syscall.CLONE_NEWUSER |
// syscall.CLONE_NEWNET |
// syscall.CLONE_NEWNS,
Unshareflags: syscall.CLONE_NEWNS,
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: int(spec.Process.User.UID),
HostID: os.Geteuid(),
Size: 1,
},
},
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: int(spec.Process.User.GID),
HostID: os.Getegid(),
Size: 1,
},
},
Unshareflags: syscall.CLONE_NEWNS,
GidMappingsEnableSetgroups: false,
// UidMappings: []syscall.SysProcIDMap{
// {
// ContainerID: int(spec.Process.User.UID),
// HostID: os.Geteuid(),
// Size: 1,
// },
// },
// GidMappings: []syscall.SysProcIDMap{
// {
// ContainerID: int(spec.Process.User.GID),
// HostID: os.Getegid(),
// Size: 1,
// },
// },
}

forkCmd.Env = spec.Process.Env
Expand Down
29 changes: 24 additions & 5 deletions internal/commands/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
"golang.org/x/sys/unix"
)

func server(conn net.Conn, spec specs.Spec) {
func server(conn net.Conn, containerID string, spec specs.Spec, log *zerolog.Logger) {
defer conn.Close()

b := make([]byte, 128)

for {
log.Info().Msg("reading in fork...")
n, err := conn.Read(b)
if err != nil {
// TODO: log it
Expand All @@ -37,12 +38,31 @@ func server(conn net.Conn, spec specs.Spec) {

switch string(b[:n]) {
case "start":
log.Info().Msg("fork received 'start' command")
log.Info().Str("command", spec.Process.Args[0]).Msg("command")
log.Info().Str("args", strings.Join(spec.Process.Args[:1], ",")).Msg("args")
cmd := exec.Command(spec.Process.Args[0], spec.Process.Args[1:]...)
cmd.Stdout = conn
cmd.Stderr = conn

log.Info().Msg("opening out.txt for writing")
f, err := os.OpenFile(
"out.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
0644,
)
if err != nil {
log.Error().Err(err).Msg("open out file")
}
abs, _ := filepath.Abs(f.Name())
log.Info().Str("name", abs).Msg("out file")
cmd.Stdout = f
cmd.Stderr = f
if err := cmd.Run(); err != nil {
log.Error().Err(err).Msg("error executing underlying command")
conn.Write([]byte(err.Error()))
} else {
conn.Write([]byte("apparently executed successfully???"))
}

return
}
}
Expand Down Expand Up @@ -261,8 +281,7 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {
continue
}

go server(containerConn, spec)
go server(containerConn, opts.ID, spec, log)
}

return nil
}
16 changes: 10 additions & 6 deletions internal/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func Start(opts *StartOpts, log *zerolog.Logger) error {
return fmt.Errorf("send start over ipc: %w", err)
}

state.Status = specs.StateRunning
if err := internal.SaveState(state); err != nil {
log.Error().Err(err).Msg("failed to save state")
}

log.Info().Msg("reading from connection")
b, err := io.ReadAll(conn)
if err != nil {
Expand All @@ -84,12 +89,6 @@ func Start(opts *StartOpts, log *zerolog.Logger) error {
// presumably this needs to be redirected to the pty (if specified in config)?
log.Info().Str("output", string(b)).Msg("run command output")
fmt.Fprint(os.Stdout, string(b))
f, _ := os.OpenFile(
"out.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
0644,
)
f.Write(b)
conn.Write(b)

// 9. Invoke poststart hooks
Expand All @@ -101,5 +100,10 @@ func Start(opts *StartOpts, log *zerolog.Logger) error {
}
}

state.Status = specs.StateStopped
if err := internal.SaveState(state); err != nil {
return fmt.Errorf("save state: %w", err)
}

return nil
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func main() {

log := zerolog.New(logfile).With().Timestamp().Logger()

if err := cmd.RootCmd(&log).Execute(); err != nil {
rootCmd := cmd.RootCmd(&log)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/capabilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pkg

var Capabilities = map[string]uintptr{}

0 comments on commit ee715ad

Please sign in to comment.