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 #ifndef INIT_SERVICE_SOCKET_ 16 #define INIT_SERVICE_SOCKET_ 17 #ifndef __LITEOS_A__ 18 #include <linux/netlink.h> 19 #endif 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <sys/types.h> 23 #include <sys/un.h> 24 #include <unistd.h> 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 #define MAX_SOCK_NAME_LEN 16 32 #define SOCK_OPT_NUMS 6 33 34 #define IsConnectionBasedSocket(sockopt) \ 35 ((sockopt)->type == SOCK_STREAM || (sockopt)->type == SOCK_SEQPACKET) 36 37 enum SockOptionTab { 38 SERVICE_SOCK_NAME = 0, 39 SERVICE_SOCK_TYPE, 40 SERVICE_SOCK_PERM, 41 SERVICE_SOCK_UID, 42 SERVICE_SOCK_GID, 43 SERVICE_SOCK_SETOPT 44 }; 45 typedef void *ServiceWatcher; 46 typedef struct Service Service; 47 // socket option 48 #define SOCKET_OPTION_INVALID 0x001 // option invalid 49 #define SOCKET_OPTION_PASSCRED 0x002 // SO_PASSCRED 50 #define SOCKET_OPTION_RCVBUFFORCE 0x004 // SO_RCVBUFFORCE 51 52 typedef union { 53 #ifndef __LITEOS_A__ 54 struct sockaddr_nl addrnl; 55 #endif 56 struct sockaddr_un addrun; 57 } sockaddr_union; 58 59 typedef struct ServiceSocket { 60 struct ServiceSocket *next; 61 struct ServiceSocket *nextNode; 62 Service *service; 63 int family; // socket family 64 unsigned int type; // socket type 65 int protocol; // socket protocol 66 mode_t perm; // Setting permissions 67 uid_t uid; // uid 68 gid_t gid; // gid 69 unsigned int option; // setsocketopt 70 int sockFd; 71 ServiceWatcher watcher; 72 char name[0]; // service name 73 } ServiceSocket; 74 75 int SetSocketEnvForService(Service *service); 76 int CreateSocketForService(Service *service); 77 void CloseServiceSocket(Service *service); 78 int AddSocketWatcher(ServiceWatcher *watcherHandle, Service *service, int fd); 79 void RemoveSocketWatcher(ServiceWatcher watcherHandle); 80 81 #ifdef __cplusplus 82 #if __cplusplus 83 } 84 #endif 85 #endif 86 #endif 87