aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/sys/unix/sys_uds.c
blob: 3b63d05cf6bd5e33c0ae42501bec4406ffe028dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2002-2009. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */


#include "sys_uds.h"

int
sys_uds_readv(int fd, struct iovec *iov, size_t iov_len,
             int *fds, int fd_count, int flags) {
    struct msghdr msg;
    struct cmsghdr *cmsg = NULL;
    char ancillary_buff[256] = {0};
    int res, i = 0;

    /* setup a place to fill in message contents */
    memset(&msg, 0, sizeof(struct msghdr));
    msg.msg_iov = iov;
    msg.msg_iovlen = iov_len;

    /* provide space for the ancillary data */
    msg.msg_control = ancillary_buff;
    msg.msg_controllen = sizeof(ancillary_buff);

    if((res = recvmsg(fd, &msg, flags)) < 0) {
        return res;
    }

    if((msg.msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
    {
        /* We assume that we have given enough space for any header
           that are sent to us. So the only remaining reason to get
           this flag set is if the caller has run out of file descriptors.
        */
        errno = EMFILE;
        return -1;
    }

    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg) ) {
        if ((cmsg->cmsg_level == SOL_SOCKET) &&
            (cmsg->cmsg_type == SCM_RIGHTS)) {
                int *cmsg_data = (int *)CMSG_DATA(cmsg);
                while ((char*)cmsg_data < (char*)cmsg + cmsg->cmsg_len) {
                    if (i < fd_count) {
                        fds[i++] = *cmsg_data++;
                    } else {
                        /* for some strange reason, we have received more FD's
                           than we wanted... close them if we are not running
                           debug. */
                        if(i >= fd_count) abort();
                        close(*cmsg_data++);
                    }
                }
            }
    }

    return res;
}

int
sys_uds_read(int fd, char *buff, size_t len,
             int *fds, int fd_count, int flags) {
    struct iovec iov;
    iov.iov_base = buff;
    iov.iov_len = len;
    return sys_uds_readv(fd, &iov, 1, fds, fd_count, flags);
}


int
sys_uds_writev(int fd, struct iovec *iov, size_t iov_len,
               int *fds, int fd_count, int flags) {

    struct msghdr msg;
    struct cmsghdr *cmsg = NULL;
    int res;

    /* initialize socket message */
    memset(&msg, 0, sizeof(struct msghdr));
    msg.msg_iov = iov;
    msg.msg_iovlen = iov_len;

    /* initialize the ancillary data */
    msg.msg_control = calloc(1, CMSG_SPACE(sizeof(int) * fd_count));
    msg.msg_controllen = CMSG_SPACE(sizeof(int) * fd_count);

    /* copy the fd array into the ancillary data */
    cmsg = CMSG_FIRSTHDR(&msg);
    if(!cmsg) abort();
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fd_count);
    memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * fd_count);

    res = sendmsg(fd, &msg, flags);

    free(msg.msg_control);

    return res;
}

int
sys_uds_write(int fd, char *buff, size_t len,
              int *fds, int fd_count, int flags) {
    struct iovec iov;
    iov.iov_base = buff;
    iov.iov_len = len;
    return sys_uds_writev(fd, &iov, 1, fds, fd_count, flags);
}