当前位置: 首页>>代码示例>>Golang>>正文


Golang syscall.Syscall函数代码示例

本文整理汇总了Golang中syscall.Syscall函数的典型用法代码示例。如果您正苦于以下问题:Golang Syscall函数的具体用法?Golang Syscall怎么用?Golang Syscall使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Syscall函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DeleteBridge

// Delete the actual bridge device.
func DeleteBridge(name string) error {
	s, err := getIfSocket()
	if err != nil {
		return err
	}
	defer syscall.Close(s)

	nameBytePtr, err := syscall.BytePtrFromString(name)
	if err != nil {
		return err
	}

	var ifr ifreqFlags
	copy(ifr.IfrnName[:len(ifr.IfrnName)-1], []byte(name))
	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
		syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifr))); err != 0 {
		return err
	}

	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
		SIOC_BRDELBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
		return err
	}
	return nil
}
开发者ID:JianfuLi,项目名称:docker,代码行数:26,代码来源:netlink_linux.go

示例2: setCommState

func setCommState(h syscall.Handle) error {
	var params structDCB
	params.DCBlength = uint32(unsafe.Sizeof(params))

	//params.flags[0] = 0x01  // fBinary
	//params.flags[0] |= 0x10 // Assert DSR

	params.flags[0] = 0x00

	params.BaudRate = uint32(9600)
	params.ByteSize = 8
	params.Parity = 0
	params.StopBits = 0
	//Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
	//   ByRef lpDCB As DCB) As Boolean
	r, _, err := syscall.Syscall(nSetCommState, 2, uintptr(h), uintptr(unsafe.Pointer(&params)), 0)

	fmt.Println("SetCommState : ", r, err)

	if r == 0 {
		_, _, err := syscall.Syscall(nGetLastError, 1, uintptr(h), 0, 0)
		fmt.Println("LastError : ", err)
		return nil
	}
	return nil
}
开发者ID:kity-xu,项目名称:xuxiaodong,代码行数:26,代码来源:parallel_windows.go

示例3: TestFormatMessage

func TestFormatMessage(t *testing.T) {
	dll := windows.MustLoadDLL("pdh.dll")

	pdhOpenQuery := func(datasrc *uint16, userdata uint32, query *windows.Handle) (errno uintptr) {
		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhOpenQueryW").Addr(), 3, uintptr(unsafe.Pointer(datasrc)), uintptr(userdata), uintptr(unsafe.Pointer(query)))
		return r0
	}

	pdhCloseQuery := func(query windows.Handle) (errno uintptr) {
		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhCloseQuery").Addr(), 1, uintptr(query), 0, 0)
		return r0
	}

	var q windows.Handle
	name, err := windows.UTF16PtrFromString("no_such_source")
	if err != nil {
		t.Fatal(err)
	}
	errno := pdhOpenQuery(name, 0, &q)
	if errno == 0 {
		pdhCloseQuery(q)
		t.Fatal("PdhOpenQuery succeeded, but expected to fail.")
	}

	const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
	buf := make([]uint16, 300)
	_, err = windows.FormatMessage(flags, uintptr(dll.Handle), uint32(errno), 0, buf, nil)
	if err != nil {
		t.Fatal("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, errno, err)
	}
}
开发者ID:eswdd,项目名称:bosun,代码行数:31,代码来源:syscall_windows_test.go

示例4: openConsole

// Tested on Suzy-Q and BeagleBone.
func openConsole(con string) (rc io.ReadCloser, err error) {
	fd, err := syscall.Open(con, syscall.O_RDONLY|syscall.O_NOCTTY|syscall.O_SYNC, 0)
	if err != nil {
		return nil, fmt.Errorf("failed to open console file: %v", err)
	}
	defer func() {
		if fd != -1 {
			syscall.Close(fd)
		}
	}()
	var term Termios
	if _, _, errno := syscall.Syscall(SYS_IOCTL, uintptr(fd), TCGETS2, uintptr(unsafe.Pointer(&term))); errno != 0 {
		return nil, fmt.Errorf("failed to get console termios: %v", errno)
	}
	// no parity bit, only need 1 stop bit, no hardware flowcontrol
	term.Cflag &^= CBAUD | CSIZE | PARENB | CSTOPB | CRTSCTS
	// ignore modem controls
	term.Cflag |= B115200 | CS8 | CLOCAL | CREAD
	// setup for non-canonical mode
	term.Iflag &^= IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON
	term.Lflag &^= ECHO | ECHONL | ICANON | ISIG | IEXTEN
	term.Oflag &^= OPOST
	term.Cc[VMIN] = 0
	term.Cc[VTIME] = 10 // 1 second timeout
	if _, _, errno := syscall.Syscall(SYS_IOCTL, uintptr(fd), TCSETS2, uintptr(unsafe.Pointer(&term))); errno != 0 {
		return nil, fmt.Errorf("failed to get console termios: %v", errno)
	}
	tmp := fd
	fd = -1
	return &tty{fd: tmp}, nil
}
开发者ID:google,项目名称:syzkaller,代码行数:32,代码来源:console.go

示例5: NewSPI

// NewSPI returns a new SPI object that is connected to the
// specified SPI device interface.
//
// NewSPI(X,Y) will open /dev/spidev-X.Y
//
// SPI is an object type that allows SPI transactions
// on hosts running the Linux kernel. The host kernel must have SPI
// support and SPI device interface support.
// All of these can be either built-in to the kernel, or loaded from modules.
//
// Because the SPI device interface is opened R/W, users of this
// module usually must have root permissions.
func NewSPI(bus, device int) (spi *SPI, err error) {
	err = embedded.LoadDeviceTree(fmt.Sprintf("%s%d", deviceTreePrefix, bus))
	if err != nil {
		return nil, err
	}

	spi = &SPI{bus: bus, device: device}

	path := fmt.Sprintf("/dev/spidev%d.%d", bus+1, device)
	spi.file, err = os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MODE, uintptr(unsafe.Pointer(&spi.mode)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_BITS_PER_WORD, uintptr(unsafe.Pointer(&spi.bitsPerWord)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MAX_SPEED_HZ, uintptr(unsafe.Pointer(&spi.maxSpeedHz)))
	if r != 0 {
		return nil, err
	}

	return spi, nil
}
开发者ID:voxadam,项目名称:go-embedded,代码行数:43,代码来源:spi.go

示例6: Configure

// Configure configures f as a 8N1 serial port with the specified baudrate.
func Configure(f fder, baudrate uint32) error {
	var termios syscall.Termios
	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), uintptr(syscall.TCGETS), uintptr(unsafe.Pointer(&termios))); err != 0 {
		return err
	}

	termios.Ispeed = baudrate
	termios.Ospeed = baudrate
	termios.Cflag &^= CBAUD
	termios.Cflag &^= CBAUDEX
	termios.Cflag |= baudrate

	// set 8N1
	termios.Cflag &^= syscall.PARENB
	termios.Cflag &^= syscall.CSTOPB
	termios.Cflag &^= syscall.CSIZE
	termios.Cflag |= syscall.CS8

	// Local connection, no modem control
	termios.Cflag |= (syscall.CLOCAL | syscall.CREAD)
	// Disable hardware flow control
	termios.Cflag &^= CRTSCTS
	// Block on a zero read (instead of returning EOF)
	termios.Cc[syscall.VMIN] = 1

	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != 0 {
		return err
	}
	return nil
}
开发者ID:stapelberg,项目名称:zkj-nas-tools,代码行数:31,代码来源:serial.go

示例7: setControlSignal

func (p *Port) setControlSignal(sigmask uint, level bool) error {
	state := uint(0)
	// get current state
	_, _, errno := syscall.Syscall(
		syscall.SYS_IOCTL,
		p.file.Fd(),
		uintptr(syscall.TIOCMGET),
		uintptr(unsafe.Pointer(&state)))
	if errno != 0 {
		return fmt.Errorf("ioctl TIOCMGET %d", errno)
	}
	// modify state
	switch level {
	case true:
		state |= sigmask // set
	case false:
		state &^= sigmask // clear
	}
	// set new state
	_, _, errno = syscall.Syscall(
		syscall.SYS_IOCTL,
		p.file.Fd(),
		uintptr(syscall.TIOCMSET),
		uintptr(unsafe.Pointer(&state)))
	if errno != 0 {
		return fmt.Errorf("ioctl TIOCMSET %d", errno)
	}
	return nil
}
开发者ID:notnot,项目名称:communication,代码行数:29,代码来源:rs232.go

示例8: Console1

