Beispiel #1
0
func getLink() string {
	l, err := os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
	if err != nil {
		return fmt.Sprintf("(nil: %v)", err)
	}

	return l
}
Beispiel #2
0
func init() {
	// Record pid and tid of init thread for use during test.
	// The call to LockOSThread is just to exercise it;
	// we can't test that it does anything.
	// Instead we're testing that the conditions are good
	// for how it is used in init (must be on main thread).
	pid, tid = syscall.Getpid(), syscall.Gettid()
	LockOSThread()
}
Beispiel #3
0
func (f *FakeSystem) Update(timestep time.Duration) (stop bool, err error) {
	f.updateTids = AppendTid(f.updateTids, syscall.Gettid())

	f.updateCount++
	if f.updateCount >= f.maxUpdates {
		stop = true
	}

	f.totalTimeMs += float64(timestep.Nanoseconds()) / 1000000

	runtime.Gosched()

	stop = stop || f.updateReturnStop
	err = f.updateReturnErr
	return
}
Beispiel #4
0
func suspend(g *godit) {
	// finalize termbox
	termbox.Close()

	// suspend the process
	pid := syscall.Getpid()
	tid := syscall.Gettid()
	err := syscall.Tgkill(pid, tid, syscall.SIGSTOP)
	if err != nil {
		panic(err)
	}

	// reset the state so we can get back to work again
	err = termbox.Init()
	if err != nil {
		panic(err)
	}
	termbox.SetInputMode(termbox.InputAlt)
	g.resize()
}
Beispiel #5
0
func main() {
	fmt.Println("--------", runtime.GOMAXPROCS(runtime.NumCPU()), runtime.NumCPU())

	for i := 0; i < 10; i++ {
		go func(id int) {
			ch := time.Tick(time.Second * 4)
			for now := range ch {
				// bounce bettween many threads => a goroutine can be run by any real thread
				fmt.Printf("%v pid: %d:%d\n", now, syscall.Gettid(), id)
			}

			// for j := 0; j < 10; j++ {
			// 	fmt.Println("pid is:", )
			// 	time.Sleep(time.Second * 3)
			// }
		}(i)
	}

	time.Sleep(time.Second * 300)
}
Beispiel #6
0
func testThreadLock(t *testing.T) {
	stop := make(chan int)
	go func() {
		// We need the G continue running,
		// so the M has a chance to run this G.
		for {
			select {
			case <-stop:
				return
			case <-time.After(time.Millisecond * 100):
			}
		}
	}()
	defer close(stop)

	for i := 0; i < 1000; i++ {
		if C.int(syscall.Gettid()) != C.Ctid() {
			t.Fatalf("cgo has not locked OS thread")
		}
	}
}
Beispiel #7
0
func find_unused_thread(tries int, wg *sync.WaitGroup, reg *pidRegistry, worker func()) {
	for i := 0; i <= tries; i++ {
		reg.lock.Lock()
		runtime.LockOSThread()
		pid := syscall.Gettid()
		if _, taken := reg.pids[pid]; !taken {
			reg.pids[pid] = true
			reg.lock.Unlock()
			runtime.Gosched() //block to aid context switching
			worker()
			reg.lock.Lock()
			delete(reg.pids, pid)
			reg.lock.Unlock()
			wg.Done()
			return
		} else {
			reg.lock.Unlock()
			runtime.UnlockOSThread()
		}
	}
	log.Println("goroutine given up finding a new thread")
	wg.Done()
}
Beispiel #8
0
func createNetworkNamespace(path string, osCreate bool) (*Info, error) {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	origns, err := netns.Get()
	if err != nil {
		return nil, err
	}
	defer origns.Close()

	if err := createNamespaceFile(path); err != nil {
		return nil, err
	}

	if osCreate {
		defer netns.Set(origns)
		newns, err := netns.New()
		if err != nil {
			return nil, err
		}
		defer newns.Close()

		if err := loopbackUp(); err != nil {
			return nil, err
		}
	}

	procNet := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid())

	if err := syscall.Mount(procNet, path, "bind", syscall.MS_BIND, ""); err != nil {
		return nil, err
	}

	interfaces := []*Interface{}
	info := &Info{Interfaces: interfaces}
	return info, nil
}
Beispiel #9
0
func Setfscreatecon(scon string) error {
	return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid()), scon)
}
		It("locks and unlocks the os thread", func() {
			err := ns.Execute(func(*os.File) error {
				defer GinkgoRecover()
				Expect(threadLocker.LockOSThreadCallCount()).To(Equal(1))
				Expect(threadLocker.UnlockOSThreadCallCount()).To(Equal(0))
				return nil
			})
			Expect(err).NotTo(HaveOccurred())
			Expect(threadLocker.LockOSThreadCallCount()).To(Equal(1))
			Expect(threadLocker.UnlockOSThreadCallCount()).To(Equal(1))
		})

		It("runs the callback on a separate os task", func() {
			var ttid int
			err := ns.Execute(func(*os.File) error {
				ttid = syscall.Gettid()
				return nil
			})
			Expect(err).NotTo(HaveOccurred())

			Expect(syscall.Gettid()).NotTo(Equal(ttid))
		})

		Context("when the callback fails", func() {
			It("logs the error", func() {
				ns.Execute(func(*os.File) error { return errors.New("potato") })

				Expect(logger).To(gbytes.Say("execute.callback-failed.*potato"))
			})
		})
	})
Beispiel #11
0
func getLink() (string, error) {
	return os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
}
Beispiel #12
0
func (f *FakeSystem) Exit() {
	f.exitTids = AppendTid(f.exitTids, syscall.Gettid())

	f.exitCount++
}
Beispiel #13
0
// getThreadID returns an opaque value that is unique per OS thread
func getThreadID() uintptr {
	return uintptr(syscall.Gettid())
}
Beispiel #14
0
func Setexeccon(scon string) error {
	return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()), scon)
}
Beispiel #15
0
func Getfscreatecon() (string, error) {
	return readCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid()))
}
Beispiel #16
0
// Get gets a handle to the current threads network namespace.
func Get() (NsHandle, error) {
	return GetFromThread(os.Getpid(), syscall.Gettid())
}
Beispiel #17
0
func gettid() int {
	tid := syscall.Gettid()
	return tid
}
Beispiel #18
0
func GetThreadId() int {
	return syscall.Gettid()
}
Beispiel #19
0
func Getexeccon() (string, error) {
	return readCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()))
}
Beispiel #20
0
func (f *FakeSystem) Init() error {
	f.initTids = AppendTid(f.initTids, syscall.Gettid())

	return f.initReturn
}
Beispiel #21
0
func Gettid() int {
	return syscall.Gettid()
}
func taskNamespacePath() string {
	return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid())
}