ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.16
Committed: Mon Feb 21 07:41:00 2011 UTC (13 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_11
Changes since 1.15: +1 -1 lines
Log Message:
the pcg is now the schmorp

File Contents

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