| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 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 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #include "config.h" |
| 33 | |
| 34 | #include <errno.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/time.h> |
| 41 | #include <unistd.h> |
| 42 | #include <sys/socket.h> |
| 43 | #include <sys/uio.h> |
| 44 | #include <sys/un.h> |
| 45 | |
| 46 | #include "fdpass.h" |
| 47 | |
| 48 | /*----- Main code ---------------------------------------------------------*/ |
| 49 | |
| 50 | /* --- @fdpass_send@ --- * |
| 51 | * |
| 52 | * Arguments: @int sock@ = socket to send over |
| 53 | * @int fd@ = file descriptor to send |
| 54 | * @const void *p@ = pointer to data to send |
| 55 | * @size_t sz@ = size of buffer to send |
| 56 | * |
| 57 | * Returns: On error, @-1@, otherwise number of bytes transferred from |
| 58 | * @p@. |
| 59 | * |
| 60 | * Use: Sends a copy of file descriptor @fd@ to the other end of |
| 61 | * @sock@. |
| 62 | */ |
| 63 | |
| 64 | ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz) |
| 65 | { |
| 66 | struct iovec iov; |
| 67 | struct msghdr msg; |
| 68 | #ifndef HAVE_MSG_ACCRIGHTS |
| 69 | char buf[CMSG_SPACE(sizeof(fd))]; |
| 70 | struct cmsghdr *cmsg; |
| 71 | #endif |
| 72 | |
| 73 | iov.iov_base = (/*unconst*/ void *)p; |
| 74 | iov.iov_len = sz; |
| 75 | msg.msg_name = 0; |
| 76 | msg.msg_namelen = 0; |
| 77 | msg.msg_iov = &iov; |
| 78 | msg.msg_iovlen = 1; |
| 79 | #ifdef HAVE_MSG_ACCRIGHTS |
| 80 | msg.msg_accrights = &fd; |
| 81 | msg.msg_accrightslen = sizeof(fd); |
| 82 | #else |
| 83 | msg.msg_flags = 0; |
| 84 | msg.msg_control = buf; |
| 85 | msg.msg_controllen = sizeof(buf); |
| 86 | cmsg = CMSG_FIRSTHDR(&msg); |
| 87 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); |
| 88 | cmsg->cmsg_level = SOL_SOCKET; |
| 89 | cmsg->cmsg_type = SCM_RIGHTS; |
| 90 | memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); |
| 91 | #endif |
| 92 | return (sendmsg(sock, &msg, 0)); |
| 93 | } |
| 94 | |
| 95 | /* --- @fdpass_recv@ --- * |
| 96 | * |
| 97 | * Arguments: @int sock@ = socket to send over |
| 98 | * @int *fd@ = where to put received descriptor |
| 99 | * @void *p@ = pointer to where to put data |
| 100 | * @size_t sz@ = size of buffer |
| 101 | * |
| 102 | * Returns: On error, @-1@, otherwise number of bytes transferred. |
| 103 | * |
| 104 | * Use: Receives a file descriptor. If the call succeeds, and there |
| 105 | * was a file descriptor, then @fd@ won't be @-1@ on exit; |
| 106 | * otherwise it will. At most one descriptor will be collected. |
| 107 | */ |
| 108 | |
| 109 | ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz) |
| 110 | { |
| 111 | struct iovec iov; |
| 112 | struct msghdr msg; |
| 113 | ssize_t rc; |
| 114 | #ifndef HAVE_MSG_ACCRIGHTS |
| 115 | char buf[CMSG_SPACE(sizeof(fd))]; |
| 116 | struct cmsghdr *cmsg; |
| 117 | int fdtmp; |
| 118 | #endif |
| 119 | |
| 120 | *fd = -1; |
| 121 | iov.iov_base = p; |
| 122 | iov.iov_len = sz; |
| 123 | msg.msg_name = 0; |
| 124 | msg.msg_namelen = 0; |
| 125 | msg.msg_iov = &iov; |
| 126 | msg.msg_iovlen = 1; |
| 127 | #ifdef HAVE_MSG_ACCRIGHTS |
| 128 | msg.msg_accrights = fd; |
| 129 | msg.msg_accrightslen = sizeof(*fd); |
| 130 | #else |
| 131 | msg.msg_flags = 0; |
| 132 | msg.msg_control = buf; |
| 133 | msg.msg_controllen = sizeof(buf); |
| 134 | #endif |
| 135 | if ((rc = recvmsg(sock, &msg, 0)) < 0) |
| 136 | return (rc); |
| 137 | #ifdef HAVE_MSG_ACCRIGHTS |
| 138 | if (msg.msg_accrightslen < sizeof(*fd)) |
| 139 | *fd = -1; |
| 140 | #else |
| 141 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 142 | if (cmsg->cmsg_level == SOL_SOCKET && |
| 143 | cmsg->cmsg_type == SCM_RIGHTS && |
| 144 | cmsg->cmsg_len >= CMSG_LEN(sizeof(fd))) { |
| 145 | memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp)); |
| 146 | if (*fd == -1) |
| 147 | *fd = fdtmp; |
| 148 | else |
| 149 | close(fdtmp); |
| 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | return (rc); |
| 154 | } |
| 155 | |
| 156 | /*----- That's all, folks -------------------------------------------------*/ |