Welcome to mirror list, hosted at ThFree Co, Russian Federation.

send_pty_slave_fork.c « socket_tests « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a88715035613410d4c7a0c1668d0e94fc1bed1dc (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
  Fork a subprocess, open a pty pair, and send the pty slave file
  descriptor to the subprocess over an AF_UNIX socket.  Invoke with
  --debug to allow time to attach gdb to the child.
*/

#include "af_unix_hdr.h"
#include "pty_master_open.h"
#include "read_line.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 100
#define MAX_SNAME 100                  /* Maximum size for pty slave name */

int
main(int argc, char *argv[])
{
  Boolean debug = FALSE;
  pid_t pid;
  int pipefd[2];
  int pfd;			/* parent's end */
  int cfd;			/* child's end */

  if (argc > 1 && strcmp (argv[1], "--debug") == 0)
    debug = TRUE;

  if (socketpair (AF_UNIX, SOCK_STREAM, 0, pipefd) < 0)
    errExit ("socketpair");
  pfd = pipefd[1];
  cfd = pipefd[0];

  if ((pid = fork ()) < 0)
    errExit ("fork");
  else if (pid > 0)		/* parent */
    {
      int mfd, sfd, junk;
      char slname[MAX_SNAME];

      if (close (cfd) < 0)
	errExit ("close");
      if ((mfd = ptyMasterOpen (slname, MAX_SNAME)) < 0)
	errExit ("ptyMasterOpen");
      if ((sfd = open (slname, O_RDWR | O_NOCTTY)) < 0)
	errExit ("open");
      if (debug)
	{
	  printf ("parent pid %d, child pid %d, sleeping...\n", getpid (), pid);
	  sleep (30);
	}

      printf ("parent sending descriptor %d for %s to child\n", sfd, slname);
      if (sendfd (pfd, sfd) < 0)
	errExit ("sendfd");
      if (close (sfd) < 0)
	errMsg ("close");
      if (write (mfd, "hello\n", 6) < 0)
	errExit ("write");
      /* Wait for child. */
      if (read (pfd, &junk, sizeof junk) != sizeof junk)
	errMsg ("read");
      if (close (pfd) < 0)
	errMsg ("close");
      if (close (mfd) < 0)
	errMsg ("close");
    }
  else			/* child */
    {
      int fd, junk;
      ssize_t nr;
      char buf[BUF_SIZE];

      if (close (pfd) < 0)
	errExit ("close");
      if (debug)
	sleep (30);

      /* Read fd from parent. */
      fd = recvfd (cfd);
      if (fd < 0)
	errExit ("recvfd");

      /* Read a line from fd. */
      if ((nr = readLine (fd, buf, BUF_SIZE)) < 0)
	{
	  close (fd);
	  errExit ("readLine");
	}

      /* Kill newline. */
      buf[nr - 1] = '\0';
      printf ("child read %zd bytes (including newline) from fd %d: %s\n", nr,
	      fd, buf);
      if (close (fd) == -1)
	errMsg ("close");
      /* Tell parent we're done. */
      if (write (cfd, &junk, sizeof junk) != sizeof junk)
	errMsg ("write");
      if (close (cfd) < 0)
	errExit ("close");
    }
}