File: ttyname.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (45 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package pinentry

import (
	"fmt"
	"os"
	"strings"

	"git.sr.ht/~rjarry/aerc/lib/log"
)

var missingGPGTTYmsg = `
You need to set GPG_TTY manually before starting aerc. Add the following to your
.bashrc or whatever initialization file is used for shell invocations:

	GPG_TTY=$(tty)
	export GPG_TTY

Further information can be found here:
https://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html
`

// ttyname returns current name of the pty. This is necessary in order to tell
// pinentry where to ask for the passphrase.
//
// If there is a GPG_TTY environment variable set, use this one. Otherwise, try
// readline() on /proc/<pid>/fd/0.
//
// If both approaches fail, the user's only option is to set GPG_TTY manually.
//
// If tty name could not be determined, an empty string is returned.
func ttyname() string {
	if s := os.Getenv("GPG_TTY"); s != "" {
		return s
	}

	// try readlink or else show missing GPG_TTY warning msg
	tty, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/0", os.Getpid()))
	if err != nil {
		log.Debugf("readlink: '%s' with err: %v", tty, err)
		log.Warnf(missingGPGTTYmsg)
		return ""
	}

	return strings.TrimSpace(tty)
}