-->

Pass File Descriptor - Execve (typecast)

2020-05-08 19:19发布

问题:

I am wondering how I can pass a file descriptor through the execve() command and then access it on the other side. I know that I can use dup2 to redirect the file-descriptor but I cannot do that. I am required to actually pass the file descriptor to the child and use it in the child.

What I have done so far:

Parent makes pipe + args like the following:

int pfd[2];
if(pipe(pfd) == -1)
    exitWithError("PIPE FAILED", 1);
char *args_1[] = {"reader", argv[1], (char*) pfd, (char *) 0};

Then the child calls execve after fork() like the following:

close(pfd[1]);
execve("./reader", args_1, NULL);

Then, in the reader program I try to access pipe descriptor that was passed:

int main(int argc, char* argv[]){
    ...
    write(argv[2][1], buf, read_test);

argv[2] should be referencing the pipe descriptor, then the argv[1] should go to the write end of the pipe. I am almost positive that I need to do some type-casting differently here, but everything I try doesn't quite work out.

NOTE: I have a working version of this program using dup2 to redirect to stdin and stdout for the children, but I have to actually pass the pipe descriptor to a child as per instructions of the project.

Any help is appreciated.

回答1:

Simply casting the file descriptor to a char* isn't a good idea. It could cause data loss if the OS chooses to copy the strings and stops at the 0 bytes, so the data isn't entirely copied. It would be safer to create a string containing the file descriptor.

int pfd[2];
if(pipe(pfd) == -1)
    exitWithError("PIPE FAILED", 1);
char string1[12]; // A 32 bit number can't take more than 11 characters, + a terminating 0
snprintf(string1,12,"%i",pfd[1]); // Copy the file descriptor into a string
char *args_1[] = {"reader", argv[1], &string1[0], (char *) 0};

The reader program then uses atoi to convert this back into an int.

int fd = atoi(argv[2]);
write(fd,buf,read_test);

If you really want to use a cast, then you need to cast argv[2] as int* in your reader.

write(((int*)argv[2])[1],buf,read_test);