func Console1(forward bool) {
	//
	const (
		xdm = 7
		msg = 10 // messages
	)
	var s stat
	syscall.Syscall(syscall.SYS_IOCTL, 0, _GETSTATE, uintptr(unsafe.Pointer(&s)))
	a := s.active
	if forward {
		switch a {
		case xdm - 1:
			a = xdm + 1
		case msg - 1:
			a = msg + 1
		case maxConsole:
			a = 1
		default:
			a++
		}
	} else {
		switch a {
		case 1:
			a = maxConsole
		case xdm + 1:
			a = xdm - 1
		case msg + 1:
			a = msg - 1
		default:
			a--
		}
	}
	syscall.Syscall(syscall.SYS_IOCTL, 0, _ACTIVATE, uintptr(a))
	syscall.Syscall(syscall.SYS_IOCTL, 0, _WAITACTIVE, uintptr(a))
}
开发者ID:CaptainSoOmA,项目名称:Uni,代码行数:35,代码来源:console.go

示例9: SaferDLLLoading

// SaferDLLLoading sets DLL load path to be safer.
func SaferDLLLoading() error {
	kernel32, err := syscall.LoadDLL("kernel32.dll")
	if err != nil {
		return err
	}

	procSetDllDirectoryW, err := kernel32.FindProc("SetDllDirectoryW")
	if err != nil {
		return err
	}
	var zero uint16
	r1, _, e1 := syscall.Syscall(procSetDllDirectoryW.Addr(), 1,
		uintptr(unsafe.Pointer(&zero)), 0, 0)

	procSetDefaultDllDirectories, err := kernel32.FindProc("SetDefaultDllDirectories")
	if err == nil && procSetDefaultDllDirectories.Addr() != 0 {
		r1, _, e1 = syscall.Syscall(procSetDefaultDllDirectories.Addr(), 1,
			loadLibrarySearchSystem32, 0, 0)
		if r1 == 0 {
			return e1
		}
	} else {
		return errors.New("SetDefaultDllDirectories not found - please install KB2533623 for safer DLL loading")
	}

	return nil
}
开发者ID:qbit,项目名称:client,代码行数:28,代码来源:saferdllloading_windows.go

示例10: Xfer

// Xfer performs a SPI transaction.
// CS will be released and reactivated between blocks.
// delay specifies delay in usec between blocks.
func (spi *SPI) Xfer(txBuf []byte, delay_usecs uint16) (rxBuf []byte, err error) {
	length := len(txBuf)
	rxBuf = make([]byte, length)

	xfer := make([]spi_ioc_transfer, length)
	for i := range xfer {
		xfer[i].tx_buf = uintptr(unsafe.Pointer(&txBuf[i]))
		xfer[i].rx_buf = uintptr(unsafe.Pointer(&rxBuf[i]))
		xfer[i].len = 1
		xfer[i].delay_usecs = delay_usecs
	}

	SPI_IOC_MESSAGE := C._IOC_WRITE<<C._IOC_DIRSHIFT | C.SPI_IOC_MAGIC<<C._IOC_TYPESHIFT | length<<C._IOC_SIZESHIFT

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), uintptr(SPI_IOC_MESSAGE), uintptr(unsafe.Pointer(&xfer[0])))
	if r != 0 {
		return nil, err
	}

	// WA:
	// in CS_HIGH mode CS isn't pulled to low after transfer, but after read
	// reading 0 bytes doesnt matter but brings cs down
	syscall.Syscall(syscall.SYS_READ, spi.file.Fd(), uintptr(unsafe.Pointer(&rxBuf[0])), 0)

	return rxBuf, nil
}
开发者ID:voxadam,项目名称:go-embedded,代码行数:29,代码来源:spi.go

示例11: raw

// enable raw mode and gather metrics, like number of columns
func (l *LineReader) raw() {
	// STD_OUTPUT_HANDLE
	h, errno := syscall.GetStdHandle(-11)
	t.h = uintptr(h)
	if int32(t.h) == -1 {
		err := os.Errno(errno)
		panic(err)
	}
	ok, _, e := syscall.Syscall(procGetConsoleMode, 2,
		t.h, uintptr(unsafe.Pointer(&t.origTerm)), 0)
	if ok == 0 {
		err := os.NewSyscallError("GetConsoleMode", int(e))
		panic(err)
	}

	raw := t.origTerm
	raw &^= _ENABLE_LINE_INPUT | _ENABLE_ECHO_INPUT | _ENABLE_PROCESSED_INPUT | _ENABLE_WINDOW_INPUT
	ok, _, e = syscall.Syscall(procSetConsoleMode, 2, t.h, uintptr(raw), 0)
	if ok == 0 {
		err := os.NewSyscallError("SetConsoleMode", int(e))
		panic(err)
	}

	win := t.getConsoleInfo()
	t.cols = int(win.dwSize.x)
	t.rows = int(win.dwSize.y)

	t.buf = new(buffer)
}
开发者ID:sbinet,项目名称:fineline,代码行数:30,代码来源:windows.go

