本文整理匯總了Golang中net.ListenUnix函數的典型用法代碼示例。如果您正苦於以下問題:Golang ListenUnix函數的具體用法?Golang ListenUnix怎麽用?Golang ListenUnix使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ListenUnix函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UnixAudienceListener
func UnixAudienceListener(sockaddr string) {
fi, err := os.Stat(sockaddr)
if err == nil {
fmode := fi.Mode()
if fmode&os.ModeType == os.ModeSocket {
o.Warn("Removing stale socket at %s", sockaddr)
os.Remove(sockaddr)
} else {
o.Fail("%s exists and is not a socket", sockaddr)
}
}
laddr, err := net.ResolveUnixAddr("unix", sockaddr)
o.MightFail(err, "Couldn't resolve audience socket address")
l, err := net.ListenUnix("unix", laddr)
o.MightFail(err, "Couldn't start audience unixsock listener")
// Fudge the permissions on the unixsock!
fi, err = os.Stat(sockaddr)
if err == nil {
os.Chmod(sockaddr, fi.Mode()|0777)
} else {
o.Warn("Couldn't fudge permission on audience socket: %s", err)
}
// make sure we clean up the unix socket when we die.
defer l.Close()
defer os.Remove(sockaddr)
AudienceListener(l)
}
示例2: newUnixServer
func newUnixServer(keys string, uuid string) (Server, error) {
var err error
abstract := getKey(keys, "abstract")
path := getKey(keys, "path")
s := new(unixServer)
s.uuid = uuid
switch {
case abstract == "" && path == "":
return nil, errors.New("dbus: invalid address (neither path nor abstract set)")
case abstract != "" && path == "":
s.listener, err = net.ListenUnix("unix", &net.UnixAddr{Name: "@" + abstract, Net: "unix"})
if err != nil {
return nil, err
}
return s, nil
case abstract == "" && path != "":
s.listener, err = net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"})
if err != nil {
return nil, err
}
return s, nil
default:
return nil, errors.New("dbus: invalid address (both path and abstract set)")
}
}
示例3: unix_coll
func unix_coll(_, address string) (Listener, error) {
var err error
r := new(unix_receiver)
r.network = "unix"
r.address = address
r.listener, err = net.ListenUnix("unix", &net.UnixAddr{address, "unix"})
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
return nil, err
}
default:
return nil, err
}
if _, r_err := os.Stat(address); r_err != nil {
return nil, err
}
os.Remove(address)
r.listener, err = net.ListenUnix("unix", &net.UnixAddr{address, "unix"})
}
return r, nil
}
示例4: create_socket
func create_socket(l *log.Logger, socket string, uid, gid int, end <-chan struct{}, wg *sync.WaitGroup) *Listener {
conn, err := net.ListenUnix("unix", &net.UnixAddr{socket, "unix"})
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
l.Printf("Listen %s : %s", socket, err)
}
default:
l.Printf("Listen %s : %s", socket, err)
}
if _, r_err := os.Stat(socket); r_err != nil {
l.Printf("Lstat %s : %s", socket, err)
}
os.Remove(socket)
conn, err = net.ListenUnix("unix", &net.UnixAddr{socket, "unix"})
}
os.Chown(socket, uid, gid)
wg.Add(1)
return &Listener{conn, end, wg}
}
示例5: UnixAudienceListener
func UnixAudienceListener(sockaddr string, sockmode os.FileMode, sockuid int, sockgid int) {
fi, err := os.Stat(sockaddr)
if err == nil {
if (fi.Mode() & os.ModeSocket) != 0 {
os.Remove(sockaddr)
} else {
o.Fail("%s exists and is not a socket", sockaddr)
}
}
err = os.MkdirAll(path.Dir(sockaddr), 0755)
o.MightFail(err, "Couldn't create socket directory")
laddr, err := net.ResolveUnixAddr("unix", sockaddr)
o.MightFail(err, "Couldn't resolve audience socket address")
old_umask := syscall.Umask(0777)
defer syscall.Umask(old_umask)
l, err := net.ListenUnix("unix", laddr)
o.MightFail(err, "Couldn't start audience unixsock listener")
if sockuid >= 0 || sockgid >= 0 {
err = os.Chown(sockaddr, sockuid, sockgid)
o.MightFail(err, "Couldn't chown audience unixsock listener")
}
err = os.Chmod(sockaddr, sockmode)
o.MightFail(err, "Couldn't chmod audience unixsock listener")
// make sure we clean up the unix socket when we die.
defer l.Close()
defer os.Remove(sockaddr)
AudienceListener(l)
}
示例6: StartClientHandler
func StartClientHandler(tree *ProcessTree, quit chan bool) {
path, _ := filepath.Abs(zeusSockName)
addr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
panic("Can't open socket.")
}
listener, err := net.ListenUnix("unix", addr)
if err != nil {
ErrorCantCreateListener()
}
defer listener.Close()
connections := make(chan *unixsocket.Usock)
go func() {
for {
if conn, err := listener.AcceptUnix(); err != nil {
errorUnableToAcceptSocketConnection()
time.Sleep(500 * time.Millisecond)
} else {
connections <- unixsocket.NewUsock(conn)
}
}
}()
for {
select {
case <-quit:
quit <- true
return
case conn := <-connections:
go handleClientConnection(tree, conn)
}
}
}
示例7: main
func main() {
e := New()
sckt, err := net.ListenUnix("unix", &net.UnixAddr{socketPath, "unix"})
if err != nil {
panic(err)
}
defer os.Remove(socketPath)
defer e.Conn().Close()
l := e.Loop(sckt)
err = e.LoadConfig(ConfigPath)
if err != nil {
e.Println(err.Error())
}
EVT:
for {
select {
case <-l.pre:
<-l.post
case msg := <-l.comm:
if verbose {
e.Println(msg)
}
case <-l.quit:
break EVT
}
}
e.Println("EXITING......\n")
}
示例8: setupTestServer
// setupTestServer creates a listener for the rest requests.
func setupTestServer() {
router := mux.NewRouter()
// register handlers for cni
t := router.Headers("Content-Type", "application/json").Methods("POST").Subrouter()
t.HandleFunc(cniapi.EPAddURL, httpWrapper(stubAddPod))
t.HandleFunc(cniapi.EPDelURL, httpWrapper(stubDeletePod))
driverPath := cniapi.ContivCniSocket
os.Remove(driverPath)
os.MkdirAll(cniapi.PluginPath, 0700)
go func() {
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: driverPath, Net: "unix"})
if err != nil {
panic(err)
}
logger.Infof("k8s test plugin listening on %s", driverPath)
http.Serve(l, router)
l.Close()
logger.Infof("k8s test plugin closing %s", driverPath)
}()
}
示例9: ListenUnix
// ListenUnix announces on the local network address laddr. The network net
// must be a: "unix" or "unixpacket". It returns an inherited net.Listener for
// the matching network and address, or creates a new one using net.ListenUnix.
func (n *Net) ListenUnix(nett string, laddr *net.UnixAddr) (*net.UnixListener, error) {
if err := n.inherit(); err != nil {
return nil, err
}
n.mutex.Lock()
defer n.mutex.Unlock()
// look for an inherited listener
for i, l := range n.inherited {
if l == nil { // we nil used inherited listeners
continue
}
if isSameAddr(l.Addr(), laddr) {
n.inherited[i] = nil
n.active = append(n.active, l)
return l.(*net.UnixListener), nil
}
}
// make a fresh listener
l, err := net.ListenUnix(nett, laddr)
if err != nil {
return nil, err
}
n.active = append(n.active, l)
return l, nil
}
示例10: unixListener
// unixListener returns the listener used for registrations (over unix sock)
func unixListener() (net.Listener, error) {
s := os.Getenv("LISTEN_FDS")
if s != "" {
// socket activated
lfds, err := strconv.ParseInt(s, 10, 16)
if err != nil {
return nil, errwrap.Wrap(errors.New("error parsing LISTEN_FDS env var"), err)
}
if lfds < 1 {
return nil, fmt.Errorf("LISTEN_FDS < 1")
}
return net.FileListener(os.NewFile(uintptr(listenFdsStart), "listen"))
} else {
dir := filepath.Dir(common.MetadataServiceRegSock)
err := os.MkdirAll(dir, 0755)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("failed to create %v", dir), err)
}
return net.ListenUnix("unix", &net.UnixAddr{
Net: "unix",
Name: common.MetadataServiceRegSock,
})
}
}
示例11: daemonIpc
func daemonIpc(done <-chan struct{}) {
if _, err := os.Stat(Config.ClueGetter.IPC_Socket); !os.IsNotExist(err) {
err = os.Remove(Config.ClueGetter.IPC_Socket)
if err != nil {
Log.Fatal(fmt.Sprintf("IPC Socket %s already exists and could not be removed: %s",
Config.ClueGetter.IPC_Socket, err.Error()))
}
}
l, err := net.ListenUnix("unix", &net.UnixAddr{Config.ClueGetter.IPC_Socket, "unix"})
if err != nil {
Log.Fatal(err)
}
go func() {
<-done
l.Close() // Also removes socket file
}()
for {
conn, err := l.AcceptUnix()
if err != nil {
_, open := <-done
if !open {
Log.Info("IPC Socket shutting down")
break
}
Log.Fatal("Critical error on IPC Socket: %s", err)
}
go daemonIpcHandleConn(conn)
}
}
示例12: ListenUnix
// ListenUnix announces on the local network address laddr. The network net
// must be a: "unix" or "unixpacket". It returns an inherited net.Listener for
// the matching network and address, or creates a new one using net.ListenUnix.
func (n *nimbleNet) ListenUnix(nett string, laddr *net.UnixAddr) (*net.UnixListener, error) {
if err := n.getInheritedListeners(); err != nil {
return nil, iodine.New(err, nil)
}
n.mutex.Lock()
defer n.mutex.Unlock()
// look for an inherited listener
for i, l := range n.inheritedListeners {
if l == nil { // we nil used inherited listeners
continue
}
equal := nimbleAddr{l.Addr()}.IsEqual(laddr)
if equal {
n.inheritedListeners[i] = nil
n.activeListeners = append(n.activeListeners, l)
return l.(*net.UnixListener), nil
}
}
// make a fresh listener
l, err := net.ListenUnix(nett, laddr)
if err != nil {
return nil, iodine.New(err, nil)
}
n.activeListeners = append(n.activeListeners, l)
return l, nil
}
示例13: StartUnixListener
// StartUnixListener starts up a unix domain socket listener for reconnects to
// be sent through
func (irc *Bot) StartUnixListener() {
unaddr, err := net.ResolveUnixAddr("unix", irc.unixastr)
if err != nil {
panic(err)
}
list, err := net.ListenUnix("unix", unaddr)
if err != nil {
panic(err)
}
irc.unixlist = list
con, err := list.AcceptUnix()
if err != nil {
fmt.Println("unix listener error: ", err)
return
}
list.Close()
fi, err := irc.con.(*net.TCPConn).File()
if err != nil {
panic(err)
}
err = sendfd.SendFD(con, fi)
if err != nil {
panic(err)
}
select {
case <-irc.Incoming:
default:
close(irc.Incoming)
}
close(irc.outgoing)
}
示例14: startIpc
func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
os.Remove(cfg.Endpoint) // in case it still exists from a previous run
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: cfg.Endpoint, Net: "unix"})
if err != nil {
return err
}
os.Chmod(cfg.Endpoint, 0600)
go func() {
for {
conn, err := l.AcceptUnix()
if err != nil {
glog.V(logger.Error).Infof("Error accepting ipc connection - %v\n", err)
continue
}
id := newIpcConnId()
glog.V(logger.Debug).Infof("New IPC connection with id %06d started\n", id)
go handle(id, conn, api, codec)
}
os.Remove(cfg.Endpoint)
}()
glog.V(logger.Info).Infof("IPC service started (%s)\n", cfg.Endpoint)
return nil
}
示例15: Open
// Open opens the transport.
func (c Config) Open() (transports.Transport, error) {
if c.Name == "" {
c.Name = path.Join(os.TempDir(), "telehash-"+randomString(8)+".sock")
}
if c.Mode == 0 {
c.Mode = 0700
}
c.Mode &= os.ModePerm
c.Mode |= os.ModeSocket
laddr, err := net.ResolveUnixAddr("unix", c.Name)
if err != nil {
return nil, err
}
listener, err := net.ListenUnix("unix", laddr)
if err != nil {
return nil, err
}
err = os.Chmod(laddr.Name, c.Mode)
if err != nil {
listener.Close()
os.Remove(laddr.Name)
return nil, err
}
return &transport{(*unixAddr)(laddr), listener}, nil
}