![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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; } |
|
||||
|
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.
|
|
||||
|
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! |
|
||||
|
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; } ~ ~ ~ |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|