1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 #pragma once 3 4 typedef struct Socket Socket; 5 typedef struct SocketPeer SocketPeer; 6 7 #include "mount.h" 8 #include "socket-util.h" 9 #include "unit.h" 10 11 typedef enum SocketExecCommand { 12 SOCKET_EXEC_START_PRE, 13 SOCKET_EXEC_START_CHOWN, 14 SOCKET_EXEC_START_POST, 15 SOCKET_EXEC_STOP_PRE, 16 SOCKET_EXEC_STOP_POST, 17 _SOCKET_EXEC_COMMAND_MAX, 18 _SOCKET_EXEC_COMMAND_INVALID = -EINVAL, 19 } SocketExecCommand; 20 21 typedef enum SocketType { 22 SOCKET_SOCKET, 23 SOCKET_FIFO, 24 SOCKET_SPECIAL, 25 SOCKET_MQUEUE, 26 SOCKET_USB_FUNCTION, 27 _SOCKET_TYPE_MAX, 28 _SOCKET_TYPE_INVALID = -EINVAL, 29 } SocketType; 30 31 typedef enum SocketResult { 32 SOCKET_SUCCESS, 33 SOCKET_FAILURE_RESOURCES, 34 SOCKET_FAILURE_TIMEOUT, 35 SOCKET_FAILURE_EXIT_CODE, 36 SOCKET_FAILURE_SIGNAL, 37 SOCKET_FAILURE_CORE_DUMP, 38 SOCKET_FAILURE_START_LIMIT_HIT, 39 SOCKET_FAILURE_TRIGGER_LIMIT_HIT, 40 SOCKET_FAILURE_SERVICE_START_LIMIT_HIT, 41 _SOCKET_RESULT_MAX, 42 _SOCKET_RESULT_INVALID = -EINVAL, 43 } SocketResult; 44 45 typedef struct SocketPort { 46 Socket *socket; 47 48 SocketType type; 49 int fd; 50 int *auxiliary_fds; 51 size_t n_auxiliary_fds; 52 53 SocketAddress address; 54 char *path; 55 sd_event_source *event_source; 56 57 LIST_FIELDS(struct SocketPort, port); 58 } SocketPort; 59 60 typedef enum SocketTimestamping { 61 SOCKET_TIMESTAMPING_OFF, 62 SOCKET_TIMESTAMPING_US, /* SO_TIMESTAMP */ 63 SOCKET_TIMESTAMPING_NS, /* SO_TIMESTAMPNS */ 64 _SOCKET_TIMESTAMPING_MAX, 65 _SOCKET_TIMESTAMPING_INVALID = -EINVAL, 66 } SocketTimestamping; 67 68 struct Socket { 69 Unit meta; 70 71 LIST_HEAD(SocketPort, ports); 72 73 Set *peers_by_address; 74 75 unsigned n_accepted; 76 unsigned n_connections; 77 unsigned n_refused; 78 unsigned max_connections; 79 unsigned max_connections_per_source; 80 81 unsigned backlog; 82 unsigned keep_alive_cnt; 83 usec_t timeout_usec; 84 usec_t keep_alive_time; 85 usec_t keep_alive_interval; 86 usec_t defer_accept; 87 88 ExecCommand* exec_command[_SOCKET_EXEC_COMMAND_MAX]; 89 ExecContext exec_context; 90 KillContext kill_context; 91 CGroupContext cgroup_context; 92 93 ExecRuntime *exec_runtime; 94 DynamicCreds dynamic_creds; 95 96 /* For Accept=no sockets refers to the one service we'll 97 * activate. For Accept=yes sockets is either NULL, or filled 98 * to refer to the next service we spawn. */ 99 UnitRef service; 100 101 SocketState state, deserialized_state; 102 103 sd_event_source *timer_event_source; 104 105 ExecCommand* control_command; 106 SocketExecCommand control_command_id; 107 pid_t control_pid; 108 109 mode_t directory_mode; 110 mode_t socket_mode; 111 112 SocketResult result; 113 SocketResult clean_result; 114 115 char **symlinks; 116 117 bool accept; 118 bool remove_on_stop; 119 bool writable; 120 bool flush_pending; 121 122 int socket_protocol; 123 124 /* Socket options */ 125 bool keep_alive; 126 bool no_delay; 127 bool free_bind; 128 bool transparent; 129 bool broadcast; 130 bool pass_cred; 131 bool pass_sec; 132 bool pass_pktinfo; 133 SocketTimestamping timestamping; 134 135 /* Only for INET6 sockets: issue IPV6_V6ONLY sockopt */ 136 SocketAddressBindIPv6Only bind_ipv6_only; 137 138 int priority; 139 int mark; 140 size_t receive_buffer; 141 size_t send_buffer; 142 int ip_tos; 143 int ip_ttl; 144 size_t pipe_size; 145 char *bind_to_device; 146 char *tcp_congestion; 147 bool reuse_port; 148 long mq_maxmsg; 149 long mq_msgsize; 150 151 char *smack; 152 char *smack_ip_in; 153 char *smack_ip_out; 154 155 bool selinux_context_from_net; 156 157 char *user, *group; 158 159 char *fdname; 160 161 RateLimit trigger_limit; 162 }; 163 164 SocketPeer *socket_peer_ref(SocketPeer *p); 165 SocketPeer *socket_peer_unref(SocketPeer *p); 166 int socket_acquire_peer(Socket *s, int fd, SocketPeer **p); 167 168 DEFINE_TRIVIAL_CLEANUP_FUNC(SocketPeer*, socket_peer_unref); 169 170 /* Called from the service code when collecting fds */ 171 int socket_collect_fds(Socket *s, int **fds); 172 173 /* Called from the service code when a per-connection service ended */ 174 void socket_connection_unref(Socket *s); 175 176 void socket_free_ports(Socket *s); 177 178 int socket_load_service_unit(Socket *s, int cfd, Unit **ret); 179 180 char *socket_fdname(Socket *s); 181 182 extern const UnitVTable socket_vtable; 183 184 const char* socket_exec_command_to_string(SocketExecCommand i) _const_; 185 SocketExecCommand socket_exec_command_from_string(const char *s) _pure_; 186 187 const char* socket_result_to_string(SocketResult i) _const_; 188 SocketResult socket_result_from_string(const char *s) _pure_; 189 190 const char* socket_port_type_to_string(SocketPort *p) _pure_; 191 SocketType socket_port_type_from_string(const char *p) _pure_; 192 193 const char* socket_timestamping_to_string(SocketTimestamping p) _const_; 194 SocketTimestamping socket_timestamping_from_string(const char *p) _pure_; 195 SocketTimestamping socket_timestamping_from_string_harder(const char *p) _pure_; 196 197 DEFINE_CAST(SOCKET, Socket); 198