169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci
1669570cc8Sopenharmony_ci#ifndef APPSPAWN_UTILS_H
1769570cc8Sopenharmony_ci#define APPSPAWN_UTILS_H
1869570cc8Sopenharmony_ci
1969570cc8Sopenharmony_ci#include <ctype.h>
2069570cc8Sopenharmony_ci#include <errno.h>
2169570cc8Sopenharmony_ci#include <inttypes.h>
2269570cc8Sopenharmony_ci#include <limits.h>
2369570cc8Sopenharmony_ci#include <stdint.h>
2469570cc8Sopenharmony_ci#include <stdio.h>
2569570cc8Sopenharmony_ci#include <stdlib.h>
2669570cc8Sopenharmony_ci#include <string.h>
2769570cc8Sopenharmony_ci#include <time.h>
2869570cc8Sopenharmony_ci#include <unistd.h>
2969570cc8Sopenharmony_ci#include <sys/types.h>
3069570cc8Sopenharmony_ci
3169570cc8Sopenharmony_ci#include "hilog/log.h"
3269570cc8Sopenharmony_ci
3369570cc8Sopenharmony_ci#ifdef __cplusplus
3469570cc8Sopenharmony_ciextern "C" {
3569570cc8Sopenharmony_ci#endif  // __cplusplus
3669570cc8Sopenharmony_ci
3769570cc8Sopenharmony_ci#ifndef APPSPAWN_TEST
3869570cc8Sopenharmony_ci#define APPSPAWN_STATIC static
3969570cc8Sopenharmony_ci#else
4069570cc8Sopenharmony_ci#define APPSPAWN_STATIC
4169570cc8Sopenharmony_ci#endif
4269570cc8Sopenharmony_ci
4369570cc8Sopenharmony_ci#ifndef APPSPAWN_BASE_DIR
4469570cc8Sopenharmony_ci#define APPSPAWN_BASE_DIR ""
4569570cc8Sopenharmony_ci#endif
4669570cc8Sopenharmony_ci#if defined(__MUSL__)
4769570cc8Sopenharmony_ci#define APPSPAWN_SOCKET_DIR APPSPAWN_BASE_DIR "/dev/unix/socket/"
4869570cc8Sopenharmony_ci#define APPSPAWN_MSG_DIR APPSPAWN_BASE_DIR "/mnt/startup/"
4969570cc8Sopenharmony_ci#else
5069570cc8Sopenharmony_ci#define APPSPAWN_SOCKET_DIR APPSPAWN_BASE_DIR "/dev/socket/"
5169570cc8Sopenharmony_ci#define APPSPAWN_MSG_DIR APPSPAWN_BASE_DIR "/mnt/startup/"
5269570cc8Sopenharmony_ci#endif
5369570cc8Sopenharmony_ci
5469570cc8Sopenharmony_ci#define DEVICE_VIRTUAL_NET_IO_FLAGS "/sys/devices/virtual/net/lo/flags"
5569570cc8Sopenharmony_ci#define IFF_LOOPBACK_VALUE "9\n"
5669570cc8Sopenharmony_ci#define IFF_LOOPBACK_SIZE 2
5769570cc8Sopenharmony_ci
5869570cc8Sopenharmony_ci#define APPSPAWN_CHECK_EXIT "AppSpawnCheckUnexpectedExitCall"
5969570cc8Sopenharmony_ci#define UNUSED(x) (void)(x)
6069570cc8Sopenharmony_ci
6169570cc8Sopenharmony_ci#define APP_COLD_START 0x01
6269570cc8Sopenharmony_ci#define APP_ASAN_DETECTOR 0x02
6369570cc8Sopenharmony_ci#define APP_DEVELOPER_MODE 0x04
6469570cc8Sopenharmony_ci#define APP_JITFORT_MODE 0x08
6569570cc8Sopenharmony_ci#define APP_BEGETCTL_BOOT 0x400
6669570cc8Sopenharmony_ci
6769570cc8Sopenharmony_ci#define MAX_LEN_SHORT_NAME 16
6869570cc8Sopenharmony_ci#define DEFAULT_UMASK 0002
6969570cc8Sopenharmony_ci#define UID_BASE 200000 // 20010029
7069570cc8Sopenharmony_ci#define DEFAULT_DIR_MODE 0711
7169570cc8Sopenharmony_ci#define USER_ID_BUFFER_SIZE 32
7269570cc8Sopenharmony_ci
7369570cc8Sopenharmony_ci#define APPSPAWN_SEC_TO_NSEC 1000000000
7469570cc8Sopenharmony_ci#define APPSPAWN_MSEC_TO_NSEC 1000000
7569570cc8Sopenharmony_ci#define APPSPAWN_USEC_TO_NSEC 1000
7669570cc8Sopenharmony_ci#define APPSPAWN_SEC_TO_MSEC 1000
7769570cc8Sopenharmony_ci
7869570cc8Sopenharmony_ci#define CHECK_FLAGS_BY_INDEX(flags, index) ((((flags) >> (index)) & 0x1) == 0x1)
7969570cc8Sopenharmony_ci#ifndef ARRAY_LENGTH
8069570cc8Sopenharmony_ci#define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
8169570cc8Sopenharmony_ci#endif
8269570cc8Sopenharmony_ci
8369570cc8Sopenharmony_ci#define INVALID_PERMISSION_INDEX (-1)
8469570cc8Sopenharmony_ci
8569570cc8Sopenharmony_citypedef struct {
8669570cc8Sopenharmony_ci    int flag;
8769570cc8Sopenharmony_ci    const char *ldPreload;
8869570cc8Sopenharmony_ci    const char *asanOptions;
8969570cc8Sopenharmony_ci    const char *tsanOptions;
9069570cc8Sopenharmony_ci    const char *ubsanOptions;
9169570cc8Sopenharmony_ci    const char *hwasanOptions;
9269570cc8Sopenharmony_ci} EnvConfig;
9369570cc8Sopenharmony_ci
9469570cc8Sopenharmony_ci#define MAX_ENV_VALUE_LEN 1024
9569570cc8Sopenharmony_ci
9669570cc8Sopenharmony_citypedef struct TagAppSpawnCommonEnv {
9769570cc8Sopenharmony_ci    const char *envName;
9869570cc8Sopenharmony_ci    const char *envValue;
9969570cc8Sopenharmony_ci    int developerModeEnable;
10069570cc8Sopenharmony_ci} AppSpawnCommonEnv;
10169570cc8Sopenharmony_ci
10269570cc8Sopenharmony_citypedef enum {
10369570cc8Sopenharmony_ci    APPSPAWN_OK = 0,
10469570cc8Sopenharmony_ci    APPSPAWN_SYSTEM_ERROR = 0xD000000,
10569570cc8Sopenharmony_ci    APPSPAWN_ARG_INVALID,
10669570cc8Sopenharmony_ci    APPSPAWN_MSG_INVALID,
10769570cc8Sopenharmony_ci    APPSPAWN_MSG_TOO_LONG,
10869570cc8Sopenharmony_ci    APPSPAWN_TLV_NOT_SUPPORT,
10969570cc8Sopenharmony_ci    APPSPAWN_TLV_NONE,
11069570cc8Sopenharmony_ci    APPSPAWN_SANDBOX_NONE,
11169570cc8Sopenharmony_ci    APPSPAWN_SANDBOX_LOAD_FAIL,
11269570cc8Sopenharmony_ci    APPSPAWN_SANDBOX_INVALID,
11369570cc8Sopenharmony_ci    APPSPAWN_SANDBOX_MOUNT_FAIL,  // 0xD00000a
11469570cc8Sopenharmony_ci    APPSPAWN_SPAWN_TIMEOUT,       // 0xD00000a
11569570cc8Sopenharmony_ci    APPSPAWN_CHILD_CRASH,         // 0xD00000b
11669570cc8Sopenharmony_ci    APPSPAWN_NATIVE_NOT_SUPPORT,
11769570cc8Sopenharmony_ci    APPSPAWN_ACCESS_TOKEN_INVALID,
11869570cc8Sopenharmony_ci    APPSPAWN_PERMISSION_NOT_SUPPORT,
11969570cc8Sopenharmony_ci    APPSPAWN_BUFFER_NOT_ENOUGH,
12069570cc8Sopenharmony_ci    APPSPAWN_TIMEOUT,
12169570cc8Sopenharmony_ci    APPSPAWN_FORK_FAIL,
12269570cc8Sopenharmony_ci    APPSPAWN_DEBUG_MODE_NOT_SUPPORT,
12369570cc8Sopenharmony_ci    APPSPAWN_ERROR_UTILS_MEM_FAIL,
12469570cc8Sopenharmony_ci    APPSPAWN_ERROR_FILE_RMDIR_FAIL,
12569570cc8Sopenharmony_ci    APPSPAWN_NODE_EXIST,
12669570cc8Sopenharmony_ci} AppSpawnErrorCode;
12769570cc8Sopenharmony_ci
12869570cc8Sopenharmony_ciuint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime);
12969570cc8Sopenharmony_civoid AppSpawnDump(const char *fmt, ...);
13069570cc8Sopenharmony_civoid SetDumpToStream(FILE *stream);
13169570cc8Sopenharmony_citypedef int (*SplitStringHandle)(const char *str, void *context);
13269570cc8Sopenharmony_ciint32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle);
13369570cc8Sopenharmony_cichar *GetLastStr(const char *str, const char *dst);
13469570cc8Sopenharmony_ciuint32_t GetSpawnTimeout(uint32_t def);
13569570cc8Sopenharmony_civoid DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath);
13669570cc8Sopenharmony_ciint IsDeveloperModeOpen();
13769570cc8Sopenharmony_civoid InitCommonEnv(void);
13869570cc8Sopenharmony_ciint ConvertEnvValue(const char *srcEnv, char *dstEnv, int len);
13969570cc8Sopenharmony_ci
14069570cc8Sopenharmony_ciint EnableNewNetNamespace(void);
14169570cc8Sopenharmony_civoid EnableCache(void);
14269570cc8Sopenharmony_ci
14369570cc8Sopenharmony_ci#ifndef APP_FILE_NAME
14469570cc8Sopenharmony_ci#define APP_FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
14569570cc8Sopenharmony_ci#endif
14669570cc8Sopenharmony_ci
14769570cc8Sopenharmony_ci#ifndef OHOS_LITE
14869570cc8Sopenharmony_ci#define APPSPAWN_DOMAIN (0xD002C00 + 0x11)
14969570cc8Sopenharmony_ci#ifndef APPSPAWN_LABEL
15069570cc8Sopenharmony_ci#define APPSPAWN_LABEL "APPSPAWN"
15169570cc8Sopenharmony_ci#endif
15269570cc8Sopenharmony_ci
15369570cc8Sopenharmony_ci#undef LOG_TAG
15469570cc8Sopenharmony_ci#define LOG_TAG APPSPAWN_LABEL
15569570cc8Sopenharmony_ci#undef LOG_DOMAIN
15669570cc8Sopenharmony_ci#define LOG_DOMAIN APPSPAWN_DOMAIN
15769570cc8Sopenharmony_ci
15869570cc8Sopenharmony_ci#define APPSPAWN_LOGI(fmt, ...) \
15969570cc8Sopenharmony_ci    HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
16069570cc8Sopenharmony_ci#define APPSPAWN_LOGE(fmt, ...) \
16169570cc8Sopenharmony_ci    HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
16269570cc8Sopenharmony_ci#define APPSPAWN_LOGV(fmt, ...) \
16369570cc8Sopenharmony_ci    HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
16469570cc8Sopenharmony_ci#define APPSPAWN_LOGW(fmt, ...) \
16569570cc8Sopenharmony_ci    HILOG_WARN(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
16669570cc8Sopenharmony_ci#define APPSPAWN_LOGF(fmt, ...) \
16769570cc8Sopenharmony_ci    HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
16869570cc8Sopenharmony_ci
16969570cc8Sopenharmony_ci#define APPSPAPWN_DUMP(fmt, ...) \
17069570cc8Sopenharmony_ci    do { \
17169570cc8Sopenharmony_ci        HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__); \
17269570cc8Sopenharmony_ci        AppSpawnDump(fmt "\n", ##__VA_ARGS__); \
17369570cc8Sopenharmony_ci    } while (0)
17469570cc8Sopenharmony_ci
17569570cc8Sopenharmony_ci#else
17669570cc8Sopenharmony_ci
17769570cc8Sopenharmony_ci#define APPSPAWN_LOGI(fmt, ...) \
17869570cc8Sopenharmony_ci    HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
17969570cc8Sopenharmony_ci#define APPSPAWN_LOGE(fmt, ...) \
18069570cc8Sopenharmony_ci    HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
18169570cc8Sopenharmony_ci#define APPSPAWN_LOGV(fmt, ...) \
18269570cc8Sopenharmony_ci    HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
18369570cc8Sopenharmony_ci#define APPSPAWN_LOGW(fmt, ...) \
18469570cc8Sopenharmony_ci    HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
18569570cc8Sopenharmony_ci#endif
18669570cc8Sopenharmony_ci
18769570cc8Sopenharmony_ci#define APPSPAWN_CHECK(retCode, exper, fmt, ...) \
18869570cc8Sopenharmony_ci    if (!(retCode)) {                    \
18969570cc8Sopenharmony_ci        APPSPAWN_LOGE(fmt, ##__VA_ARGS__);         \
19069570cc8Sopenharmony_ci        exper;                           \
19169570cc8Sopenharmony_ci    }
19269570cc8Sopenharmony_ci
19369570cc8Sopenharmony_ci#define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \
19469570cc8Sopenharmony_ci    if (!(retCode)) {                  \
19569570cc8Sopenharmony_ci        exper;                 \
19669570cc8Sopenharmony_ci    }                         \
19769570cc8Sopenharmony_ci
19869570cc8Sopenharmony_ci#define APPSPAWN_ONLY_EXPER(retCode, exper) \
19969570cc8Sopenharmony_ci    if ((retCode)) {                  \
20069570cc8Sopenharmony_ci        exper;                 \
20169570cc8Sopenharmony_ci    }
20269570cc8Sopenharmony_ci
20369570cc8Sopenharmony_ci#define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \
20469570cc8Sopenharmony_ci    if (!(retCode)) {                    \
20569570cc8Sopenharmony_ci        APPSPAWN_LOGE(fmt, ##__VA_ARGS__);      \
20669570cc8Sopenharmony_ci    }
20769570cc8Sopenharmony_ci#ifdef __cplusplus
20869570cc8Sopenharmony_ci}
20969570cc8Sopenharmony_ci#endif  // __cplusplus
21069570cc8Sopenharmony_ci
21169570cc8Sopenharmony_ci#endif  // APPSPAWN_UTILS_H
212