Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Startup Init Lite
提交
dc86bf8a
S
Startup Init Lite
项目概览
OpenHarmony
/
Startup Init Lite
大约 1 年 前同步成功
通知
3
Star
37
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Startup Init Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
dc86bf8a
编写于
9月 13, 2021
作者:
S
sun_fan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init: fix param ...
Signed-off-by:
N
sun_fan
<
sun_fan1@hoperun.com
>
上级
7c3a6709
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
70 addition
and
64 deletion
+70
-64
services/src/init_cmds.c
services/src/init_cmds.c
+43
-40
services/src/init_service.c
services/src/init_service.c
+27
-24
未找到文件。
services/src/init_cmds.c
浏览文件 @
dc86bf8a
...
...
@@ -122,92 +122,88 @@ inline int GetParamValue(const char *symValue, char *paramValue, unsigned int pa
}
#endif
struct
CmdArgs
*
GetCmd
(
const
char
*
cmdContent
,
const
char
*
delim
,
int
argsCount
)
static
struct
CmdArgs
*
CopyCmd
(
struct
CmdArgs
*
args
,
const
char
*
cmd
,
size_t
allocSize
)
{
if
(
cmd
==
NULL
)
{
return
NULL
;
}
ctx
->
argv
[
ctx
->
argc
]
=
calloc
(
sizeof
(
char
),
allocSize
);
INIT_CHECK
(
ctx
->
argv
[
ctx
->
argc
]
!=
NULL
,
FreeCmd
(
ctx
);
return
NULL
);
INIT_CHECK
(
GetParamValue
(
cmd
,
ctx
->
argv
[
ctx
->
argc
],
allocSize
)
==
0
,
FreeCmd
(
ctx
);
return
NULL
);
ctx
->
argc
+=
1
;
ctx
->
argv
[
ctx
->
argc
]
=
NULL
;
return
ctx
;
}
#define SKIP_SPACES(p) \
do
{
\
while
(
isspace
(
*
(
p
)))
{
\
(
p
)
++
;
\
}
\
}
while
(
0
)
struct
CmdArgs
*
GetCmd
(
const
char
*
cmdContent
,
const
char
*
delim
,
int
argsCount
)
{
INIT_CHECK_RETURN_VALUE
(
cmdContent
!=
NULL
,
NULL
);
struct
CmdArgs
*
ctx
=
(
struct
CmdArgs
*
)
malloc
(
sizeof
(
struct
CmdArgs
));
INIT_CHECK_RETURN_VALUE
(
ctx
!=
NULL
,
NULL
);
if
(
argsCount
>
SPACES_CNT_IN_CMD_MAX
)
{
INIT_LOGW
(
"Too much arguments for command, max number is %d"
,
SPACES_CNT_IN_CMD_MAX
);
argsCount
=
SPACES_CNT_IN_CMD_MAX
;
}
ctx
->
argv
=
(
char
**
)
malloc
(
sizeof
(
char
*
)
*
(
size_t
)
argsCount
+
1
);
INIT_CHECK
(
ctx
->
argv
!=
NULL
,
FreeCmd
(
ctx
);
return
NULL
);
char
tmpCmd
[
MAX_BUFFER
];
char
tmpCmd
[
MAX_BUFFER
]
=
{
0
}
;
size_t
cmdLength
=
strlen
(
cmdContent
);
if
(
cmdLength
>
MAX_BUFFER
-
1
)
{
INIT_LOGE
(
"command line is too larget, should not bigger than %d. ignore...
\n
"
,
MAX_BUFFER
);
FreeCmd
(
ctx
);
return
NULL
;
}
INIT_CHECK
(
strncpy_s
(
tmpCmd
,
MAX_BUFFER
-
1
,
cmdContent
,
cmdLength
)
==
EOK
,
FreeCmd
(
ctx
);
return
NULL
);
tmpCmd
[
strlen
(
cmdContent
)]
=
'\0'
;
char
*
p
=
tmpCmd
;
INIT_CHECK_RETURN_VALUE
(
p
!=
NULL
,
NULL
);
char
*
token
=
NULL
;
size_t
allocSize
=
0
;
// Skip lead whitespaces
while
(
isspace
(
*
p
))
{
p
++
;
}
SKIP_SPACES
(
p
);
ctx
->
argc
=
0
;
token
=
strstr
(
p
,
delim
);
if
(
token
==
NULL
)
{
// No whitespaces
// Make surce there is enough memory to store parameter value
allocSize
=
(
size_t
)(
cmdLength
+
MAX_PARAM_VALUE_LEN
+
1
);
ctx
->
argv
[
ctx
->
argc
]
=
calloc
(
sizeof
(
char
),
allocSize
);
INIT_CHECK
(
ctx
->
argv
[
ctx
->
argc
]
!=
NULL
,
FreeCmd
(
ctx
);
return
NULL
);
INIT_CHECK
(
GetParamValue
(
p
,
ctx
->
argv
[
ctx
->
argc
],
allocSize
)
==
0
,
FreeCmd
(
ctx
);
return
NULL
);
ctx
->
argc
+=
1
;
ctx
->
argv
[
ctx
->
argc
]
=
NULL
;
return
ctx
;
return
CopyCmd
(
ctx
,
p
,
allocSize
);
}
int
index
=
ctx
->
argc
;
while
(
token
!=
NULL
)
{
// Too more arguments, treat rest of data as one argument
if
(
index
==
(
argsCount
-
1
))
{
if
(
ctx
->
argc
==
(
argsCount
-
1
))
{
break
;
}
*
token
=
'\0'
;
// replace it with '\0';
allocSize
=
(
size_t
)((
token
-
p
)
+
MAX_PARAM_VALUE_LEN
+
1
);
ctx
->
argv
[
index
]
=
calloc
(
sizeof
(
char
),
allocSize
);
INIT_CHECK
(
ctx
->
argv
[
index
]
!=
NULL
,
FreeCmd
(
ctx
);
return
NULL
);
INIT_CHECK
(
GetParamValue
(
p
,
ctx
->
argv
[
index
],
allocSize
)
==
0
,
FreeCmd
(
ctx
);
return
NULL
);
ctx
=
CopyCmd
(
ctx
,
p
,
allocSize
);
INIT_CHECK_RETURN_VALUE
(
ctx
!=
NULL
,
return
NULL
);
p
=
token
+
1
;
// skip '\0'
// Skip lead whitespaces
while
(
isspace
(
*
p
))
{
p
++
;
}
index
++
;
SKIP_SPACES
(
p
)
ctx
->
argc
++
;
token
=
strstr
(
p
,
delim
);
}
ctx
->
argc
=
index
;
if
(
p
<
tmpCmd
+
cmdLength
)
{
// no more white space or encounter max argument count
size_t
restSize
=
tmpCmd
+
cmdLength
-
p
;
allocSize
=
restSize
+
MAX_PARAM_VALUE_LEN
+
1
;
ctx
->
argv
[
index
]
=
calloc
(
sizeof
(
char
),
allocSize
);
INIT_CHECK
(
ctx
->
argv
[
index
]
!=
NULL
,
FreeCmd
(
ctx
);
return
NULL
);
INIT_CHECK
(
GetParamValue
(
p
,
ctx
->
argv
[
index
],
allocSize
)
==
0
,
FreeCmd
(
ctx
);
return
NULL
);
ctx
->
argc
=
index
+
1
;
ctx
=
CopyCmd
(
ctx
,
p
,
allocSize
);
INIT_CHECK_RETURN_VALUE
(
ctx
!=
NULL
,
return
NULL
);
}
ctx
->
argv
[
ctx
->
argc
]
=
NULL
;
ctx
->
argv
[
++
ctx
->
argc
]
=
NULL
;
return
ctx
;
}
...
...
@@ -473,6 +469,10 @@ static void DoChown(const char *cmdContent, int maxArg)
uid_t
owner
=
DecodeUid
(
ctx
->
argv
[
0
]);
gid_t
group
=
DecodeUid
(
ctx
->
argv
[
1
]);
if
(
owner
==
(
uid_t
)
-
1
||
group
==
(
uid_t
)
-
1
)
{
INIT_LOGE
(
"Change path owner with invalid user/group"
);
return
;
}
const
int
pathPos
=
2
;
if
(
chown
(
ctx
->
argv
[
pathPos
],
owner
,
group
)
!=
0
)
{
...
...
@@ -542,7 +542,10 @@ static void DoMkDir(const char *cmdContent, int maxArg)
}
}
if
(
rc
<
0
)
{
INIT_LOGE
(
"Run command mkdir failed"
);
INIT_LOGE
(
"Run command mkdir failed, unlink path"
);
if
(
unlink
(
ctx
->
argv
[
0
])
<
0
)
{
INIT_LOGE
(
"Failed unlink %s errno %d "
,
ctx
->
argv
[
0
],
errno
);
}
}
}
while
(
0
);
...
...
services/src/init_service.c
浏览文件 @
dc86bf8a
...
...
@@ -148,6 +148,30 @@ static void OpenConsole(void)
return
;
}
static
void
WriteServicePid
(
Service
*
service
,
pid_t
pid
)
{
char
pidString
[
MAX_PID_STRING_LENGTH
];
INIT_ERROR_CHECK
(
snprintf_s
(
pidString
,
MAX_PID_STRING_LENGTH
,
MAX_PID_STRING_LENGTH
-
1
,
"%d"
,
pid
)
>=
0
,
_exit
(
0x7f
),
"Build pid string failed"
);
for
(
int
i
=
0
;
i
<
MAX_WRITEPID_FILES
;
i
++
)
{
if
(
service
->
writepidFiles
[
i
]
==
NULL
)
{
break
;
}
char
*
realPath
=
realpath
(
service
->
writepidFiles
[
i
],
NULL
);
if
(
realPath
==
NULL
)
{
continue
;
}
free
(
realPath
);
realPath
=
NULL
;
FILE
*
fd
=
fopen
(
realPath
,
"wb"
);
INIT_ERROR_CHECK
(
fd
!=
NULL
,
continue
,
"Open file %s failed, err = %d"
,
service
->
writepidFiles
[
i
],
errno
);
INIT_CHECK_ONLY_ELOG
(
fwrite
(
pidString
,
1
,
strlen
(
pidString
),
fd
)
==
strlen
(
pidString
),
"write pid %s to file %s failed, err = %d"
,
pidString
,
service
->
writepidFiles
[
i
],
errno
);
fclose
(
fd
);
}
}
int
ServiceStart
(
Service
*
service
)
{
INIT_ERROR_CHECK
(
service
!=
NULL
,
return
SERVICE_FAILURE
,
"start service failed! null ptr."
);
...
...
@@ -161,38 +185,17 @@ int ServiceStart(Service *service)
service
->
attribute
&=
(
~
(
SERVICE_ATTR_NEED_RESTART
|
SERVICE_ATTR_NEED_STOP
));
INIT_ERROR_CHECK
(
stat
(
service
->
pathArgs
[
0
],
&
pathStat
)
==
0
,
service
->
attribute
|=
SERVICE_ATTR_INVALID
;
return
SERVICE_FAILURE
,
"start service %s invalid, please check %s."
,
service
->
name
,
service
->
pathArgs
[
0
]);
in
t
pid
=
fork
();
pid_
t
pid
=
fork
();
if
(
pid
==
0
)
{
if
(
service
->
socketCfg
!=
NULL
)
{
// start socket service
INIT_LOGI
(
"Create socket "
);
INIT_ERROR_CHECK
(
DoCreateSocket
(
service
->
socketCfg
)
>=
0
,
_exit
(
0x7f
),
"DoCreateSocket failed. "
);
INIT_ERROR_CHECK
(
DoCreateSocket
(
service
->
socketCfg
)
>=
0
,
_exit
(
0x7f
),
"Create Socket failed. "
);
}
if
(
service
->
attribute
&
SERVICE_ATTR_CONSOLE
)
{
OpenConsole
();
}
INIT_ERROR_CHECK
(
SetPerms
(
service
)
==
SERVICE_SUCCESS
,
_exit
(
0x7f
),
"service %s exit! set perms failed! err %d."
,
service
->
name
,
errno
);
char
pidString
[
MAX_PID_STRING_LENGTH
];
pid_t
childPid
=
getpid
();
INIT_ERROR_CHECK
(
snprintf_s
(
pidString
,
MAX_PID_STRING_LENGTH
,
MAX_PID_STRING_LENGTH
-
1
,
"%d"
,
childPid
)
>=
0
,
_exit
(
0x7f
),
"start service writepid sprintf failed."
);
for
(
int
i
=
0
;
i
<
MAX_WRITEPID_FILES
;
i
++
)
{
if
(
service
->
writepidFiles
[
i
]
==
NULL
)
{
continue
;
}
char
*
realPath
=
realpath
(
service
->
writepidFiles
[
i
],
NULL
);
if
(
realPath
==
NULL
)
{
continue
;
}
FILE
*
fd
=
fopen
(
realPath
,
"wb"
);
free
(
realPath
);
realPath
=
NULL
;
INIT_ERROR_CHECK
(
fd
!=
NULL
,
continue
,
"writepidFiles %s invalid."
,
service
->
writepidFiles
[
i
]);
INIT_CHECK_ONLY_ELOG
(
fwrite
(
pidString
,
1
,
strlen
(
pidString
),
fd
)
==
strlen
(
pidString
),
"writepid error.file:%s pid:%s"
,
service
->
writepidFiles
[
i
],
pidString
);
fclose
(
fd
);
INIT_LOGI
(
"writepid filename=%s, childPid=%s, ok"
,
service
->
writepidFiles
[
i
],
pidString
);
}
WriteServicePid
(
service
,
getpid
());
INIT_LOGI
(
"service->name is %s "
,
service
->
name
);
#ifndef OHOS_LITE
if
(
service
->
importance
!=
0
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录