4040#include "xbps_api_impl.h"
4141
4242static int
43- pfcexec (struct xbps_handle * xhp , const char * file , const char * * argv )
43+ pfcexec (const struct xbps_handle * xhp , const char * file , const char * * argv )
4444{
4545 pid_t child ;
4646 int status ;
@@ -86,7 +86,54 @@ pfcexec(struct xbps_handle *xhp, const char *file, const char **argv)
8686}
8787
8888static int
89- vfcexec (struct xbps_handle * xhp , const char * arg , va_list ap )
89+ pfcexecp (const struct xbps_handle * xhp , const char * file , const char * * argv )
90+ {
91+ pid_t child ;
92+ int status ;
93+
94+ child = fork ();
95+ switch (child ) {
96+ case 0 :
97+ /*
98+ * If rootdir != / and uid==0 and bin/sh exists,
99+ * change root directory and exec command.
100+ */
101+ if (strcmp (xhp -> rootdir , "/" )) {
102+ if ((geteuid () == 0 ) && (access ("bin/sh" , X_OK ) == 0 )) {
103+ if (chroot (xhp -> rootdir ) == -1 ) {
104+ xbps_dbg_printf ("%s: chroot() "
105+ "failed: %s\n" , * argv , strerror (errno ));
106+ _exit (errno );
107+ }
108+ if (chdir ("/" ) == -1 ) {
109+ xbps_dbg_printf ("%s: chdir() "
110+ "failed: %s\n" , * argv , strerror (errno ));
111+ _exit (errno );
112+ }
113+ }
114+ }
115+ umask (022 );
116+ (void )execvp (file , __UNCONST (argv ));
117+ _exit (errno );
118+ /* NOTREACHED */
119+ case -1 :
120+ return -1 ;
121+ }
122+
123+ while (waitpid (child , & status , 0 ) < 0 ) {
124+ if (errno != EINTR )
125+ return -1 ;
126+ }
127+
128+ if (!WIFEXITED (status ))
129+ return -1 ;
130+
131+ return WEXITSTATUS (status );
132+ }
133+
134+
135+ static int
136+ vfcexec (const struct xbps_handle * xhp , const char * arg , va_list ap )
90137{
91138 const char * * argv ;
92139 size_t argv_size , argc ;
@@ -122,8 +169,9 @@ vfcexec(struct xbps_handle *xhp, const char *arg, va_list ap)
122169 return retval ;
123170}
124171
172+
125173int HIDDEN
126- xbps_file_exec (struct xbps_handle * xhp , const char * arg , ...)
174+ xbps_file_exec (const struct xbps_handle * xhp , const char * arg , ...)
127175{
128176 va_list ap ;
129177 int result ;
@@ -134,3 +182,15 @@ xbps_file_exec(struct xbps_handle *xhp, const char *arg, ...)
134182
135183 return result ;
136184}
185+
186+ int HIDDEN
187+ xbps_file_exec_argv (const struct xbps_handle * xhp , const char * * argv )
188+ {
189+ return pfcexecp (xhp , argv [0 ], argv );
190+ }
191+
192+ int HIDDEN
193+ xbps_file_execp_argv (const struct xbps_handle * xhp , const char * * argv )
194+ {
195+ return pfcexecp (xhp , argv [0 ], argv );
196+ }
0 commit comments