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