Invoke shell commands and run shell scripts in linux C

1. system (execute shell commands)

Related functions fork, execve, waitpid, popen
header file #include<stdlib.h>
defines the function int system(const char * string);
function description system() will call fork() to generate a child process, and the child process will call /bin /sh-c
string to execute the command represented by the parameter string. After the command is executed, it
will return to the original calling process. During the call to system() the SIGCHLD signal is temporarily
put on hold, and the SIGINT and SIGQUIT signals are ignored.
Return value 127 if system() fails when calling /bin/sh,
-1 for other failure reasons. If the parameter string is a null pointer (NULL), a non-zero value is returned. If the system()
call is successful, the return value after executing the shell command will be returned at the end, but this return value may also
be 127 returned by the system() call /bin/sh failure, so it is best to check
errno again to confirm the execution success.
Additional Notes Do not use system() when writing programs with SUID/SGID permissions, system() will
inherit environment variables, which may cause system security problems.

 Function description
system() will call fork() to generate a child process, and the child process will call /bin/sh-c string to execute the command represented by the parameter string string. After the command is executed, it will return to the original calling process. During the call to system() the SIGCHLD signal is temporarily put on hold, and the SIGINT and SIGQUIT signals are ignored.
Return Value
Returns 127 if system() fails when calling /bin/sh, and returns -1 for other failure reasons. If the parameter string is a null pointer (NULL), a non-zero value is returned. If the system() call is successful, the return value after executing the shell command will be returned at the end, but this return value may also be 127 returned by the system() call /bin/sh failure, so it is best to check errno again to confirm the success of the execution .
Additional Notes

Do not use system() when writing programs with SUID/SGID permissions, system() will inherit environment variables, which may cause system security problems.

example:

 

#include<stdlib.h>

main()

{

system(“ls -al /etc/passwd /etc/shadow”);

}

 

2. popen (create pipeline I/O)

Related functions pipe, mkfifo, pclose, fork, system, fopen
header file #include<stdio.h>
definition function FILE * popen( const char * command, const char * type);
function description popen() will call fork() Spawn a child process, and then call /bin/sh -c from the child process
to execute the command of the parameter command. The parameter type can use "r" for read and "w"
for write. According to this type value, popen() will establish a pipe to the standard
output , and then return a file pointer. The process can then
use this file pointer to read from the child's output device or write to the child's standard
input. In addition, all functions that operate on file pointers (FILE*) are
available , except fclose().
The return value returns the file pointer if successful, otherwise returns NULL, and the cause of the error is stored in errno.
Error code EINVAL The parameter type is invalid.
Precautions Please avoid using popen() as much as possible when writing programs with SUID/SGID authority. Popen()
will inherit environment variables, which may cause system security problems.

 

example:

#include<stdio.h>
main()
{
FILE * fp;
char  buffer[80];
fp=popen(“cat /etc/passwd”,”r”);
fgets(buffer, sizeof (buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
 

execute root :x:0 0: root: /root: /bin/bash

 

3. Use vfork() to create a new child process, and then call the exec function family

 

#include<unistd.h>

main()

{

    char * argv[ ]={“ls”,”-al”,”/etc/passwd”,(char*) };

   

    if(vfork() = =0)

    {

        execv (“/ bin / ls”, argv);

    }else{        

        printf(“This is the parent process\n”);

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885965&siteId=291194637