modify: socket service ondemand adapt L1 & L2

Signed-off-by: xionglei6 <xionglei6@huawei.com>
This commit is contained in:
xionglei6 2022-01-24 17:30:54 +08:00
parent 986583f939
commit 0dd79435e4
12 changed files with 175 additions and 129 deletions

View File

@ -51,6 +51,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/init/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/loopevent/include",
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
@ -64,6 +65,7 @@ if (defined(ohos_lite)) {
deps = [
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
"//base/startup/init_lite/initsync:initsync",
"//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/syspara_lite/frameworks/parameter:parameter",
"//build/lite/config/component/cJSON:cjson_shared",
"//third_party/bounds_checking_function:libsec_shared",

View File

@ -141,9 +141,6 @@ int ServiceExec(const Service *service);
void CloseServiceFds(Service *service, bool needFree);
int UpdaterServiceFds(Service *service, int *fds, size_t fdCount);
int ServiceAddWatcher(ServiceWatcher *watcherHandle, Service *service, int fd);
void ServiceDelWatcher(ServiceWatcher watcherHandle);
#ifdef __cplusplus
#if __cplusplus
}

View File

@ -27,6 +27,10 @@ extern "C" {
#define MAX_SOCK_NAME_LEN 16
#define SOCK_OPT_NUMS 6
#define IsConnectionBasedSocket(sockopt) \
((sockopt)->type == SOCK_STREAM || (sockopt)->type == SOCK_SEQPACKET)
enum SockOptionTab {
SERVICE_SOCK_NAME = 0,
SERVICE_SOCK_TYPE,
@ -51,6 +55,8 @@ typedef struct ServiceSocket_ {
int CreateServiceSocket(struct Service_ *service);
void CloseServiceSocket(struct Service_ *service);
int SocketAddWatcher(ServiceWatcher *watcherHandle, struct Service_ *service, int fd);
void SocketDelWatcher(ServiceWatcher watcherHandle);
#ifdef __cplusplus
#if __cplusplus

View File

@ -381,7 +381,7 @@ static void PollSocketAfresh(Service *service)
INIT_LOGE("Invaid socket %s for service", service->name);
tmpSock = tmpSock->next;
}
ServiceAddWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
SocketAddWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
tmpSock = tmpSock->next;
}
return;

View File

@ -376,9 +376,12 @@ static int ParseServiceSocket(const cJSON *curArrItem, Service *curServ)
cJSON *sockJ = cJSON_GetArrayItem(filedJ, i);
ret = AddServiceSocket(sockJ, curServ);
if (ret != 0) {
break;
return ret;
}
}
if (IsOnDemandService(curServ)) {
ret = CreateServiceSocket(curServ);
}
return ret;
}

View File

@ -25,6 +25,7 @@
#include "init_log.h"
#include "init_service.h"
#include "loop_event.h"
#include "securec.h"
#define HOS_SOCKET_DIR "/dev/unix/socket"
@ -105,6 +106,49 @@ static int SetSocketEnv(int fd, const char *name)
return 0;
}
static void ProcessWatchEvent_(const WatcherHandle watcherHandle, int fd, uint32_t *events, const void *context)
{
*events = 0;
Service *service = (Service *)context;
ServiceSocket *tmpSock = service->socketCfg;;
while (tmpSock != NULL) {
if (tmpSock->sockFd == fd) {
tmpSock->watcher = NULL;
break;
}
tmpSock = tmpSock->next;
}
if (tmpSock == NULL) { // not found socket
INIT_LOGE("Service %s not match socket fd %d!", service->name, fd);
close(fd);
return;
}
INIT_LOGI("Socket information detected, fd:%d service name:%s", fd, service->name);
SocketDelWatcher(watcherHandle);
if (ServiceStart(service) != SERVICE_SUCCESS) {
INIT_LOGE("Service %s start failed!", service->name);
}
}
int SocketAddWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
{
WatcherHandle handle;
LE_WatchInfo info = {};
info.fd = fd;
info.flags = WATCHER_ONCE;
info.events = Event_Read;
info.processEvent = ProcessWatchEvent_;
int ret = LE_StartWatcher(LE_GetDefaultLoop(), &handle, &info, service);
INIT_LOGI("Start to monitor socket, fd:%d service name:%s", fd, service->name);
*watcherHandle = (ServiceWatcher)handle;
return ret;
}
void SocketDelWatcher(ServiceWatcher watcherHandle)
{
LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)watcherHandle);
}
int CreateServiceSocket(Service *service)
{
INIT_CHECK(service != NULL && service->socketCfg != NULL, return 0);
@ -114,7 +158,11 @@ int CreateServiceSocket(Service *service)
int fd = CreateSocket(tmpSock);
INIT_CHECK_RETURN_VALUE(fd >= 0, -1);
if (IsOnDemandService(service)) {
ret = ServiceAddWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
if (IsConnectionBasedSocket(tmpSock)) {
ret = listen(tmpSock->sockFd, MAX_SOCKET_FD_LEN);
INIT_CHECK_RETURN_VALUE(ret == 0, -1);
}
ret = SocketAddWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
INIT_CHECK_RETURN_VALUE(ret == 0, -1);
}
ret = SetSocketEnv(fd, tmpSock->name);
@ -131,7 +179,7 @@ void CloseServiceSocket(Service *service)
ServiceSocket *sockopt = service->socketCfg;
while (sockopt != NULL) {
if (sockopt->watcher != NULL) {
ServiceDelWatcher(sockopt->watcher);
SocketDelWatcher(sockopt->watcher);
}
if (sockopt->sockFd >= 0) {
close(sockopt->sockFd);

View File

@ -15,9 +15,11 @@
#include "init.h"
#include "init_log.h"
#include "init_jobs_internal.h"
#include "init_utils.h"
#ifndef __LINUX__
#include "init_stage.h"
#endif
#include "loop_event.h"
#include "parameter.h"
#include "securec.h"
@ -33,6 +35,7 @@ static void PrintSysInfo(void)
void SystemInit(void)
{
SignalInit();
MakeDirRecursive("/dev/unix/socket", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
void LogInit(void)
@ -77,9 +80,5 @@ void SystemConfig(void)
void SystemRun(void)
{
while (1) {
// pause only returns when a signal was caught and the signal-catching function returned.
// pause only returns -1, no need to process the return value.
(void)pause();
}
LE_RunLoop(LE_GetDefaultLoop());
}

View File

@ -74,13 +74,3 @@ int ServiceExec(const Service *service)
}
return SERVICE_SUCCESS;
}
int ServiceAddWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
{
return 0;
}
void ServiceDelWatcher(ServiceWatcher watcherHandle)
{
return;
}

View File

@ -23,7 +23,6 @@
#include "init_log.h"
#include "init_param.h"
#include "init_utils.h"
#include "loop_event.h"
#include "securec.h"
#define MIN_IMPORTANT_LEVEL (-20)
@ -80,43 +79,3 @@ int ServiceExec(const Service *service)
}
return SERVICE_SUCCESS;
}
static void ProcessWatchEvent_(const WatcherHandle watcherHandle, int fd, uint32_t *events, const void *context)
{
*events = 0;
Service *service = (Service *)context;
ServiceSocket *tmpSock = service->socketCfg;
while (tmpSock != NULL) {
if (tmpSock->sockFd == fd) {
tmpSock->watcher = NULL;
break;
}
tmpSock = tmpSock->next;
}
if (tmpSock == NULL) { // not found socket
INIT_LOGE("Service %s not match socket fd %d!", service->name, fd);
close(fd);
return;
}
if (ServiceStart(service) != SERVICE_SUCCESS) {
INIT_LOGE("Service %s start failed!", service->name);
}
}
int ServiceAddWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
{
WatcherHandle handle;
LE_WatchInfo info = {};
info.fd = fd;
info.flags = WATCHER_ONCE;
info.events = Event_Read;
info.processEvent = ProcessWatchEvent_;
int ret = LE_StartWatcher(LE_GetDefaultLoop(), &handle, &info, service);
*watcherHandle = (ServiceWatcher)handle;
return ret;
}
void ServiceDelWatcher(ServiceWatcher watcherHandle)
{
LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)watcherHandle);
}

