ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/fdpass.C
Revision: 1.1
Committed: Sat Jan 21 22:06:16 2006 UTC (20 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*--------------------------------*-C-*---------------------------------*
2     * File: fdpass.C
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com>
7     *
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     #include "../config.h"
24    
25     #include <sys/types.h>
26     #include <sys/socket.h>
27    
28     #include "fdpass.h"
29    
30     #ifndef CMSG_LEN // CMSG_SPACe && CMSG_LEN are rfc2292 extensions to unix
31     # define CMSG_LEN(len) (sizeof (cmsghdr) + len)
32     #endif
33    
34     int
35     rxvt_send_fd (int socket, int fd)
36     {
37     msghdr msg;
38     iovec iov;
39     char buf [CMSG_LEN (sizeof (int))];
40     char data = 0;
41    
42     iov.iov_base = &data;
43     iov.iov_len = 1;
44    
45     msg.msg_name = 0;
46     msg.msg_namelen = 0;
47     msg.msg_iov = &iov;
48     msg.msg_iovlen = 1;
49     msg.msg_control = (void *)buf;
50     msg.msg_controllen = sizeof buf;
51    
52     cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
53     cmsg->cmsg_level = SOL_SOCKET;
54     cmsg->cmsg_type = SCM_RIGHTS;
55     cmsg->cmsg_len = CMSG_LEN (sizeof (int));
56    
57     *(int *)CMSG_DATA (cmsg) = fd;
58    
59     msg.msg_controllen = cmsg->cmsg_len;
60    
61     return sendmsg (socket, &msg, 0);
62     }
63    
64     int
65     rxvt_recv_fd (int socket)
66     {
67     msghdr msg;
68     iovec iov;
69     char buf [CMSG_LEN (sizeof (int))]; /* ancillary data buffer */
70     char data = 1;
71    
72     iov.iov_base = &data;
73     iov.iov_len = 1;
74    
75     msg.msg_name = 0;
76     msg.msg_namelen = 0;
77     msg.msg_iov = &iov;
78     msg.msg_iovlen = 1;
79     msg.msg_control = buf;
80     msg.msg_controllen = sizeof buf;
81    
82     cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
83     cmsg->cmsg_level = SOL_SOCKET;
84     cmsg->cmsg_type = SCM_RIGHTS;
85     cmsg->cmsg_len = CMSG_LEN (sizeof (int));
86    
87     msg.msg_controllen = cmsg->cmsg_len;
88    
89     if (recvmsg (socket, &msg, 0) <= 0
90     || data != 0
91     || msg.msg_controllen < CMSG_LEN (sizeof (int))
92     || cmsg->cmsg_level != SOL_SOCKET
93     || cmsg->cmsg_type != SCM_RIGHTS
94     || cmsg->cmsg_len < CMSG_LEN (sizeof (int)))
95     return -1;
96    
97     return *(int *)CMSG_DATA (cmsg);
98     }
99