func (t *pty) open(cols, rows int) error { var winp = new(C.struct_winsize) winp.ws_col = C.ushort(cols) winp.ws_row = C.ushort(rows) winp.ws_xpixel = 0 winp.ws_ypixel = 0 var master, slave C.int var name = make([]C.char, 40) if ret := int(C.GoOpenpty(&master, &slave, &name[0], winp)); ret == -1 { return errors.New("openpty(3) failed") } t.master = int(master) t.slave = int(slave) t.pty = C.GoString(&name[0]) if err := syscall.SetNonblock(t.master, true); err != nil { return err } if err := syscall.SetNonblock(t.slave, true); err != nil { return err } return nil }
func makeCWindowSize(sz *WindowSize) C.struct_winsize { var winsize C.struct_winsize winsize.ws_row = C.ushort(sz.Rows) winsize.ws_col = C.ushort(sz.Cols) winsize.ws_xpixel = C.ushort(sz.PixelWidth) winsize.ws_ypixel = C.ushort(sz.PixelHeight) return winsize }
func (t *pty) resize(cols, rows int) error { var winp = new(C.struct_winsize) winp.ws_col = C.ushort(cols) winp.ws_row = C.ushort(rows) winp.ws_xpixel = 0 winp.ws_ypixel = 0 if ret := int(C.GoResize(C.int(t.fd), winp)); ret == -1 { return errors.New("ioctl(2) failed") } return nil }
func (t *pty) fork(file string, args, env []string, cwd string, cols, rows, uid, gid int) error { var winp = new(C.struct_winsize) winp.ws_col = C.ushort(cols) winp.ws_row = C.ushort(rows) winp.ws_xpixel = 0 winp.ws_ypixel = 0 //fork the pty var master C.int = -1 var name []C.char = make([]C.char, 40) var pid C.int = C.GoForkpty(&master, &name[0], winp) t.fd = int(master) t.pid = int(pid) t.pty = C.GoString(&name[0]) switch t.pid { case -1: return errors.New("forkpty(3) failed") case 0: if cwd != "" { if err := syscall.Chdir(cwd); err != nil { panic("chdir failed") } } if uid != -1 && gid != -1 { if err := syscall.Setgid(gid); err != nil { panic("setgid failed") } if err := syscall.Setuid(uid); err != nil { panic("setuid failed") } } syscall.Exec(file, args, env) panic("exec failed") } return nil }
func setColsRows(winsz *C.struct_winsize, cols int, rows int) { winsz.ws_row = C.ushort(rows) winsz.ws_col = C.ushort(cols) winsz.ws_xpixel = C.ushort(cols * 9) winsz.ws_ypixel = C.ushort(rows * 16) }