diff --git a/lite/appspawn_server.h b/lite/appspawn_server.h new file mode 100644 index 0000000000000000000000000000000000000000..c344470d65b25e60ca855701a046b557fd8f8237 --- /dev/null +++ b/lite/appspawn_server.h @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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. + */ + +#ifndef APPSPAWN_SERVER_H +#define APPSPAWN_SERVER_H +#include "beget_ext.h" + +#include +#include +#include +#include +#ifdef OHOS_DEBUG +#include +#endif // OHOS_DEBUG + +#ifdef __cplusplus +extern "C" { +#endif + +#define UNUSED(x) (void)(x) +#define APP_COLD_START 0x01 +#define ERR_PIPE_FAIL (-100) +#define MAX_LEN_SHORT_NAME 16 +#define WAIT_DELAY_US (100 * 1000) // 100ms +#define GID_USER_DATA_RW 1008 + +typedef struct AppSpawnClient_ { + uint32_t id; + int32_t flags; +} AppSpawnClient; + +#define MAX_SOCKEYT_NAME_LEN 128 +typedef struct AppSpawnContent_ { + char *longProcName; + uint32_t longProcNameLen; + + // system + void (*loadExtendLib)(struct AppSpawnContent_ *content); + void (*initAppSpawn)(struct AppSpawnContent_ *content); + void (*runAppSpawn)(struct AppSpawnContent_ *content, int argc, char *const argv[]); + + // for child + void (*clearEnvironment)(struct AppSpawnContent_ *content, AppSpawnClient *client); + void (*setAppAccessToken)(struct AppSpawnContent_ *content, AppSpawnClient *client); + int (*setAppSandbox)(struct AppSpawnContent_ *content, AppSpawnClient *client); + int (*setKeepCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); + int (*setFileDescriptors)(struct AppSpawnContent_ *content, AppSpawnClient *client); + int (*setProcessName)(struct AppSpawnContent_ *content, AppSpawnClient *client, + char *longProcName, uint32_t longProcNameLen); + int (*setUidGid)(struct AppSpawnContent_ *content, AppSpawnClient *client); + int (*setCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); + + int (*notifyResToParent)(struct AppSpawnContent_ *content, AppSpawnClient *client, int result); + void (*runChildProcessor)(struct AppSpawnContent_ *content, AppSpawnClient *client); + + // for cold start + int (*coldStartApp)(struct AppSpawnContent_ *content, AppSpawnClient *client); +} AppSpawnContent; + +AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold); +int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t *childPid); +int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *longProcName, uint32_t longProcNameLen); + +#ifdef OHOS_DEBUG +void GetCurTime(struct timespec* tmCur); +#endif + +typedef enum { + DEBUG = 0, + INFO, + WARN, + ERROR, + FATAL, +} AppspawnLogLevel; + +void AppspawnLogPrint(AppspawnLogLevel logLevel, const char *file, int line, const char *fmt, ...); + +#ifndef FILE_NAME +#define FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) +#endif + +#define UNUSED(x) (void)(x) + +#ifndef APPSPAWN_LABEL +#define APPSPAWN_LABEL "APPSPAWN" +#endif +#define APPSPAWN_DOMAIN (BASE_DOMAIN + 0x11) + +#define APPSPAWN_LOGI(fmt, ...) STARTUP_LOGI(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) +#define APPSPAWN_LOGE(fmt, ...) STARTUP_LOGE(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) +#define APPSPAWN_LOGV(fmt, ...) STARTUP_LOGV(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) +#define APPSPAWN_LOGW(fmt, ...) STARTUP_LOGW(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) + +#define APPSPAWN_CHECK(retCode, exper, ...) \ + if (!(retCode)) { \ + APPSPAWN_LOGE(__VA_ARGS__); \ + exper; \ + } + +#ifdef __cplusplus +} +#endif +#endif // APPSPAWN_SERVER_H diff --git a/lite/appspawn_service.h b/lite/appspawn_service.h new file mode 100644 index 0000000000000000000000000000000000000000..3ef432fd6401b1eae6631ec46725f3fb389fe5c8 --- /dev/null +++ b/lite/appspawn_service.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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. + */ + +#ifndef APPSPAWN_SERVICE_H +#define APPSPAWN_SERVICE_H + +#include "appspawn_msg.h" +#include "appspawn_server.h" +#include "init_hashmap.h" +#include "loop_event.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define APP_HASH_BUTT 32 +#define FLAGS_ON_DEMAND 0x1 +#define FLAGS_MODE_COLD 0x2 +#define FLAGS_SANDBOX_PRIVATE 0x10 +#define FLAGS_SANDBOX_APP 0x20 + +#define START_INDEX 1 +#define FD_INDEX 2 +#define PARAM_INDEX 3 +#define NULL_INDEX 4 +#define PARAM_BUFFER_LEN 128 +typedef struct { + AppSpawnClient client; + TaskHandle stream; + int32_t fd[2]; // 2 fd count + AppParameter property; +} AppSpawnClientExt; + +typedef struct { + HashNode node; + pid_t pid; + char name[0]; +} AppInfo; + +typedef struct { + AppSpawnContent content; + uint32_t flags; + TaskHandle server; + SignalHandle sigHandler; + TimerHandle timer; + HashMapHandle appMap; // save app pid and name +} AppSpawnContentExt; + +void SetContentFunction(AppSpawnContent *content); +void AppSpawnColdRun(AppSpawnContent *content, int argc, char *const argv[]); +int GetAppSpawnClientFromArg(int argc, char *const argv[], AppSpawnClientExt *client); + +#ifdef __cplusplus +} +#endif +#endif // APPSPAWN_SERVICE_H