xref: /titanic_41/usr/src/cmd/svc/svccfg/svccfg.h (revision 0f1702c5201310f0529cd5abb77652e5e9b241b6)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_CMD_SVCCFG_H
28 #define	_CMD_SVCCFG_H
29 
30 
31 #include <sys/types.h>
32 
33 #include <libxml/tree.h>
34 
35 #include <libscf.h>
36 #include <libtecla.h>
37 #include <libuutil.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /* Command scope bits for command tab completion */
44 #define	CS_SCOPE	0x01
45 #define	CS_SVC		0x02
46 #define	CS_INST		0x04
47 #define	CS_SNAP		0x08
48 #define	CS_GLOBAL	0x0f
49 
50 /* Flags for lscf_bundle_import() & co. */
51 #define	SCI_NOREFRESH	0x01		/* Don't refresh instances */
52 #define	SCI_GENERALLAST 0x04		/* Add general property group last */
53 #define	SCI_NOENABLED	0x08		/* Don't import general/enabled. */
54 #define	SCI_FRESH	0x10		/* Freshly imported service */
55 #define	SCI_FORCE	0x20		/* Override-import. */
56 #define	SCI_KEEP	0x40		/* Don't delete when SCI_FORCEing */
57 #define	SCI_NOSNAP	0x80		/* Don't take last-import snapshot */
58 #define	SCI_DELAYENABLE	0x100		/* Delay the general/enable property */
59 
60 /* Flags for lscf_service_export() */
61 #define	SCE_ALL_VALUES	0x01		/* Include all property values */
62 
63 #ifdef lint
64 extern int yyerror(const char *);
65 extern int yyparse(void);
66 #endif /* lint */
67 
68 extern int lex_lineno;
69 
70 #define	MANIFEST_DTD_PATH	"/usr/share/lib/xml/dtd/service_bundle.dtd.1"
71 /*
72  * The following list must be kept in the same order as that of
73  * lxml_prop_types[]
74  */
75 typedef enum element {
76 	SC_ASTRING = 0, SC_BOOLEAN, SC_CARDINALITY, SC_CHOICES,
77 	SC_COMMON_NAME, SC_CONSTRAINTS, SC_COUNT,
78 	SC_INSTANCE_CREATE_DEFAULT, SC_DEPENDENCY, SC_DEPENDENT,
79 	SC_DESCRIPTION, SC_DOC_LINK, SC_DOCUMENTATION, SC_ENABLED,
80 	SC_EXEC_METHOD, SC_FMRI, SC_HOST, SC_HOSTNAME, SC_INCLUDE_VALUES,
81 	SC_INSTANCE, SC_INTEGER, SC_INTERNAL_SEPARATORS, SC_LOCTEXT, SC_MANPAGE,
82 	SC_METHOD_CONTEXT, SC_METHOD_CREDENTIAL,
83 	SC_METHOD_PROFILE, SC_METHOD_ENVIRONMENT, SC_METHOD_ENVVAR,
84 	SC_NET_ADDR_V4, SC_NET_ADDR_V6, SC_OPAQUE, SC_PG_PATTERN,
85 	SC_PROP_PATTERN, SC_PROPERTY, SC_PROPERTY_GROUP, SC_PROPVAL, SC_RANGE,
86 	SC_RESTARTER, SC_SERVICE, SC_SERVICE_BUNDLE, SC_SERVICE_FMRI,
87 	SC_INSTANCE_SINGLE, SC_STABILITY, SC_TEMPLATE, SC_TIME, SC_UNITS,
88 	SC_URI, SC_USTRING, SC_VALUE, SC_VALUE_NODE, SC_VALUES,
89 	SC_VISIBILITY, SC_XI_FALLBACK, SC_XI_INCLUDE
90 } element_t;
91 
92 typedef enum bundle_type {
93 	SVCCFG_UNKNOWN_BUNDLE, SVCCFG_MANIFEST, SVCCFG_PROFILE, SVCCFG_ARCHIVE
94 } bundle_type_t;
95 
96 typedef struct bundle {
97 	uu_list_t	*sc_bundle_services;
98 
99 	xmlChar		*sc_bundle_name;
100 	bundle_type_t	sc_bundle_type;
101 } bundle_t;
102 
103 typedef enum service_type {
104 	SVCCFG_UNKNOWN_SERVICE = 0x0, SVCCFG_SERVICE, SVCCFG_RESTARTER,
105 	SVCCFG_MILESTONE
106 } service_type_t;
107 
108 typedef enum entity_type {
109 	SVCCFG_SERVICE_OBJECT = 0x0, SVCCFG_INSTANCE_OBJECT
110 } entity_type_t;
111 
112 enum import_state {
113 	IMPORT_NONE = 0,
114 	IMPORT_PREVIOUS,
115 	IMPORT_PROP_BEGUN,
116 	IMPORT_PROP_DONE,
117 	IMPORT_COMPLETE,
118 	IMPORT_REFRESHED
119 };
120 
121 typedef enum svccfg_op {
122 	SVCCFG_OP_IMPORT = 0,
123 	SVCCFG_OP_APPLY,
124 	SVCCFG_OP_RESTORE
125 } svccfg_op_t;
126 
127 /*
128  * Return values for functions that validate an entity against the templates.
129  */
130 typedef enum tmpl_validate_status {
131 	TVS_SUCCESS = 0,
132 	/*
133 	 * Either conversion of ASTRING property value to a number failed,
134 	 * or base 32 decoding of a property value failed.
135 	 */
136 	TVS_BAD_CONVERSION,
137 	/* Template is defective. */
138 	TVS_BAD_TEMPLATE,
139 	/* Template type spec is invalid. */
140 	TVS_INVALID_TYPE_SPECIFICATION,
141 	/* Property group is missing a type specification. */
142 	TVS_MISSING_PG_TYPE,
143 	/* Template with required == true is missing type specification. */
144 	TVS_MISSING_TYPE_SPECIFICATION,
145 	/* No match was found for specified item. */
146 	TVS_NOMATCH,
147 	/* Validation error occurred */
148 	TVS_VALIDATION,
149 	/* Validation error that should not inhibit import. */
150 	TVS_WARN,
151 	/* Could not validate because of fatal errors. */
152 	TVS_FATAL = -1
153 } tmpl_validate_status_t;
154 
155 /*
156  * The composed_pg structure is used for templates validation.  It is
157  * defined in svccfg_tmpl.c
158  */
159 typedef struct composed_pg composed_pg_t;
160 
161 typedef struct entity {
162 	uu_list_node_t	sc_node;
163 	entity_type_t sc_etype;
164 
165 	/* Common fields to all entities. */
166 	const char	*sc_name;
167 	const char	*sc_fmri;
168 	uu_list_t	*sc_pgroups;
169 	uu_list_t	*sc_dependents;
170 	struct entity	*sc_parent;
171 	enum import_state  sc_import_state;
172 	int		sc_seen;
173 
174 	union {
175 		struct {
176 			uu_list_t	*sc_service_instances;
177 			service_type_t	sc_service_type;
178 			uint_t		sc_service_version;
179 			/* Following used by template validation */
180 			struct entity	*sc_restarter;
181 			struct entity	*sc_global;
182 		} sc_service;
183 		struct {
184 			uu_avl_t *sc_composed;
185 			/* Following used by template validation */
186 			struct entity	*sc_instance_restarter;
187 		} sc_instance;
188 	} sc_u;
189 } entity_t;
190 
191 /*
192  * sc_pgroup_composed is only used for templates validation of properties.
193  * It is created in build_composed_property_groups() and destroyed in
194  * composed_pg_destroy().  It will only be set for property groups that are
195  * part of an instance -- not for service property groups.
196  */
197 typedef struct pgroup {
198 	uu_list_node_t	sc_node;
199 	uu_list_t	*sc_pgroup_props;
200 	composed_pg_t	*sc_pgroup_composed;	/* Composed properties */
201 
202 	const char	*sc_pgroup_name;
203 	const char	*sc_pgroup_type;
204 	uint_t		sc_pgroup_flags;
205 	struct entity	*sc_parent;
206 
207 	int		sc_pgroup_delete;
208 	int		sc_pgroup_override;
209 	const char	*sc_pgroup_fmri;	/* Used for dependents */
210 
211 	int		sc_pgroup_seen;
212 } pgroup_t;
213 
214 typedef struct property {
215 	uu_list_node_t	sc_node;
216 	uu_avl_node_t	sc_composed_node;	/* Composed props linkage */
217 	uu_list_t	*sc_property_values;
218 
219 	char		*sc_property_name;
220 	scf_type_t	sc_value_type;
221 
222 	int		sc_property_override;
223 	int		sc_seen;
224 } property_t;
225 
226 typedef struct value {
227 	uu_list_node_t	sc_node;
228 
229 	scf_type_t	sc_type;
230 
231 	void (*sc_free)(struct value *);
232 
233 	union {
234 		uint64_t	sc_count;
235 		int64_t		sc_integer;
236 		char		*sc_string;
237 	} sc_u;
238 } value_t;
239 
240 typedef struct scf_callback {
241 	scf_handle_t	*sc_handle;
242 	void		*sc_parent;	/* immediate parent: scope, service,  */
243 					/* instance, property group, property */
244 	scf_transaction_t *sc_trans;
245 	int		sc_service;	/* True if sc_parent is a service. */
246 	uint_t		sc_flags;
247 	pgroup_t	*sc_general;	/* pointer to general property group */
248 	property_t	*sc_enable;	/* pointer to enable property */
249 
250 	const char	*sc_source_fmri;
251 	const char	*sc_target_fmri;
252 	int		sc_err;
253 } scf_callback_t;
254 
255 /*
256  * Collection of template validation errors.
257  */
258 typedef struct tmpl_errors tmpl_errors_t;
259 
260 #ifndef NDEBUG
261 #define	bad_error(func, err)	{					\
262 	(void) fprintf(stderr, "%s:%d: %s() failed with unexpected "	\
263 	    "error %d.  Aborting.\n", __FILE__, __LINE__, (func), (err)); \
264 	abort();							\
265 }
266 #else
267 #define	bad_error(func, err)	abort()
268 #endif
269 
270 #define	SC_CMD_LINE		0x0
271 #define	SC_CMD_FILE		0x1
272 #define	SC_CMD_EOF		0x2
273 #define	SC_CMD_IACTIVE		0x4
274 #define	SC_CMD_DONT_EXIT	0x8
275 
276 typedef struct engine_state {
277 	uint_t		sc_cmd_flags;
278 	FILE		*sc_cmd_file;
279 	uint_t		sc_cmd_lineno;
280 	const char	*sc_cmd_filename;
281 	char		*sc_cmd_buf;
282 	size_t		sc_cmd_bufsz;
283 	off_t		sc_cmd_bufoff;
284 	GetLine		*sc_gl;
285 
286 	pid_t		sc_repo_pid;
287 	const char	*sc_repo_filename;
288 	const char	*sc_repo_doordir;
289 	const char	*sc_repo_doorname;
290 	const char	*sc_repo_server;
291 } engine_state_t;
292 
293 extern engine_state_t *est;
294 
295 typedef struct string_list {
296 	uu_list_node_t	node;
297 	char		*str;
298 } string_list_t;
299 
300 extern uu_list_pool_t *string_pool;
301 
302 struct help_message {
303 	int		token;
304 	const char	*message;
305 };
306 
307 extern struct help_message help_messages[];
308 
309 extern scf_handle_t *g_hndl;	/* global repcached connection handle */
310 extern int g_exitcode;
311 extern int g_verbose;
312 
313 extern ssize_t max_scf_fmri_len;
314 extern ssize_t max_scf_name_len;
315 extern ssize_t max_scf_value_len;
316 extern ssize_t max_scf_pg_type_len;
317 
318 /* Common strings */
319 extern const char * const name_attr;
320 extern const char * const type_attr;
321 extern const char * const value_attr;
322 extern const char * const enabled_attr;
323 extern const char * const scf_pg_general;
324 extern const char * const scf_group_framework;
325 extern const char * const true;
326 extern const char * const false;
327 
328 #define	uu_list_append(list, elem)	uu_list_insert_before(list, NULL, elem)
329 #define	uu_list_prepend(list, elem)	uu_list_insert_after(list, NULL, elem)
330 
331 void *safe_malloc(size_t);
332 char *safe_strdup(const char *);
333 void warn(const char *, ...);
334 void synerr(int);
335 void semerr(const char *, ...);
336 
337 void internal_init(void);
338 void internal_dump(bundle_t *);
339 
340 int value_cmp(const void *, const void *, void *);
341 
342 bundle_t *internal_bundle_new(void);
343 void internal_bundle_free(bundle_t *);
344 entity_t *internal_service_new(const char *);
345 void internal_service_free(entity_t *);
346 entity_t *internal_instance_new(const char *);
347 void internal_instance_free(entity_t *);
348 entity_t *internal_template_new(void);
349 pgroup_t *internal_pgroup_new(void);
350 void internal_pgroup_free(pgroup_t *);
351 pgroup_t *internal_pgroup_find(entity_t *, const char *, const char *);
352 pgroup_t *internal_dependent_find(entity_t *, const char *);
353 pgroup_t *internal_pgroup_find_or_create(entity_t *, const char *,
354     const char *);
355 pgroup_t *internal_pgroup_create_strict(entity_t *, const char *,
356     const char *);
357 property_t *internal_property_new(void);
358 void internal_property_free(property_t *);
359 property_t *internal_property_find(pgroup_t *, const char *);
360 property_t *internal_property_create(const char *, scf_type_t, uint_t, ...);
361 value_t *internal_value_new(void);
362 
363 int internal_attach_service(bundle_t *, entity_t *);
364 int internal_attach_entity(entity_t *, entity_t *);
365 int internal_attach_pgroup(entity_t *, pgroup_t *);
366 void internal_detach_pgroup(entity_t *, pgroup_t *);
367 int internal_attach_dependent(entity_t *, pgroup_t *);
368 int internal_attach_property(pgroup_t *, property_t *);
369 void internal_detach_property(pgroup_t *, property_t *);
370 void internal_attach_value(property_t *, value_t *);
371 
372 int load_init(void);
373 void load_fini(void);
374 int load_instance(const char *, const char *, entity_t **);
375 int load_pg_attrs(const scf_propertygroup_t *, pgroup_t **);
376 int load_pg(const scf_propertygroup_t *, pgroup_t **, const char *,
377     const char *);
378 int prop_equal(property_t *, property_t *, const char *, const char *, int);
379 int pg_attrs_equal(const pgroup_t *, const pgroup_t *, const char *, int);
380 int pg_equal(pgroup_t *, pgroup_t *);
381 
382 void lscf_cleanup(void);
383 void lscf_prep_hndl(void);
384 void lscf_init(void);
385 int lscf_bundle_import(bundle_t *, const char *, uint_t);
386 int lscf_bundle_apply(bundle_t *, const char *);
387 void lscf_delete(const char *, int);
388 void lscf_list(const char *);
389 void lscf_select(const char *);
390 void lscf_unselect();
391 void lscf_get_selection_str(char *, size_t);
392 void lscf_add(const char *);
393 void lscf_listpg(const char *);
394 void lscf_addpg(const char *, const char *, const char *);
395 void lscf_delpg(char *);
396 void lscf_delhash(char *, int);
397 void lscf_listprop(const char *);
398 void lscf_addprop(char *, const char *, const uu_list_t *);
399 void lscf_delprop(char *);
400 int lscf_describe(uu_list_t *, int);
401 void lscf_listsnap();
402 void lscf_selectsnap(const char *);
403 void lscf_revert(const char *);
404 void lscf_refresh();
405 char *filename_to_propname(const char *);
406 int lscf_retrieve_hash(const char *, unsigned char *);
407 int lscf_store_hash(const char *, unsigned char *);
408 CPL_MATCH_FN(complete_select);
409 CPL_MATCH_FN(complete_command);
410 
411 int lxml_init(void);
412 int lxml_get_bundle_file(bundle_t *, const char *, svccfg_op_t);
413 
414 void engine_init(void);
415 int engine_exec_cmd(void);
416 int engine_exec(char *);
417 int add_cmd_matches(WordCompletion *, const char *, int, uint32_t);
418 int engine_interp(void);
419 int engine_source(const char *, boolean_t);
420 int engine_import(uu_list_t *);
421 void help(int);
422 
423 int engine_cmd_getc(engine_state_t *);
424 int engine_cmd_ungetc(engine_state_t *, char);
425 void engine_cmd_nputs(engine_state_t *, char *, size_t);
426 
427 void tmpl_errors_destroy(tmpl_errors_t *);
428 void tmpl_errors_print(FILE *, tmpl_errors_t *, const char *);
429 void tmpl_init(void);
430 void tmpl_property_fini(property_t *);
431 void tmpl_property_init(property_t *);
432 tmpl_validate_status_t tmpl_validate_bundle(bundle_t *, tmpl_errors_t **);
433 
434 #ifdef	__cplusplus
435 }
436 #endif
437 
438 #endif	/* _CMD_SVCCFG_H */
439