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