POPEN(3) Linux Programmer’s Manual POPEN(3)
NAME
popen, pclose - pipe stream to or from a process
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _POSIX_SOURCE
_BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and invoking the
shell. Since a pipe is by definition unidirectional, the type argument may specify
only reading or writing, not both; the resulting stream is correspondingly read-
only or write-only.
The command argument is a pointer to a null-terminated string containing a shell
command line. This command is passed to /bin/sh using the -c flag; interpretation,
if any, is performed by the shell. The type argument is a pointer to a null-termi-
nated string which must contain either the letter 'r' for reading or the letter 'w'
for writing. Since glibc 2.9, this argument can additionally include the letter
'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying
file descriptor; see the description of the O_CLOEXEC flag in open(2) for reasons
why this may be useful.
The return value from popen() is a normal standard I/O stream in all respects save
that it must be closed with pclose() rather than fclose(3). Writing to such a
stream writes to the standard input of the command; the command’s standard output
is the same as that of the process that called popen(), unless this is altered by
the command itself. Conversely, reading from a "popened" stream reads the com-
mand’s standard output, and the command’s standard input is the same as that of the
process that called popen().
Note that output popen() streams are fully buffered by default.
The pclose() function waits for the associated process to terminate and returns the
exit status of the command as returned by wait4(2).
RETURN VALUE
The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it
cannot allocate memory.
The pclose() function returns -1 if wait4(2) returns an error, or some other error
is detected.
ERRORS
The popen() function does not set errno if memory allocation fails. If the under-
lying fork(2) or pipe(2) fails, errno is set appropriately. If the type argument
is invalid, and this condition is detected, errno is set to EINVAL.
If pclose() cannot obtain the child status, errno is set to ECHILD.
CONFORMING TO
POSIX.1-2001.
The 'e' value for type is a Linux extension.
BUGS
Since the standard input of a command opened for reading shares its seek offset
with the process that called popen(), if the original process has done a buffered
read, the command’s input position may not be as expected. Similarly, the output
from a command opened for writing may become intermingled with that of the original
process. The latter can be avoided by calling fflush(3) before popen().
Failure to execute the shell is indistinguishable from the shell’s failure to exe-
cute command, or an immediate exit of the command. The only hint is an exit status
of 127.
SEE ALSO
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), sys-
tem(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A description of
the project, and information about reporting bugs, can be found at http://www.ker-
nel.org/doc/man-pages/.
POPEN(3) Linux程序手册 POPEN(3)
名称:
popen,pclose – 来源于或到达一个进程的管道流。
概要:
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
glibc 的宏要求:(参见feature_test_macros(7)):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _POSIX_SOURCE
_BSD_SOURCE || _SVID_SOURCE
描述:
popen()函数通过创建管道,forking和调用shell的方式来打开一个进程。由于管道被定义成单向的,所以type参数只能指定为只读或者只写,不能同时指定读写;输出的结果流也是相应地只读或只写。
command参数是一个指向包含shell命令行并以null结尾的字符串的指针。该命令通过使用-c标志来传送给/bin/sh。然后由shell来解释执行。type参数是一个指向以null结尾的字符串的指针,该字符串必须只是’r’(只读)或者’w’(只写)中的一个。自从glibc 2。9版本以后,这个参数(type)又增加了一个值 ’e’,它是close-on-exec的标志(FD_CLOEXEC)来设置潜在的文件描述符。参考open(2)中的O_CLOEXEC标志,来了解它可能使用到的原因。
popen()函数的返回值是一个普通的标准I/O流,它必须使用pclose()函数来关闭而不是用fclose()函数。通过对这个流的写数据,来实现对command命令的标准输入,而command的标准输出和调用popen函数的进程相同,除非command自己改变标准输出。相反地,通过读一个已经popen的流来读取command命令的标准输出,并且command命令的标准输入和调用poen函数的进程相同。
要注意的是,poen的输出流默认是全缓冲的。
pclose()函数会等待相关的进程结束,并且返回command命令通过wait4()函数返回的退出码。
返回值:
如果调用fork()或者pipe()失败或者分配内存空间失败,popen函数返回NULL。
如果wait4函数返回一个错误,或者出现了其他的错误,pclose()会返回-1.
错误:
如果poen函数获取内存失败,将不会设置全局变量errno,而fork或者pipe失败,则会相应的设置errno的值。如果检测到type参数是无效的,则会将errno设置为ECHILD。
如果pclose函数不能获取子进程的状态值,errno会被设置成ECHILD.
遵循:
POSIX.1-2001.
type参数的值 'e'是Linux的扩展。
漏洞:
因为以只读打开的command的标准输入和调用popen的进程共享流搜索偏移量,如果源进程完成了一个缓冲区的读取操作,则command命令进程的输入位置将不可预料。同样地,以只写打开的command的标准输出将也会和源进程混淆。后者可以在调用popen函数之前调用fflush(3)来避免情况的发生。
如果shell执行失败,将不能辨别这个错误是因为shell执行command失败还是command立即退出所引起的。唯一的暗示就是退出状态值127。
另请参阅:
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3)
版本记录:
这一页是Linux man-pages工程3.22版本的一部分。关于此工程的描述和报告的bugs信息,可以从网站中找到,网址是:http://www.kernel.org/doc/man-pages/