ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.3
Committed: Fri Jan 13 07:03:48 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_0
Changes since 1.2: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

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