ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
(Generate patch)

Comparing rxvt-unicode/src/fdpass.C (file contents):
Revision 1.1 by root, Fri Dec 23 14:46:34 2005 UTC vs.
Revision 1.16 by root, Mon Feb 21 07:41:00 2011 UTC

1// This file is part of libptytty. Do not make local modifications.
2// http://software.schmorp.de/pkg/libptytty
3
1/*--------------------------------*-C-*---------------------------------* 4/*----------------------------------------------------------------------*
2 * File: fdpass.C 5 * File: fdpass.C
3 *----------------------------------------------------------------------* 6 *----------------------------------------------------------------------*
4 * 7 *
5 * All portions of code are copyright by their respective author/s. 8 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005 Marc Lehmann <pcg@goof.com> 9 * Copyright (c) 2005-2006 Marc Lehmann <schmorp@schmorp.de>
7 * 10 *
8 * This program is free software; you can redistribute it and/or modify 11 * 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 12 * 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 13 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 14 * (at your option) any later version.
18 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/ 24 *----------------------------------------------------------------------*/
22 25
23#include "../config.h" /* NECESSARY */ 26#include "config.h"
24 27
25#if ENABLE_FRILLS && HAVE_UNIX_FDPASS 28#include <cstddef> // needed by broken bsds for NULL used in sys/uio.h
29#include <cstdlib>
26 30
27#include <sys/types.h> 31#include <sys/types.h>
32#include <sys/uio.h>
28#include <sys/socket.h> 33#include <sys/socket.h>
29 34
30#include "fdpass.h" 35#include "libptytty.h"
31 36
32#include <cstdio> //d 37// CMSG_SPACE & CMSG_LEN are rfc2292 extensions to unix
38#ifndef CMSG_SPACE
39# define CMSG_SPACE(len) (sizeof (cmsghdr) + len)
40#endif
33 41
34#ifndef CMSG_LEN // CMSG_SPACe && CMSG_LEN are rfc2292 extensions to unix 42#ifndef CMSG_LEN
35# define CMSG_LEN(len) (sizeof (cmsghdr) + len) 43# define CMSG_LEN(len) (sizeof (cmsghdr) + len)
36#endif 44#endif
37 45
38int 46bool
39rxvt_send_fd (int socket, int fd) 47ptytty::send_fd (int socket, int fd)
40{ 48{
49 void *buf = malloc (CMSG_SPACE (sizeof (int)));
50
51 if (!buf)
52 return 0;
53
41 msghdr msg; 54 msghdr msg;
42 iovec iov; 55 iovec iov;
43 char buf [CMSG_LEN (sizeof (int))]; 56 cmsghdr *cmsg;
44 char data = 0; 57 char data = 0;
45 58
46 iov.iov_base = &data; 59 iov.iov_base = &data;
47 iov.iov_len = 1; 60 iov.iov_len = 1;
48 61
49 msg.msg_name = 0; 62 msg.msg_name = 0;
50 msg.msg_namelen = 0; 63 msg.msg_namelen = 0;
51 msg.msg_iov = &iov; 64 msg.msg_iov = &iov;
52 msg.msg_iovlen = 1; 65 msg.msg_iovlen = 1;
53 msg.msg_control = (void *)buf; 66 msg.msg_control = buf;
54 msg.msg_controllen = sizeof buf; 67 msg.msg_controllen = CMSG_SPACE (sizeof (int));
55 68
56 cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); 69 cmsg = CMSG_FIRSTHDR (&msg);
57 cmsg->cmsg_level = SOL_SOCKET; 70 cmsg->cmsg_level = SOL_SOCKET;
58 cmsg->cmsg_type = SCM_RIGHTS; 71 cmsg->cmsg_type = SCM_RIGHTS;
59 cmsg->cmsg_len = CMSG_LEN (sizeof (int)); 72 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
60 73
61 *(int *)CMSG_DATA (cmsg) = fd; 74 *(int *)CMSG_DATA (cmsg) = fd;
62 75
63 msg.msg_controllen = cmsg->cmsg_len; 76 ssize_t result = sendmsg (socket, &msg, 0);
64 77
65 return sendmsg (socket, &msg, 0); 78 free (buf);
79
80 return result >= 0;
66} 81}
67 82
68int 83int
69rxvt_recv_fd (int socket) 84ptytty::recv_fd (int socket)
70{ 85{
86 void *buf = malloc (CMSG_SPACE (sizeof (int)));
87
88 if (!buf)
89 return -1;
90
71 msghdr msg; 91 msghdr msg;
72 iovec iov; 92 iovec iov;
73 char buf [CMSG_LEN (sizeof (int))]; /* ancillary data buffer */
74 char data = 1; 93 char data = 1;
75 94
76 iov.iov_base = &data; 95 iov.iov_base = &data;
77 iov.iov_len = 1; 96 iov.iov_len = 1;
78 97
79 msg.msg_name = 0; 98 msg.msg_name = 0;
80 msg.msg_namelen = 0; 99 msg.msg_namelen = 0;
81 msg.msg_iov = &iov; 100 msg.msg_iov = &iov;
82 msg.msg_iovlen = 1; 101 msg.msg_iovlen = 1;
83 msg.msg_control = buf; 102 msg.msg_control = buf;
84 msg.msg_controllen = sizeof buf; 103 msg.msg_controllen = CMSG_SPACE (sizeof (int));
85 104
86 cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); 105 int fd = -1;
87 cmsg->cmsg_level = SOL_SOCKET;
88 cmsg->cmsg_type = SCM_RIGHTS;
89 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
90 106
91 msg.msg_controllen = cmsg->cmsg_len; 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);
92 112
93 if (recvmsg (socket, &msg, 0) <= 0
94 || data != 0
95 || msg.msg_controllen < CMSG_LEN (sizeof (int))
96 || cmsg->cmsg_level != SOL_SOCKET 113 if (cmsg->cmsg_level == SOL_SOCKET
97 || cmsg->cmsg_type != SCM_RIGHTS 114 && cmsg->cmsg_type == SCM_RIGHTS
98 || cmsg->cmsg_len < CMSG_LEN (sizeof (int))) 115 && cmsg->cmsg_len >= CMSG_LEN (sizeof (int)))
99 return -1; 116 fd = *(int *)CMSG_DATA (cmsg);
117 }
100 118
101 return *(int *)CMSG_DATA (cmsg); 119 free (buf);
120
121 return fd;
102} 122}
103 123
104#endif
105

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines