github.com/fafucoder/cilium@v1.6.11/pkg/modules/modules_linux.go (about) 1 // Copyright 2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package modules 16 17 import ( 18 "bufio" 19 "fmt" 20 "io" 21 "os" 22 "strings" 23 ) 24 25 const ( 26 modulesFilepath = "/proc/modules" 27 ) 28 29 func moduleLoader() string { 30 return "modprobe" 31 } 32 33 // parseModulesFile returns the list of loaded kernel modules names. 34 func parseModulesFile(r io.Reader) ([]string, error) { 35 var result []string 36 37 scanner := bufio.NewScanner(r) 38 scanner.Split(bufio.ScanLines) 39 40 for scanner.Scan() { 41 moduleInfoRaw := scanner.Text() 42 moduleInfoSeparated := strings.Split(moduleInfoRaw, " ") 43 if len(moduleInfoSeparated) < 6 { 44 return nil, fmt.Errorf( 45 "invalid module info - it has %d fields (less than 6): %s", 46 len(moduleInfoSeparated), moduleInfoRaw) 47 } 48 49 result = append(result, moduleInfoSeparated[0]) 50 } 51 52 return result, nil 53 } 54 55 // listModules returns the list of loaded kernel modules names parsed from 56 // /proc/modules. 57 func listModules() ([]string, error) { 58 fModules, err := os.Open(modulesFilepath) 59 if err != nil { 60 return nil, fmt.Errorf( 61 "failed to open modules information at %s: %s", 62 modulesFilepath, err) 63 } 64 defer fModules.Close() 65 return parseModulesFile(fModules) 66 }