1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci
16d9f0492fSopenharmony_ci#ifndef CONTROL_FD_
17d9f0492fSopenharmony_ci#define CONTROL_FD_
18d9f0492fSopenharmony_ci
19d9f0492fSopenharmony_ci#include <stdint.h>
20d9f0492fSopenharmony_ci#include <fcntl.h>
21d9f0492fSopenharmony_ci#include <limits.h>
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_ci#include "list.h"
24d9f0492fSopenharmony_ci#include "loop_event.h"
25d9f0492fSopenharmony_ci
26d9f0492fSopenharmony_ci#ifdef __cplusplus
27d9f0492fSopenharmony_ci#if __cplusplus
28d9f0492fSopenharmony_ciextern "C" {
29d9f0492fSopenharmony_ci#endif
30d9f0492fSopenharmony_ci#endif
31d9f0492fSopenharmony_ci
32d9f0492fSopenharmony_ci#define INIT_CONTROL_FD_SOCKET_PATH "/dev/unix/socket/init_control_fd"
33d9f0492fSopenharmony_ci
34d9f0492fSopenharmony_ci#define CONTROL_FD_FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
35d9f0492fSopenharmony_ci#define PTY_BUF_SIZE 4096
36d9f0492fSopenharmony_ci#define PTY_PATH_SIZE 128
37d9f0492fSopenharmony_ci
38d9f0492fSopenharmony_ci#ifdef STARTUP_INIT_TEST
39d9f0492fSopenharmony_ci#define CONTROL_FD_STATIC
40d9f0492fSopenharmony_ci#else
41d9f0492fSopenharmony_ci#define CONTROL_FD_STATIC static
42d9f0492fSopenharmony_ci#endif
43d9f0492fSopenharmony_ci
44d9f0492fSopenharmony_citypedef struct CmdService_ {
45d9f0492fSopenharmony_ci    TaskHandle serverTask;
46d9f0492fSopenharmony_ci    struct ListNode head;
47d9f0492fSopenharmony_ci} CmdService;
48d9f0492fSopenharmony_ci
49d9f0492fSopenharmony_citypedef struct CmdAgent_ {
50d9f0492fSopenharmony_ci    TaskHandle task;
51d9f0492fSopenharmony_ci    WatcherHandle input; // watch stdin
52d9f0492fSopenharmony_ci    WatcherHandle reader; // watch read pty
53d9f0492fSopenharmony_ci    int ptyFd;
54d9f0492fSopenharmony_ci} CmdAgent;
55d9f0492fSopenharmony_ci
56d9f0492fSopenharmony_citypedef struct CmdTask_ {
57d9f0492fSopenharmony_ci    TaskHandle task;
58d9f0492fSopenharmony_ci    struct ListNode item;
59d9f0492fSopenharmony_ci    int ptyFd;
60d9f0492fSopenharmony_ci    pid_t pid;
61d9f0492fSopenharmony_ci} CmdTask;
62d9f0492fSopenharmony_ci
63d9f0492fSopenharmony_citypedef void (* CallbackControlFdProcess)(uint16_t type, const char *serviceCmd, const void *context);
64d9f0492fSopenharmony_citypedef int (* CallbackSendMsgProcess)(const CmdAgent *agent, uint16_t type, const char *cmd, const char *ptyName);
65d9f0492fSopenharmony_ci
66d9f0492fSopenharmony_citypedef enum {
67d9f0492fSopenharmony_ci    ACTION_SANDBOX = 0,
68d9f0492fSopenharmony_ci    ACTION_DUMP,
69d9f0492fSopenharmony_ci    ACTION_MODULEMGR,
70d9f0492fSopenharmony_ci    ACTION_APP_SPAWNTIME,
71d9f0492fSopenharmony_ci    ACTION_APP_SANDBOX,
72d9f0492fSopenharmony_ci    ACTION_MAX
73d9f0492fSopenharmony_ci} ActionType;
74d9f0492fSopenharmony_ci
75d9f0492fSopenharmony_citypedef struct {
76d9f0492fSopenharmony_ci    uint16_t msgSize;
77d9f0492fSopenharmony_ci    uint16_t type;
78d9f0492fSopenharmony_ci    char ptyName[PTY_PATH_SIZE];
79d9f0492fSopenharmony_ci    char cmd[0];
80d9f0492fSopenharmony_ci} CmdMessage;
81d9f0492fSopenharmony_ci
82d9f0492fSopenharmony_civoid CmdServiceInit(const char *socketPath, CallbackControlFdProcess func, LoopHandle loop);
83d9f0492fSopenharmony_civoid CmdClientInit(const char *socketPath, uint16_t type, const char *cmd, CallbackSendMsgProcess callback);
84d9f0492fSopenharmony_civoid CmdServiceProcessDelClient(pid_t pid);
85d9f0492fSopenharmony_civoid CmdServiceProcessDestroyClient(void);
86d9f0492fSopenharmony_ciint InitPtyInterface(CmdAgent *agent, uint16_t type, const char *cmd, CallbackSendMsgProcess callback);
87d9f0492fSopenharmony_ci
88d9f0492fSopenharmony_ci#ifdef __cplusplus
89d9f0492fSopenharmony_ci#if __cplusplus
90d9f0492fSopenharmony_ci}
91d9f0492fSopenharmony_ci#endif
92d9f0492fSopenharmony_ci#endif
93d9f0492fSopenharmony_ci
94d9f0492fSopenharmony_ci#endif
95