param_service.c 10.3 KB
Newer Older
Z
zhong_ning 已提交
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
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * 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.
 */

#include "param_service.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "sys_param.h"
#include "param_manager.h"
#include "param_request.h"
#include "init_param.h"

#include "uv.h"

#define BUFFER_SIZE 256
#define LABEL "Server"

static char *g_initContext = "";
static ParamWorkSpace g_paramWorkSpace = {ATOMIC_VAR_INIT(0), {}, {}, {}};

void InitParamService()
{
    int ret = InitParamWorkSpace(&g_paramWorkSpace, 0, g_initContext);
    PARAM_CHECK(ret == 0, return, "Init parameter workspace fail");
}

int LoadDefaultParams(const char *fileName)
{
    u_int32_t flags = atomic_load_explicit(&g_paramWorkSpace.flags, memory_order_relaxed);
    if ((flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) {
        return PARAM_CODE_NOT_INIT;
    }
    FILE *fp = fopen(fileName, "r");
    PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
    char buff[BUFFER_SIZE];
    SubStringInfo *info = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_LABEL + 1));
S
sun_fan 已提交
52 53
    PARAM_CHECK(info != NULL, return -1, "malloc failed");

Z
zhong_ning 已提交
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
    while(fgets(buff, BUFFER_SIZE, fp) != NULL) {
        int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=',  info, SUBSTR_INFO_LABEL + 1);
        if (subStrNumber <= SUBSTR_INFO_LABEL) {
            continue;
        }

        if (strncmp(info[0].value, "ctl.", strlen("ctl.")) == 0) {
            PARAM_LOGE("Do not set ctl. parameters from init %s", info[0].value);
            continue;
        }
        if (strcmp(info[0].value, "selinux.restorecon_recursive") == 0) {
            PARAM_LOGE("Do not set selinux.restorecon_recursive from init %s", info[0].value);
            continue;
        }
        int ret = CheckParamName(info[0].value, 0);
        PARAM_CHECK(ret == 0, continue, "Illegal param name %s", info[0].value);

        ret = WriteParam(&g_paramWorkSpace.paramSpace, info[0].value, info[1].value);
        PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buff);
    }
    fclose(fp);
    free(info);
    PARAM_LOGI("LoadDefaultParams proterty success %s", fileName);
    return 0;
}

int LoadParamInfos(const char *fileName)
{
    u_int32_t flags = atomic_load_explicit(&g_paramWorkSpace.flags, memory_order_relaxed);
    if ((flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) {
        return PARAM_CODE_NOT_INIT;
    }
    FILE *fp = fopen(fileName, "r");
    PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
    SubStringInfo *info = malloc(sizeof(SubStringInfo) * SUBSTR_INFO_MAX);
S
sun_fan 已提交
89
    PARAM_CHECK(info != NULL, return -1, "Load parameter malloc failed.");
Z
zhong_ning 已提交
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
    char buff[BUFFER_SIZE];
    int infoCount = 0;
    while(fgets(buff, BUFFER_SIZE, fp) != NULL) {
        int subStrNumber = GetSubStringInfo(buff, strlen(buff), ' ',  info, SUBSTR_INFO_MAX);
        if (subStrNumber <= 0) {
            continue;
        }
        int ret = WriteParamInfo(&g_paramWorkSpace, info, subStrNumber);
        PARAM_CHECK(ret == 0, continue, "Failed to write param info %d %s", ret, buff);
        infoCount++;
    }
    fclose(fp);
    free(info);
    PARAM_LOGI("Load parameter info %d success %s", infoCount, fileName);
    return 0;
}

static int ProcessParamSet(RequestMsg *msg)
{
    PARAM_CHECK(msg != NULL, return PARAM_CODE_INVALID_PARAM, "Failed to check param");

    SubStringInfo info[3];
    int ret = GetSubStringInfo(msg->content, msg->contentSize, '=',  info, sizeof(info)/sizeof(info[0]));
    PARAM_CHECK(ret >= 2, return ret, "Failed to get name from content %s", msg->content);

Z
zhong_ning 已提交
115
    PARAM_LOGD("ProcessParamSet name %s value: %s", info[0].value, info[1].value);
Z
zhong_ning 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    ret = WriteParamWithCheck(&g_paramWorkSpace, &msg->securitylabel, info[0].value, info[1].value);
    PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, info[0].value, info[1].value);
    ret = WritePersistParam(info[0].value, info[1].value);
    PARAM_CHECK(ret == 0, return ret, "Failed to set param");
    // notify event to process trigger
    PostTrigger(EVENT_PROPERTY, msg->content, msg->contentSize);
    return 0;
}

