本文整理汇总了Golang中os.File.Fd方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Fd方法的具体用法?Golang File.Fd怎么用?Golang File.Fd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.File
的用法示例。
在下文中一共展示了File.Fd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sendTTYToCommand
func sendTTYToCommand(commandUsock *unixsocket.Usock, clientFile *os.File, err error) error {
if err != nil {
return err
}
return commandUsock.WriteFD(int(clientFile.Fd()))
}
示例2: setFileLock
func setFileLock(f *os.File, lock bool) error {
how := syscall.LOCK_UN
if lock {
how = syscall.LOCK_EX
}
return syscall.Flock(int(f.Fd()), how|syscall.LOCK_NB)
}
示例3: flock
// flock acquires an advisory lock on a file descriptor.
func flock(f *os.File, exclusive bool, timeout time.Duration) error {
var t time.Time
for {
// If we're beyond our timeout then return an error.
// This can only occur after we've attempted a flock once.
if t.IsZero() {
t = time.Now()
} else if timeout > 0 && time.Since(t) > timeout {
return ErrTimeout
}
flag := syscall.LOCK_SH
if exclusive {
flag = syscall.LOCK_EX
}
// Otherwise attempt to obtain an exclusive lock.
err := syscall.Flock(int(f.Fd()), flag|syscall.LOCK_NB)
if err == nil {
return nil
} else if err != syscall.EWOULDBLOCK {
return err
}
// Wait for a bit and try again.
time.Sleep(50 * time.Millisecond)
}
}
示例4: SetCapacity
// SetCapacity reloads the size for the loopback device.
func SetCapacity(file *os.File) error {
if err := ioctlLoopSetCapacity(file.Fd(), 0); err != nil {
logrus.Errorf("Error loopbackSetCapacity: %s", err)
return ErrSetCapacity
}
return nil
}
示例5: getFuseConn
func getFuseConn(local *os.File) (f *os.File, err os.Error) {
var data [4]byte
control := make([]byte, 4*256)
// n, oobn, recvflags, from, errno - todo: error checking.
_, oobn, _, _,
errno := syscall.Recvmsg(
local.Fd(), data[:], control[:], 0)
if errno != 0 {
return
}
message := *(*syscall.Cmsghdr)(unsafe.Pointer(&control[0]))
fd := *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&control[0])) + syscall.SizeofCmsghdr))
if message.Type != 1 {
err = os.NewError(fmt.Sprintf("getFuseConn: recvmsg returned wrong control type: %d", message.Type))
return
}
if oobn <= syscall.SizeofCmsghdr {
err = os.NewError(fmt.Sprintf("getFuseConn: too short control message. Length: %d", oobn))
return
}
if fd < 0 {
err = os.NewError(fmt.Sprintf("getFuseConn: fd < 0: %d", fd))
return
}
f = os.NewFile(int(fd), "<fuseConnection>")
return
}
示例6: setupTerminal
func setupTerminal(file *os.File) (*sys.Termios, error) {
fd := int(file.Fd())
term, err := sys.NewTermiosFromFd(fd)
if err != nil {
return nil, fmt.Errorf("can't get terminal attribute: %s", err)
}
savedTermios := term.Copy()
term.SetICanon(false)
term.SetEcho(false)
term.SetVMin(1)
term.SetVTime(0)
err = term.ApplyToFd(fd)
if err != nil {
return nil, fmt.Errorf("can't set up terminal attribute: %s", err)
}
/*
err = sys.FlushInput(fd)
if err != nil {
return nil, fmt.Errorf("can't flush input: %s", err)
}
*/
return savedTermios, nil
}
示例7: ptsname
// ptsname retrieves the name of the first available pts for the given master.
func ptsname(f *os.File) (string, error) {
var n int32
if err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
return "", err
}
return fmt.Sprintf("/dev/pts/%d", n), nil
}
示例8: startInitialProcess
func (mon *SlaveMonitor) startInitialProcess(sock *os.File) {
for {
command := mon.tree.ExecCommand
parts := strings.Split(command, " ")
executable := parts[0]
args := parts[1:]
cmd := exec.Command(executable, args...)
cmd.Env = append(os.Environ(), fmt.Sprintf("ZEUS_MASTER_FD=%d", sock.Fd()))
cmd.ExtraFiles = []*os.File{sock}
// We want to let this process run "forever", but it will eventually
// die... either on program termination or when its dependencies change
// and we kill it. when it's requested to restart, err is "signal 9",
// and we do nothing.
go func() {
output, err := cmd.CombinedOutput()
if err == nil {
ErrorConfigCommandCrashed(string(output))
}
msg := err.Error()
if len(msg) > 11 && err.Error()[:11] != "exit status" {
ErrorConfigCommandCouldntStart(err.Error())
}
}()
restartNow := make(chan bool)
go func() {
mon.tree.Root.WaitUntilRestartRequested()
restartNow <- true
}()
<-restartNow
mon.tree.Root.Kill()
}
}
示例9: TryExclusive
// TryExclusive is the non-blocking form of Exclusive and will return an
// error if the lock could not be obtained immediately.
func TryExclusive(file *os.File) error {
lock := syscall.LOCK_EX | syscall.LOCK_NB
if err := syscall.Flock(int(file.Fd()), lock); err != nil {
return err
}
return nil
}
示例10: map_file
// Implement mmap for windows
func map_file(file *os.File, offset, size int) ([]byte, error) {
// create the mapping handle
h, err := syscall.CreateFileMapping(syscall.Handle(file.Fd()), nil, syscall.PAGE_READONLY, 0, uint32(size), nil)
if err != nil {
return nil, err
}
// perform the file map operation
addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, uint32(offset>>32), uint32(offset), uintptr(size))
if err != nil {
return nil, err
}
// store the mapping handle
win_mapper_mutex.Lock()
win_mapper_handle[addr] = h
win_mapper_mutex.Unlock()
// Slice memory layout
sl := reflect.SliceHeader{Data: addr, Len: size, Cap: size}
// Use unsafe to turn sl into a []byte.
bp := *(*[]byte)(unsafe.Pointer(&sl))
return bp, err
}
示例11: tarCp
func (g *GraphTool) tarCp(srcName string, tw *tar.Writer) (int64, error) {
var (
src *os.File
err error
)
if src, err = os.Open(srcName); err != nil {
return 0, err
}
defer src.Close()
srcStat, err := src.Stat()
if err != nil {
g.logger.Error(err.Error())
} else if err := unix.Fadvise(int(src.Fd()), 0, srcStat.Size(), unix.MADV_SEQUENTIAL); err != nil {
g.logger.Error(err.Error())
}
if n, err := io.Copy(tw, src); err != nil {
g.logger.Error(err.Error())
} else {
return n, nil
}
return 0, nil
}
示例12: NewBufferFile
// NewBufferFile maps a file to shared memory and returns a handle to the shared memory buffer
func NewBufferFile(file *os.File, size, prot int) (Buffer, error) {
fi, err := file.Stat()
if err != nil {
return nil, err
}
sys := fi.Sys().(*syscall.Stat_t)
if sys.Size != int64(size) {
return nil, errWrongSize
}
// Dup to allow file parameter to be closed regardless
fd, err := syscall.Dup(int(file.Fd()))
if err != nil {
return nil, err
}
const flags = syscall.MAP_SHARED
b, err := syscall.Mmap(fd, 0, size, prot, flags)
if err != nil {
return nil, err
}
// localFile is nil because fd is from somewhere else
buf := &sharedBuffer{os.NewFile(uintptr(fd), ""), nil, b, stackToKeep()}
runtime.SetFinalizer(buf, (*sharedBuffer).finalize)
return buf, nil
}
示例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)
}
}
示例14: handleSignals
func handleSignals(processGroup *ProcessGroup, c <-chan os.Signal, startTime int, sockfile *os.File) {
for {
signal := <-c // os.Signal
syscallSignal := signal.(syscall.Signal)
switch syscallSignal {
case syscall.SIGUSR1:
socketMasterFdEnvVar := fmt.Sprintf("SOCKETMASTER_FD=%d", sockfile.Fd())
syscall.Exec(os.Args[0], os.Args, append(os.Environ(), socketMasterFdEnvVar))
case syscall.SIGHUP:
process, err := processGroup.StartProcess()
if err != nil {
log.Printf("Could not start new process: %v\n", err)
} else {
if startTime > 0 {
time.Sleep(time.Duration(startTime) * time.Millisecond)
}
// A possible improvement woud be to only swap the
// process if the new child is still alive.
processGroup.SignalAll(signal, process)
}
default:
// Forward signal
processGroup.SignalAll(signal, nil)
}
}
}
示例15: fallocate
func fallocate(f *os.File, sz int64) error {
err := syscall.Fallocate(int(f.Fd()), 0, 0, sz)
if err == syscall.ENOTSUP {
return f.Truncate(sz)
}
return err
}