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