1818package ldconfig
1919
2020import (
21+ "flag"
2122 "fmt"
2223 "os"
2324 "os/exec"
@@ -39,40 +40,58 @@ const (
3940type Ldconfig struct {
4041 ldconfigPath string
4142 inRoot string
43+ directories []string
4244}
4345
4446// NewRunner creates an exec.Cmd that can be used to run ldconfig.
4547func NewRunner (id string , ldconfigPath string , containerRoot string , additionalargs ... string ) (* exec.Cmd , error ) {
4648 args := []string {
4749 id ,
48- strings .TrimPrefix (config .NormalizeLDConfigPath ("@" + ldconfigPath ), "@" ),
49- containerRoot ,
50+ "--ldconfig-path" , strings .TrimPrefix (config .NormalizeLDConfigPath ("@" + ldconfigPath ), "@" ),
51+ "--container-root" , containerRoot ,
5052 }
5153 args = append (args , additionalargs ... )
5254
5355 return createReexecCommand (args )
5456}
5557
56- // New creates an Ldconfig struct that is used to perform operations on the
57- // ldcache and libraries in a particular root (e.g. a container).
58- func New (ldconfigPath string , inRoot string ) (* Ldconfig , error ) {
59- l := & Ldconfig {
60- ldconfigPath : ldconfigPath ,
61- inRoot : inRoot ,
62- }
63- if ldconfigPath == "" {
58+ // NewFromRunnerArgs creates an Ldconfig struct from the args passed to the Cmd
59+ // above.
60+ // This struct is used to perform operations on the ldcache and libraries in a
61+ // particular root (e.g. a container).
62+ //
63+ // args[0] is the reexec initializer function name
64+ // args[1] is the path of the ldconfig binary on the host
65+ // args[2] is the container root directory
66+ // args[3] is the flag indicating whether the container is being run rootless.
67+ // The remaining args are folders where soname symlinks need to be created.
68+ func NewFromArgs (args ... string ) (* Ldconfig , error ) {
69+ fs := flag .NewFlagSet (args [1 ], flag .ExitOnError )
70+ ldconfigPath := fs .String ("ldconfig-path" , "" , "the path to ldconfig on the host" )
71+ containerRoot := fs .String ("container-root" , "" , "the path in which ldconfig must be run" )
72+ if err := fs .Parse (args [1 :]); err != nil {
73+ return nil , err
74+ }
75+
76+ if * ldconfigPath == "" {
6477 return nil , fmt .Errorf ("an ldconfig path must be specified" )
6578 }
66- if inRoot == "" || inRoot == "/" {
79+ if * containerRoot == "" || * containerRoot == "/" {
6780 return nil , fmt .Errorf ("ldconfig must be run in the non-system root" )
6881 }
82+
83+ l := & Ldconfig {
84+ ldconfigPath : * ldconfigPath ,
85+ inRoot : * containerRoot ,
86+ directories : fs .Args (),
87+ }
6988 return l , nil
7089}
7190
7291// CreateSonameSymlinks uses ldconfig to create the soname symlinks in the
7392// specified directories.
74- func (l * Ldconfig ) CreateSonameSymlinks (directories ... string ) error {
75- if len (directories ) == 0 {
93+ func (l * Ldconfig ) CreateSonameSymlinks () error {
94+ if len (l . directories ) == 0 {
7695 return nil
7796 }
7897 ldconfigPath , err := l .prepareRoot ()
@@ -87,12 +106,12 @@ func (l *Ldconfig) CreateSonameSymlinks(directories ...string) error {
87106 // Specify -n to only process the specified directories.
88107 "-n" ,
89108 }
90- args = append (args , directories ... )
109+ args = append (args , l . directories ... )
91110
92111 return SafeExec (ldconfigPath , args , nil )
93112}
94113
95- func (l * Ldconfig ) UpdateLDCache (directories ... string ) error {
114+ func (l * Ldconfig ) UpdateLDCache () error {
96115 ldconfigPath , err := l .prepareRoot ()
97116 if err != nil {
98117 return err
@@ -115,12 +134,12 @@ func (l *Ldconfig) UpdateLDCache(directories ...string) error {
115134 // containing the required directories, otherwise we add the specified
116135 // directories to the ldconfig command directly.
117136 if l .ldsoconfdDirectoryExists () {
118- err := createLdsoconfdFile (ldsoconfdFilenamePattern , directories ... )
137+ err := createLdsoconfdFile (ldsoconfdFilenamePattern , l . directories ... )
119138 if err != nil {
120139 return fmt .Errorf ("failed to update ld.so.conf.d: %w" , err )
121140 }
122141 } else {
123- args = append (args , directories ... )
142+ args = append (args , l . directories ... )
124143 }
125144
126145 return SafeExec (ldconfigPath , args , nil )
0 commit comments