1 // This file is part of libptytty. Do not make local modifications.
2 // http://software.schmorp.de/pkg/libptytty
4 /*----------------------------------------------------------------------*
6 *----------------------------------------------------------------------*
8 * All portions of code are copyright by their respective author/s.
9 * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com>
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.
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.
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 *----------------------------------------------------------------------*/
26 #include "../config.h"
28 #include <cstddef> // needed by broken bsds for NULL used in sys/uio.h
31 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include "libptytty.h"
37 // CMSG_SPACE & CMSG_LEN are rfc2292 extensions to unix
39 # define CMSG_SPACE(len) (sizeof (cmsghdr) + len)
43 # define CMSG_LEN(len) (sizeof (cmsghdr) + len)
47 ptytty::send_fd (int socket, int fd)
49 void *buf = malloc (CMSG_SPACE (sizeof (int)));
66 msg.msg_control = buf;
67 msg.msg_controllen = CMSG_SPACE (sizeof (int));
69 cmsg = CMSG_FIRSTHDR (&msg);
70 cmsg->cmsg_level = SOL_SOCKET;
71 cmsg->cmsg_type = SCM_RIGHTS;
72 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
74 *(int *)CMSG_DATA (cmsg) = fd;
76 ssize_t result = sendmsg (socket, &msg, 0);
84 ptytty::recv_fd (int socket)
86 void *buf = malloc (CMSG_SPACE (sizeof (int)));
102 msg.msg_control = buf;
103 msg.msg_controllen = CMSG_SPACE (sizeof (int));
107 if (recvmsg (socket, &msg, 0) > 0
109 && msg.msg_controllen >= CMSG_SPACE (sizeof (int)))
111 cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
113 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);