github.com/rminnich/u-root@v7.0.0+incompatible/cmds/core/lsdrivers/main_linux.go (about) 1 // Copyright 2012-2017 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 // lsdrivers lists driver usage on Linux systems 6 // 7 // Synopsis: 8 // lsdrivers [-u] 9 // 10 // Description: 11 // List driver usage. This program is mostly useful for scripts. 12 // 13 // Options: 14 // -u: list unused drivers 15 package main 16 17 import ( 18 "flag" 19 "fmt" 20 "io/ioutil" 21 "log" 22 "os" 23 "path/filepath" 24 "strings" 25 ) 26 27 var unused = flag.Bool("u", false, "Show unused drivers") 28 29 // hasDevices returns true if there is a non-symlink, i.e. regular, 30 // file in driverpath. This indicates that it is real hardware. 31 // If the path has any sort of error, return false. 32 func hasDevices(driverpath string) bool { 33 files, err := ioutil.ReadDir(driverpath) 34 if err != nil { 35 return false 36 } 37 for _, file := range files { 38 if file.Mode()&os.ModeSymlink != 0 { 39 return true 40 } 41 } 42 return false 43 } 44 45 func lsdrivers(bus string, unused bool) ([]string, error) { 46 var d []string 47 files, err := ioutil.ReadDir(bus) 48 if err != nil { 49 return nil, err 50 } 51 52 for _, f := range files { 53 n := filepath.Join(bus, f.Name(), "drivers") 54 drivers, err := ioutil.ReadDir(n) 55 // In some cases the directory does not exist. 56 if err != nil { 57 continue 58 } 59 for _, driver := range drivers { 60 n := filepath.Join(bus, f.Name(), "drivers", driver.Name()) 61 if hasDevices(n) != unused { 62 d = append(d, fmt.Sprintf(f.Name()+"."+driver.Name())) 63 } 64 } 65 } 66 return d, nil 67 } 68 func main() { 69 flag.Parse() 70 drivers, err := lsdrivers("/sys/bus", *unused) 71 if err != nil { 72 log.Fatal(err) 73 } 74 if _, err := fmt.Println(strings.Join(drivers, "\n")); err != nil { 75 log.Fatal(err) 76 } 77 }