File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "os"
5
+ "os/signal"
4
6
"strings"
7
+ "syscall"
5
8
6
9
// TODO: fix this dependency. It's a nice log tho
7
10
log "github.com/sirupsen/logrus"
@@ -253,7 +256,38 @@ func (app *Application) Main() {
253
256
gtk .Main ()
254
257
}
255
258
259
+ const lockFile = "/tmp/launchy.lock"
260
+
261
+ func handleInterruptSignal () {
262
+ c := make (chan os.Signal , 1 )
263
+ signal .Notify (c , os .Interrupt , syscall .SIGTERM )
264
+ go func () {
265
+ <- c
266
+ log .Info ("Interrupt signal received. Exiting gracefully..." )
267
+ gtk .MainQuit ()
268
+ }()
269
+ }
270
+
256
271
func main () {
272
+ file , err := os .OpenFile (lockFile , os .O_CREATE | os .O_RDWR , 0644 )
273
+ if err == nil {
274
+ defer os .Remove (lockFile )
275
+ defer file .Close ()
276
+
277
+ // Try to lock the file
278
+ err = syscall .Flock (int (file .Fd ()), syscall .LOCK_EX | syscall .LOCK_NB )
279
+ if err != nil {
280
+ log .Info ("Another instance of Launchy is already running." )
281
+ return
282
+ }
283
+ defer syscall .Flock (int (file .Fd ()), syscall .LOCK_UN )
284
+ } else {
285
+ log .Warnf ("Failed to create/open lock file: %s" , err )
286
+ return
287
+ }
288
+
289
+ handleInterruptSignal ()
290
+
257
291
app := NewApplication ()
258
292
app .Main ()
259
293
}
You can’t perform that action at this time.
0 commit comments