Debian Codegraph @

lib/pinentry/ttyname.go

raw
1 package pinentry
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7
8 "git.sr.ht/~rjarry/aerc/lib/log"
9 )
10
11 var missingGPGTTYmsg = `
12 You need to set GPG_TTY manually before starting aerc. Add the following to your
13 .bashrc or whatever initialization file is used for shell invocations:
14
15 GPG_TTY=$(tty)
16 export GPG_TTY
17
18 Further information can be found here:
19 https://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html
20 `
21
22 // ttyname returns current name of the pty. This is necessary in order to tell
23 // pinentry where to ask for the passphrase.
24 //
25 // If there is a GPG_TTY environment variable set, use this one. Otherwise, try
26 // readline() on /proc/<pid>/fd/0.
27 //
28 // If both approaches fail, the user's only option is to set GPG_TTY manually.
29 //
30 // If tty name could not be determined, an empty string is returned.
31 func ttyname() string {
32 if s := os.Getenv("GPG_TTY"); s != "" {
33 return s
34 }
35
36 // try readlink or else show missing GPG_TTY warning msg
37 tty, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/0", os.Getpid()))
38 if err != nil {
39 log.Debugf("readlink: '%s' with err: %v", tty, err)
40 log.Warnf(missingGPGTTYmsg)
41 return ""
42 }
43
44 return strings.TrimSpace(tty)
45 }