getting the return code of forked child process (ftp)


 
Thread Tools Search this Thread
Top Forums Programming getting the return code of forked child process (ftp)
# 1  
Old 08-09-2007
getting the return code of forked child process (ftp)

Hi,

From within my C++ program, I fork a child process and execl an ftp session (solaris), like this :

std::string szStartCmd = "ftp -i -n -v 192.168.149.31";

int nExecRes = execl("/bin/sh", "sh", "-c", szStartCmd.c_str(), (char *)0);

I use 2 pipes to communicate between my parent process and the ftp. But I'm not sure if it's possible to catch the output of the solaris ftp in a pipe, so that I can use it in my parent process. Until now I've only used uni-directional communication between parent and child process.
Getting commands from my parent to the ftp process works fine, but the ftp output is just printed on the screen. I would like to "catch" it in my parent process. Does anyone know if this is possible ?
# 2  
Old 08-09-2007
Call waitpid() using the pid of the child.

waitpid() will return an exit status that can be decoded by macros defined in sys/wait.h like WIFEXITED. Make sure your child returns an exit code from what you are trying to check - ftp. This means your child will have to learn about the return codes in RFC 959 and decide which ones are bad and which ones are okay. The ftp process itself doesn't usually return errors the way you would think, it displays return codes to stdout. And there are quite a few of them.
# 3  
Old 08-09-2007
As jim mentioned, since ftp displays return codes on the stdout, you might want to think about using popen instead of execl to read the return codes straight off the child's stdout.
# 4  
Old 08-10-2007
Hello,

Thank you for the answers.
So, now I'm sure it's not something "impossible" that I'm trying to do.

But I'm not there yet. I've copied what I've got so far below.
With this piece of code, I can send ftp commands and get the response on standard output. But how do I catch it now ?

I thought read(fd[0],receive,strlen(receive)); would do this.
Maybe something is wrong with the close and dup'ing in the child process ?

I haven't tried the popen yet, because I have the idea that I'm very close with what I have now.

Does anyone see what's still wrong ?


int fd[2], nStatus;
pid_t pid;

pipe(&fd[0]); //create pipe
if ((pid=fork()) != 0) {
// parent
close(fd[0]);
//close(STD_OUTPUT);
//dup(fd[1]); // sets stdout to this pipe end
//close(fd[1]); // don't need fd anymore

std::cout << "I'm the parent. I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;

} else {
// child
close(fd[1]);
close(STD_INPUT);
dup(fd[0]); // replace stdin
close(fd[0]);

std::cout << "I'm the child. I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;

std::string szStartCmd = "ftp -i -n -v 192.168.149.31";

int nExecRes = execl("/bin/sh", "sh", "-c", szStartCmd.c_str(), (char *)0);

}

sleep(1);


std::cout << "I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;


// Wait for the child process to end, but without blocking !
// If the child process exits immediately, we assume ftp couldn't be started -> return false
// else the child process is waited for later (it becomes a "zombie" process when ftp is closed,
// by calling waitpid, the zombie processes are cleared.
int nChildDied = waitpid(pid, &nStatus, WNOHANG);

if (nChildDied == 0)
{
// Child is not dead -> protocol analyser was started OK
std::cout << "ftp started, child not dead" << std::endl;

}
else if (nChildDied > 0)
{
// A child process died -> error starting protocol analyser
if (WIFEXITED(nStatus))
{
std::cout << "child process with pid = " << nChildDied << " died. Child WIFEXITED status = "
<< nChildDied << WEXITSTATUS(nStatus);
}
if (WIFSIGNALED(nStatus))
{
std::cout << "child process with pid = " << nChildDied << "died. Child WIFSIGNALED status = "
<< nChildDied << WTERMSIG(nStatus);
}

std::cout << "error starting ftp";

}
else if (nChildDied < 0)
{
// An error occured
if (errno == ECHILD)
{
std::cout << "no child process exists";
}
else if (errno == EINTR)
{
std::cout << "calling process interrupted by a signal";
}
else if (errno == EINVAL)
{
std::cout << "bad argument passed to waitpid";
}
}

