chiark / gitweb /
Qualify name given to @bres_byname@.
[mLib] / fdpass.c
CommitLineData
759ef658 1/* -*-c-*-
2 *
3 * $Id: fdpass.c,v 1.1 2003/11/29 11:58:49 mdw Exp $
4 *
5 * File descriptor passing
6 *
7 * (c) 2003 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: fdpass.c,v $
33 * Revision 1.1 2003/11/29 11:58:49 mdw
34 * File descriptor passing.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include <sys/types.h>
46#include <sys/time.h>
47#include <unistd.h>
48#include <sys/socket.h>
49#include <sys/uio.h>
50#include <sys/un.h>
51
52#include "fdpass.h"
53
54/*----- Main code ---------------------------------------------------------*/
55
56/* --- @fdpass_send@ --- *
57 *
58 * Arguments: @int sock@ = socket to send over
59 * @int fd@ = file descriptor to send
60 * @const void *p@ = pointer to data to send
61 * @size_t sz@ = size of buffer to send
62 *
63 * Returns: On error, @-1@, otherwise number of bytes transferred from
64 * @p@.
65 *
66 * Use: Sends a copy of file descriptor @fd@ to the other end of
67 * @sock@.
68 */
69
70ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz)
71{
72 struct iovec iov;
73 struct msghdr msg;
74#ifndef HAVE_MSG_ACCRIGHTS
75 char buf[CMSG_SPACE(sizeof(fd))];
76 struct cmsghdr *cmsg;
77#endif
78
79 iov.iov_base = (/*unconst*/ void *)p;
80 iov.iov_len = sz;
81 msg.msg_name = 0;
82 msg.msg_namelen = 0;
83 msg.msg_iov = &iov;
84 msg.msg_iovlen = 1;
85 msg.msg_flags = 0;
86#ifdef HAVE_MSG_ACCRIGHTS
87 msg.msg_accrights = &fd;
88 msg.msg_accrightslen = sizeof(fd);
89#else
90 msg.msg_control = buf;
91 msg.msg_controllen = sizeof(buf);
92 cmsg = CMSG_FIRSTHDR(&msg);
93 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
94 cmsg->cmsg_level = SOL_SOCKET;
95 cmsg->cmsg_type = SCM_RIGHTS;
96 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
97#endif
98 return (sendmsg(sock, &msg, 0));
99}
100
101/* --- @fdpass_recv@ --- *
102 *
103 * Arguments: @int sock@ = socket to send over
104 * @int *fd@ = where to put received descriptor
105 * @void *p@ = pointer to where to put data
106 * @size_t sz@ = size of buffer
107 *
108 * Returns: On error, @-1@, otherwise number of bytes transferred.
109 *
110 * Use: Receives a file descriptor. If the call succeeds, and there
111 * was a file descriptor, then @fd@ won't be @-1@ on exit;
112 * otherwise it will. At most one descriptor will be collected.
113 */
114
115ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz)
116{
117 struct iovec iov;
118 struct msghdr msg;
119 ssize_t rc;
120#ifndef HAVE_MSG_ACCRIGHTS
121 char buf[CMSG_SPACE(sizeof(fd))];
122 struct cmsghdr *cmsg;
123 int fdtmp;
124#endif
125
126 *fd = -1;
127 iov.iov_base = p;
128 iov.iov_len = sz;
129 msg.msg_name = 0;
130 msg.msg_namelen = 0;
131 msg.msg_iov = &iov;
132 msg.msg_iovlen = 1;
133 msg.msg_flags = 0;
134#ifdef HAVE_MSG_ACCRIGHTS
135 msg.msg_accrights = fd;
136 msg.msg_accrightslen = sizeof(*fd);
137#else
138 msg.msg_control = buf;
139 msg.msg_controllen = sizeof(buf);
140#endif
141 if ((rc = recvmsg(sock, &msg, 0)) < 0)
142 return (rc);
143#ifdef HAVE_MSG_ACCRIGHTS
144 if (msg.msg_accrightslen < sizeof(*fd))
145 *fd = -1;
146#else
147 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
148 if (cmsg->cmsg_level == SOL_SOCKET &&
149 cmsg->cmsg_type == SCM_RIGHTS &&
150 cmsg->cmsg_len >= CMSG_LEN(sizeof(fd))) {
151 memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp));
152 if (*fd == -1)
153 *fd = fdtmp;
154 else
155 close(fdtmp);
156 }
157 }
158#endif
159 return (rc);
160}
161
162/*----- That's all, folks -------------------------------------------------*/