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