xref: /f-stack/tools/compat/ioctl.c (revision 488adaba)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include <string.h>
28 #include <stdarg.h>
29 #include "sys/ioctl.h"
30 #include "ff_ipc.h"
31 
32 /*
33  * In general, we always call like this: ioctl(fd, com, data),
34  * but if there is a pointer in the data and the pointer points to
35  * a memory area, for example, data is struct ifreq, and it uses
36  * ifreq.ifr_ifru.ifru_data, we must copy the memory to msg->buf_addr,
37  * after this, it can be used to communicate with F-Stack process.
38  * Otherwise, an unknown error will occur.
39  *
40  * Two cases:
41  * 1.Normal, there is no need to copy memory: ioctl_va(fd, com, data, 0).
42  * 2.There is a memory need to be copied: ioctl_va(fd, com, data, 3, offset, cpy_mem, clen).
43  *     offset: the offset of cpy_mem relative to data struct.
44  *     cpy_mem: the memory address that need to be copied.
45  *     clen: the size of memory that the cpy_mem pointed to.
46  *
47  */
48 int
49 ioctl_va(int fd, unsigned long com, void *data, int argc, ...)
50 {
51     struct ff_msg *msg, *retmsg = NULL;
52     unsigned size;
53     void *cpy_mem;
54     size_t offset, clen;
55 
56     if (argc != 0 && argc != 3) {
57         errno = EINVAL;
58         return -1;
59     }
60 
61     if (argc == 3) {
62         va_list ap;
63         va_start(ap, argc);
64         offset = va_arg(ap, size_t);
65         cpy_mem = va_arg(ap, void *);
66         clen = va_arg(ap, size_t);
67         va_end(ap);
68     }
69 
70     if (com > 0xffffffff) {
71         printf("WARNING: ioctl sign-extension ioctl %lx\n", com);
72         com &= 0xffffffff;
73     }
74 
75     size = IOCPARM_LEN(com);
76     if ((size > IOCPARM_MAX) ||
77         ((com & (IOC_IN | IOC_OUT)) == 0) ||
78         (size == 0) ||
79         (com & IOC_VOID))
80         return (ENOTTY);
81 
82     msg = ff_ipc_msg_alloc();
83     if (msg == NULL) {
84         errno = ENOMEM;
85         return -1;
86     }
87 
88     if (size > msg->buf_len) {
89         errno = EINVAL;
90         ff_ipc_msg_free(msg);
91         return -1;
92     }
93 
94     msg->msg_type = FF_IOCTL;
95     msg->ioctl.cmd = com;
96     msg->ioctl.data = msg->buf_addr;
97     memcpy(msg->ioctl.data, data, size);
98     msg->buf_addr += size;
99 
100     if (argc == 3) {
101         if (size + clen > msg->buf_len) {
102             errno = EINVAL;
103             ff_ipc_msg_free(msg);
104             return -1;
105         }
106         char *ptr = (char *)(msg->ioctl.data) + offset;
107         char *buf_addr = msg->buf_addr;
108         memcpy(ptr, &buf_addr, sizeof(char *));
109         memcpy(buf_addr, cpy_mem, clen);
110     }
111 
112     int ret = ff_ipc_send(msg);
113     if (ret < 0) {
114         errno = EPIPE;
115         ff_ipc_msg_free(msg);
116         return -1;
117     }
118 
119     do {
120         if (retmsg != NULL) {
121             ff_ipc_msg_free(retmsg);
122         }
123         ret = ff_ipc_recv(&retmsg);
124         if (ret < 0) {
125             errno = EPIPE;
126             ff_ipc_msg_free(msg);
127             return -1;
128         }
129     } while (msg != retmsg);
130 
131     if (retmsg->result == 0) {
132         ret = 0;
133 
134         if (com & IOC_OUT) {
135             memcpy(data, retmsg->ioctl.data, size);
136             if (argc == 3) {
137                 memcpy(cpy_mem, retmsg->buf_addr, clen);
138                 char *ptr = (char *)data + offset;
139                 memcpy(ptr, &cpy_mem, sizeof(void *));
140             }
141         }
142     } else {
143         ret = -1;
144         errno = retmsg->result;
145     }
146 
147     ff_ipc_msg_free(msg);
148 
149     return ret;
150 }
151