The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-10-2008 12:22 AM
about child process compbug UNIX for Dummies Questions & Answers 12 03-22-2006 07:55 PM
gdb to child process shriashishpatil UNIX for Advanced & Expert Users 4 12-12-2005 07:57 AM
KDM child process larryase UNIX for Dummies Questions & Answers 6 01-24-2005 05:41 PM
Child Process PID skannan High Level Programming 2 06-10-2002 08:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-12-2002
hit hit is offline
Registered User
  
 

Join Date: May 2002
Posts: 10
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 (permalink)  
Old 07-12-2002
hell666 hell666 is offline
Registered User
  
 

Join Date: Jul 2002
Location: void
Posts: 53
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 (permalink)  
Old 07-15-2002
hit hit is offline
Registered User
  
 

Join Date: May 2002
Posts: 10
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 (permalink)  
Old 07-16-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,123
See this post.
  #5 (permalink)  
Old 07-16-2002
hit hit is offline
Registered User
  
 

Join Date: May 2002
Posts: 10
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 (permalink)  
Old 07-16-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,123
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 (permalink)  
Old 07-18-2002
hit hit is offline
Registered User
  
 

Join Date: May 2002
Posts: 10
thanks a lot ,

your suggestion is good!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0