xref: /f-stack/tools/compat/sysctl.c (revision e7145e36)
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 <sys/socket.h>
29 #include <rte_malloc.h>
30 
31 #include "ff_ipc.h"
32 
33 int
34 sysctl(int *name, unsigned namelen, void *old,
35     size_t *oldlenp, const void *new, size_t newlen)
36 {
37     struct ff_msg *msg, *retmsg = NULL;
38     char *extra_buf = NULL, *original_buf = NULL;
39     size_t total_len, original_buf_len;
40 
41     if (old != NULL && oldlenp == NULL) {
42         errno = EINVAL;
43         return -1;
44     }
45 
46     msg = ff_ipc_msg_alloc();
47     if (msg == NULL) {
48         errno = ENOMEM;
49         return -1;
50     }
51 
52     size_t oldlen = 0;
53     if (old && oldlenp) {
54         oldlen = *oldlenp;
55     }
56 
57     total_len = namelen + oldlen + newlen;
58     if (total_len > msg->buf_len) {
59         extra_buf = rte_malloc(NULL, total_len, 0);
60         if (extra_buf == NULL) {
61             errno = ENOMEM;
62             ff_ipc_msg_free(msg);
63             return -1;
64         }
65         original_buf = msg->buf_addr;
66         original_buf_len = msg->buf_len;
67         msg->buf_addr = extra_buf;
68         msg->buf_len = total_len;
69     }
70 
71     char *buf_addr = msg->buf_addr;
72 
73     msg->msg_type = FF_SYSCTL;
74     msg->sysctl.name = (int *)buf_addr;
75     msg->sysctl.namelen = namelen;
76     memcpy(msg->sysctl.name, name, namelen * sizeof(int));
77 
78     buf_addr += namelen * sizeof(int);
79 
80     if (new != NULL && newlen != 0) {
81         msg->sysctl.new = buf_addr;
82         msg->sysctl.newlen = newlen;
83         memcpy(msg->sysctl.new, new, newlen);
84 
85         buf_addr += newlen;
86     } else {
87         msg->sysctl.new = NULL;
88         msg->sysctl.newlen = 0;
89     }
90 
91     if (oldlenp != NULL) {
92         msg->sysctl.oldlenp = (size_t *)buf_addr;
93         memcpy(msg->sysctl.oldlenp, oldlenp, sizeof(size_t));
94         buf_addr += sizeof(size_t);
95 
96         if (old != NULL) {
97             msg->sysctl.old = (void *)buf_addr;
98             memcpy(msg->sysctl.old, old, *oldlenp);
99             buf_addr += *oldlenp;
100         } else {
101             msg->sysctl.old = NULL;
102         }
103     } else {
104         msg->sysctl.oldlenp = NULL;
105         msg->sysctl.old = NULL;
106     }
107 
108     int ret = ff_ipc_send(msg);
109     if (ret < 0) {
110         errno = EPIPE;
111         goto error;
112     }
113 
114     do {
115         if (retmsg != NULL) {
116             ff_ipc_msg_free(retmsg);
117         }
118         ret = ff_ipc_recv(&retmsg, msg->msg_type);
119         if (ret < 0) {
120             errno = EPIPE;
121             goto error;
122         }
123     } while (msg != retmsg);
124 
125     if (retmsg->result == 0) {
126         ret = 0;
127         if (oldlenp && retmsg->sysctl.oldlenp) {
128             *oldlenp = *retmsg->sysctl.oldlenp;
129         }
130 
131         if (old && retmsg->sysctl.old && oldlenp) {
132             memcpy(old, retmsg->sysctl.old, *oldlenp);
133         }
134     } else {
135         ret = -1;
136         errno = retmsg->result;
137     }
138 
139 error:
140     if (original_buf) {
141         msg->buf_addr = original_buf;
142         msg->buf_len = original_buf_len;
143     }
144     ff_ipc_msg_free(msg);
145     if (extra_buf) {
146         rte_free(extra_buf);
147     }
148 
149     return ret;
150 }
151