5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-07 19:31:03 +08:00
wails/v3/pkg/application/single_instance_linux.go
Fabio Massaioli e7c134de4e
[v3] Late service registration and error handling overhaul (#4066)
* Add service registration method

* Fix error handling and formatting in messageprocessor

* Add configurable error handling

* Improve error strings

* Fix service shutdown on macOS

* Add post shutdown hook

* Better fatal errors

* Add startup/shutdown sequence tests

* Improve debug messages

* Update JS runtime

* Update docs

* Update changelog

* Fix log message in clipboard message processor

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Remove panic in RegisterService

* Fix linux tests (hopefully)

* Fix error formatting everywhere

* Fix typo in windows webview

* Tidy example mods

* Set application name in tests

* Fix ubuntu test workflow

* Cleanup template test pipeline

* Fix dev build detection on Go 1.24

* Update template go.mod/sum to Go 1.24

* Remove redundant caching in template tests

* Final format string cleanup

* Fix wails3 tool references

* Fix legacy log calls

* Remove formatJS and simplify format strings

* Fix indirect import

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-02-19 09:27:41 +01:00

102 lines
2.2 KiB
Go

//go:build linux
package application
import (
"errors"
"os"
"strings"
"sync"
"syscall"
"github.com/godbus/dbus/v5"
)
type dbusHandler func(string)
var setup sync.Once
func (f dbusHandler) SendMessage(message string) *dbus.Error {
f(message)
return nil
}
type linuxLock struct {
file *os.File
uniqueID string
dbusPath string
dbusName string
manager *singleInstanceManager
}
func newPlatformLock(manager *singleInstanceManager) (platformLock, error) {
return &linuxLock{
manager: manager,
}, nil
}
func (l *linuxLock) acquire(uniqueID string) error {
if uniqueID == "" {
return errors.New("UniqueID is required for single instance lock")
}
id := "wails_app_" + strings.ReplaceAll(strings.ReplaceAll(uniqueID, "-", "_"), ".", "_")
l.dbusName = "org." + id + ".SingleInstance"
l.dbusPath = "/org/" + id + "/SingleInstance"
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
setup.Do(func() {
f := dbusHandler(func(message string) {
secondInstanceBuffer <- message
})
err = conn.Export(f, dbus.ObjectPath(l.dbusPath), l.dbusName)
})
if err != nil {
return err
}
reply, err := conn.RequestName(l.dbusName, dbus.NameFlagDoNotQueue)
if err != nil {
return err
}
// if name already taken, try to send args to existing instance, if no success just launch new instance
if reply == dbus.RequestNameReplyExists {
return alreadyRunningError
}
return nil
}
func (l *linuxLock) release() {
if l.file != nil {
syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
l.file.Close()
os.Remove(l.file.Name())
l.file = nil
}
}
func (l *linuxLock) notify(data string) error {
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
err = conn.Object(l.dbusName, dbus.ObjectPath(l.dbusPath)).Call(l.dbusName+".SendMessage", 0, data).Store()
if err != nil {
return err
}
os.Exit(l.manager.options.ExitCode)
return nil
}