System functions of Process Control

Source: Internet
Author: User

Iso c defines system functions, but its operations are highly dependent on the system. POSIX.1 includes the system interface, which extends the iso c Definition to describe the running behavior of the system in the POSIX.1 environment.

#include <stdlib.h> system(   *cmdstring );

If struct string is a null pointer, system returns a non-zero value only when the command handler is available. This feature determines whether the system function is supported on a given operating system. In UNIX, system is always available.

Because system calls fork, exec, and waitpid in its implementation, there are three return values:

(1) If fork fails or waitpid returns an error other than EINTR, system returns-1 and sets the corresponding error type value in errno.

(2) If exec fails (the shell cannot be executed), the return value is the same as that of the shell that executes exit (127.

(3) otherwise, all three functions (fork, exec, and waitpid) are successfully executed, and the return value of system is the termination state of shell, the format is described in waipid (see http://www.cnblogs.com/nufangrensheng/p/3510101.html ).

Program list 8-12 system function (no signal processing)

<sys/wait.h><errno.h><unistd.h>  *cmdstring)    (cmdstring ==();               ((pid = fork()) < = -;      (pid == )                       ***/                 (execl(, , , cmdstring, ( *)) < );                          (waitpid(pid, &status, ) <  (errno !== -;                                 

The shell-c option tells the shell program to take the next command line parameter (in this case, the argument string) as the command input (rather than the standard input or read the command from a given file ). Shell performs syntax analysis on the command strings terminated with null characters and divides them into command line parameters (if we try to execute this command without using shell, we will execute it ourselves, so it will be quite difficult ). The actual command string passed to the shell can contain any valid shell command.

Note: We call _ exit instead of exit. This is to prevent any standard I/O buffer (The buffer is replicated by the parent process to the child process in fork, see: http://www.cnblogs.com/nufangrensheng/p/3509492.html for details) In the sub-process.

Program list 8-13 call the system function

[root@localhost apue]# cat prog8-<sys/wait.h> ((status = system()) <  ((status = system()) <  ((status = system()) < 

Run the program in listing 8-13:

[Root @ localhost apue] #./prog8-: PST ==/ --: (: = for exit

The advantage of using system instead of using fork and exec is that system performs various error processing and various signal processing.(In the system version with signal processing ).

 

Set User ID program

What happens if system is called in a set user ID program? This is a security vulnerability and should never be done in this way.

If a process is running with special permissions (Set User ID or set group ID) and it wants to generate another process to execute another program, it should directly use fork and exec, in addition, after fork and before exec, you need to change back to normal permissions. The system function should never be called to set the user ID or group ID.

 

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.