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