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