[BACK]Return to monitor_fdpass.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_fdpass.c, Revision 1.6.6.1

1.6.6.1 ! brad        1: /* $OpenBSD: monitor_fdpass.c,v 1.12 2006/08/03 03:34:42 deraadt Exp $ */
1.1       provos      2: /*
                      3:  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.6.6.1 ! brad       27: #include <sys/types.h>
        !            28: #include <sys/socket.h>
1.1       provos     29: #include <sys/uio.h>
1.6.6.1 ! brad       30:
        !            31: #include <errno.h>
        !            32: #include <string.h>
        !            33: #include <stdarg.h>
1.1       provos     34:
                     35: #include "log.h"
                     36: #include "monitor_fdpass.h"
                     37:
                     38: void
1.5       avsm       39: mm_send_fd(int sock, int fd)
1.1       provos     40: {
                     41:        struct msghdr msg;
                     42:        char tmp[CMSG_SPACE(sizeof(int))];
                     43:        struct cmsghdr *cmsg;
                     44:        struct iovec vec;
1.2       stevesk    45:        char ch = '\0';
1.4       deraadt    46:        ssize_t n;
1.1       provos     47:
                     48:        memset(&msg, 0, sizeof(msg));
                     49:        msg.msg_control = (caddr_t)tmp;
                     50:        msg.msg_controllen = CMSG_LEN(sizeof(int));
                     51:        cmsg = CMSG_FIRSTHDR(&msg);
                     52:        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
                     53:        cmsg->cmsg_level = SOL_SOCKET;
                     54:        cmsg->cmsg_type = SCM_RIGHTS;
                     55:        *(int *)CMSG_DATA(cmsg) = fd;
                     56:
                     57:        vec.iov_base = &ch;
                     58:        vec.iov_len = 1;
                     59:        msg.msg_iov = &vec;
                     60:        msg.msg_iovlen = 1;
                     61:
1.5       avsm       62:        if ((n = sendmsg(sock, &msg, 0)) == -1)
1.3       markus     63:                fatal("%s: sendmsg(%d): %s", __func__, fd,
1.2       stevesk    64:                    strerror(errno));
                     65:        if (n != 1)
1.4       deraadt    66:                fatal("%s: sendmsg: expected sent 1 got %ld",
                     67:                    __func__, (long)n);
1.1       provos     68: }
                     69:
                     70: int
1.5       avsm       71: mm_receive_fd(int sock)
1.1       provos     72: {
                     73:        struct msghdr msg;
                     74:        char tmp[CMSG_SPACE(sizeof(int))];
                     75:        struct cmsghdr *cmsg;
                     76:        struct iovec vec;
1.4       deraadt    77:        ssize_t n;
1.1       provos     78:        char ch;
1.4       deraadt    79:        int fd;
1.1       provos     80:
                     81:        memset(&msg, 0, sizeof(msg));
                     82:        vec.iov_base = &ch;
                     83:        vec.iov_len = 1;
                     84:        msg.msg_iov = &vec;
                     85:        msg.msg_iovlen = 1;
                     86:        msg.msg_control = tmp;
                     87:        msg.msg_controllen = sizeof(tmp);
                     88:
1.5       avsm       89:        if ((n = recvmsg(sock, &msg, 0)) == -1)
1.3       markus     90:                fatal("%s: recvmsg: %s", __func__, strerror(errno));
1.2       stevesk    91:        if (n != 1)
1.4       deraadt    92:                fatal("%s: recvmsg: expected received 1 got %ld",
                     93:                    __func__, (long)n);
1.1       provos     94:
                     95:        cmsg = CMSG_FIRSTHDR(&msg);
1.6       djm        96:        if (cmsg == NULL)
                     97:                fatal("%s: no message header", __func__);
1.1       provos     98:        if (cmsg->cmsg_type != SCM_RIGHTS)
1.3       markus     99:                fatal("%s: expected type %d got %d", __func__,
1.1       provos    100:                    SCM_RIGHTS, cmsg->cmsg_type);
1.2       stevesk   101:        fd = (*(int *)CMSG_DATA(cmsg));
                    102:        return fd;
1.1       provos    103: }