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

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(3) 						     Linux Programmer's Manual							   CMSG(3)

NAME
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - Access ancillary data. SYNOPSIS
#include <sys/socket.h> struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *msgh); struct cmsghdr *CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg); size_t CMSG_ALIGN(size_t length); size_t CMSG_SPACE(size_t length); size_t CMSG_LEN(size_t length); unsigned char *CMSG_DATA(struct cmsghdr *cmsg); struct cmsghdr { socklen_t cmsg_len; /* data byte count, including header */ int cmsg_level; /* originating protocol */ int cmsg_type; /* protocol-specific type */ /* followed by unsigned char cmsg_data[]; */ }; DESCRIPTION
These macros are used to create and access control messages (also called ancillary data) that are not a part of the socket payload. This control information may include the interface the packet was received on, various rarely used header fields, an extended error description, a set of file descriptors or unix credentials. For instance, control messages can be used to send additional header fields such as IP options. Ancillary data is sent by calling sendmsg(2) and received by calling recvmsg(2). See their manual pages for more information. Ancillary data is a sequence of struct cmsghdr structures with appended data. This sequence should only be accessed using the macros described in this manual page and never directly. See the specific protocol man pages for the available control message types. The maxi- mum ancillary buffer size allowed per socket can be set using the net.core.optmem_max sysctl; see socket(7). CMSG_FIRSTHDR returns a pointer to the first cmsghdr in the ancillary data buffer associated with the passed msghdr. CMSG_NXTHDR returns the next valid cmsghdr after the passed cmsghdr. It returns NULL when there isn't enough space left in the buffer. CMSG_ALIGN, given a length, returns it including the required alignment. This is a constant expression. CMSG_SPACE returns the number of bytes an ancillary element with payload of the passed data length occupies. This is a constant expression. CMSG_DATA returns a pointer to the data portion of a cmsghdr. CMSG_LEN returns the value to store in the cmsg_len member of the cmsghdr structure, taking into account any necessary alignment. It takes the data length as an argument. This is a constant expression. To create ancillary data, first initialize the msg_controllen member of the msghdr with the length of the control message buffer. Use CMSG_FIRSTHDR on the msghdr to get the first control message and CMSG_NEXTHDR to get all subsequent ones. In each control message, ini- tialize cmsg_len (with CMSG_LEN), the other cmsghdr header fields, and the data portion using CMSG_DATA. Finally, the msg_controllen field of the msghdr should be set to the sum of the CMSG_SPACE of the length of all control messages in the buffer. For more information on the msghdr, see recvmsg(2). When the control message buffer is too short to store all messages, the MSG_CTRUNC flag is set in the msg_flags member of the msghdr. EXAMPLE
This code looks for the IP_TTL option in a received ancillary buffer: struct msghdr msgh; struct cmsghdr *cmsg; int *ttlptr; int received_ttl; /* Receive auxiliary data in msgh */ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh,cmsg) { if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_TTL) { ttlptr = (int *) CMSG_DATA(cmsg); received_ttl = *ttlptr; break; } } if (cmsg == NULL) { /* * Error: IP_TTL not enabled or small buffer * or I/O error. */ } The code below passes an array of file descriptors over a Unix socket using SCM_RIGHTS: struct msghdr msg = {0}; struct cmsghdr *cmsg; int myfds[NUM_FD]; /* Contains the file descriptors to pass. */ char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */ int *fdptr; msg.msg_control = buf; msg.msg_controllen = sizeof buf; cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD); /* Initialize the payload: */ fdptr = (int *)CMSG_DATA(cmsg); memcpy(fdptr, myfds, NUM_FD * sizeof(int)); /* Sum of the length of all control messages in the buffer: */ msg.msg_controllen = cmsg->cmsg_len; NOTES
For portability, ancillary data should be accessed only using the macros described here. CMSG_ALIGN is a Linux extension and should be not used in portable programs. In Linux, CMSG_LEN, CMSG_DATA, and CMSG_ALIGN are constant expressions (assuming their argument is constant) - this could be used to declare the size of global variables. This may be not portable, however. CONFORMS TO
This ancillary data model conforms to the POSIX.1003.1g draft, 4.4BSD-Lite, the IPv6 advanced API described in RFC2292 and the Single Unix specification v2. CMSG_ALIGN is a Linux extension. SEE ALSO
sendmsg(2), recvmsg(2) RFC 2292 Linux Man Page 1998-10-02 CMSG(3)
All times are GMT -4. The time now is 09:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy