Skip to content

Instantly share code, notes, and snippets.

View 0xc0d's full-sized avatar

Ali Josie 0xc0d

View GitHub Profile
@0xc0d
0xc0d / main.go
Created September 7, 2021 12:36
CPU Cache Line vs Memory Speed
package main
import (
"fmt"
"time"
)
type BigStruct struct {
_ int64
_ int64
@0xc0d
0xc0d / newNetworkNamespace.go
Last active November 6, 2020 21:12
Create new network namespace without any process member
func MountNewNetworkNamespace(nsTarget string) (filesystem.Unmounter, error) {
_, err := os.OpenFile(nsTarget, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_EXCL, 0644)
if err != nil {
return nil, errors.Wrap(err, "unable to create target file")
}
// store current network namespace
file, err = os.OpenFile("/proc/self/ns/net", os.O_RDONLY, 0)
if err != nil {
return nil, err
@0xc0d
0xc0d / networkns.go
Last active November 6, 2020 19:51
creating network for container
func (c *Container) SetupNetwork(bridge string) (filesystem.Unmounter, error) {
nsMountTarget := filepath.Join(netnsPath, c.Digest)
vethName := fmt.Sprintf("veth%.7s", c.Digest)
peerName := fmt.Sprintf("P%s", vethName)
if err := network.SetupVirtualEthernet(vethName, peerName); err != nil {
return nil, err
}
if err := network.LinkSetMaster(vethName, bridge); err != nil {
return nil, err
@0xc0d
0xc0d / reexec.go
Last active November 6, 2020 01:13
reexec is something like clone.
args := []string{"fork"}
...
cmd := reexec.Command(args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS |
syscall.CLONE_NEWIPC |
syscall.CLONE_NEWPID |
syscall.CLONE_NEWNS,
@0xc0d
0xc0d / unshare.go
Last active November 5, 2020 23:30
change current process namespace
err := syscall.Unshare(syscall.CLONE_NEWPID|syscall.CLONE_NEWUTS)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
err = syscall.Sethostname([]byte("container"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
cmd := exec.Command("/bin/sh")
cmd.Stdin = os.Stdin
@0xc0d
0xc0d / http.go
Last active October 27, 2020 19:33
simple buggy http server
import (
"encoding/json"
"math/rand"
"net/http"
_ "net/http/pprof"
"time"
)
func main() {
http.HandleFunc("/log", logHandler)
@0xc0d
0xc0d / allProfiles.go
Created October 27, 2020 14:58
Different profiles in pkg/profile
// CPUProfile enables cpu profiling. Note: Default is CPU
defer profile.Start(profile.CPUProfile).Stop()
// GoroutineProfile enables goroutine profiling.
// It returns all Goroutines alive when defer occurs.
defer profile.Start(profile.GoroutineProfile).Stop()
// BlockProfile enables block (contention) profiling.
defer profile.Start(profile.BlockProfile).Stop()
@0xc0d
0xc0d / profile.go
Created October 27, 2020 14:35
Dave Cheney Profile
func main() {
defer profile.Start(profile.ProfilePath(".")).Stop()
// do something
}
@0xc0d
0xc0d / server.go
Created October 27, 2020 14:09
Run http server
func init() {
go func() {
http.ListenAndServe(":1234", nil)
}()
}
@0xc0d
0xc0d / init.go
Created October 27, 2020 14:00
net http pprof init
func init() {
http.HandleFunc("/debug/pprof/", Index)
http.HandleFunc("/debug/pprof/cmdline", Cmdline)
http.HandleFunc("/debug/pprof/profile", Profile)
http.HandleFunc("/debug/pprof/symbol", Symbol)
http.HandleFunc("/debug/pprof/trace", Trace)
}