Exemple #1
0
func getUnitFileName() (unit string, err error) {
	libname := C.CString("libsystemd.so")
	defer C.free(unsafe.Pointer(libname))
	handle := C.dlopen(libname, C.RTLD_LAZY)
	if handle == nil {
		err = fmt.Errorf("error opening libsystemd.so")
		return
	}
	defer func() {
		if r := C.dlclose(handle); r != 0 {
			err = fmt.Errorf("error closing libsystemd.so")
		}
	}()

	sym := C.CString("sd_pid_get_unit")
	defer C.free(unsafe.Pointer(sym))
	sd_pid_get_unit := C.dlsym(handle, sym)
	if sd_pid_get_unit == nil {
		err = fmt.Errorf("error resolving sd_pid_get_unit function")
		return
	}

	var s string
	u := C.CString(s)
	defer C.free(unsafe.Pointer(u))

	ret := C.my_sd_pid_get_unit(sd_pid_get_unit, 0, &u)
	if ret < 0 {
		err = fmt.Errorf("error calling sd_pid_get_unit: %v", syscall.Errno(-ret))
		return
	}

	unit = C.GoString(u)
	return
}
Exemple #2
0
func Close(handle uintptr) error {
	ret := C.dlclose(unsafe.Pointer(handle))
	if ret != 0 {
		return errors.New(C.GoString(C.dlerror()))
	}
	return nil
}
Exemple #3
0
func getSlice() (slice string, err error) {
	libname := C.CString("libsystemd.so")
	defer C.free(unsafe.Pointer(libname))
	handle := C.dlopen(libname, C.RTLD_LAZY)
	if handle == nil {
		err = fmt.Errorf("error opening libsystemd.so")
		return
	}
	defer func() {
		if r := C.dlclose(handle); r != 0 {
			err = fmt.Errorf("error closing libsystemd.so")
		}
	}()

	sym := C.CString("sd_pid_get_slice")
	defer C.free(unsafe.Pointer(sym))
	sd_pid_get_slice := C.dlsym(handle, sym)
	if sd_pid_get_slice == nil {
		err = fmt.Errorf("error resolving sd_pid_get_slice function")
		return
	}

	var s string
	sl := C.CString(s)
	defer C.free(unsafe.Pointer(sl))

	ret := C.my_sd_pid_get_slice(sd_pid_get_slice, 0, &sl)
	if ret < 0 {
		err = fmt.Errorf("error calling sd_pid_get_slice: %v", syscall.Errno(-ret))
		return
	}

	slice = C.GoString(sl)
	return
}
Exemple #4
0
func (h Handle) Close() error {
	o := C.dlclose(h.c)
	if o != C.int(0) {
		c_err := C.dlerror()
		return fmt.Errorf("dl: %s", C.GoString(c_err))
	}
	return nil
}
Exemple #5
0
func (h *libHandle) close() error {
	C.dlerror()
	C.dlclose(h.handle)
	e := C.dlerror()
	if e != nil {
		return errwrap.Wrap(fmt.Errorf("error closing %v", h.libname), errors.New(C.GoString(e)))
	}
	return nil
}
Exemple #6
0
// Close closes a LibHandle.
func (l *LibHandle) Close() error {
	C.dlerror()
	C.dlclose(l.Handle)
	e := C.dlerror()
	if e != nil {
		return fmt.Errorf("error closing %v: %v", l.Libname, errors.New(C.GoString(e)))
	}

	return nil
}
Exemple #7
0
func dlclose(handle uintptr) error {
	Chandle := unsafe.Pointer(handle)
	errno, _ := C.dlclose(Chandle)
	if errno != 0 {
		CErrString, _ := C.dlerror()
		return errors.New(C.GoString(CErrString))
	} else {
		return nil
	}
}
Exemple #8
0
func dlclose(lib unsafe.Pointer) (err error) {
	dlmtx.Lock()
	defer dlmtx.Unlock()

	if C.dlclose(lib) != 0 {
		err = dlerror()
	}

	return
}
Exemple #9
0
func (library *Library) Close() int {
	if library == nil {
		return -1
	}
	if library.ptr == nil {
		return -1
	}
	res := C.dlclose(library.ptr)
	library.ptr = nil
	return int(res)
}
Exemple #10
0
func loadThySelf(t *testing.T, symbol string) {
	this_process := C.dlopen(nil, C.RTLD_NOW)
	if this_process == nil {
		t.Fatal("dlopen:", C.GoString(C.dlerror()))
	}
	defer C.dlclose(this_process)

	symbol_address := C.dlsym(this_process, C.CString(symbol))
	if symbol_address == nil {
		t.Fatal("dlsym:", C.GoString(C.dlerror()))
	} else {
		t.Log(symbol, symbol_address)
	}
}
Exemple #11
0
/*
* loadThySelf()
* Go doesn't support dynamic linking. However, it supports a C interface that supports
* dynamic linking. And it supports symbol export allowing callbacks into go functions
* using a C calling convention. So, Go supports dynamic linking.
 */