示例12: SetNOCOWFile

// SetNOCOWFile sets NOCOW flag for file
func SetNOCOWFile(path string) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	fileinfo, err := file.Stat()
	if err != nil {
		return err
	}
	if fileinfo.IsDir() {
		return fmt.Errorf("skip directory")
	}
	if fileinfo.Size() != 0 {
		return fmt.Errorf("skip nonempty file")
	}

	var attr int
	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_GETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
		return errno
	}
	attr |= FS_NOCOW_FL
	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_SETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
		return errno
	}
	log.Infof("Set NOCOW to path %v succeeded", path)
	return nil
}
开发者ID:BREWTAN,项目名称:etcd,代码行数:30,代码来源:btrfs_linux.go

示例13: main

func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 {
		usage()
	}

	vtnum, err := strconv.Atoi(flag.Arg(0))
	ck(err)

	consoles := []string{"/dev/console", "/dev/vc/0", "/dev/tty"}
	var f *os.File
	for _, cc := range consoles {
		f, err = os.OpenFile(cc, os.O_RDWR, 0644)
		if err == nil {
			break
		}
	}
	if f == nil {
		fmt.Fprintln(os.Stderr, "chvt: couldn't get file descriptor referring to console")
		os.Exit(1)
	}

	fd := f.Fd()
	_, _, err = syscall.Syscall(syscall.SYS_IOCTL, fd, 0x5606, uintptr(vtnum))
	if err != nil {
		_, _, err = syscall.Syscall(syscall.SYS_IOCTL, fd, 0x5607, uintptr(vtnum))
	}
	f.Close()

	if err != nil {
		fmt.Fprintln(os.Stderr, "chvt:", err)
		os.Exit(1)
	}
}
开发者ID:qeedquan,项目名称:misc_utilities,代码行数:35,代码来源:chvt.go

示例14: NewSPI

// NewSPI returns a new SPI object that is connected to the
// specified SPI device interface.
//
// NewSPI(X,Y) will open /dev/spidev-X.Y
//
// SPI is an object type that allows SPI transactions
// on hosts running the Linux kernel. The host kernel must have SPI
// support and SPI device interface support.
// All of these can be either built-in to the kernel, or loaded from modules.
//
// Because the SPI device interface is opened R/W, users of this
// module usually must have root permissions.
func NewSPI(bus, device int) (*SPI, error) {
	deviceTreeName := fmt.Sprintf("ADAFRUIT-SPI%d", bus)
	err := LoadDeviceTree(deviceTreeName)
	if err != nil {
		return nil, err
	}

	spi := new(SPI)

	path := fmt.Sprintf("/dev/spidev%d.%d", bus+1, device)
	spi.file, err = os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MODE, uintptr(unsafe.Pointer(&spi.mode)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_BITS_PER_WORD, uintptr(unsafe.Pointer(&spi.bitsPerWord)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MAX_SPEED_HZ, uintptr(unsafe.Pointer(&spi.maxSpeedHz)))
	if r != 0 {
		return nil, err
	}

	return spi, nil
}
开发者ID:ungerik,项目名称:go-bbio,代码行数:44,代码来源:spi.go

示例15: NewLiner

// NewLiner initializes a new *State, and sets the terminal into raw mode. To
// restore the terminal to its previous state, call State.Close().
// NewLiner handles SIGWINCH, so it will leak a channel every time you call
// it. Therefore, it is recommened that NewLiner only be called once.
func NewLiner() *State {
	bad := map[string]bool{"": true, "dumb": true, "cons25": true}
	var s State
	s.r = bufio.NewReader(os.Stdin)
	s.supported = !bad[strings.ToLower(os.Getenv("TERM"))]

	if s.supported {
		syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), getTermios, uintptr(unsafe.Pointer(&s.origMode)))
		mode := s.origMode
		mode.Iflag &^= icrnl | inpck | istrip | ixon
		mode.Cflag |= cs8
		mode.Lflag &^= syscall.ECHO | icanon | iexten
		syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), setTermios, uintptr(unsafe.Pointer(&mode)))

		winch := make(chan os.Signal, 1)
		signal.Notify(winch, syscall.SIGWINCH)
		s.winch = winch

		next := make(chan nexter)
		go func() {
			for {
				var n nexter
				n.r, _, n.err = s.r.ReadRune()
				next <- n
			}
		}()
		s.next = next
	}

	return &s
}
开发者ID:polaris1119,项目名称:liner,代码行数:35,代码来源:input.go


注:本文中的syscall.Syscall函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。