]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/socket.h
81cfc975784cc8f05a10d07a20699fff33f25c9e
[thirdparty/systemd.git] / src / core / socket.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 typedef struct Socket Socket;
24 typedef struct SocketPeer SocketPeer;
25
26 #include "mount.h"
27 #include "service.h"
28 #include "socket-util.h"
29
30 typedef enum SocketExecCommand {
31 SOCKET_EXEC_START_PRE,
32 SOCKET_EXEC_START_CHOWN,
33 SOCKET_EXEC_START_POST,
34 SOCKET_EXEC_STOP_PRE,
35 SOCKET_EXEC_STOP_POST,
36 _SOCKET_EXEC_COMMAND_MAX,
37 _SOCKET_EXEC_COMMAND_INVALID = -1
38 } SocketExecCommand;
39
40 typedef enum SocketType {
41 SOCKET_SOCKET,
42 SOCKET_FIFO,
43 SOCKET_SPECIAL,
44 SOCKET_MQUEUE,
45 SOCKET_USB_FUNCTION,
46 _SOCKET_FIFO_MAX,
47 _SOCKET_FIFO_INVALID = -1
48 } SocketType;
49
50 typedef enum SocketResult {
51 SOCKET_SUCCESS,
52 SOCKET_FAILURE_RESOURCES,
53 SOCKET_FAILURE_TIMEOUT,
54 SOCKET_FAILURE_EXIT_CODE,
55 SOCKET_FAILURE_SIGNAL,
56 SOCKET_FAILURE_CORE_DUMP,
57 SOCKET_FAILURE_START_LIMIT_HIT,
58 SOCKET_FAILURE_TRIGGER_LIMIT_HIT,
59 SOCKET_FAILURE_SERVICE_START_LIMIT_HIT,
60 _SOCKET_RESULT_MAX,
61 _SOCKET_RESULT_INVALID = -1
62 } SocketResult;
63
64 typedef struct SocketPort {
65 Socket *socket;
66
67 SocketType type;
68 int fd;
69 int *auxiliary_fds;
70 int n_auxiliary_fds;
71
72 SocketAddress address;
73 char *path;
74 sd_event_source *event_source;
75
76 LIST_FIELDS(struct SocketPort, port);
77 } SocketPort;
78
79 struct Socket {
80 Unit meta;
81
82 LIST_HEAD(SocketPort, ports);
83
84 Set *peers_by_address;
85
86 unsigned n_accepted;
87 unsigned n_connections;
88 unsigned max_connections;
89 unsigned max_connections_per_source;
90
91 unsigned backlog;
92 unsigned keep_alive_cnt;
93 usec_t timeout_usec;
94 usec_t keep_alive_time;
95 usec_t keep_alive_interval;
96 usec_t defer_accept;
97
98 ExecCommand* exec_command[_SOCKET_EXEC_COMMAND_MAX];
99 ExecContext exec_context;
100 KillContext kill_context;
101 CGroupContext cgroup_context;
102
103 ExecRuntime *exec_runtime;
104 DynamicCreds dynamic_creds;
105
106 /* For Accept=no sockets refers to the one service we'll
107 activate. For Accept=yes sockets is either NULL, or filled
108 when the next service we spawn. */
109 UnitRef service;
110
111 SocketState state, deserialized_state;
112
113 sd_event_source *timer_event_source;
114
115 ExecCommand* control_command;
116 SocketExecCommand control_command_id;
117 pid_t control_pid;
118
119 mode_t directory_mode;
120 mode_t socket_mode;
121
122 SocketResult result;
123
124 char **symlinks;
125
126 bool accept;
127 bool remove_on_stop;
128 bool writable;
129
130 int socket_protocol;
131
132 /* Socket options */
133 bool keep_alive;
134 bool no_delay;
135 bool free_bind;
136 bool transparent;
137 bool broadcast;
138 bool pass_cred;
139 bool pass_sec;
140
141 /* Only for INET6 sockets: issue IPV6_V6ONLY sockopt */
142 SocketAddressBindIPv6Only bind_ipv6_only;
143
144 int priority;
145 int mark;
146 size_t receive_buffer;
147 size_t send_buffer;
148 int ip_tos;
149 int ip_ttl;
150 size_t pipe_size;
151 char *bind_to_device;
152 char *tcp_congestion;
153 bool reuse_port;
154 long mq_maxmsg;
155 long mq_msgsize;
156
157 char *smack;
158 char *smack_ip_in;
159 char *smack_ip_out;
160
161 bool selinux_context_from_net;
162
163 char *user, *group;
164
165 bool reset_accounting:1;
166
167 char *fdname;
168
169 RateLimit trigger_limit;
170 };
171
172 SocketPeer *socket_peer_ref(SocketPeer *p);
173 SocketPeer *socket_peer_unref(SocketPeer *p);
174 int socket_acquire_peer(Socket *s, int fd, SocketPeer **p);
175
176 DEFINE_TRIVIAL_CLEANUP_FUNC(SocketPeer*, socket_peer_unref);
177
178 /* Called from the service code when collecting fds */
179 int socket_collect_fds(Socket *s, int **fds);
180
181 /* Called from the service code when a per-connection service ended */
182 void socket_connection_unref(Socket *s);
183
184 void socket_free_ports(Socket *s);
185
186 int socket_instantiate_service(Socket *s);
187
188 char *socket_fdname(Socket *s);
189
190 extern const UnitVTable socket_vtable;
191
192 const char* socket_exec_command_to_string(SocketExecCommand i) _const_;
193 SocketExecCommand socket_exec_command_from_string(const char *s) _pure_;
194
195 const char* socket_result_to_string(SocketResult i) _const_;
196 SocketResult socket_result_from_string(const char *s) _pure_;
197
198 const char* socket_port_type_to_string(SocketPort *p) _pure_;