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 "service_watcher.h"
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "beget_ext.h"
22 #include "init_utils.h"
23 #include "parameter.h"
24 #include "securec.h"
25 #include "service_control.h"
26 #include "sysparam_errno.h"
27
ServiceStateChange(const char * key,const char * value,void * context)28 static void ServiceStateChange(const char *key, const char *value, void *context)
29 {
30 ServiceStatusChangePtr callback = (ServiceStatusChangePtr)context;
31 uint32_t v = 0;
32 int ret = StringToUint(value, &v);
33 BEGET_ERROR_CHECK(ret == 0, return, "Failed to get value from %s", value);
34
35 // get pid
36 char paramName[PARAM_NAME_LEN_MAX] = { 0 };
37 ret = snprintf_s(paramName, sizeof(paramName), sizeof(paramName) - 1, "%s.pid", key);
38 BEGET_ERROR_CHECK(ret != -1, return, "Failed to get format pid ret %d for %s ", ret, key);
39
40 ServiceInfo info = {0};
41 info.status = (ServiceStatus)v;
42 info.pid = (pid_t)GetUintParameter(paramName, INVALID_PID);
43 if (strlen(key) > strlen(STARTUP_SERVICE_CTL)) {
44 callback(key + strlen(STARTUP_SERVICE_CTL) + 1, &info);
45 } else {
46 BEGET_LOGE("Invalid service name %s %s", key, value);
47 }
48 }
49
ServiceWatchForStatus(const char * serviceName,ServiceStatusChangePtr changeCallback)50 int ServiceWatchForStatus(const char *serviceName, ServiceStatusChangePtr changeCallback)
51 {
52 BEGET_ERROR_CHECK(serviceName != NULL, return EC_INVALID, "Service watch failed, service is null.");
53 BEGET_ERROR_CHECK(changeCallback != NULL, return EC_INVALID, "Service watch failed, callback is null.");
54
55 char paramName[PARAM_NAME_LEN_MAX] = {0};
56 BEGET_LOGI("Watcher service %s status", serviceName);
57 if (snprintf_s(paramName, PARAM_NAME_LEN_MAX, PARAM_NAME_LEN_MAX - 1,
58 "%s.%s", STARTUP_SERVICE_CTL, serviceName) == -1) {
59 BEGET_LOGE("Failed snprintf_s err=%d", errno);
60 return EC_SYSTEM_ERR;
61 }
62 int ret = SystemWatchParameter(paramName, ServiceStateChange, (void *)changeCallback);
63 if (ret != 0) {
64 BEGET_LOGE("Failed to watcher service %s ret %d.", serviceName, ret);
65 if (ret == DAC_RESULT_FORBIDED) {
66 return SYSPARAM_PERMISSION_DENIED;
67 }
68 return EC_SYSTEM_ERR;
69 }
70 return 0;
71 }
72
WatchParameter(const char * keyprefix,ParameterChgPtr callback,void * context)73 int WatchParameter(const char *keyprefix, ParameterChgPtr callback, void *context)
74 {
75 if (keyprefix == NULL) {
76 return EC_INVALID;
77 }
78 #ifdef NO_PARAM_WATCHER
79 printf("ParameterWatcher is disabled.");
80 return EC_INVALID;
81 #else
82 return SystemWatchParameter(keyprefix, callback, context);
83 #endif
84 }