xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/inetd/inetd_impl.h (revision 1573d361e97690e67db291a1e0dc9a9b58f73fd8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _INETD_IMPL_H
27 #define	_INETD_IMPL_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 
32 /*
33  * Header file containing inetd's shared types/data structures and
34  * function declarations.
35  */
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <stdarg.h>
44 #include <rpc/rpc.h>
45 #include <assert.h>
46 #include <libscf.h>
47 #include <libinetutil.h>
48 #include <inetsvc.h>
49 #include <librestart.h>
50 #include <libuutil.h>
51 #include <wordexp.h>
52 
53 
54 /*
55  * Number of consecutive retries of a repository operation that failed due
56  * to a broken connection performed before giving up and failing.
57  */
58 #define	REP_OP_RETRIES 10
59 
60 /* retryable SMF method error */
61 #define	SMF_EXIT_ERR_OTHER 1
62 
63 /* inetd's syslog ident string */
64 #define	SYSLOG_IDENT    "inetd"
65 
66 /* Is this instance currently executing a method ? */
67 #define	INST_IN_TRANSITION(i)	((i)->next_istate != IIS_NONE)
68 
69 /* Names of properties that inetd uses to store instance state. */
70 #define	PR_NAME_NON_START_PID	"non_start_pid"
71 #define	PR_NAME_START_PIDS	"start_pids"
72 #define	PR_NAME_CUR_INT_STATE	"cur_state"
73 #define	PR_NAME_NEXT_INT_STATE	"next_state"
74 
75 /*
76  * Instance states used internal to svc.inetd.
77  * NOTE: The states table in cmd/cmd-inetd/inetd/inetd.c relies on the
78  * ordering of this enumeration, so take care if modifying it.
79  */
80 typedef enum {
81 	IIS_UNINITIALIZED,
82 	IIS_ONLINE,
83 	IIS_IN_ONLINE_METHOD,
84 	IIS_OFFLINE,
85 	IIS_IN_OFFLINE_METHOD,
86 	IIS_DISABLED,
87 	IIS_IN_DISABLE_METHOD,
88 	IIS_IN_REFRESH_METHOD,
89 	IIS_MAINTENANCE,
90 	IIS_OFFLINE_CONRATE,
91 	IIS_OFFLINE_BIND,
92 	IIS_OFFLINE_COPIES,
93 	IIS_DEGRADED,
94 	IIS_NONE
95 } internal_inst_state_t;
96 
97 /*
98  * inetd's instance methods.
99  * NOTE: The methods table in cmd/cmd-inetd/inetd/util.c relies on the
100  * ordering of this enumeration, so take care if modifying it.
101  */
102 typedef enum {
103 	IM_START,
104 	IM_ONLINE,
105 	IM_OFFLINE,
106 	IM_DISABLE,
107 	IM_REFRESH,
108 	NUM_METHODS,
109 	IM_NONE
110 } instance_method_t;
111 
112 /* Collection of information pertaining to a method */
113 typedef struct {
114 	char *exec_path;	/* path passed to exec() */
115 
116 	/*
117 	 * Structure returned from wordexp(3c) that contains an expansion of the
118 	 * exec property into a form suitable for exec(2).
119 	 */
120 	wordexp_t	exec_args_we;
121 
122 	/*
123 	 * Copy of the first argument of the above wordexp_t structure in the
124 	 * event that an alternate arg0 is provided, and we replace the first
125 	 * argument with the alternate arg0. This is necessary so the
126 	 * contents of the wordexp_t structure can be returned to their
127 	 * original form as returned from wordexp(3c), which is a requirement
128 	 * for calling wordfree(3c), wordexp()'s associated cleanup routine.
129 	 */
130 	const char	*wordexp_arg0_backup;
131 
132 	/* time a method can run for before being considered broken */
133 	int		timeout;
134 } method_info_t;
135 
136 typedef struct {
137 	basic_cfg_t	*basic;
138 	method_info_t	*methods[NUM_METHODS];
139 } instance_cfg_t;
140 
141 /*
142  * Structure used to construct a list of int64_t's and their associated
143  * scf values. Used to store lists of process ids, internal states, and to
144  * store the associated scf value used when writing the values back to the
145  * repository.
146  */
147 typedef struct {
148 	int64_t		val;
149 	scf_value_t	*scf_val;
150 	uu_list_node_t	link;
151 } rep_val_t;
152 
153 /* Structure containing the state and configuration of a service instance. */
154 typedef struct {
155 	char			*fmri;
156 
157 	/* fd we're going to take a connection on */
158 	int			conn_fd;
159 
160 	/* number of copies of this instance active */
161 	int64_t			copies;
162 
163 	/* connection rate counters */
164 	int64_t			conn_rate_count;
165 	time_t			conn_rate_start;
166 
167 	/* failure rate counters */
168 	int64_t			fail_rate_count;
169 	time_t			fail_rate_start;
170 	/* bind failure count */
171 	int64_t			bind_fail_count;
172 
173 	/* pids of currently running methods */
174 	uu_list_t		*non_start_pid;
175 	uu_list_t		*start_pids;
176 
177 	/* ctids of currently running start methods */
178 	uu_list_t		*start_ctids;
179 
180 	/* remote address, used for TCP tracing */
181 	struct sockaddr_storage	remote_addr;
182 
183 	internal_inst_state_t	cur_istate;
184 	internal_inst_state_t	next_istate;
185 
186 	/* repository compatible versions of the above 2 states */
187 	uu_list_t		*cur_istate_rep;
188 	uu_list_t		*next_istate_rep;
189 
190 	/*
191 	 * Current instance configuration resulting from its repository
192 	 * configuration.
193 	 */
194 	instance_cfg_t		*config;
195 
196 	/*
197 	 * Soon to be applied instance configuration. This configuration was
198 	 * read during a refresh when this instance was online, and the
199 	 * instance needed taking offline for this configuration to be applied.
200 	 * The instance is currently on its way offline, and this configuration
201 	 * will become the current configuration when it arrives there.
202 	 */
203 	instance_cfg_t		*new_config;
204 
205 	/* current pending conrate-offline/method timer; -1 if none pending */
206 	iu_timer_id_t		timer_id;
207 
208 	/* current pending bind retry timer; -1 if none pending */
209 	iu_timer_id_t		bind_timer_id;
210 
211 	/*
212 	 * Flags that assist in the fanout of an instance arriving in the
213 	 * offline state on-route to some other state.
214 	 */
215 	boolean_t		disable_req;
216 	boolean_t		maintenance_req;
217 	boolean_t		conn_rate_exceeded;
218 	boolean_t		bind_retries_exceeded;
219 
220 	/*
221 	 * Event waiting to be processed. RESTARTER_EVENT_TYPE_INVALID is used
222 	 * to mean no event waiting.
223 	 */
224 	restarter_event_type_t	pending_rst_event;
225 
226 	/* link to next instance in list */
227 	uu_list_node_t		link;
228 } instance_t;
229 
230 
231 /* Structure used to store information pertaining to instance method types. */
232 typedef struct {
233 	instance_method_t	method;
234 	const char		*name;
235 	internal_inst_state_t	dst_state;
236 } method_type_info_t;
237 
238 
239 extern uu_list_t *instance_list;
240 extern struct pollfd *poll_fds;
241 extern nfds_t num_pollfds;
242 extern method_type_info_t methods[];
243 extern iu_tq_t *timer_queue;
244 extern uu_list_pool_t *conn_ind_pool;
245 
246 /*
247  * util.c
248  */
249 extern void msg_init(void);
250 extern void msg_fini(void);
251 /* PRINTFLIKE1 */
252 extern void debug_msg(const char *, ...);
253 /* PRINTFLIKE1 */
254 extern void error_msg(const char *, ...);
255 /* PRINTFLIKE1 */
256 extern void warn_msg(const char *, ...);
257 extern void poll_fini(void);
258 extern boolean_t isset_pollfd(int);
259 extern void clear_pollfd(int);
260 extern int set_pollfd(int, uint16_t);
261 extern struct pollfd *find_pollfd(int);
262 extern int safe_read(int, void *, size_t);
263 extern boolean_t copies_limit_exceeded(instance_t *);
264 extern void cancel_inst_timer(instance_t *);
265 extern void cancel_bind_timer(instance_t *);
266 extern void enable_blocking(int);
267 extern void disable_blocking(int);
268 
269 /*
270  * tlx.c
271  */
272 extern rpc_info_t *create_rpc_info(const char *, const char *, const char *,
273     int, int);
274 extern void destroy_rpc_info(rpc_info_t *);
275 extern boolean_t rpc_info_equal(const rpc_info_t *, const rpc_info_t *);
276 extern int register_rpc_service(const char *, const rpc_info_t *);
277 extern void unregister_rpc_service(const char *, const rpc_info_t *);
278 extern int create_bound_endpoint(const instance_t *, tlx_info_t *);
279 extern void close_net_fd(instance_t *, int);
280 extern int tlx_accept(const char *, tlx_info_t *, struct sockaddr_storage *);
281 extern struct t_call *dequeue_conind(uu_list_t *);
282 extern int queue_conind(uu_list_t *, struct t_call *);
283 extern void tlx_fini(void);
284 extern int tlx_init(void);
285 extern boolean_t tlx_info_equal(const tlx_info_t *, const tlx_info_t *,
286     boolean_t);
287 extern void consume_wait_data(instance_t *, int);
288 
289 /*
290  * config.c
291  */
292 extern int config_init(void);
293 extern void config_fini(void);
294 extern boolean_t socket_info_equal(const socket_info_t *, const socket_info_t *,
295     boolean_t);
296 extern boolean_t method_info_equal(const method_info_t *,
297     const method_info_t *);
298 extern struct method_context *read_method_context(const char *, const char *,
299     const char *, const char **);
300 extern void destroy_instance_cfg(instance_cfg_t *);
301 extern instance_cfg_t *read_instance_cfg(const char *);
302 extern boolean_t bind_config_equal(const basic_cfg_t *, const basic_cfg_t *);
303 extern int read_enable_merged(const char *, boolean_t *);
304 
305 /*
306  * repval.c
307  */
308 extern void repval_fini(void);
309 extern int repval_init(void);
310 extern uu_list_t *create_rep_val_list(void);
311 extern void destroy_rep_val_list(uu_list_t *);
312 extern scf_error_t store_rep_vals(uu_list_t *, const char *, const char *);
313 extern scf_error_t retrieve_rep_vals(uu_list_t *, const char *, const char *);
314 extern rep_val_t *find_rep_val(uu_list_t *, int64_t);
315 extern int set_single_rep_val(uu_list_t *, int64_t);
316 extern int64_t get_single_rep_val(uu_list_t *);
317 extern int add_rep_val(uu_list_t *, int64_t);
318 extern void remove_rep_val(uu_list_t *, int64_t);
319 extern void empty_rep_val_list(uu_list_t *);
320 extern int make_handle_bound(scf_handle_t *);
321 extern int add_remove_contract(instance_t *, boolean_t, ctid_t);
322 extern int iterate_repository_contracts(instance_t *, int);
323 
324 /*
325  * contracts.c
326  */
327 extern int contract_init(void);
328 extern void contract_fini(void);
329 void contract_postfork(void);
330 int contract_prefork(void);
331 extern int get_latest_contract(ctid_t *cid);
332 extern int adopt_contract(ctid_t, const char *);
333 extern int abandon_contract(ctid_t);
334 
335 /*
336  * inetd.c
337  */
338 extern void process_offline_inst(instance_t *);
339 extern void process_non_start_term(instance_t *, int);
340 extern void process_start_term(instance_t *);
341 extern void remove_method_ids(instance_t *, pid_t, ctid_t, instance_method_t);
342 
343 /*
344  * env.c
345  */
346 char **set_smf_env(struct method_context *, instance_t *, const char *);
347 
348 /*
349  * wait.c
350  */
351 extern int register_method(instance_t *, pid_t, ctid_t cid, instance_method_t);
352 extern int method_init(void);
353 extern void method_fini(void);
354 extern void process_terminated_methods(void);
355 extern void unregister_instance_methods(const instance_t *);
356 extern void method_preexec(void);
357 
358 #ifdef __cplusplus
359 }
360 #endif
361 
362 #endif /* _INETD_IMPL_H */
363