summaryrefslogtreecommitdiff
path: root/gocryptfs_main/sendsig.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-10-06 00:29:08 +0200
committerJakob Unterwurzacher2015-10-06 00:31:18 +0200
commit022a6968ae0ede1259141e32b8e32553dad7d824 (patch)
treebc9aa404e3bf7d6778a81b168fc6c9743531ceab /gocryptfs_main/sendsig.go
parent552c32c5e9378b85e52c420c4dd2d7ccc827556f (diff)
Implement proper daemonization
The shell wrapper sends gocryptfs into the background and waits for SIGUSR1
Diffstat (limited to 'gocryptfs_main/sendsig.go')
-rw-r--r--gocryptfs_main/sendsig.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/gocryptfs_main/sendsig.go b/gocryptfs_main/sendsig.go
new file mode 100644
index 0000000..98bd448
--- /dev/null
+++ b/gocryptfs_main/sendsig.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "syscall"
+ "bytes"
+ "fmt"
+ "os"
+ "io/ioutil"
+)
+
+// cmdline looks like this: /bin/bash \0 /path/to/gocryptfs \0 --zerokey \0 ...
+const (
+ WRAPPER_PREFIX = "/bin/bash\000"
+ WRAPPER_CONTAINS = "gocryptfs\000"
+)
+
+// Send USR1 to the "gocryptfs" wrapper shell script. This notifies it that the
+// mounting has completed sucessfully.
+//
+// Checks /proc/$PPID/cmdline to make sure we do not kill an unrelated process.
+func sendSig() {
+ ppid := os.Getppid()
+ fn := fmt.Sprintf("/proc/%d/cmdline", ppid)
+ cmdline, err := ioutil.ReadFile(fn)
+ if err != nil {
+ fmt.Printf("sendSig: ReadFile: %v\n", err)
+ return
+ }
+ if bytes.HasPrefix(cmdline, []byte(WRAPPER_PREFIX)) && bytes.Contains(cmdline, []byte(WRAPPER_CONTAINS)) {
+ p, err := os.FindProcess(ppid)
+ if err != nil {
+ fmt.Printf("sendSig: FindProcess: %v\n", err)
+ return
+ }
+ err = p.Signal(syscall.SIGUSR1)
+ if err != nil {
+ fmt.Printf("sendSig: Signal: %v\n", err)
+ }
+ } else {
+ fmt.Printf("Not running under the gocryptfs wrapper - will not daemonize\n")
+ }
+}