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