提交 dec0f815 编写于 作者: X xlei1030

appspawn 归一化

Signed-off-by: Nxlei1030 <xionglei6@huawei.com>
上级 779e1987
......@@ -32,7 +32,7 @@
#define DOREBOOT_PARAM "reboot.ut"
#endif
int DoReboot(const char *option)
int DoReboot_(const char *option)
{
char value[MAX_REBOOT_OPTION_SIZE];
int ret = 0;
......@@ -63,3 +63,25 @@ int DoReboot(const char *option)
BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
return 0;
}
int DoReboot(const char *option)
{
// check if param set ok
const int maxCount = 10;
int count = 0;
DoReboot_(option);
while (count < maxCount) {
usleep(100 * 1000); // 100 * 1000 wait 100ms
char result[10] = {0}; // 10 stop len
uint32_t len = sizeof(result);
int ret = SystemGetParameter(STARTUP_DEVICE_CTL, result, &len);
if (ret == 0 && strcmp(result, DEVICE_CMD_STOP) == 0) {
BEGET_LOGE("Success to reboot system");
return 0;
}
count++;
DoReboot_(option);
}
BEGET_LOGE("Failed to reboot system");
return 0;
}
......@@ -59,6 +59,8 @@ void HashMapRemove(HashMapHandle handle, const void *key);
HashNode *HashMapGet(HashMapHandle handle, const void *key);
HashNode *HashMapFind(HashMapHandle handle,
int hashCode, const void *key, HashKeyCompare keyCompare);
void HashMapTraverse(HashMapHandle handle, void (*hashNodeTraverse)(const HashNode *node, const void *context),
const void *context);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -229,7 +229,10 @@ static void PublishHoldFds(Service *service)
fdBuffer[pos - 1] = '\0'; // Remove last ' '
INIT_LOGI("fd buffer: [%s]", fdBuffer);
char envName[MAX_BUFFER_LEN] = {};
(void)snprintf_s(envName, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, ENV_FD_HOLD_PREFIX"%s", service->name);
if (snprintf_s(envName, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, ENV_FD_HOLD_PREFIX"%s", service->name) < 0) {
INIT_LOGE("snprintf_s failed err=%d", errno);
return;
}
if (setenv(envName, fdBuffer, 1) < 0) {
INIT_LOGE("Failed to set env %s", envName);
}
......@@ -256,13 +259,15 @@ static int BindCpuCore(Service *service)
return SERVICE_SUCCESS;
}
static void ClearEnvironment(void)
static void ClearEnvironment(Service *service)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGTERM);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
if (strcmp(service->name, "appspawn") != 0) {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGTERM);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
return;
}
......@@ -294,7 +299,7 @@ int ServiceStart(Service *service)
DoJobNow(service->serviceJobs.jobsName[JOB_ON_START]);
}
ClearEnvironment();
ClearEnvironment(service);
if (!IsOnDemandService(service)) {
INIT_ERROR_CHECK(CreateServiceSocket(service) >= 0, return SERVICE_FAILURE,
......@@ -353,6 +358,7 @@ int ServiceStop(Service *service)
"stop service %s pid %d failed! err %d.", service->name, service->pid, errno);
NotifyServiceChange(service, SERVICE_STOPPING);
INIT_LOGI("stop service %s, pid %d.", service->name, service->pid);
service->pid = -1;
return SERVICE_SUCCESS;
}
......@@ -451,14 +457,14 @@ void ServiceReap(Service *service)
}
if (service->attribute & SERVICE_ATTR_CRITICAL) { // critical
if (CalculateCrashTime(service, service->crashTime, service->crashCount) == false) {
if (!CalculateCrashTime(service, service->crashTime, service->crashCount)) {
INIT_LOGE("Critical service \" %s \" crashed %d times, rebooting system",
service->name, service->crashCount);
ServiceStop(GetServiceByName("appspawn"));
ExecReboot("reboot");
}
} else if (!(service->attribute & SERVICE_ATTR_NEED_RESTART)) {
if (CalculateCrashTime(service, service->crashTime, service->crashCount) == false) {
if (!CalculateCrashTime(service, service->crashTime, service->crashCount)) {
INIT_LOGE("Service name=%s, crash %d times, no more start.", service->name, service->crashCount);
return;
}
......@@ -570,7 +576,7 @@ static void ServiceTimerStartProcess(const TimerHandle handler, void *context)
ServiceStopTimer(service);
int ret = ServiceStart(service);
if (ret != SERVICE_SUCCESS) {
INIT_LOGE("Start service \' %s \' in timer failed");
INIT_LOGE("Start service \' %s \' in timer failed", service->name);
}
}
......
......@@ -814,8 +814,7 @@ int ParseOneService(const cJSON *curItem, Service *service)
INIT_CHECK_RETURN_VALUE(curItem != NULL && service != NULL, SERVICE_FAILURE);
int ret = 0;
#ifdef WITH_SELINUX
ret = GetStringItem(curItem, SECON_STR_IN_CFG, service->secon, MAX_SECON_LEN);
INIT_CHECK_ONLY_ELOG(ret == 0, "GetServiceSecon %s section not found, skip", SECON_STR_IN_CFG);
(void)GetStringItem(curItem, SECON_STR_IN_CFG, service->secon, MAX_SECON_LEN);
#endif // WITH_SELINUX
ret = GetServiceArgs(curItem, "path", MAX_PATH_ARGS_CNT, &service->pathArgs);
INIT_ERROR_CHECK(ret == 0, return SERVICE_FAILURE, "Failed to get path for service %s", service->name);
......@@ -984,8 +983,10 @@ void StopAllServices(int flags, const char **exclude, int size,
int (*filter)(const Service *service, const char **exclude, int size))
{
Service *service = GetServiceByName("appspawn");
if (((SERVICE_ATTR_NEEDWAIT & flags) == SERVICE_ATTR_NEEDWAIT) && service != NULL && service->pid != 0) {
if (service != NULL && service->pid != -1) { // notify appspawn stop
kill(service->pid, SIGTERM);
waitpid(service->pid, 0, 0);
service->pid = -1;
}
InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
......
......@@ -27,7 +27,7 @@ extern "C" {
// 配置
#define LOOP_EVENT_USE_EPOLL 1
#define LOOP_DEFAULT_BUFFER 1024
#define LOOP_DEFAULT_BUFFER (1024 * 5)
#define LOOP_MAX_BUFFER (1024 * 64)
#define LOOP_MAX_CLIENT 1024
......@@ -100,6 +100,7 @@ typedef int (*LE_IncommingConntect)(const LoopHandle loopHandle, const TaskHandl
typedef struct {
LE_BaseInfo baseInfo;
char *server;
int socketId;
LE_DisConntectComplete disConntectComplete;
LE_IncommingConntect incommingConntect;
LE_SendMessageComplete sendMessageComplete;
......
......@@ -33,7 +33,7 @@ static LE_STATUS HandleSignalEvent_(const LoopHandle loop, const TaskHandle task
}
struct signalfd_siginfo fdsi;
ssize_t s = read(GetSocketFd(task), &fdsi, sizeof(fdsi));
LE_CHECK(s == sizeof(fdsi), return LE_FAILURE, "Failed to read sign");
LE_CHECK(s == sizeof(fdsi), return LE_FAILURE, "Failed to read sign %d %d", s, errno);
SignalTask *sigTask = (SignalTask *)task;
if (sigTask->processSignal) {
sigTask->processSignal(&fdsi);
......
......@@ -36,11 +36,15 @@ static int CreatePipeServerSocket_(const char *server, int maxClient)
unlink(server);
struct sockaddr_un serverAddr;
memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
int ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
LE_CHECK(ret == 0, close(listenfd);
return ret, "Failed to memory set. error: %s", strerror(errno));
serverAddr.sun_family = AF_UNIX;
strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), server);
ret = strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), server);
LE_CHECK(ret == 0, close(listenfd);
return ret, "Failed to copy. error: %s", strerror(errno));
uint32_t size = offsetof(struct sockaddr_un, sun_path) + strlen(server);
int ret = bind(listenfd, (struct sockaddr *)&serverAddr, size);
ret = bind(listenfd, (struct sockaddr *)&serverAddr, size);
LE_CHECK(ret >= 0, close(listenfd);
return ret, "Failed to bind socket. error: %s", strerror(errno));
......@@ -91,13 +95,14 @@ static int CreatePipeSocket_(const char *server)
LE_STATUS GetSockaddrFromServer_(const char *server, struct sockaddr_in *addr)
{
memset_s(addr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
int ret = memset_s(addr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
LE_CHECK(ret == 0, return ret, "Failed to memory set. error: %s", strerror(errno));
addr->sin_family = AF_INET;
const char *portStr = strstr(server, ":");
LE_CHECK(portStr != NULL, return LE_FAILURE, "Failed to get addr %s", server);
uint16_t port = atoi(portStr + 1);
addr->sin_port = htons(port);
int ret = inet_pton(AF_INET, server, &addr->sin_addr);
ret = inet_pton(AF_INET, server, &addr->sin_addr);
LE_CHECK(ret >= 0, return LE_FAILURE, "Failed to inet_pton addr %s", server);
return LE_SUCCESS;
}
......
......@@ -164,8 +164,11 @@ LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle,
LE_CHECK(info->incommingConntect != NULL, return LE_INVALID_PARAM,
"Invalid parameters incommingConntect %s", info->server);
int fd = CreateSocket(info->baseInfo.flags, info->server);
LE_CHECK(fd > 0, return LE_FAILURE, "Failed to create socket %s", info->server);
int fd = info->socketId;
if (info->socketId <= 0) {
fd = CreateSocket(info->baseInfo.flags, info->server);
LE_CHECK(fd > 0, return LE_FAILURE, "Failed to create socket %s", info->server);
}
EventLoop *loop = (EventLoop *)loopHandle;
StreamServerTask *task = (StreamServerTask *)CreateTask(loopHandle, fd, &info->baseInfo,
......
......@@ -113,7 +113,6 @@ void HashMapRemove(HashMapHandle handle, const void *key)
while (node != NULL) {
int ret = tab->keyCompare(node, key);
if (ret == 0) {
INIT_LOGV("HashMapRemove tableId %d hashCode %d node %p", tab->tableId, hashCode, node);
if (node == tab->buckets[hashCode]) {
tab->buckets[hashCode] = node->next;
} else {
......@@ -173,4 +172,18 @@ HashNode *HashMapFind(HashMapHandle handle,
"Invalid hashcode %d %d", tab->maxBucket, hashCode);
INIT_LOGV("HashMapGet tableId %d hashCode %d", tab->tableId, hashCode);
return GetHashNodeByKey(tab, tab->buckets[hashCode], key, keyCompare);
}
\ No newline at end of file
}
void HashMapTraverse(HashMapHandle handle, void (*hashNodeTraverse)(const HashNode *node, const void *context), const void *context)
{
INIT_ERROR_CHECK(handle != NULL && hashNodeTraverse != NULL, return, "Invalid param");
HashTab *tab = (HashTab *)handle;
for (int i = 0; i < tab->maxBucket; i++) {
HashNode *node = tab->buckets[i];
while (node != NULL) {
HashNode *next = node->next;
hashNodeTraverse(node, context);
node = next;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册