1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ueventd_parameter.h"
16
17 #include <pthread.h>
18 #include <sys/time.h>
19
20 #define INIT_LOG_TAG "ueventd"
21 #include "init_log.h"
22 #include "list.h"
23 #include "init_param.h"
24 #include "ueventd.h"
25 #include "ueventd_read_cfg.h"
26 #include "securec.h"
27
28 typedef struct {
29 int inited;
30 int shutdown;
31 int empty;
32 pthread_mutex_t lock;
33 pthread_cond_t hasData;
34 pthread_mutex_t parameterLock;
35 struct ListNode parameterList;
36 pthread_t threadId;
37 } DeviceParameterCtrl;
38
39 static DeviceParameterCtrl g_parameterCtrl = {
40 .inited = 1,
41 .shutdown = 0,
42 .empty = 1,
43 .lock = PTHREAD_MUTEX_INITIALIZER,
44 .hasData = PTHREAD_COND_INITIALIZER,
45 .parameterLock = PTHREAD_MUTEX_INITIALIZER,
46 .parameterList = {&g_parameterCtrl.parameterList, &g_parameterCtrl.parameterList},
47 .threadId = 0
48 };
49
GetFristParameter(DeviceParameterCtrl * parameterCtrl)50 static struct DeviceUdevConf *GetFristParameter(DeviceParameterCtrl *parameterCtrl)
51 {
52 struct DeviceUdevConf *conf = NULL;
53 pthread_mutex_lock(&(parameterCtrl->parameterLock));
54 if (!ListEmpty(parameterCtrl->parameterList)) {
55 conf = ListEntry(parameterCtrl->parameterList.next, struct DeviceUdevConf, paramNode);
56 OH_ListRemove(&conf->paramNode);
57 OH_ListInit(&conf->paramNode);
58 }
59 pthread_mutex_unlock(&(parameterCtrl->parameterLock));
60 return conf;
61 }
62
ThreadRun(void * data)63 static void *ThreadRun(void *data)
64 {
65 DeviceParameterCtrl *parameterCtrl = (DeviceParameterCtrl *)data;
66 INIT_LOGV("[uevent] ThreadRun %d %d", parameterCtrl->empty, parameterCtrl->shutdown);
67 char paramName[PARAM_NAME_LEN_MAX] = {0};
68 while (1) {
69 pthread_mutex_lock(&(parameterCtrl->lock));
70 if (parameterCtrl->empty) {
71 struct timespec abstime = {0};
72 struct timeval now = {0};
73 const long timeout = 5000; // wait time 5000ms
74 gettimeofday(&now, NULL);
75 long nsec = now.tv_usec * 1000 + (timeout % 1000) * 1000000; // 1000 unit 1000000 unit nsec
76 abstime.tv_sec = now.tv_sec + nsec / 1000000000 + timeout / 1000; // 1000 unit 1000000000 unit nsec
77 abstime.tv_nsec = nsec % 1000000000; // 1000000000 unit nsec
78 pthread_cond_timedwait(&(parameterCtrl->hasData), &(parameterCtrl->lock), &abstime);
79 }
80 if (parameterCtrl->shutdown) {
81 break;
82 }
83 pthread_mutex_unlock(&(parameterCtrl->lock));
84 struct DeviceUdevConf *config = GetFristParameter(parameterCtrl);
85 if (config == NULL) {
86 parameterCtrl->empty = 1;
87 continue;
88 }
89 parameterCtrl->empty = 0;
90 const char *paramValue = (config->action == ACTION_ADD) ? "added" : "removed";
91 INIT_LOGI("[uevent] SystemSetParameter %s act %s", config->parameter, paramValue);
92 int len = sprintf_s(paramName, sizeof(paramName), "startup.uevent.%s", config->parameter);
93 if ((len <= 0) || (SystemSetParameter(paramName, paramValue) != 0)) {
94 INIT_LOGE("[uevent] SystemSetParameter %s failed", config->parameter);
95 pthread_mutex_lock(&(parameterCtrl->parameterLock));
96 OH_ListAddTail(¶meterCtrl->parameterList, &config->paramNode);
97 pthread_mutex_unlock(&(parameterCtrl->parameterLock));
98 parameterCtrl->empty = 1;
99 }
100 }
101 return NULL;
102 }
103
AddParameter(DeviceParameterCtrl * parameterCtrl,struct DeviceUdevConf * config)104 static void AddParameter(DeviceParameterCtrl *parameterCtrl, struct DeviceUdevConf *config)
105 {
106 pthread_mutex_lock(&(parameterCtrl->parameterLock));
107 if (ListEmpty(config->paramNode)) {
108 OH_ListAddTail(¶meterCtrl->parameterList, &config->paramNode);
109 }
110 pthread_mutex_unlock(&(parameterCtrl->parameterLock));
111 if (parameterCtrl->threadId == 0) {
112 (void)pthread_create(&(parameterCtrl->threadId), NULL, ThreadRun, (void *)parameterCtrl);
113 }
114 pthread_mutex_unlock(&(parameterCtrl->lock));
115 parameterCtrl->empty = 0;
116 pthread_cond_signal(&(parameterCtrl->hasData));
117 pthread_mutex_unlock(&(parameterCtrl->lock));
118 }
119
SetUeventDeviceParameter(const char * devNode,int action)120 int SetUeventDeviceParameter(const char *devNode, int action)
121 {
122 if (devNode == NULL) {
123 return -1;
124 }
125 struct DeviceUdevConf *config = GetDeviceUdevConfByDevNode(devNode);
126 if ((config != NULL) && (config->parameter != NULL)) {
127 INIT_LOGI("[uevent] SetUeventDeviceParameter parameter %s", config->parameter);
128 config->action = action;
129 AddParameter(&g_parameterCtrl, config);
130 }
131 return 0;
132 }
133