internal/wayland: relocate connection struct
All checks were successful
Test / Create distribution (push) Successful in 46s
Test / Sandbox (push) Successful in 2m30s
Test / Hakurei (push) Successful in 3m28s
Test / Sandbox (race detector) (push) Successful in 4m27s
Test / Hpkg (push) Successful in 4m26s
Test / Hakurei (race detector) (push) Successful in 5m17s
Test / Flake checks (push) Successful in 1m44s
All checks were successful
Test / Create distribution (push) Successful in 46s
Test / Sandbox (push) Successful in 2m30s
Test / Hakurei (push) Successful in 3m28s
Test / Sandbox (race detector) (push) Successful in 4m27s
Test / Hpkg (push) Successful in 4m26s
Test / Hakurei (race detector) (push) Successful in 5m17s
Test / Flake checks (push) Successful in 1m44s
This interface is getting replaced, so relocating it to the deprecated wrapper package before working on its replacement. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
12751932d1
commit
20dd2adaa4
@ -8,7 +8,7 @@ import (
|
|||||||
"hakurei.app/container/check"
|
"hakurei.app/container/check"
|
||||||
"hakurei.app/hst"
|
"hakurei.app/hst"
|
||||||
"hakurei.app/internal/acl"
|
"hakurei.app/internal/acl"
|
||||||
"hakurei.app/internal/wayland"
|
"hakurei.app/system/wayland"
|
||||||
)
|
)
|
||||||
|
|
||||||
type waylandConn interface {
|
type waylandConn interface {
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"hakurei.app/container/stub"
|
"hakurei.app/container/stub"
|
||||||
"hakurei.app/internal/acl"
|
"hakurei.app/internal/acl"
|
||||||
"hakurei.app/internal/wayland"
|
"hakurei.app/system/wayland"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stubWaylandConn struct {
|
type stubWaylandConn struct {
|
||||||
|
|||||||
@ -1,101 +1,26 @@
|
|||||||
package wayland
|
package wayland
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Conn represents a connection to the wayland display server.
|
func bindRawConn(done chan struct{}, rc syscall.RawConn, p, appID, instanceID string) ([2]int, error) {
|
||||||
type Conn struct {
|
var closeFds [2]int
|
||||||
conn *net.UnixConn
|
if err := syscall.Pipe2(closeFds[0:], syscall.O_CLOEXEC); err != nil {
|
||||||
|
return closeFds, err
|
||||||
done chan struct{}
|
|
||||||
doneOnce sync.Once
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach connects Conn to a wayland socket.
|
|
||||||
func (c *Conn) Attach(p string) (err error) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
if c.conn != nil {
|
|
||||||
return errors.New("socket already attached")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.conn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: p, Net: "unix"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close releases resources and closes the connection to the wayland compositor.
|
|
||||||
func (c *Conn) Close() error {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
if c.done == nil {
|
|
||||||
return errors.New("no socket bound")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.doneOnce.Do(func() {
|
|
||||||
c.done <- struct{}{}
|
|
||||||
<-c.done
|
|
||||||
})
|
|
||||||
|
|
||||||
// closed by wayland
|
|
||||||
runtime.SetFinalizer(c.conn, nil)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bind binds the new socket to pathname.
|
|
||||||
func (c *Conn) Bind(pathname, appID, instanceID string) (*os.File, error) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
if c.conn == nil {
|
|
||||||
return nil, errors.New("socket not attached")
|
|
||||||
}
|
|
||||||
if c.done != nil {
|
|
||||||
return nil, errors.New("socket already bound")
|
|
||||||
}
|
|
||||||
|
|
||||||
if rc, err := c.conn.SyscallConn(); err != nil {
|
|
||||||
// unreachable
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
c.done = make(chan struct{})
|
|
||||||
return bindRawConn(c.done, rc, pathname, appID, instanceID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func bindRawConn(done chan struct{}, rc syscall.RawConn, p, appID, instanceID string) (*os.File, error) {
|
|
||||||
var syncPipe [2]*os.File
|
|
||||||
|
|
||||||
if r, w, err := os.Pipe(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
syncPipe[0] = r
|
|
||||||
syncPipe[1] = w
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setupDone := make(chan error, 1) // does not block with c.done
|
setupDone := make(chan error, 1) // does not block with c.done
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := rc.Control(func(fd uintptr) {
|
if err := rc.Control(func(fd uintptr) {
|
||||||
// prevent runtime from closing the read end of sync fd
|
|
||||||
runtime.SetFinalizer(syncPipe[0], nil)
|
|
||||||
|
|
||||||
// allow the Bind method to return after setup
|
// allow the Bind method to return after setup
|
||||||
setupDone <- bind(fd, p, appID, instanceID, syncPipe[0].Fd())
|
setupDone <- bind(fd, p, appID, instanceID, uintptr(closeFds[1]))
|
||||||
close(setupDone)
|
close(setupDone)
|
||||||
|
|
||||||
// keep socket alive until done is requested
|
// keep socket alive until done is requested
|
||||||
<-done
|
<-done
|
||||||
runtime.KeepAlive(syncPipe[1])
|
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
setupDone <- err
|
setupDone <- err
|
||||||
}
|
}
|
||||||
@ -105,7 +30,7 @@ func bindRawConn(done chan struct{}, rc syscall.RawConn, p, appID, instanceID st
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// return write end of the pipe
|
// return write end of the pipe
|
||||||
return syncPipe[1], <-setupDone
|
return closeFds, <-setupDone
|
||||||
}
|
}
|
||||||
|
|
||||||
func bind(fd uintptr, p, appID, instanceID string, syncFd uintptr) error {
|
func bind(fd uintptr, p, appID, instanceID string, syncFd uintptr) error {
|
||||||
|
|||||||
@ -36,7 +36,7 @@ hakurei_wayland_res hakurei_bind_wayland_fd(
|
|||||||
int fd,
|
int fd,
|
||||||
const char *app_id,
|
const char *app_id,
|
||||||
const char *instance_id,
|
const char *instance_id,
|
||||||
int sync_fd) {
|
int close_fd) {
|
||||||
hakurei_wayland_res res = HAKUREI_WAYLAND_SUCCESS; /* see wayland.go for handling */
|
hakurei_wayland_res res = HAKUREI_WAYLAND_SUCCESS; /* see wayland.go for handling */
|
||||||
|
|
||||||
struct wl_display *display = NULL;
|
struct wl_display *display = NULL;
|
||||||
@ -88,7 +88,7 @@ hakurei_wayland_res hakurei_bind_wayland_fd(
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
security_context = wp_security_context_manager_v1_create_listener(security_context_manager, listen_fd, sync_fd);
|
security_context = wp_security_context_manager_v1_create_listener(security_context_manager, listen_fd, close_fd);
|
||||||
if (security_context == NULL) { /* not reached */
|
if (security_context == NULL) { /* not reached */
|
||||||
res = HAKUREI_WAYLAND_NOT_AVAIL;
|
res = HAKUREI_WAYLAND_NOT_AVAIL;
|
||||||
goto out;
|
goto out;
|
||||||
|
|||||||
@ -21,4 +21,4 @@ hakurei_wayland_res hakurei_bind_wayland_fd(
|
|||||||
int fd,
|
int fd,
|
||||||
const char *app_id,
|
const char *app_id,
|
||||||
const char *instance_id,
|
const char *instance_id,
|
||||||
int sync_fd);
|
int close_fd);
|
||||||
|
|||||||
@ -4,13 +4,86 @@
|
|||||||
package wayland
|
package wayland
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
_ "unsafe" // for go:linkname
|
_ "unsafe" // for go:linkname
|
||||||
|
|
||||||
"hakurei.app/internal/wayland"
|
"hakurei.app/internal/wayland"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Conn represents a connection to the wayland display server.
|
// Conn represents a connection to the wayland display server.
|
||||||
type Conn = wayland.Conn
|
type Conn struct {
|
||||||
|
conn *net.UnixConn
|
||||||
|
|
||||||
|
done chan struct{}
|
||||||
|
doneOnce sync.Once
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attach connects Conn to a wayland socket.
|
||||||
|
func (c *Conn) Attach(p string) (err error) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if c.conn != nil {
|
||||||
|
return errors.New("socket already attached")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.conn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: p, Net: "unix"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close releases resources and closes the connection to the wayland compositor.
|
||||||
|
func (c *Conn) Close() error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if c.done == nil {
|
||||||
|
return errors.New("no socket bound")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.doneOnce.Do(func() {
|
||||||
|
c.done <- struct{}{}
|
||||||
|
<-c.done
|
||||||
|
})
|
||||||
|
|
||||||
|
// closed by wayland
|
||||||
|
runtime.SetFinalizer(c.conn, nil)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname bindRawConn hakurei.app/internal/wayland.bindRawConn
|
||||||
|
func bindRawConn(done chan struct{}, rc syscall.RawConn, p, appID, instanceID string) ([2]int, error)
|
||||||
|
|
||||||
|
// Bind binds the new socket to pathname.
|
||||||
|
func (c *Conn) Bind(pathname, appID, instanceID string) (*os.File, error) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if c.conn == nil {
|
||||||
|
return nil, errors.New("socket not attached")
|
||||||
|
}
|
||||||
|
if c.done != nil {
|
||||||
|
return nil, errors.New("socket already bound")
|
||||||
|
}
|
||||||
|
|
||||||
|
if rc, err := c.conn.SyscallConn(); err != nil {
|
||||||
|
// unreachable
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
c.done = make(chan struct{})
|
||||||
|
if closeFds, err := bindRawConn(c.done, rc, pathname, appID, instanceID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return os.NewFile(uintptr(closeFds[1]), "close_fd"), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// WaylandDisplay contains the name of the server socket
|
// WaylandDisplay contains the name of the server socket
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user