1/*
2* Copyright (c) 2021 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 BASE_STARTUP_INITLITE_FD_HOLDER_INTERNAL_H
17#define BASE_STARTUP_INITLITE_FD_HOLDER_INTERNAL_H
18#include <stdbool.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <sys/un.h>
23#include <sys/socket.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define INIT_HOLDER_SOCKET_PATH "/dev/unix/socket/fd_holder"
30#define MAX_HOLD_FDS (64)
31#define MAX_FD_HOLDER_BUFFER (MAX_HOLD_FDS * sizeof(int))
32
33#define ENV_FD_HOLD_PREFIX "OHOS_FD_HOLD_"
34
35// actions
36#define ACTION_HOLD "hold"
37#define ACTION_GET "get"
38
39// poll or not
40#define WITHPOLL "poll"
41#define WITHOUTPOLL "withoutpoll"
42
43int BuildControlMessage(struct msghdr *msghdr,  int *fds, int fdCount, bool sendUcred);
44// This function will allocate memory to store FDs
45// Remember to delete when not used anymore.
46int *ReceiveFds(int sock, struct iovec iovec, size_t *outFdCount, bool nonblock, pid_t *requestPid);
47
48#define CMSG_BUFFER_TYPE(size)                                  \
49    union {                                                     \
50        struct cmsghdr cmsghdr;                                 \
51        uint8_t buf[size];                                      \
52        uint8_t align_check[(size) >= CMSG_SPACE(0) &&          \
53        (size) == CMSG_ALIGN(size) ? 1 : -1];                   \
54    }
55
56#ifdef __cplusplus
57}
58#endif
59#endif // BASE_STARTUP_INITLITE_FD_HOLDER_INTERNAL_H
60