system() popen(), execl(). , popen(), , execl() . , mumbo jumbo - , , , popen(), !
-...
execl() . , argv[0] main() . , :
execl("/usr/bin/php", "/usr/bin/php", "-q",
"/var/www/html/phpinfo.php", (char *) NULL);
( (char *), , , 0, NULL 0, (void *) 0, .)
...
execl(), . exec fork() pipe(). , exec ; ! , execl(), . . execl() . , , , , /usr/bin/php.
, fork() pipe()? , , - . "" , execl() /usr/bin/php. , , .
, , Google . -, (!) , fork/exec.
. , , , . popen(), , child stderr stdin stdout.
...
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
pid_t execute(const char *command, FILE **in, FILE **out, FILE **err)
{
pid_t pid;
int fd[6];
pipe(&fd[0]);
pipe(&fd[2]);
pipe(&fd[4]);
switch (pid = fork()) {
case -1:
perror("unable to fork()");
exit(1);
case 0:
close(fd[1]);
close(fd[2]);
close(fd[4]);
dup2(fd[0], STDIN_FILENO);
dup2(fd[3], STDOUT_FILENO);
dup2(fd[5], STDERR_FILENO);
execlp("/bin/sh", "/bin/sh", "-c", command, (char *) NULL);
perror("execlp() failed");
_exit(1);
default:
close(fd[0]);
close(fd[3]);
close(fd[5]);
if (in) *in = fdopen(fd[1], "wb"); else close(fd[1]);
if (out) *out = fdopen(fd[2], "rb"); else close(fd[2]);
if (err) *err = fdopen(fd[4], "rb"); else close(fd[4]);
return pid;
}
}