summaryrefslogtreecommitdiff
path: root/src/sysdeps/tryancilautoclose.c
blob: 1da87bc0cabe58c6f291165516763c69a618f932 (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
103
104
105
106
107
108
109
110
111
112
113
114
/* ISC license. */

#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE

#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif

#ifndef _XPG4_2
#define _XPG4_2
#endif

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/uio.h>

union aligner_u
{
  struct cmsghdr cmsghdr ;
  int i ;
} ;

static int ancil_send_fd (int sock, int fd)
{
  union aligner_u ancilbuf[1 + CMSG_SPACE(sizeof(int)) / sizeof(union aligner_u)] ;
  char s[8] = "blahblah" ;
  struct iovec v = { .iov_base = s, .iov_len = 8 } ;
  struct msghdr msghdr =
  {
    .msg_name = 0,
    .msg_namelen = 0,
    .msg_iov = &v,
    .msg_iovlen = 1,
    .msg_flags = 0,
    .msg_control = ancilbuf,
    .msg_controllen = CMSG_SPACE(sizeof(int))
  } ;
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr) ;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int)) ;
  cmsg->cmsg_level = SOL_SOCKET ;
  cmsg->cmsg_type = SCM_RIGHTS ;
  *((int *)CMSG_DATA(cmsg)) = fd ;
  if (sendmsg(sock, &msghdr, 0) < 0) return 0 ;
  return 1 ;
}

static int ancil_recv_fd (int sock)
{
  union aligner_u ancilbuf[1 + CMSG_SPACE(sizeof(int)) / sizeof(union aligner_u)] ;
  char s[8] ;
  struct iovec v = { .iov_base = s, .iov_len = 8 } ;
  struct msghdr msghdr =
  {
    .msg_name = 0,
    .msg_namelen = 0,
    .msg_iov = &v,
    .msg_iovlen = 1,
    .msg_flags = 0,
    .msg_control = ancilbuf,
    .msg_controllen = CMSG_SPACE(sizeof(int))
  } ;
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr) ;
  cmsg->cmsg_len = msghdr.msg_controllen ;
  cmsg->cmsg_level = SOL_SOCKET ;
  cmsg->cmsg_type = SCM_RIGHTS ;
  *((int *)CMSG_DATA(cmsg)) = -1 ;
  if (recvmsg(sock, &msghdr, 0) != 8) return -1 ;
  if (memcmp(s, "blahblah", 8)) return -1 ;
  return *((int *)CMSG_DATA(cmsg)) ;
}

static int client (int sock)
{
  int p ;
  char c ;
  if (read(sock, &c, 1) < 1) return 111 ;
  if (c != 'b') return 111 ;
  p = ancil_recv_fd(sock) ;
  if (p < 0) return 111 ;
  if (read(sock, &c, 1) < 1) return 111 ;
  if (c != 'a') return 111 ;
  if (read(p, &c, 1) < 1) return 111 ;
  if (c != 'K') return 111 ;
  if (read(p, &c, 1) < 1) return 111 ;
  return c ;
}

static void server (int sock)
{
  int p[2] ;
  char c = 1 ;
  if (pipe(p) < 0) return ;
  if (write(sock, "b", 1) < 1) return ;
  if (!ancil_send_fd(sock, p[0])) return ;
  if (write(sock, "a", 1) < 1) return ;
  if (write(p[1], "K", 1) < 1) return ;
  if ((close(p[0]) < 0) && (errno == EBADF)) c = 0 ;
  write(p[1], &c, 1) ;
}

int main (void)
{
  int fd[2] ;
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) return 111 ;
  switch (fork())
  {
    case -1 : return 111 ;
    case 0 : close(fd[0]) ; server(fd[1]) ; return 0 ;
    default : close(fd[1]) ; return client(fd[0]) ;
  }
}