github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/getty/getty.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // getty Open a TTY and invoke a shell 6 // There are no special options and no login support 7 // Also getty exits after starting the shell so if one exits the shell, there 8 // is no more shell! 9 // 10 // Synopsis: 11 // getty <port> <baud> [term] 12 package main 13 14 import ( 15 "flag" 16 "fmt" 17 "log" 18 "os" 19 "os/exec" 20 "strconv" 21 22 "github.com/u-root/u-root/pkg/termios" 23 "github.com/u-root/u-root/pkg/upath" 24 ) 25 26 var ( 27 verbose = flag.Bool("v", false, "verbose log") 28 debug = func(string, ...interface{}) {} 29 cmdList []string 30 envs []string 31 ) 32 33 func init() { 34 r := upath.UrootPath 35 cmdList = []string{ 36 r("/bin/defaultsh"), 37 r("/bin/sh"), 38 } 39 } 40 41 func main() { 42 flag.Parse() 43 44 if *verbose { 45 debug = log.Printf 46 } 47 48 port := flag.Arg(0) 49 baud, err := strconv.Atoi(flag.Arg(1)) 50 if err != nil { 51 baud = 0 52 } 53 term := flag.Arg(2) 54 55 log.SetPrefix("getty: ") 56 57 ttyS, err := termios.NewTTYS(port) 58 if err != nil { 59 log.Fatalf("Unable to open port %s: %v", port, err) 60 } 61 62 if _, err := ttyS.Serial(baud); err != nil { 63 log.Printf("Unable to configure port %s and set baudrate %d: %v", port, baud, err) 64 } 65 66 // Output the u-root banner 67 log.New(ttyS, "", log.LstdFlags).Printf("Welcome to u-root!") 68 fmt.Fprintln(ttyS, ` _`) 69 fmt.Fprintln(ttyS, ` _ _ _ __ ___ ___ | |_`) 70 fmt.Fprintln(ttyS, ` | | | |____| '__/ _ \ / _ \| __|`) 71 fmt.Fprintln(ttyS, ` | |_| |____| | | (_) | (_) | |_`) 72 fmt.Fprintln(ttyS, ` \__,_| |_| \___/ \___/ \__|`) 73 fmt.Fprintln(ttyS) 74 75 if term != "" { 76 err = os.Setenv("TERM", term) 77 if err != nil { 78 debug("Unable to set 'TERM=%s': %v", port, err) 79 } 80 } 81 envs = os.Environ() 82 debug("envs %v", envs) 83 84 for _, v := range cmdList { 85 debug("Trying to run %v", v) 86 if _, err := os.Stat(v); os.IsNotExist(err) { 87 debug("%v", err) 88 continue 89 } 90 91 cmd := exec.Command(v) 92 cmd.Env = envs 93 ttyS.Ctty(cmd) 94 debug("running %v", cmd) 95 if err := cmd.Start(); err != nil { 96 log.Printf("Error starting %v: %v", v, err) 97 continue 98 } 99 if err := cmd.Process.Release(); err != nil { 100 log.Printf("Error releasing process %v:%v", v, err) 101 } 102 // stop after first valid command 103 return 104 } 105 log.Printf("No suitable executable found in %+v", cmdList) 106 }