Sponsored Content
Top Forums Programming i want to pass the connect fd to child process,how can i do ti? Post 24461 by hit on Friday 12th of July 2002 12:26:43 AM
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;
}
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
CMSG_DATA(3)						   BSD Library Functions Manual 					      CMSG_DATA(3)

NAME
CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE -- socket control message routines SYNOPSIS
#include <sys/socket.h> void * CMSG_DATA(struct cmsghdr *); struct cmsghdr * CMSG_FIRSTHDR(struct msghdr *); size_t CMSG_LEN(size_t); struct cmsghdr * CMSG_NXTHDR(struct msghdr *, struct cmsghdr *); size_t CMSG_SPACE(size_t); DESCRIPTION
The control message API is used to construct ancillary data objects for use in control messages sent and received across sockets. Control messages are passed around by the recvmsg(2) and sendmsg(2) system calls. The cmsghdr structure, described in recvmsg(2), is used to specify a chain of control messages. These routines should be used instead of directly accessing the control message header members and data buffers as they ensure that necessary alignment constraints are met. The following routines are provided: CMSG_DATA(cmsg) This routine accesses the data portion of the control message header cmsg. It ensures proper alignment constraints on the beginning of ancillary data are met. CMSG_FIRSTHDR(mhdr) This routine accesses the first control message attached to the message msg. If no control messages are attached to the message, this routine returns NULL. CMSG_LEN(len) This routine determines the size in bytes of a control message, which includes the control message header. len specifies the length of the data held by the control message. This value is what is normally stored in the cmsg_len of each control message. This rou- tine accounts for any alignment constraints on the beginning of ancillary data. This macro might not evaluate to a compile-time con- stant. CMSG_NXTHDR(mhdr, cmsg) This routine returns the location of the control message following cmsg in the message mhdr. If cmsg is the last control message in the chain, this routine returns NULL. CMSG_SPACE(len) This routine determines the size in bytes needed to hold a control message and its contents of length len, which includes the control message header. This value is what is normally stored in msg_msgcontrollen. This routine accounts for any alignment constraints on the beginning of ancillary data as well as any needed to pad the next control message. This macro might not evaluate to a compile- time constant. EXAMPLES
The following example constructs a control message containing a file descriptor and passes it over a socket: struct msghdr msg; struct cmsghdr *cmsg; /* We use a union to make sure hdr is aligned */ union { struct cmsghdr hdr; unsigned char buf[CMSG_SPACE(sizeof(int))]; } *cmsgbuf; /* * We allocate in the heap instead of the stack to avoid C99 * variable stack allocation, which breaks gcc -fstack-protector. */ if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL) err(1, "malloc"); (void)memset(&msg, 0, sizeof(msg)); msg.msg_control = cmsgbuf->buf; msg.msg_controllen = sizeof(cmsgbuf->buf); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; *(int *)CMSG_DATA(cmsg) = fd; if (sendmsg(s, &msg, 0) == -1) err(1, "sendmsg"); free(cmsgbuf); And an example that receives and decomposes the control message: struct msghdr msg; struct cmsghdr *cmsg; union { struct cmsghdr hdr; unsigned char buf[CMSG_SPACE(sizeof(int))]; } *cmsgbuf; if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL) err(1, "malloc"); (void)memset(&msg, 0, sizeof(msg)); msg.msg_control = cmsgbuf->buf; msg.msg_controllen = sizeof(cmsgbuf->buf); if (recvmsg(s, &msg, 0) == -1) err(1, "recvmsg"); if ((msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) errx(1, "control message truncated"); for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { fd = *(int *)CMSG_DATA(cmsg); /* Do something with the descriptor. */ } } free(cmsgbuf); SEE ALSO
recvmsg(2), sendmsg(2), socket(2) HISTORY
The control message API first appeared in 4.2BSD. BSD
June 20, 2008 BSD
All times are GMT -4. The time now is 10:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy