File: fdleak_cmsg.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
file content (207 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

/* On AIX 5.2, _LINUX_SOURCE_COMPAT needs to be defined when reading
   sys/socket.h in order to make CMSG_SPACE and CMSG_LEN visible. */

#if defined(_AIX)
#define _LINUX_SOURCE_COMPAT 1
#endif

#include <sys/socket.h>

#if defined(_AIX)
#undef _LINUX_SOURCE_COMPAT
#endif


/* Needed for 'memset' on AIX 5.2 */
#if defined(_AIX)
# include <memory.h>
#endif


#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>


char filea[24];
char fileb[24];
char sock[24];

void
server (void)
{
   int s, fd1, fd2;
   struct sockaddr_un addr;
   
   fd1 = open(filea, O_RDWR | O_CREAT | O_TRUNC, 0750);
   if(fd1 == -1) {
      perror("open");
      exit(1);
   }

   fd2 = open(fileb, O_RDWR | O_CREAT | O_TRUNC, 0750);
   if(fd2 == -1) {
      perror("open");
      exit(1);
   }

   s = socket(PF_UNIX, SOCK_STREAM, 0);
   if(s == -1) {
      perror("socket");
      exit(1);
   }

   memset(&addr, 0, sizeof(addr));
   addr.sun_family = AF_UNIX;
   sprintf(addr.sun_path, sock);

   unlink(addr.sun_path);
   if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
      perror("bind");
      exit(1);
   }

   if(listen(s, 5) == -1) {
      perror("listen");
      exit(1);
   }

   {
      int x;
      int baddrsize = 0;
      struct sockaddr_un baddr;
      struct msghdr msg = {NULL, 0, NULL, 0, 0, 0, 0};
      struct cmsghdr *cmsg;
      char buf[CMSG_SPACE(sizeof(int) * 2)];
      struct iovec iov[1];

      memset(&baddr, 0, sizeof(baddr));
      x = accept(s, (struct sockaddr *)&baddr, &baddrsize);
      if(x == -1) {
         perror("accept");
         exit(1);
      }

      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) * 2);
      ((int *)CMSG_DATA(cmsg))[0] = fd1;
      ((int *)CMSG_DATA(cmsg))[1] = fd2;

      iov[0].iov_base = "hello";
      iov[0].iov_len = 6;

      msg.msg_iov = iov;
      msg.msg_iovlen = 1;

      if(sendmsg(x, &msg, 0) == -1) {
         perror("sendmsg");
         exit(1);
      }
   }
}

void
client (void)
{
   int s, fd1 = -1, fd2 = -1, size, count = 0, ret;
   struct sockaddr_un addr;
   struct iovec iov[1];
   union {
      struct cmsghdr cm;
      char control[CMSG_SPACE(sizeof(int) * 2)];
   } control_un;
   struct msghdr msg = { NULL, 0, iov, 1, control_un.control,
                         sizeof(control_un), 0 };
   struct cmsghdr *cmsg = &control_un.cm;
   char buf[1024];

   iov[0].iov_base = buf;
   iov[0].iov_len = sizeof(buf);

   s = socket(PF_UNIX, SOCK_STREAM, 0);
   if(s == -1) {
      perror("socket");
      exit(1);
   }

   addr.sun_family = AF_UNIX;
   sprintf(addr.sun_path, sock);

   do {
     count++;
     ret = connect(s, (struct sockaddr *)&addr, sizeof(addr));
     if(ret == -1) sleep(1);
   } while (count < 10 && ret == -1);

   if(ret == -1) {
      perror("connect");
      exit(1);
   }

  again:
   if((size = recvmsg(s, &msg, 0)) == -1) {
      if (errno == EINTR)
	 goto again;		/* SIGCHLD from server exiting could interrupt */
      perror("recvmsg");
      exit(1);
   }


   cmsg = CMSG_FIRSTHDR(&msg);
   while(cmsg) {
      if(cmsg->cmsg_level == SOL_SOCKET &&
         cmsg->cmsg_type == SCM_RIGHTS &&
         cmsg->cmsg_len == CMSG_LEN(sizeof(int) * 2)) {
         fd1 = ((int *)CMSG_DATA(cmsg))[0];
         fd2 = ((int *)CMSG_DATA(cmsg))[1];
      }

      cmsg = CMSG_NXTHDR(&msg, cmsg);
   }

   if(fd1 != -1) write(fd1, "Yeah 1\n", 8);
   if(fd2 != -1) write(fd2, "Yeah 2\n", 8);
}


int
main (int argc, char **argv)
{
   int pid, status;

   /*
    * Fedora Core 1's Perl opens /dev/pts/2 as fd 10.  Let's close it
    * now to get consistent results across different releases.
    */

   close(10);  close(4);

   pid = getpid();
   sprintf(filea, "/tmp/data1.%d", pid);
   sprintf(fileb, "/tmp/data2.%d", pid);
   sprintf(sock, "/tmp/sock.%d", pid);

   if((pid = fork()) == 0) {
      server();
      return 0;
   }

   client();

   wait(&status);

   unlink(filea);
   unlink(fileb);
   unlink(sock);
   return 0;
}