static void OnClose(uv_handle_t *handle)
{
    free(handle);
}

static void OnReceiveAlloc(uv_handle_t *handle, size_t suggestedSize, uv_buf_t* buf)
{
    // 这里需要按实际消息的大小申请内存,取最大消息的长度
    buf->len = sizeof(RequestMsg) + BUFFER_SIZE * 2;
    buf->base = (char *)malloc(buf->len);
}

static void OnWriteResponse(uv_write_t *req, int status)
{
    // 发送成功,释放请求内存
Z
zhong_ning 已提交
140
    PARAM_LOGD("OnWriteResponse status %d", status);
Z
zhong_ning 已提交
141 142 143 144
    ResponseNode *node = (ResponseNode*)req;
    free(node);
}

S
sun_fan 已提交
145
static void SendResponse(uv_stream_t *handle, RequestType type, int result, const void *content, int size)
Z
zhong_ning 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
    int ret = 0;
    // 申请整块内存,用于回复数据和写请求
    ResponseNode *response = (ResponseNode *)malloc(sizeof(ResponseNode) + size);
    PARAM_CHECK(response != NULL, return, "Failed to alloc memory for response");
    response->msg.type = type;
    response->msg.contentSize = size;
    response->msg.result = result;
    if (content != NULL && size != 0) {
        ret = memcpy_s(response->msg.content, size, content, size);
        PARAM_CHECK(ret == 0, return, "Failed to copy content");
    }
    uv_buf_t buf = uv_buf_init((char *)&response->msg, sizeof(response->msg) + size);
    ret = uv_write2(&response->writer, handle, &buf, 1, handle, OnWriteResponse);
    PARAM_CHECK(ret >= 0, return, "Failed to uv_write2 ret %s", uv_strerror(ret));
}

S
sun_fan 已提交
163
static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, uv_buf_t *buf)
Z
zhong_ning 已提交
164 165 166
{
    if (nread <= 0 || buf == NULL || buf->base == NULL) {
        uv_close((uv_handle_t*)handle, OnClose);
S
sun_fan 已提交
167 168 169
        if (buf != NULL && buf->base != NULL) {
            free(buf->base);
        }
Z
zhong_ning 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        return;
    }
    int freeHandle = 1;
    RequestMsg *msg = (RequestMsg *)buf->base;
    switch (msg->type) {
        case SET_PARAM: {
            freeHandle = 0;
            int ret = ProcessParamSet(msg);
            SendResponse(handle, SET_PARAM, ret, NULL, 0);
            break;
        }
        default:
            PARAM_LOGE("not supported the command: %d", msg->type);
            break;
    }
    free(buf->base);
S
sun_fan 已提交
186
    buf->base = NULL;
Z
zhong_ning 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    uv_close((uv_handle_t*)handle, OnClose);
}

