|
18 | 18 | package ldconfig |
19 | 19 |
|
20 | 20 | import ( |
| 21 | + "bufio" |
21 | 22 | "fmt" |
22 | 23 | "os" |
23 | 24 | "os/exec" |
@@ -75,21 +76,81 @@ func (l *Ldconfig) UpdateLDCache(directories ...string) error { |
75 | 76 | return err |
76 | 77 | } |
77 | 78 |
|
| 79 | + // Explicitly specify using /etc/ld.so.conf since the host's ldconfig may |
| 80 | + // be configured to use a different config file by default. |
| 81 | + configFilePath := "/etc/ld.so.conf" |
| 82 | + filteredDirectories, err := l.filterDirectories(configFilePath, directories...) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
78 | 87 | args := []string{ |
79 | 88 | filepath.Base(ldconfigPath), |
80 | | - // Explicitly specify using /etc/ld.so.conf since the host's ldconfig may |
81 | | - // be configured to use a different config file by default. |
82 | | - "-f", "/etc/ld.so.conf", |
| 89 | + "-f", configFilePath, |
83 | 90 | "-C", "/etc/ld.so.cache", |
84 | 91 | } |
85 | 92 |
|
86 | | - if err := createLdsoconfdFile(ldsoconfdFilenamePattern, directories...); err != nil { |
| 93 | + if err := createLdsoconfdFile(ldsoconfdFilenamePattern, filteredDirectories...); err != nil { |
87 | 94 | return fmt.Errorf("failed to update ld.so.conf.d: %w", err) |
88 | 95 | } |
89 | 96 |
|
90 | 97 | return SafeExec(ldconfigPath, args, nil) |
91 | 98 | } |
92 | 99 |
|
| 100 | +func (l *Ldconfig) filterDirectories(configFilePath string, directories ...string) ([]string, error) { |
| 101 | + processedConfFiles := make(map[string]bool) |
| 102 | + ldconfigFilenames := []string{configFilePath} |
| 103 | + |
| 104 | + ldconfigDirs := make(map[string]string) |
| 105 | + for len(ldconfigFilenames) > 0 { |
| 106 | + ldconfigFilename := ldconfigFilenames[0] |
| 107 | + ldconfigFilenames = ldconfigFilenames[1:] |
| 108 | + if processedConfFiles[ldconfigFilename] { |
| 109 | + continue |
| 110 | + } |
| 111 | + processedConfFiles[ldconfigFilename] = true |
| 112 | + |
| 113 | + if len(ldconfigFilename) == 0 { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + ldsoconf, err := os.Open(ldconfigFilename) |
| 118 | + if os.IsNotExist(err) { |
| 119 | + continue |
| 120 | + } |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + defer ldsoconf.Close() |
| 125 | + |
| 126 | + scanner := bufio.NewScanner(ldsoconf) |
| 127 | + for scanner.Scan() { |
| 128 | + line := strings.TrimSpace(scanner.Text()) |
| 129 | + switch { |
| 130 | + case strings.HasPrefix(line, "#") || len(line) == 0: |
| 131 | + continue |
| 132 | + case strings.HasPrefix(line, "include "): |
| 133 | + includes, err := filepath.Glob(strings.TrimPrefix(line, "include ")) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + ldconfigFilenames = append(ldconfigFilenames, includes...) |
| 138 | + default: |
| 139 | + ldconfigDirs[line] = ldconfigFilename |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + var filtered []string |
| 145 | + for _, d := range directories { |
| 146 | + if _, ok := ldconfigDirs[d]; ok { |
| 147 | + continue |
| 148 | + } |
| 149 | + filtered = append(filtered, d) |
| 150 | + } |
| 151 | + return filtered, nil |
| 152 | +} |
| 153 | + |
93 | 154 | func (l *Ldconfig) prepareRoot() (string, error) { |
94 | 155 | // To prevent leaking the parent proc filesystem, we create a new proc mount |
95 | 156 | // in the specified root. |
|
0 commit comments