1818package ldconfig
1919
2020import (
21+ "flag"
2122 "fmt"
2223 "os"
2324 "os/exec"
@@ -39,37 +40,60 @@ 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 ,
58+ // NewFromArgs 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+ // The following flags are required:
65+ //
66+ // --ldconfig-path=LDCONFIG_PATH the path to ldconfig on the host
67+ // --container-root=CONTAINER_ROOT the path in which ldconfig must be run
68+ //
69+ // The remaining args are folders where soname symlinks need to be created.
70+ func NewFromArgs (args ... string ) (* Ldconfig , error ) {
71+ if len (args ) < 1 {
72+ return nil , fmt .Errorf ("incorrect arguments: %v" , args )
73+ }
74+ fs := flag .NewFlagSet (args [1 ], flag .ExitOnError )
75+ ldconfigPath := fs .String ("ldconfig-path" , "" , "the path to ldconfig on the host" )
76+ containerRoot := fs .String ("container-root" , "" , "the path in which ldconfig must be run" )
77+ if err := fs .Parse (args [1 :]); err != nil {
78+ return nil , err
6279 }
63- if ldconfigPath == "" {
80+
81+ if * ldconfigPath == "" {
6482 return nil , fmt .Errorf ("an ldconfig path must be specified" )
6583 }
66- if inRoot == "" || inRoot == "/" {
84+ if * containerRoot == "" || * containerRoot == "/" {
6785 return nil , fmt .Errorf ("ldconfig must be run in the non-system root" )
6886 }
87+
88+ l := & Ldconfig {
89+ ldconfigPath : * ldconfigPath ,
90+ inRoot : * containerRoot ,
91+ directories : fs .Args (),
92+ }
6993 return l , nil
7094}
7195
72- func (l * Ldconfig ) UpdateLDCache (directories ... string ) error {
96+ func (l * Ldconfig ) UpdateLDCache () error {
7397 ldconfigPath , err := l .prepareRoot ()
7498 if err != nil {
7599 return err
@@ -83,7 +107,7 @@ func (l *Ldconfig) UpdateLDCache(directories ...string) error {
83107 "-C" , "/etc/ld.so.cache" ,
84108 }
85109
86- if err := createLdsoconfdFile (ldsoconfdFilenamePattern , directories ... ); err != nil {
110+ if err := createLdsoconfdFile (ldsoconfdFilenamePattern , l . directories ... ); err != nil {
87111 return fmt .Errorf ("failed to update ld.so.conf.d: %w" , err )
88112 }
89113
0 commit comments