本文整理匯總了Golang中syscall.Close函數的典型用法代碼示例。如果您正苦於以下問題:Golang Close函數的具體用法?Golang Close怎麽用?Golang Close使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Close函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: childMain
func childMain() error {
var err error
pipe := os.NewFile(uintptr(3), "pipe")
if pipe != nil {
defer pipe.Close()
if err == nil {
pipe.Write([]byte{DaemonSuccess})
} else {
pipe.Write([]byte{DaemonFailure})
}
}
signal.Ignore(syscall.SIGCHLD)
syscall.Close(0)
syscall.Close(1)
syscall.Close(2)
syscall.Setsid()
syscall.Umask(022)
// syscall.Chdir("/")
return nil
}
示例2: listenStream
func listenStream(netw, addr string) (l net.Listener, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
l.Close()
return nil, err
}
return l, err
}
示例3: TestSelect
func TestSelect(t *testing.T) {
var p1, p2 [2]int
mustNil(syscall.Pipe(p1[:]))
mustNil(syscall.Pipe(p2[:]))
fs := NewFdSet(p1[0], p2[0])
var maxfd int
if p1[0] > p2[0] {
maxfd = p1[0] + 1
} else {
maxfd = p2[0] + 1
}
go func() {
syscall.Write(p1[1], []byte("to p1"))
syscall.Write(p2[1], []byte("to p2"))
syscall.Close(p1[1])
syscall.Close(p2[1])
}()
e := Select(maxfd+1, fs, nil, nil, nil)
if e != nil {
t.Errorf("Select(%v, %v, nil, nil, nil) => %v, want <nil>",
maxfd+1, fs, e)
}
syscall.Close(p1[0])
syscall.Close(p2[0])
}
示例4: CaptureStream
func CaptureStream(file *os.File) (*os.File, func(), error) {
pipeR, pipeW, err := os.Pipe()
if err != nil {
return nil, nil, err
}
bkupFD, err := syscall.Dup(int(file.Fd()))
if err != nil {
pipeW.Close()
pipeR.Close()
return nil, nil, err
}
err = syscall.Dup2(int(pipeW.Fd()), int(file.Fd()))
if err != nil {
syscall.Close(bkupFD)
pipeW.Close()
pipeR.Close()
}
cleanFunc := func() {
syscall.Dup2(bkupFD, int(file.Fd()))
syscall.Close(bkupFD)
pipeW.Close()
pipeR.Close()
}
return pipeR, cleanFunc, nil
}
示例5: Init
func (sc *selectCtx) Init() error {
sc.rset.Zero()
sc.wset.Zero()
sc.fdmax = -1
// Create and configure the pipe-ends.
b := make([]int, 2)
err := syscall.Pipe(b)
if err != nil {
return err
}
sc.pfdr, sc.pfdw = b[0], b[1]
syscall.CloseOnExec(sc.pfdr)
syscall.CloseOnExec(sc.pfdw)
err = syscall.SetNonblock(sc.pfdr, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
err = syscall.SetNonblock(sc.pfdw, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
// pfdr (read-end of pipe) is set (and remains set forever) on
// rset.
sc.rset.Set(sc.pfdr)
sc.fdmax = sc.pfdr
// allocate dummy selectCtx.{b,b1} buffers.
sc.b = make([]byte, 1)
sc.b1 = make([]byte, 128)
return nil
}
示例6: Close
// Close wraps syscall.Close.
func (sw *Switch) Close(s int) (err error) {
so := sw.sockso(s)
if so == nil {
return syscall.Close(s)
}
sw.fmu.RLock()
f, _ := sw.fltab[FilterClose]
sw.fmu.RUnlock()
af, err := f.apply(so)
if err != nil {
return err
}
so.Err = syscall.Close(s)
if err = af.apply(so); err != nil {
return err
}
if so.Err != nil {
return so.Err
}
sw.smu.Lock()
delete(sw.sotab, s)
sw.stats.getLocked(so.Cookie).Closed++
sw.smu.Unlock()
return nil
}
示例7: main
func main() {
args := Arguments()
infile, args, err := args.String("in-file", nil)
Check(err)
outfile, args, err := args.String("out-file", nil)
Check(err)
infd, err := syscall.Open(infile, int(ReadOnly), 0)
Check(err)
defer func() { Check(syscall.Close(infd)) }()
flags := Create | WriteOnly | Truncate
perms := 0 |
UserRead | UserWrite |
GroupRead | GroupWrite |
OtherRead | OtherWrite
outfd, err := syscall.Open(outfile, int(flags), uint32(perms))
Check(err)
defer func() { Check(syscall.Close(outfd)) }()
bufSize := 1024
buf := make([]byte, bufSize)
var n int
for {
n, err = syscall.Read(infd, buf)
Check(err)
if n == 0 {
break
}
_, err = syscall.Write(outfd, buf[:n])
Check(err)
}
}
示例8: CopyBytes
func (s *Splicer) CopyBytes(in, out *net.TCPConn) {
// Close the tcp connection given to us.
defer func() {
// TODO: Double close problems?
out.Close()
in.Close()
}()
// Invoking File() on the connection will duplicate the socket fd, setting
// the original (socket fd) to block.
inFile, err := in.File()
if err != nil {
log.Fatal(err)
}
inFD := int(inFile.Fd())
defer syscall.Close(inFD)
outFile, err := out.File()
if err != nil {
log.Fatal(err)
}
outFD := int(outFile.Fd())
defer syscall.Close(outFD)
var wg sync.WaitGroup
wg.Add(2)
go s.splice(fmt.Sprintf("from backend: %v -> %v", in.RemoteAddr(), out.RemoteAddr()), inFD, outFD, &wg)
go s.splice(fmt.Sprintf("to backend: %v -> %v", out.RemoteAddr(), in.RemoteAddr()), outFD, inFD, &wg)
wg.Wait()
}
示例9: main
func main() {
fd := make([]int, 2)
if err := syscall.Pipe(fd); err != nil {
log.Fatal("Socketpair:", err)
}
defer syscall.Close(fd[0])
defer syscall.Close(fd[1])
name := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd[1])
fmt.Println(name)
cmd := exec.Command("ubertooth-btle", "-f", "-d", name)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
f := os.NewFile(uintptr(fd[0]), "server")
buf := make([]byte, 1024)
for {
n, err := f.Read(buf)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q\n", buf[:n])
}
}
示例10: socket
/*
c.sa = new(syscall.SockaddrInet4)
c.sa.Addr = [4]byte{10, 1, 1, 131}
c.sa.Port = port
*/
func (c *Worker) socket() {
begin := time.Now()
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
syscall.Close(s)
return
}
err = syscall.Connect(s, nil) //TODO
if err != nil {
lg(err)
syscall.Close(s)
return
}
_, err = syscall.Write(s, message.content)
if err != nil {
syscall.Close(s)
return
}
_, err = syscall.Read(s, c.data)
if err != nil {
syscall.Close(s)
return
}
syscall.Close(s)
time.Now().Sub(begin)
}
示例11: ListenPipe
func ListenPipe(path, sddl string) (net.Listener, error) {
var (
sd []byte
err error
)
if sddl != "" {
sd, err = sddlToSecurityDescriptor(sddl)
if err != nil {
return nil, err
}
}
h, err := makeServerPipeHandle(path, sd, true)
if err != nil {
return nil, err
}
// Immediately open and then close a client handle so that the named pipe is
// created but not currently accepting connections.
h2, err := createFile(path, 0, 0, nil, syscall.OPEN_EXISTING, cSECURITY_SQOS_PRESENT|cSECURITY_ANONYMOUS, 0)
if err != nil {
syscall.Close(h)
return nil, err
}
syscall.Close(h2)
l := &win32PipeListener{
firstHandle: h,
path: path,
securityDescriptor: sd,
acceptCh: make(chan (chan acceptResponse)),
closeCh: make(chan int),
doneCh: make(chan int),
}
go l.listenerRoutine()
return l, nil
}
示例12: afterDaemonize
func afterDaemonize(err error) {
// Ignore SIGCHLD signal
signal.Ignore(syscall.SIGCHLD)
// Close STDOUT, STDIN, STDERR
syscall.Close(0)
syscall.Close(1)
syscall.Close(2)
// Become the process group leader
syscall.Setsid()
// // Clear umask
syscall.Umask(022)
// // chdir for root directory
syscall.Chdir("/")
// Notify that the child process started successfuly
pipe := os.NewFile(uintptr(3), "pipe")
if pipe != nil {
defer pipe.Close()
if err == nil {
pipe.Write([]byte{DAEMONIZE_SUCCESS})
} else {
pipe.Write([]byte{DAEMONIZE_FAIL})
}
}
}
示例13: readEvent
func (w *Watcher) readEvent() {
var (
buf [syscall.SizeofInotifyEvent * 4096]byte // Buffer for a maximum of 4096 raw events
n int // Number of bytes read with read()
errno error // Syscall errno
)
for {
select {
case <-w.done:
syscall.Close(w.fd)
close(w.acceptEvent)
close(w.Error)
return
default:
}
n, errno = syscall.Read(w.fd, buf[0:])
if n == 0 {
syscall.Close(w.fd)
close(w.acceptEvent)
close(w.Error)
return
}
if n < syscall.SizeofInotifyEvent {
log.Fatal("size of InotifyEvent error", errno)
}
var offset uint32 = 0
for offset <= uint32(n-syscall.SizeofInotifyEvent) {
raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
event := new(FileEvent)
event.wd = raw.Wd
event.nameLen = raw.Len
event.mask = raw.Mask
event.cookie = raw.Cookie
path := w.wm.paths[int(raw.Wd)]
if raw.Len > 0 {
// Point "bytes" at the first byte of the filename
bytes := (*[syscall.PathMax]byte)(unsafe.Pointer(&buf[offset+syscall.SizeofInotifyEvent]))
// The filename is padded with NUL bytes. TrimRight() gets rid of those.
event.fileName = path + "/" + strings.TrimRight(string(bytes[0:raw.Len]), "\000")
}
if _, found := w.skipExt[filepath.Ext(event.fileName)]; !found {
fmt.Println("--->", w.skipExt, "--->", filepath.Ext(event.fileName), "--->", found)
//發送事件acceptEvent通道
w.acceptEvent <- event
} else {
fmt.Println("過濾文件:", event.fileName)
}
offset += syscall.SizeofInotifyEvent + raw.Len
}
}
}
示例14: ProxyNetAccept
func (io *NetIOManager) ProxyNetAccept(serverinfo syscall.Sockaddr) (sa syscall.Sockaddr, err error) {
var clientfd, serverfd int
// accpet mongodb client connection request
clientfd, clientinfo, err := syscall.Accept(io.proxy_server_fd)
if err != nil {
goto ClientError
}
err = syscall.SetNonblock(clientfd, true)
if err != nil {
goto ClientCleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, clientfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(clientfd)})
if err != nil {
goto ClientCleanup
}
// establish connection with mongodb server
serverfd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM,
syscall.IPPROTO_TCP)
if err != nil {
goto ServerError
}
err = syscall.Connect(serverfd, serverinfo)
if err != nil {
goto ServerCleanup
}
err = syscall.SetNonblock(serverfd, true)
if err != nil {
goto ServerCleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, serverfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(serverfd)})
if err != nil {
goto ServerCleanup
}
// now proxy server becomes a bridge between client <-> server
add_sock_peer(io, clientfd, clientinfo, serverfd, serverinfo)
return clientinfo, nil
ServerCleanup:
syscall.Close(serverfd)
ServerError:
syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_DEL, clientfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(clientfd)})
ClientCleanup:
syscall.Close(clientfd)
ClientError:
return nil, err
}
示例15: initJob
func initJob() {
// Create Job object and assign current process to it.
jobObject, err := createJobObject(nil, nil)
if err != nil {
log.Printf("CreateJobObject failed: %v", err)
return
}
if err = assignProcessToJobObject(jobObject, currentProcess); err != nil {
log.Printf("AssignProcessToJobObject failed: %v", err)
syscall.Close(jobObject)
return
}
iocp, err := syscall.CreateIoCompletionPort(syscall.InvalidHandle, 0, 0, 1)
if err != nil {
log.Printf("CreateIoCompletionPort failed: %v", err)
syscall.Close(jobObject)
return
}
port := JOBOBJECT_ASSOCIATE_COMPLETION_PORT{
CompletionKey: uintptr(jobObject),
CompletionPort: iocp,
}
err = setInformationJobObject(jobObject, JobObjectAssociateCompletionPortInformation, uintptr(unsafe.Pointer(&port)), uint32(unsafe.Sizeof(port)))
if err != nil {
log.Printf("SetInformationJobObject failed: %v", err)
syscall.Close(jobObject)
syscall.Close(iocp)
return
}
// Read Job notifications about new "child" processes and collect them in childProcesses.
go func() {
var code, key uint32
var o *syscall.Overlapped
for {
err := syscall.GetQueuedCompletionStatus(iocp, &code, &key, &o, syscall.INFINITE)
if err != nil {
log.Printf("GetQueuedCompletionStatus failed: %v", err)
return
}
if key != uint32(jobObject) {
panic("Invalid GetQueuedCompletionStatus key parameter")
}
if code == JOB_OBJECT_MSG_NEW_PROCESS {
pid := int(uintptr(unsafe.Pointer(o)))
if pid == syscall.Getpid() {
continue
}
c, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, uint32(pid))
if err != nil {
log.Printf("OpenProcess failed: %v", err)
continue
}
childMu.Lock()
childProcesses = append(childProcesses, c)
childMu.Unlock()
}
}
}()
}