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