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 struct Service_; 47 48 // socket option 49 #define SOCKET_OPTION_INVALID 0x001 // option invalid 50 #define SOCKET_OPTION_PASSCRED 0x002 // SO_PASSCRED 51 #define SOCKET_OPTION_RCVBUFFORCE 0x004 // SO_RCVBUFFORCE 52 53 typedef union { 54 #ifndef __LITEOS_A__ 55 struct sockaddr_nl addrnl; 56 #endif 57 struct sockaddr_un addrun; 58 } sockaddr_union; 59 60 typedef struct ServiceSocket_ { 61 struct ServiceSocket_ *next; 62 int family; // socket family 63 unsigned int type; // socket type 64 int protocol; // socket protocol 65 mode_t perm; // Setting permissions 66 uid_t uid; // uid 67 gid_t gid; // gid 68 unsigned int option; // setsocketopt 69 int sockFd; 70 ServiceWatcher watcher; 71 char name[0]; // service name 72 } ServiceSocket; 73 74 int CreateServiceSocket(struct Service_ *service); 75 void CloseServiceSocket(struct Service_ *service); 76 int AddSocketWatcher(ServiceWatcher *watcherHandle, struct Service_ *service, int fd); 77 void RemoveSocketWatcher(ServiceWatcher watcherHandle); 78 79 #ifdef __cplusplus 80 #if __cplusplus 81 } 82 #endif 83 #endif 84 #endif 85