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