char send[80], receive[80] = "initial value";

fprintf(stdout, "Ftp Command ? > "); fgets(send, 80, stdin);

write(fd[1],send,strlen(send));

read(fd[0],receive,strlen(receive));

std::cout << "response from ftp : " << receive << std::endl;

fprintf(stdout, "Ftp Command ? > "); fgets(send, 80, stdin);

write(fd[1],send,strlen(send));

...
# 5  
Old 08-10-2007
You can solve your problem with library functions - "popen", "pclose"

http://www.freebsd.org/cgi/man.cgi?q...SE&format=html

Best regards,
Iliyan Varshilov
# 6  
Old 08-18-2007
hi
i wrote a reply that took me about an half our or more (my english is not good). when i clicked the "submit request" button another login was requested an all i had written was lost.
therefore a short version of my post:
1. popen is not ansi c and you cannot both write and read from the pipe it creates, espesially not in solaris9 as the manual states.
2. in m.bach,The design of the UNIX operating system,ISBN:0-13-201799-7
one can find a description what one must do
3. i found to examples in the net which i do not check:
Mapping UNIX pipe descriptors to stdin and stdout in C
Simple popen2 implementation [c] [unix] [ipc] [popen] [popen2] [pipes]
4. in your program i am missing the second pipe. you correctly create one pipe and use the dup function to that the child's process stdin now is sent through the pipe. you must create another pipe for the stdout in the same manner
5. after exec the filehandles are inherited so your ftp-progrma will read from one pipe and write to the other. but everything in the program beinning with the line
sleep(1);
will never be executed by the child (but by the parent)

now i make a copy of this text befor i press the submit-button
mfg guenter
# 7  
Old 08-18-2007
If you get another login screen and you then login, your operation will be completed, did you do this or did you press "back" on your browser?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Why is the return code of child required by parent ?

Hello everyone, I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent... (1 Reply)
Discussion started by: sreyan32
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Programming

getting the return from forked child process to parent in C++

This needs to work on HPUX and Linux. I do a fork and create a child process. During execution of the child process, it is possible child become lost or get killed. That is the reason why I create the child process. However if the child process doesnt get killed, I do want to know the return... (2 Replies)
Discussion started by: usustarr
2 Replies

4. UNIX Desktop Questions & Answers

ftp return code not working

below is my code , but for some reason the return part is not working, only file transfer is happening and no exit status is checked .please me help me to fix this code #!/bin/sh #set -vx ftp -nv sitelocation << ! user username password lcd localdir cd /remote dir mget *.* ... (4 Replies)
Discussion started by: gwrm
4 Replies

5. UNIX for Advanced & Expert Users

commands not working if the executed from forked process

Hi, I have an application where if it runs indivisually could able to execute commands (like system("ls")) and could able to execute tcl script. Same application if started from health monitor process (From health monitor process my application will be forked), it could not execute the... (1 Reply)
Discussion started by: chandrutiptur
1 Replies

6. UNIX for Dummies Questions & Answers

FTP Return Code

Hi All, I have a problem to identify the error code thrown by FTP Server while uploading files. The message is : ftp return 32. I couldn't find out what is the meaning of that. :confused: OS is Sun Solaris 2.10. Anyone can help? Thanks a lot (1 Reply)
Discussion started by: wilsonSurya
1 Replies

7. Shell Programming and Scripting

Return code of background process

Hi, I have a process that I run in the background that looks like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & and I would like to get the return code of the sqler.ksh script. so my code is like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & retcode=$? (3 Replies)
Discussion started by: c19h28O2
3 Replies

8. Programming

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

9. UNIX for Advanced & Expert Users

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

10. Shell Programming and Scripting

background process return code

Hi I have the following piece of code that is calling another child process archive.ksh in the background while read file; do file_name=`ls $file`; ksh archive.ksh $file_name &; done < $indirect_file The problem is, indirect_file may contain anwhere from 2 to 20 different... (5 Replies)
Discussion started by: Vikas Sood
5 Replies
Login or Register to Ask a Question