ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/fdpass.C
Revision: 1.14
Committed: Wed Jan 18 12:19:31 2012 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +1 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.4 /*----------------------------------------------------------------------*
2 root 1.1 * File: fdpass.C
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.14 * Copyright (c) 2005-2006,2012 Marc Lehmann <schmorp@schmorp.de>
7 root 1.1 *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *----------------------------------------------------------------------*/
22    
23 root 1.13 #if defined(__SVR4) && defined(__sun)
24     # define _XPG4_2 1
25     #endif
26    
27 sf-exg 1.11 #include "config.h"
28 root 1.1
29 root 1.7 #include <cstddef> // needed by broken bsds for NULL used in sys/uio.h
30 root 1.10 #include <cstdlib>
31 root 1.6
32 ayin 1.9 #include <sys/types.h>
33 root 1.2 #include <sys/uio.h>
34 root 1.1 #include <sys/socket.h>
35    
36 root 1.3 #include "libptytty.h"
37 root 1.1
38 root 1.10 // CMSG_SPACE & CMSG_LEN are rfc2292 extensions to unix
39     #ifndef CMSG_SPACE
40     # define CMSG_SPACE(len) (sizeof (cmsghdr) + len)
41     #endif
42    
43     #ifndef CMSG_LEN
44 root 1.1 # define CMSG_LEN(len) (sizeof (cmsghdr) + len)
45     #endif
46    
47 root 1.5 bool
48 root 1.3 ptytty::send_fd (int socket, int fd)
49 root 1.1 {
50 root 1.10 void *buf = malloc (CMSG_SPACE (sizeof (int)));
51    
52     if (!buf)
53     return 0;
54    
55 root 1.1 msghdr msg;
56     iovec iov;
57 root 1.10 cmsghdr *cmsg;
58 root 1.1 char data = 0;
59    
60     iov.iov_base = &data;
61     iov.iov_len = 1;
62    
63     msg.msg_name = 0;
64     msg.msg_namelen = 0;
65     msg.msg_iov = &iov;
66     msg.msg_iovlen = 1;
67 root 1.10 msg.msg_control = buf;
68     msg.msg_controllen = CMSG_SPACE (sizeof (int));
69 root 1.1
70 root 1.10 cmsg = CMSG_FIRSTHDR (&msg);
71 root 1.1 cmsg->cmsg_level = SOL_SOCKET;
72     cmsg->cmsg_type = SCM_RIGHTS;
73     cmsg->cmsg_len = CMSG_LEN (sizeof (int));
74    
75     *(int *)CMSG_DATA (cmsg) = fd;
76    
77 root 1.10 ssize_t result = sendmsg (socket, &msg, 0);
78    
79     free (buf);
80 root 1.1
81 root 1.10 return result >= 0;
82 root 1.1 }
83    
84     int
85 root 1.3 ptytty::recv_fd (int socket)
86 root 1.1 {
87 root 1.10 void *buf = malloc (CMSG_SPACE (sizeof (int)));
88    
89     if (!buf)
90     return -1;
91    
92 root 1.1 msghdr msg;
93     iovec iov;
94     char data = 1;
95    
96     iov.iov_base = &data;
97     iov.iov_len = 1;
98    
99     msg.msg_name = 0;
100     msg.msg_namelen = 0;
101     msg.msg_iov = &iov;
102     msg.msg_iovlen = 1;
103     msg.msg_control = buf;
104 root 1.10 msg.msg_controllen = CMSG_SPACE (sizeof (int));
105 root 1.1
106 root 1.10 int fd = -1;
107    
108     if (recvmsg (socket, &msg, 0) > 0
109     && data == 0
110     && msg.msg_controllen >= CMSG_SPACE (sizeof (int)))
111     {
112     cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
113 root 1.1
114 root 1.10 if (cmsg->cmsg_level == SOL_SOCKET
115     && cmsg->cmsg_type == SCM_RIGHTS
116     && cmsg->cmsg_len >= CMSG_LEN (sizeof (int)))
117     fd = *(int *)CMSG_DATA (cmsg);
118     }
119 root 1.1
120 root 1.10 free (buf);
121 root 1.1
122 root 1.10 return fd;
123 root 1.1 }
124