github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/sys/hidepass_unix.go (about)

     1  // +build !windows
     2  
     3  package sys
     4  
     5  import (
     6  	"os"
     7  	"fmt"
     8  	"syscall"
     9  	"os/signal"
    10  )
    11  
    12  var wsta syscall.WaitStatus = 0
    13  
    14  
    15  func enterpassext(b []byte) (n int) {
    16  	si := make(chan os.Signal, 10)
    17  	br := make(chan bool)
    18  	fd := []uintptr{os.Stdout.Fd()}
    19  
    20  	signal.Notify(si, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGTERM)
    21  	go sighndl(fd, si, br)
    22  
    23  	pid, er := syscall.ForkExec("/bin/stty", []string{"stty", "-echo"}, &syscall.ProcAttr{Dir: "", Files: fd})
    24  	if er == nil {
    25  		syscall.Wait4(pid, &wsta, 0, nil)
    26  		n = getline(b)
    27  		close(br)
    28  		echo(fd)
    29  		fmt.Println()
    30  	} else {
    31  		n = getline(b)
    32  	}
    33  
    34  	return
    35  }
    36  
    37  
    38  func echo(fd []uintptr) {
    39  	pid, e := syscall.ForkExec("/bin/stty", []string{"stty", "echo"}, &syscall.ProcAttr{Dir: "", Files: fd})
    40  	if e == nil {
    41  		syscall.Wait4(pid, &wsta, 0, nil)
    42  	}
    43  }
    44  
    45  
    46  func sighndl(fd []uintptr, signal chan os.Signal, br chan bool) {
    47  	select {
    48  		case <-signal:
    49  			echo(fd)
    50  			os.Exit(-1)
    51  		case <-br:
    52  	}
    53  }
    54  
    55  func init() {
    56  	secrespass = enterpassext
    57  }