Exemple #1
0
func (c conn) Write(b []byte) (n int, err error) {
	n, err = unix.Write(c.fd, b)
	if err != nil {
		err = Err{err}
	}
	return
}
func (tfd testFd) put(t *testing.T) {
	buf := make([]byte, 10)
	_, errno := unix.Write(tfd[1], buf)
	if errno != nil {
		t.Fatalf("Failed to write to pipe: %v", errno)
	}
}
Exemple #3
0
func TestDup(t *testing.T) {
	file, err := ioutil.TempFile("", "TestDup")
	if err != nil {
		t.Fatalf("Tempfile failed: %v", err)
	}
	defer os.Remove(file.Name())
	defer file.Close()
	f := int(file.Fd())

	newFd, err := unix.Dup(f)
	if err != nil {
		t.Fatalf("Dup: %v", err)
	}

	err = unix.Dup2(newFd, newFd+1)
	if err != nil {
		t.Fatalf("Dup2: %v", err)
	}

	b1 := []byte("Test123")
	b2 := make([]byte, 7)
	_, err = unix.Write(newFd+1, b1)
	if err != nil {
		t.Fatalf("Write to dup2 fd failed: %v", err)
	}
	_, err = unix.Seek(f, 0, 0)
	_, err = unix.Read(f, b2)
	if err != nil {
		t.Fatalf("Read back failed: %v", err)
	}
	if string(b1) != string(b2) {
		t.Errorf("Dup: stdout write not in file, expected %v, got %v", string(b1), string(b2))
	}
}
// Close the write end of the poller.
func (poller *fdPoller) wake() error {
	buf := make([]byte, 1)
	n, errno := unix.Write(poller.pipe[1], buf)
	if n == -1 {
		if errno == unix.EAGAIN {
			// Buffer is full, poller will wake.
			return nil
		}
		return errno
	}
	return nil
}
Exemple #5
0
func (d *Device) recvResponse() {
	var n int
	buf := make([]byte, 4)
	for resp := range d.respChan {
		err := d.completeCommand(resp)
		if err != nil {
			log.Errorf("error completing command: %s", err)
			return
		}
		/* Tell the fd there's something new */
		n, err = unix.Write(d.uioFd, buf)
		if n == -1 && err != nil {
			log.Errorln("poll write")
			return
		}
	}
}
Exemple #6
0
func stop() {
	if cxt != nil {
		// notify HandleRequest() that we're done
		if _, err := unix.Write(pipeFds[1], []byte{0}); err != nil {
			log.Errorln("Fail to notify poll for finishing: ", err)
		}
		close(done)
		C.tcmulib_close(cxt)
		if err := unix.Close(pipeFds[0]); err != nil {
			log.Errorln("Fail to close pipeFds[0]: ", err)
		}
		if err := unix.Close(pipeFds[1]); err != nil {
			log.Errorln("Fail to close pipeFds[1]: ", err)
		}
		cxt = nil
	}
}
Exemple #7
0
func main() {
	fmt.Println("Listening on port :", port)
	counter := new(connCounter)

	// create socket
	fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
	if err != nil {
		log.Fatal("socket-error: ", err)
	}

	// sa struct
	sa := new(unix.SockaddrInet4)
	sa.Port = 9090

	// bind
	err = unix.Bind(fd, sa)
	if err != nil {
		log.Fatal("bind: ", err)
	}

	// listen
	err = unix.Listen(fd, 3)
	if err != nil {
		log.Fatal("listen: ", err)
	}

	for {
		connInfo := new(connection)
		// accept connection, discard SA struct
		newFd, _, err := unix.Accept(fd)
		connInfo.fd = newFd
		if err != nil {
			log.Fatal("accept: ", err)
		}

		// client reads until closes, adding
		// a gorutine allows dealing with more
		// requests.
		counter.mu.Lock()
		counter.num += 1
		counter.mu.Unlock()
		fmt.Println("Number of connections: ", counter.num)
		go func(c *connection, counter *connCounter) {
			fmt.Printf("Conn.fd=%d\n", c.fd)
			for {
				// read
				c.buf = make([]byte, 50)
				n, err := unix.Read(c.fd, c.buf)
				if err != nil {
					log.Fatal("read: ", err)
				}

				fmt.Printf("Read: %d Value: %s\n", n, string(c.buf[0:n]))

				// close
				if string(c.buf[0:5]) == "close" {
					_, err = unix.Write(c.fd, []byte(`Bye bye buddy`))
					if err != nil {
						log.Fatal("close: ", err)
					}

					err = unix.Close(c.fd)
					if err != nil {
						log.Fatal("close: ", err)
					}
					counter.mu.Lock()
					counter.num = counter.num - 1
					counter.mu.Unlock()
					return
				}
			}
		}(connInfo, counter)
	}
}
Exemple #8
0
func (s *Socket) Write(p []byte) (int, error) {
	s.wmu.Lock()
	defer s.wmu.Unlock()
	n, err := unix.Write(s.fd, p)
	return n, errors.Wrap(err, "can't write hci socket")
}
Exemple #9
0
// ReadPassword reads characters from the input until press Enter or until
// fill in the given slice.
//
// Only reads characters that include letters, marks, numbers, punctuation,
// and symbols from Unicode categories L, M, N, P, S, besides of the
// ASCII space character.
// Ctrl-C interrumpts, and backspace removes the last character read.
//
// Returns the number of bytes read.
func ReadPassword(password []byte) (n int, err error) {
	ter, err := New()
	if err != nil {
		return 0, err
	}
	defer func() {
		err2 := ter.Restore()
		if err2 != nil && err == nil {
			err = err2
		}
	}()

	if err = ter.RawMode(); err != nil {
		return 0, err
	}

	key := make([]byte, 4) // In-memory representation of a rune.
	lenPassword := 0       // Number of characters read.

	if PasswordShadowed {
		rand.Seed(int64(time.Now().Nanosecond()))
	}

L:
	for {
		n, err = unix.Read(InputFD, key)
		if err != nil {
			return 0, err
		}

		if n == 1 {
			switch key[0] {
			case sys.K_RETURN:
				break L
			case sys.K_BACK:
				if lenPassword != 0 {
					lenPassword--
					password[lenPassword] = 0
				}
				continue
			case sys.K_CTRL_C:
				unix.Write(unix.Stdout, _CTRL_C)
				// Clean data stored, if any.
				for i, v := range password {
					if v == 0 {
						break
					}
					password[i] = 0
				}
				return 0, nil
			}
		}

		char, _ := utf8.DecodeRune(key)
		if unicode.IsPrint(char) {
			password[lenPassword] = key[0] // Only want a character by key
			lenPassword++

			if PasswordShadowed {
				unix.Write(unix.Stdout, bytes.Repeat(_SHADOW_CHAR, rand.Intn(3)+1))
			}
			if lenPassword == len(password) {
				break
			}
		}
	}

	unix.Write(unix.Stdout, _RETURN)
	n = lenPassword
	return
}