i want to pass the connect fd to child process,how can i do ti?


 
Thread Tools Search this Thread
Top Forums Programming i want to pass the connect fd to child process,how can i do ti?
# 1  
Old 07-12-2002
i want to pass the connect fd to child process,how can i do ti?

i write a function using to pass the socket connected fd to child process in the sco unix open server 5.0.5,but in fact i execute the program calling the fuction,system report send the fd error:

Jul 12 12:15 send_fd.c[41]: send_fd sendmsg to sd[4] error[Bad file number]

how can i solve the problem ,please help me!!!

fuction source code:
int
send_fd(int sd, int fd)
{
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov[1];
char buf[1];
int cmsg_size;

iov[0].iov_base = buf;
iov[0].iov_len = 1;

msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;

cmsg_size = sizeof(struct cmsghdr)+sizeof(int);

if ( (cmsg = (struct cmsghdr*)malloc(cmsg_size)) == NULL )
{
wlog(err_log, "send_fd malloc cmsghdr error[%s]", __FILE__,\
__LINE__, strerror(errno));
return -1;
}

cmsg->cmsg_len = cmsg_size;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;


msg.msg_control = (caddr_t)cmsg;
msg.msg_controllen = cmsg_size;
//msg.msg_flags = 0;
if ( sendmsg(sd, &msg, 0) != 1 )
{
wlog(err_log, "send_fd sendmsg to sd[%d] error[%s]", __FILE__,\
__LINE__, sd, strerror(errno));
return -1;
}
return 0;
}
# 2  
Old 07-12-2002
This is not a solution to your problem, but just curious, why are you sending the FD through a socket to your child process? If you fork()'ed the child, the child automatically gets a copy of all the open file descriptors the parent owns.
# 3  
Old 07-15-2002
i fork the child before accept the connect,so ...

if i fork the child process after accept the connect,the child will automatically gets a copy of all the open file descriptors the parent owns.but in fact, i want to fork serveral child processes, and then accept the connection,and want to transfer the connection fd to the pre_fork child process,how can i do it?
help me!
# 4  
Old 07-16-2002
See this post.
# 5  
Old 07-16-2002
thanks!but i still meet a problem calling sendmsg

this is two fuctions that sends and recvs fd,variable "sd" is created by calling socketpair function,but running the program calling recv_fd and send_fd,system still report error.
please help me to see whether the two function is called rightly
int
recv_fd(int sd)
{
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov[1];
char buf[32];
int cmsg_size;

iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);

msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;

cmsg_size = sizeof(struct cmsghdr)+sizeof(int);

if ( (cmsg = (struct cmsghdr*)malloc(cmsg_size)) == NULL )
return -1;

msg.msg_control = (caddr_t)cmsg;
msg.msg_controllen = cmsg_size;
if ( recvmsg(sd, &msg, 0) <= 0 )
return -1;
return *(int *)CMSG_DATA(cmsg);
}

int
send_fd(int sd, int fd)
{
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov[1];
char buf[1];
int cmsg_size;

iov[0].iov_base = buf;
iov[0].iov_len = 1;

msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;

cmsg_size = sizeof(struct cmsghdr)+sizeof(int);

if ( (cmsg = (struct cmsghdr*)malloc(cmsg_size)) == NULL )

return -1;

cmsg->cmsg_len = cmsg_size;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;

msg.msg_control = (caddr_t)cmsg;
msg.msg_controllen = cmsg_size;
if ( sendmsg(sd, &msg, 0) != 1 )

return -1;
return 0;
}
~
~
~
# 6  
Old 07-16-2002
I don't see anything wrong with the code that you posted. But it's not like I have anyway to test it. I sure would not return a pointer to an integer...I would just return the integer. But what you did is legal provided that you call your own function correctly.

I suggest that write a small sample program that forks and then passes a fd. If you can't get that to work, you could post the whole program. Also Rich Steven's book "Advanced Unix Programming" has complete examples.
# 7  
Old 07-18-2002
thanks a lot ,

your suggestion is good!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

3. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

4. Programming

Pass parameter from a child make to a parent

Hello, I have the following problem: I have makefileproj and makefilemod in a build process for a complex project - from makefileproj I call the makefilemod. In makefilemod I generate a list containing objects eg,: "../../../25_Build/Results/Objects/FBL/Fls.o... (4 Replies)
Discussion started by: marina_lmv
4 Replies

5. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

6. Shell Programming and Scripting

Pass all received args to a (wrapped) child script

I'm writing a wrapper script (in bash) that wraps another (bash) script. When calling the wrapped script, I need to pass all the received arguments/options to it. Is there a built in variable that holds all the options? I wrote a little while loop (see below) which works. But I wanted to know if... (1 Reply)
Discussion started by: Dilbert
1 Replies

7. Shell Programming and Scripting

Child Process Name

Hi , I want to find out the child process name given its PID. I have used the ps command but it displays the parent process name against child PID. Is there any way to find out name of child program executing under any parent program? (1 Reply)
Discussion started by: sneha_heda
1 Replies

8. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

9. UNIX for Dummies Questions & Answers

about child process

hello every one, i want to know more about creation of child process. UNDER WHAT CIRCUMSTANCES child process is created? WHAT ARE THE PREREQUISITES for a child process to be created? let us say we have a prog.c, prog.obj(compiled.c),.a\.out files. is any child PROCESS CREATED... (12 Replies)
Discussion started by: compbug
12 Replies

10. IP Networking

child process problem

please do answer it is urgent can any body tell me how can i find whether the child process has been killed or not in a program (1 Reply)
Discussion started by: ramneek
1 Replies
Login or Register to Ask a Question