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