Skip to content

Commit

Permalink
Attach c binaries inside go program
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonB committed Jul 19, 2015
1 parent 33fc35c commit f0dbe18
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
shmwrapper1.exe
shmwrapper2.bin
assets/*
!assets/.gitkeep
bindata.go
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
all: shmwrapper1.exe shmwrapper2.bin
all: assets/shmwrapper1.exe assets/shmwrapper2.bin bindata.go

CC=gcc
WINECC=i686-w64-mingw32-gcc
CFLAGS=-Wall -Os -g

shmwrapper2.bin: shmwrapper2.c
assets/shmwrapper2.bin: shmwrapper2.c
$(CC) $< $(CFLAGS) -o $@

shmwrapper1.exe: shmwrapper1.c
assets/shmwrapper1.exe: shmwrapper1.c
$(WINECC) $< $(CFLAGS) -mconsole -o $@

bindata.go: assets/shmwrapper1.exe assets/shmwrapper2.bin
go-bindata -pkg=wineshm -ignore=.gitkeep assets/

clean:
rm -f shmwrapper1.exe shmwrapper2.bin
rm -f assets/shmwrapper1.exe assets/shmwrapper2.bin bindata.go

# vim: syntax=make ts=4 sw=4 sts=4 sr noet
Empty file added assets/.gitkeep
Empty file.
64 changes: 64 additions & 0 deletions bin/test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"log"
"os"
"unsafe"

mmap "github.com/edsrzf/mmap-go"
wineshm "github.com/leonb/wineshm-go"
)

type StatusField int32

const (
MAX_BUFS = 4
)

type Header struct {
Ver int32 // api version 1 for now
Status StatusField // bitfield using StatusField
TickRate int32 // ticks per second (60 or 360 etc)

// session information, updated periodicaly
SessionInfoUpdate int32 // Incremented when session info changes
SessionInfoLen int32 // Length in bytes of session info string
SessionInfoOffset int32 // Session info, encoded in YAML format

// State data, output at tickRate
NumVars int32 // length of array pointed to by varHeaderOffset
VarHeaderOffset int32 // offset to VarHeader[numVars] array, Describes the variables recieved in varBuf

NumBuf int32 // <= MAX_BUFS (3 for now)
BufLen int32 // length in bytes for one line
Pad1 [2]int32 // (16 byte align)
VarBuf [MAX_BUFS]VarBuf
}

type VarBuf struct {
TickCount int32 // used to detect changes in data
BufOffset int32 // offset from header
Pad [2]int32 // (16 byte align)
}

func main() {
// Get wine file descriptor
wineshm.WineCmd = []string{"/opt/iracing/bin/wine", "--bottle", "default"}
shmfd, err := wineshm.GetWineShm("Local\\IRSDKMemMapFileName", wineshm.FILE_MAP_READ)
if err != nil {
log.Fatal(err)
}

// Turn file descriptor into os.File
file := os.NewFile(shmfd, "Local\\IRSDKMemMapFileName")

mmap, err := mmap.Map(file, mmap.RDONLY, 0)
if err != nil {
log.Fatalf("error mapping: %s", err)
}

fmt.Println(len(mmap))
header := (*Header)(unsafe.Pointer(&mmap))
fmt.Printf("%+v", header)
}
45 changes: 43 additions & 2 deletions wineshm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
Expand All @@ -15,6 +16,7 @@ import (
)

const (
TMP_PREFIX = "wineshm-go"
FILE_MAP_READ = "r"
FILE_MAP_WRITE = "w"
SOCKET_TIMEOUT = 5 * time.Second
Expand All @@ -38,16 +40,28 @@ func GetWineShm(shmname string, mode string) (uintptr, error) {
defer unix.Close(fds[0])
defer unix.Close(fds[1])

shmwrapper1Path, err := lookPath("shmwrapper1.exe")
// Create the first wrapper from binary data in bindata.go and place in in
// the systems temp directory
shmwrapper1, err := getAsset("assets/shmwrapper1.exe")
if err != nil {
return 0, err
}
shmwrapper1Path := shmwrapper1.Name()
defer os.Remove(shmwrapper1Path)

shmwrapper2Path, err := lookPath("shmwrapper2.bin")
// Create the second wrapper from binary data in bindata.go and place in in
// the systems temp directory
shmwrapper2, err := getAsset("assets/shmwrapper2.bin")
if err != nil {
return 0, err
}
shmwrapper2Path := shmwrapper2.Name()
// This is tricky: on some distro's the temp directory doesn't allow for
// running executables
os.Chmod(shmwrapper2Path, 0500)
defer os.Remove(shmwrapper2Path)

// Lookup the wine location of the wine binary
winePath, err := lookPath(WineCmd[0])
if err != nil {
return 0, err
Expand Down Expand Up @@ -104,6 +118,7 @@ func GetWineShm(shmname string, mode string) (uintptr, error) {
// Retrieve message on socket
_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
if closeUnix.Stop() == false {
fmt.Println(stderr)
return 0, ErrSockTimeout
}

Expand Down Expand Up @@ -132,5 +147,31 @@ func lookPath(file string) (string, error) {
return path, nil
}

gopath := os.Getenv("GOPATH")
path, err = exec.LookPath(gopath + "/src/github.com/leonb/wineshm-go/" + file)
if err == nil {
return path, nil
}

return exec.LookPath(file)
}

func getAsset(assetName string) (*os.File, error) {
f, err := ioutil.TempFile("", TMP_PREFIX)
defer f.Close()
if err != nil {
return nil, err
}

data, err := Asset(assetName)
if err != nil {
return nil, err
}

_, err = f.Write(data)
if err != nil {
return nil, err
}

return f, nil
}

0 comments on commit f0dbe18

Please sign in to comment.