Questions tagged [pclose]

In C, the pclose() function closes a stream that was opened by popen().

In C, the pclose() function closes a stream that was opened by popen(), waits for the command to terminate, and returns the termination status of the process that was running the command interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() returns -1 with errno set to ECHILD to report this situation. This can happen if the application calls one of the following functions:

  • wait()
  • waitpid() with the pid argument of -1 or equal to the process ID of the command interpreter.

In any case, pclose() does not return before the child process created by popen() terminates.

If some error prevents the command interpreter from executing after the child process is created, the return value from pclose() is as if the command language interpreter had terminated using exit(127) or _exit(127).

Source: Man page for pclose() -- close pipe stream at mkSoftware

33 questions
7
votes
3 answers

use fclose to pipe of popen is a serious bug?

Some months ago I write a CGI application for Linux that uses popen() to read the output of a command, and then I close the pipe with fclose(). Now, I read that for close pipes is needs use pclose(). The manual says: The return value from popen()…
carlos
  • 1,261
  • 1
  • 12
  • 15
4
votes
1 answer

Oracle beq and popen()

I have a program like this (that's for Pro*C precompiler): #include #include #include #include EXEC SQL BEGIN DECLARE SECTION; static VARCHAR ora_connect_str[81]; EXEC SQL END DECLARE SECTION; EXEC SQL…
user332325
4
votes
1 answer

Executing and get pid a background process in PHP on Windows

I started a process in background in Windows apache server. index.php following this: test.php following…
Wen NanZhe
  • 43
  • 6
4
votes
2 answers

pclose() on file descriptor opened with popen() returns errno 10 (No child processes)

I'm running linux and I try to do the following: Run ls on current directory (using popen) Output the result to buffer (using fread from pipe descriptor) close pipe (using pclose). Everything works fine (the buffer is filled correctly with the ls…
Ofer Levi
  • 41
  • 1
  • 4
3
votes
1 answer

WEXITSTATUS returning invalid exit status of popen() on success case

I am trying to get the exist status of the child process using popen(). Case 1: Calling function with the shell command returning error. This is working as expected. func("du -sh _invalid_file_"); Output: du: cannot access '_invalid_file_': No such…
Kirubakaran
  • 378
  • 4
  • 20
3
votes
1 answer

How to get the correct exit code of a shell command executed via pipe using popen() and pclose() in a daemonized process?

I'm writing a C++ daemon that is going to run on a Linux embedded system and execute a shell command. I'm trying to get both the output of the command (stdout) as well as the exit code of said shell command. I followed the following SO question on…
N0x
  • 51
  • 5
3
votes
4 answers

Is output read from popen()ed FILE* complete before pclose()?

pclose()'s man page says: The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2). I feel like this means if the associated FILE* created by popen() was opened with…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
3
votes
1 answer

C++ popen pipe doesn't close correctly with pclose for "ps aux" command

I am running MacOS and want to execute the "ps aux" command and get its output through my application. I have written a method which executes commands using popen function: std::string exec(const char* cmd) { char buffer[128]; std::string…
MrWhite
  • 179
  • 3
  • 11
3
votes
1 answer

Is there a way to test whether pclose() will succeed?

In my C++ application, I am seeing a pclose() that hangs because the pipe's process hung and never exited. Is there anyway I could do something like select() to test whether the pclose() will return because the child process has completed? I'd…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
3
votes
2 answers

Does pclose() return pipe's termination status shifted left by eight bits on all platforms?

I found on Centos4 that the man page for popen() states in part: DESCRIPTION The pclose() function shall close a stream that was opened by popen(), wait for the command to termi- nate, and return the termination status of the process that…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
2
votes
0 answers

How to capture errors when reading an analog input value

I am currently trying to make my code safer and more structured. So far, when I want to read an analog input value, I use the following function which works fine: FILE *fp = popen("cat /sys/bus/iio/devices/iio\\:device0/in_value_raw", "r"); char *ln…
Fabian
  • 91
  • 8
2
votes
1 answer

Do a popen(), put the FILE* pointer in an fstream, what about the pclose()?

So... I start another process that accepts some input from my program (it could go the other way around too). Something of the sort: FILE *f(popen("sendmail", "w")); Now, I can put f in an fstream so that way I can just use the stream (since my…
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
2
votes
3 answers

Way to force file descriptor to close so that pclose() will not block?

I am creating a pipe using popen() and the process is invoking a third party tool which in some rare cases I need to terminate. ::popen(thirdPartyCommand.c_str(), "w"); If I just throw an exception and unwind the stack, my unwind attempts to call…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
1
vote
1 answer

Windows/php pclose and popen issue

Here is my code:
Naftali
  • 144,921
  • 39
  • 244
  • 303
1
vote
1 answer

How to ensure popen()ed process runs destructors on exit?

If I have a pipe to run some command, the piped command needs to do some cleanup, however, if the processes that started the pipe has an error, the piped command is not cleaning up. Is the piped command getting SIGPIPE in this case? How can I…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
1
2 3