static void OnConnection(uv_stream_t *server, int status)
{
    PARAM_CHECK(status >= 0, return, "Error status %d", status);
    PARAM_CHECK(server != NULL, return, "Error server");
    uv_pipe_t *stream = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
    PARAM_CHECK(stream != NULL, return, "Failed to alloc stream");

    int ret = uv_pipe_init(uv_default_loop(), (uv_pipe_t*)stream, 1);
    PARAM_CHECK(ret == 0, free(stream); return, "Failed to uv_pipe_init %d", ret);

    stream->data = server;
    ret = uv_accept(server, (uv_stream_t *)stream);
    PARAM_CHECK(ret == 0, uv_close((uv_handle_t*)stream, NULL); free(stream);
        return, "Failed to uv_accept %d", ret);

    ret = uv_read_start((uv_stream_t *)stream, OnReceiveAlloc, OnReceiveRequest);
    PARAM_CHECK(ret == 0, uv_close((uv_handle_t*)stream, NULL); free(stream);
        return, "Failed to uv_read_start %d", ret);
}

void StopParamService()
{
    uv_fs_t req;
    uv_fs_unlink(uv_default_loop(), &req, PIPE_NAME, NULL);
    CloseParamWorkSpace(&g_paramWorkSpace);
    ClosePersistParamWorkSpace();
    uv_stop(uv_default_loop());
    PARAM_LOGI("StopParamService.");
}

int StartParamService()
{
    PARAM_LOGI("StartParamService.");
    uv_fs_t req;
    uv_fs_unlink(uv_default_loop(), &req, PIPE_NAME, NULL);

    uv_pipe_t pipeServer;
    int ret = uv_pipe_init(uv_default_loop(), &pipeServer, 0);
    PARAM_CHECK(ret == 0, return ret, "Failed to uv_pipe_init %d", ret);
    ret = uv_pipe_bind(&pipeServer, PIPE_NAME);
    PARAM_CHECK(ret == 0, return ret, "Failed to uv_pipe_bind %d %s", ret, uv_err_name(ret));
S
sun_fan 已提交
231
    ret = chmod(PIPE_NAME, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
Z
zhong_ning 已提交
232
    PARAM_CHECK(ret == 0, return ret, "Failed to chmod %s, err %d. ", PIPE_NAME, errno);
Z
zhong_ning 已提交
233 234 235 236 237 238 239 240 241 242 243 244
    ret = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, OnConnection);
    PARAM_CHECK(ret == 0, return ret, "Failed to uv_listen %d %s", ret, uv_err_name(ret));
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    PARAM_LOGI("Start service exit.");
    return 0;
}

int SystemWriteParam(const char *name, const char *value)
{
    PARAM_CHECK(name != NULL && value != NULL, return -1, "The name is null");
    PARAM_LOGI("SystemWriteParam name %s value: %s", name, value);
    int ret = WriteParamWithCheck(&g_paramWorkSpace, &g_paramWorkSpace.label, name, value);
S
sun_fan 已提交
245 246 247 248
    PARAM_CHECK(ret == 0, return ret, "Failed to set param %s", name);
    ret = WritePersistParam(name, value);
    PARAM_CHECK(ret == 0, return ret, "Failed to set persist param %s", name);

Z
zhong_ning 已提交
249 250
    // notify event to process trigger
    PostParamTrigger(name, value);
S
sun_fan 已提交
251
    return ret;
Z
zhong_ning 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

int SystemReadParam(const char *name, char *value, unsigned int *len)
{
    PARAM_CHECK(name != NULL && len != NULL, return -1, "The name is null");
    ParamHandle handle = 0;
    int ret = ReadParamWithCheck(&g_paramWorkSpace, name, &handle);
    if (ret == 0) {
        ret = ReadParamValue(&g_paramWorkSpace, handle, value, len);
    }
    return ret;
}

ParamWorkSpace *GetParamWorkSpace()
{
    return &g_paramWorkSpace;
}

int LoadPersistParams()
{
    return RefreshPersistParams(&g_paramWorkSpace, g_initContext);
}

int SystemTraversalParam(void (*traversalParameter)(ParamHandle handle, void* cookie), void* cookie)
{
    PARAM_CHECK(traversalParameter != NULL, return -1, "The param is null");
    return TraversalParam(&g_paramWorkSpace, traversalParameter, cookie);
}