File: fdpassing.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (40 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <unistd.h>
#include "io.h"
#include "buffer.h"
#include <stdlib.h>

void child(int64 fd) {
  int64 x=io_receivefd(fd);
  char buf[8192];
  int i;
  if (x==-1) {
    buffer_putsflush(buffer_2,"fd passing failed!\n");
    exit(1);
  }
  i=read(x,buf,sizeof(buf));
  write(1,buf,i);
  io_close(x);
}

void father(int64 fd) {
  int64 x;
  if (io_readfile(&x,"/etc/resolv.conf"))
    io_passfd(fd,x);
}

int main() {
  int64 sp[2];
  if (io_socketpair(sp)==-1) return 1;
  switch (fork()) {
  case -1: return 1;
  case 0:	/* child */
    io_close(sp[0]);
    child(sp[1]);
    break;
  default:
    io_close(sp[1]);
    father(sp[0]);
    break;
  }
  return 0;
}