View File

@ -10,40 +10,59 @@
# 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.
import("//build/ohos.gni")
ohos_static_library("loopevent") {
sources = [
"loop/le_epoll.c",
"loop/le_loop.c",
"signal/le_signal.c",
"socket/le_socket.c",
"task/le_asynctask.c",
"task/le_streamtask.c",
"task/le_task.c",
"task/le_watchtask.c",
"timer/le_timer.c",
"utils/le_utils.c",
]
common_sources = [
"loop/le_epoll.c",
"loop/le_loop.c",
"signal/le_signal.c",
"socket/le_socket.c",
"task/le_asynctask.c",
"task/le_streamtask.c",
"task/le_task.c",
"task/le_watchtask.c",
"timer/le_timer.c",
"utils/le_utils.c",
]
include_dirs = [
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
"include",
"loop",
"socket",
"task",
"timer",
"utils",
"signal",
]
common_include = [
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
"include",
"loop",
"socket",
"task",
"timer",
"utils",
"signal",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_tools",
"//third_party/bounds_checking_function:libsec_static",
]
part_name = "init"
subsystem_name = "startup"
if (defined(ohos_lite)) {
static_library("loopevent") {
sources = common_sources
include_dirs = common_include
deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_tools",
"//third_party/bounds_checking_function:libsec_static",
]
}
} else {
import("//build/ohos.gni")
ohos_static_library("loopevent") {
sources = common_sources
include_dirs = common_include
deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_tools",
"//third_party/bounds_checking_function:libsec_static",
]
part_name = "init"
subsystem_name = "startup"
}
}

View File

@ -11,42 +11,63 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
if (defined(ohos_lite)) {
static_library("libinit_tools") {
sources = [
"//base/startup/init_lite/services/utils/init_hashmap.c",
"//base/startup/init_lite/services/utils/list.c",
]
ohos_static_library("libinit_tools") {
sources = [
"//base/startup/init_lite/services/utils/init_hashmap.c",
"//base/startup/init_lite/services/utils/list.c",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
}
} else {
import("//build/ohos.gni")
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
part_name = "init"
}
ohos_static_library("libinit_tools") {
sources = [
"//base/startup/init_lite/services/utils/init_hashmap.c",
"//base/startup/init_lite/services/utils/list.c",
]
ohos_static_library("libinit_utils") {
sources = [ "init_utils.c" ]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
part_name = "init"
}
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
defines = [ "_GNU_SOURCE" ]
part_name = "init"
}
ohos_static_library("libinit_utils") {
sources = [ "init_utils.c" ]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
defines = [ "_GNU_SOURCE" ]
part_name = "init"
}
}

View File

@ -31,6 +31,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/init/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/loopevent/include",
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
@ -65,6 +66,7 @@ if (defined(ohos_lite)) {
deps = [
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
"//base/startup/init_lite/initsync:initsync",
"//base/startup/init_lite/services/loopevent:loopevent",
"//base/startup/syspara_lite/frameworks/parameter:parameter",
"//build/lite/config/component/cJSON:cjson_shared",
"//third_party/bounds_checking_function:libsec_shared",