ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.4
Committed: Tue Jan 17 15:17:39 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_1
Changes since 1.3: +0 -4 lines
Log Message:
logging.C

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