func (w *window) newBuffer(width, height int, format uint32) *C.struct_wl_buffer { stride := width * 4 size := stride * height fd := C.os_create_anonymous_file(C.off_t(size)) if fd < 0 { panic("Could not create buffer file.") } data := C.mmap(nil, C.size_t(size), C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, C.int(fd), 0) if *(*int)(data) == -1 { panic("mmap failed") C.close(fd) return nil } pool := C.wl_shm_create_pool(w.display.shm, C.int32_t(fd), C.int32_t(size)) buffer := C.wl_shm_pool_create_buffer(pool, 0, C.int32_t(width), C.int32_t(height), C.int32_t(stride), C.uint32_t(format)) C.wl_shm_pool_destroy(pool) C.close(fd) w.shmData = data return buffer }
func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.ReadConfig, j *C.sd_journal, pfd [2]C.int, cursor string) { s.readers.mu.Lock() s.readers.readers[logWatcher] = logWatcher s.readers.mu.Unlock() go func() { // Keep copying journal data out until we're notified to stop // or we hit an error. status := C.wait_for_data_or_close(j, pfd[0]) for status == 1 { cursor = s.drainJournal(logWatcher, config, j, cursor) status = C.wait_for_data_or_close(j, pfd[0]) } if status < 0 { cerrstr := C.strerror(C.int(-status)) errstr := C.GoString(cerrstr) fmtstr := "error %q while attempting to follow journal for container %q" logrus.Errorf(fmtstr, errstr, s.vars["CONTAINER_ID_FULL"]) } // Clean up. C.close(pfd[0]) s.readers.mu.Lock() delete(s.readers.readers, logWatcher) s.readers.mu.Unlock() C.sd_journal_close(j) close(logWatcher.Msg) }() // Wait until we're told to stop. select { case <-logWatcher.WatchClose(): // Notify the other goroutine that its work is done. C.close(pfd[1]) } }
func GetShareMem(path string, size int) (*Mem, error) { info, err := os.Lstat(path) if err != nil { return nil, err } if info.IsDir() { return nil, errors.New("Must Is File") } cstr := C.CString(path) num := C.getMem(cstr, C.int(size)) C.free(unsafe.Pointer(cstr)) switch num { case 0: return &Mem{C.Shm_id, size}, nil case -1: return nil, errors.New("Get Key error") case -2: return nil, errors.New("Get Shm_id error") case -3: C.close() return nil, errors.New("Map AddrSpace error") } C.close() return nil, errors.New("Unknow error") }
func (rw *RWops) Close() error { if C.close(rw.c) != 0 { return getError() } return nil }
// close a file descriptor func fdClose(fd C.int) (err error) { res := C.close(fd) if res == C.int(-1) { err = errors.New("close returned -1") } return }
func (self *Mem) Close() error { num := C.close() if num != 0 { cmd := exec.Command("ipcrm", "-m", fmt.Sprint(self.Shm_id)) return cmd.Run() } return nil }
// Close cleans up the TPacket. It should not be used after the Close call. func (h *TPacket) Close() { if h.fd == -1 { return // already closed. } if h.ring != nil { C.munmap(h.ring, C.size_t(h.opts.blockSize*h.opts.numBlocks)) } h.ring = nil C.close(h.fd) h.fd = -1 runtime.SetFinalizer(h, nil) }
func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.ReadConfig, j *C.sd_journal, pfd [2]C.int, cursor string) { go func() { // Keep copying journal data out until we're notified to stop. for C.wait_for_data_or_close(j, pfd[0]) == 1 { cursor = s.drainJournal(logWatcher, config, j, cursor) } // Clean up. C.close(pfd[0]) s.readers.mu.Lock() delete(s.readers.readers, logWatcher) s.readers.mu.Unlock() }() s.readers.mu.Lock() s.readers.readers[logWatcher] = logWatcher s.readers.mu.Unlock() // Wait until we're told to stop. select { case <-logWatcher.WatchClose(): // Notify the other goroutine that its work is done. C.close(pfd[1]) } }
func AuditLogUserEvent(event_type int, message string, result bool) error { var r int fd := C.audit_open() if result { r = 1 } else { r = 0 } if fd > 0 { cmsg := C.CString(message) _, err := C.audit_log_user_message(fd, C.int(event_type), cmsg, nil, nil, nil, C.int(r)) C.free(unsafe.Pointer(cmsg)) C.close(fd) return err } return nil }
func main() { fd, err := C.my_shm_open(C.CString(SHM_NAME)) if err != nil { fmt.Println(err) return } ptr, err := C.mmap(nil, SHM_SIZE, C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, fd, 0) if err != nil { fmt.Println(err) return } C.close(fd) data := (*MyData)(unsafe.Pointer(ptr)) fmt.Println(data) }
func main() { devId := C.hci_get_route(nil) sock := C.hci_open_dev(devId) defer C.close(sock) if devId < 0 || sock < 0 { fmt.Println("Error opening socket for Bluetooth connection.") fmt.Println("Ensure Bluetooth is enabled.") os.Exit(1) } fmt.Println("Searching for Bluetooth devices...") flags := C.IREQ_CACHE_FLUSH maxRsp := 255 var ii *C.inquiry_info iisize := unsafe.Sizeof(ii) iiptr := (*C.inquiry_info)(C.malloc(C.size_t(maxRsp) * C.size_t(iisize))) defer C.free(unsafe.Pointer(iiptr)) numRsp := int(C.hci_inquiry(devId, 8, 255, nil, &iiptr, C.long(flags))) if numRsp <= 0 { fmt.Println("Unable to find Bluetooth devices.") os.Exit(0) } fmt.Printf("Found %d Bluetooth device(s)\n", numRsp) var addr [19]C.char var devAddr string var name [248]C.char var devName string for i := 0; i < numRsp; i++ { ptrOffset := uintptr(unsafe.Pointer(iiptr)) + (uintptr(iisize) * uintptr(i)) ii = (*C.inquiry_info)(unsafe.Pointer(ptrOffset)) C.ba2str(&ii.bdaddr, &addr[0]) devAddr = C.GoString(&addr[0]) result := C.hci_read_remote_name(sock, &ii.bdaddr, C.int(len(name)), &name[0], 0) if result < 0 { devName = "UNKNOWN" } else { devName = C.GoString(&name[0]) } fmt.Printf("Device %s - %s\n", devName, devAddr) } }
func main() { fd, err := C.my_shm_new(C.CString(SHM_NAME)) if err != nil { fmt.Println(err) return } C.ftruncate(fd, SHM_SIZE) ptr, err := C.mmap(nil, SHM_SIZE, C.PROT_READ|C.PROT_WRITE, C.MAP_SHARED, fd, 0) if err != nil { fmt.Println(err) return } C.close(fd) data := (*MyData)(unsafe.Pointer(ptr)) data.Col1 = 500 data.Col2 = 200 data.Col3 = 100 }
func (self *ShareMem) Close() { C.close(self.Addr) }
func (self NamedPort) Ethernet() (PortEthernetProperty, error) { switch self.hatype { case 0: return PortEthernetProperty{}, nil case syscall.ARPHRD_ETHER: // pass default: return PortEthernetProperty{}, fmt.Errorf("%s not an ether", self.name) } cname := C.CString(self.name) defer C.free(unsafe.Pointer(cname)) fd := C.socket(C.AF_INET, C.SOCK_DGRAM, 0) defer C.close(fd) state := PortEthernetProperty{} ecmd := C.struct_ethtool_cmd{cmd: C.ETHTOOL_GSET} if r, err := C.ethtool_cmd_call(fd, cname, &ecmd); err != nil { return state, fmt.Errorf("ethtool for %s: %s", self.name, err.Error()) } else if r != 0 { return state, fmt.Errorf("ethtool_cmd_call error") } else { for k, v := range supportedSpeed { if ecmd.supported&k != 0 && v > state.MaxSpeed { state.MaxSpeed = v } } state.Supported = 0 for k, v := range supportedConvert { if ecmd.supported&k != 0 { state.Supported |= v } } state.Advertised = 0 state.Peer = 0 for k, v := range advertisedConvert { if ecmd.advertising&k != 0 { state.Advertised |= v } if ecmd.lp_advertising&k != 0 { state.Peer |= v } } var curr uint32 switch C.ethtool_cmd_speed(&ecmd) { case C.SPEED_10: state.CurrSpeed = 10000 switch ecmd.duplex { case C.DUPLEX_HALF: curr |= ofp4.OFPPF_10MB_HD case C.DUPLEX_FULL: curr |= ofp4.OFPPF_10MB_FD default: curr |= ofp4.OFPPF_OTHER } case C.SPEED_100: state.CurrSpeed = 100000 switch ecmd.duplex { case C.DUPLEX_HALF: curr |= ofp4.OFPPF_100MB_HD case C.DUPLEX_FULL: curr |= ofp4.OFPPF_100MB_FD default: curr |= ofp4.OFPPF_OTHER } case C.SPEED_1000: state.CurrSpeed = 1000000 switch ecmd.duplex { case C.DUPLEX_HALF: curr |= ofp4.OFPPF_1GB_HD case C.DUPLEX_FULL: curr |= ofp4.OFPPF_1GB_FD default: curr |= ofp4.OFPPF_OTHER } case C.SPEED_10000: state.CurrSpeed = 1000000 switch ecmd.duplex { case C.DUPLEX_FULL: curr |= ofp4.OFPPF_10GB_FD default: curr |= ofp4.OFPPF_OTHER } default: curr |= ofp4.OFPPF_OTHER } switch ecmd.port { case C.PORT_TP: curr |= ofp4.OFPPF_COPPER case C.PORT_FIBRE: curr |= ofp4.OFPPF_FIBER } if ecmd.autoneg != C.AUTONEG_DISABLE { curr |= ofp4.OFPPF_AUTONEG } state.Curr = curr } return state, nil }