func loadThySelf(symbol string) *[0]byte {

	this_process := C.dlopen(nil, C.RTLD_NOW)
	if this_process == nil {
		panic(C.GoString(C.dlerror()))
	}

	symbol_address := C.dlsym(this_process, C.CString(symbol))
	if symbol_address == nil {
		panic(C.GoString(C.dlerror()))
	}

	C.dlclose(this_process)
	return (*[0]byte)(unsafe.Pointer(symbol_address))
}
Exemple #12
0
// Close closes the shared library handle. All symbols
// loaded from the library will become invalid.
func (d *DL) Close() error {
	if d.handle != nil {
		d.mu.Lock()
		defer d.mu.Unlock()
		if d.handle != nil {
			mu.Lock()
			defer mu.Unlock()
			if C.dlclose(d.handle) != 0 {
				return dlerror()
			}
			d.handle = nil
		}
	}
	return nil
}
Exemple #13
0
// Unload closes the handle to the dynamically-linked gssapi library.
func (lib *Lib) Unload() error {
	if lib == nil || lib.handle == nil {
		return nil
	}

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	i := C.dlclose(lib.handle)
	if i == -1 {
		return fmt.Errorf("%s", C.GoString(C.dlerror()))
	}

	lib.handle = nil
	return nil
}
Exemple #14
0
func loadThySelf(t *testing.T, symbol string) {
	this_process := C.dlopen(nil, C.RTLD_NOW)
	if this_process == nil {
		t.Error("dlopen:", C.GoString(C.dlerror()))
		return
	}
	defer C.dlclose(this_process)

	symbol_address := C.dlsym(this_process, C.CString(symbol))
	if symbol_address == nil {
		t.Error("dlsym:", C.GoString(C.dlerror()))
		return
	}
	t.Log(symbol, symbol_address)
	C.call4029(symbol_address)
}
Exemple #15
0
func isRunningFromUnitFile() (ret bool, err error) {
	libname := C.CString("libsystemd.so")
	defer C.free(unsafe.Pointer(libname))
	handle := C.dlopen(libname, C.RTLD_LAZY)
	if handle == nil {
		// we can't open libsystemd.so so we assume systemd is not
		// installed and we're not running from a unit file
		ret = false
		return
	}
	defer func() {
		if r := C.dlclose(handle); r != 0 {
			err = fmt.Errorf("error closing libsystemd.so")
		}
	}()

	sd_pid_get_owner_uid := C.dlsym(handle, C.CString("sd_pid_get_owner_uid"))
	if sd_pid_get_owner_uid == nil {
		err = fmt.Errorf("error resolving sd_pid_get_owner_uid function")
		return
	}

	var uid C.uid_t
	errno := C.my_sd_pid_get_owner_uid(sd_pid_get_owner_uid, 0, &uid)
	// when we're running from a unit file, sd_pid_get_owner_uid returns
	// ENOENT (systemd <220) or ENXIO (systemd >=220)
	switch {
	case errno >= 0:
		ret = false
		return
	case syscall.Errno(-errno) == syscall.ENOENT || syscall.Errno(-errno) == syscall.ENXIO:
		ret = true
		return
	default:
		err = fmt.Errorf("error calling sd_pid_get_owner_uid: %v", syscall.Errno(-errno))
		return
	}
}
Exemple #16
0
func dlclose(p uintptr) {
	C.dlclose(unsafe.Pointer(p))
}
Exemple #17
0
//TODO(t): add error handling
func Release(handle unsafe.Pointer) int {
	return int(C.dlclose(handle))
}
Exemple #18
0
func (h *libHandle) Close() error {
	if r := C.dlclose(h.handle); r != 0 {
		return fmt.Errorf("error closing %v: %d", h.libname, r)
	}
	return nil
}