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