xref: /titanic_41/usr/src/cmd/svc/svccfg/svccfg.h (revision cd37da7426f0c49c14ad9a8a07638ca971477566)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_CMD_SVCCFG_H
28 #define	_CMD_SVCCFG_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 
34 #include <libxml/tree.h>
35 
36 #include <libscf.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 
60 /* Flags for lscf_service_export() */
61 #define	SCE_ALL_VALUES	0x01		/* Include all property values */
62 
63 #ifdef lint
64 extern int yyerror(const char *);
65 extern int yyparse(void);
66 #endif /* lint */
67 
68 extern int lex_lineno;
69 
70 #define	MANIFEST_DTD_PATH	"/usr/share/lib/xml/dtd/service_bundle.dtd.1"
71 /*
72  * The following list must be kept in the same order as that of
73  * lxml_prop_types[]
74  */
75 typedef enum element {
76 	SC_ASTRING = 0x0, SC_BOOLEAN, SC_COMMON_NAME, SC_COUNT,
77 	SC_INSTANCE_CREATE_DEFAULT, SC_DEPENDENCY, SC_DEPENDENT, SC_DESCRIPTION,
78 	SC_DOC_LINK, SC_DOCUMENTATION, SC_ENABLED, SC_EXEC_METHOD, SC_FMRI,
79 	SC_HOST, SC_HOSTNAME, SC_INSTANCE, SC_INTEGER, SC_LOCTEXT, SC_MANPAGE,
80 	SC_METHOD_CONTEXT, SC_METHOD_CREDENTIAL, SC_METHOD_PROFILE,
81 	SC_METHOD_ENVIRONMENT, SC_METHOD_ENVVAR, SC_NET_ADDR_V4, SC_NET_ADDR_V6,
82 	SC_OPAQUE, SC_PROPERTY, SC_PROPERTY_GROUP, SC_PROPVAL, SC_RESTARTER,
83 	SC_SERVICE, SC_SERVICE_BUNDLE, SC_SERVICE_FMRI, SC_INSTANCE_SINGLE,
84 	SC_STABILITY, SC_TEMPLATE, SC_TIME, SC_URI, SC_USTRING, SC_VALUE_NODE,
85 	SC_XI_FALLBACK, SC_XI_INCLUDE
86 } element_t;
87 
88 typedef enum bundle_type {
89 	SVCCFG_UNKNOWN_BUNDLE, SVCCFG_MANIFEST, SVCCFG_PROFILE, SVCCFG_ARCHIVE
90 } bundle_type_t;
91 
92 typedef struct bundle {
93 	uu_list_t	*sc_bundle_services;
94 
95 	xmlChar		*sc_bundle_name;
96 	bundle_type_t	sc_bundle_type;
97 } bundle_t;
98 
99 typedef enum service_type {
100 	SVCCFG_UNKNOWN_SERVICE = 0x0, SVCCFG_SERVICE, SVCCFG_RESTARTER,
101 	SVCCFG_MILESTONE
102 } service_type_t;
103 
104 typedef enum entity_type {
105 	SVCCFG_SERVICE_OBJECT = 0x0, SVCCFG_INSTANCE_OBJECT,
106 	SVCCFG_TEMPLATE_OBJECT
107 } entity_type_t;
108 
109 enum import_state {
110 	IMPORT_NONE = 0,
111 	IMPORT_PREVIOUS,
112 	IMPORT_PROP_BEGUN,
113 	IMPORT_PROP_DONE,
114 	IMPORT_COMPLETE,
115 	IMPORT_REFRESHED
116 };
117 
118 typedef enum svccfg_op {
119 	SVCCFG_OP_IMPORT = 0,
120 	SVCCFG_OP_APPLY,
121 	SVCCFG_OP_RESTORE
122 } svccfg_op_t;
123 
124 typedef struct entity {
125 	uu_list_node_t	sc_node;
126 	entity_type_t sc_etype;
127 
128 	/* Common fields to all entities. */
129 	const char	*sc_name;
130 	const char	*sc_fmri;
131 	uu_list_t	*sc_pgroups;
132 	uu_list_t	*sc_dependents;
133 	struct entity	*sc_parent;
134 	enum import_state  sc_import_state;
135 	int		sc_seen;
136 
137 	union {
138 		struct {
139 			uu_list_t	*sc_service_instances;
140 			service_type_t	sc_service_type;
141 			uint_t		sc_service_version;
142 
143 			struct entity *sc_service_template;
144 		} sc_service;
145 		struct {
146 			uint_t		sc_instance_dummy;
147 		} sc_instance;
148 		struct {
149 			uint_t		sc_template_dummy;
150 		} sc_template;
151 	} sc_u;
152 } entity_t;
153 
154 typedef struct pgroup {
155 	uu_list_node_t	sc_node;
156 	uu_list_t	*sc_pgroup_props;
157 
158 	const char	*sc_pgroup_name;
159 	const char	*sc_pgroup_type;
160 	uint_t		sc_pgroup_flags;
161 	struct entity	*sc_parent;
162 
163 	int		sc_pgroup_delete;
164 	int		sc_pgroup_override;
165 	const char	*sc_pgroup_fmri;	/* Used for dependents */
166 
167 	int		sc_pgroup_seen;
168 } pgroup_t;
169 
170 typedef struct property {
171 	uu_list_node_t	sc_node;
172 	uu_list_t	*sc_property_values;
173 
174 	char		*sc_property_name;
175 	scf_type_t	sc_value_type;
176 
177 	int		sc_property_override;
178 	int		sc_seen;
179 } property_t;
180 
181 typedef struct value {
182 	uu_list_node_t	sc_node;
183 
184 	scf_type_t	sc_type;
185 
186 	void (*sc_free)(struct value *);
187 
188 	union {
189 		uint64_t	sc_count;
190 		int64_t		sc_integer;
191 		char		*sc_string;
192 	} sc_u;
193 } value_t;
194 
195 typedef struct scf_callback {
196 	scf_handle_t	*sc_handle;
197 	void		*sc_parent;	/* immediate parent: scope, service,  */
198 					/* instance, property group, property */
199 	scf_transaction_t *sc_trans;
200 	int		sc_service;	/* True if sc_parent is a service. */
201 	uint_t		sc_flags;
202 	pgroup_t	*sc_general;	/* pointer to general property group */
203 
204 	const char	*sc_source_fmri;
205 	const char	*sc_target_fmri;
206 	int		sc_err;
207 } scf_callback_t;
208 
209 #ifndef NDEBUG
210 #define	bad_error(func, err)	{					\
211 	(void) fprintf(stderr, "%s:%d: %s() failed with unexpected "	\
212 	    "error %d.  Aborting.\n", __FILE__, __LINE__, (func), (err)); \
213 	abort();							\
214 }
215 #else
216 #define	bad_error(func, err)	abort()
217 #endif
218 
219 #define	SC_CMD_LINE		0x0
220 #define	SC_CMD_FILE		0x1
221 #define	SC_CMD_EOF		0x2
222 #define	SC_CMD_IACTIVE		0x4
223 #define	SC_CMD_DONT_EXIT	0x8
224 
225 typedef struct engine_state {
226 	uint_t		sc_cmd_flags;
227 	FILE		*sc_cmd_file;
228 	uint_t		sc_cmd_lineno;
229 	const char	*sc_cmd_filename;
230 	char		*sc_cmd_buf;
231 	size_t		sc_cmd_bufsz;
232 	off_t		sc_cmd_bufoff;
233 	GetLine		*sc_gl;
234 
235 	pid_t		sc_repo_pid;
236 	const char	*sc_repo_filename;
237 	const char	*sc_repo_doordir;
238 	const char	*sc_repo_doorname;
239 	const char	*sc_repo_server;
240 } engine_state_t;
241 
242 extern engine_state_t *est;
243 
244 typedef struct string_list {
245 	uu_list_node_t	node;
246 	char		*str;
247 } string_list_t;
248 
249 extern uu_list_pool_t *string_pool;
250 
251 struct help_message {
252 	int		token;
253 	const char	*message;
254 };
255 
256 extern struct help_message help_messages[];
257 
258 extern scf_handle_t *g_hndl;	/* global repcached connection handle */
259 extern int g_exitcode;
260 extern int g_verbose;
261 
262 extern ssize_t max_scf_fmri_len;
263 extern ssize_t max_scf_name_len;
264 extern ssize_t max_scf_value_len;
265 extern ssize_t max_scf_pg_type_len;
266 
267 /* Common strings */
268 extern const char * const name_attr;
269 extern const char * const type_attr;
270 extern const char * const value_attr;
271 extern const char * const enabled_attr;
272 extern const char * const scf_pg_general;
273 extern const char * const scf_group_framework;
274 extern const char * const true;
275 extern const char * const false;
276 
277 #define	uu_list_append(list, elem)	uu_list_insert_before(list, NULL, elem)
278 #define	uu_list_prepend(list, elem)	uu_list_insert_after(list, NULL, elem)
279 
280 void *safe_malloc(size_t);
281 char *safe_strdup(const char *);
282 void warn(const char *, ...);
283 void synerr(int);
284 void semerr(const char *, ...);
285 
286 void internal_init(void);
287 void internal_dump(bundle_t *);
288 
289 int value_cmp(const void *, const void *, void *);
290 
291 bundle_t *internal_bundle_new(void);
292 void internal_bundle_free(bundle_t *);
293 entity_t *internal_service_new(const char *);
294 void internal_service_free(entity_t *);
295 entity_t *internal_instance_new(const char *);
296 void internal_instance_free(entity_t *);
297 entity_t *internal_template_new(void);
298 pgroup_t *internal_pgroup_new(void);
299 void internal_pgroup_free(pgroup_t *);
300 pgroup_t *internal_pgroup_find(entity_t *, const char *, const char *);
301 pgroup_t *internal_dependent_find(entity_t *, const char *);
302 pgroup_t *internal_pgroup_find_or_create(entity_t *, const char *,
303     const char *);
304 property_t *internal_property_new(void);
305 void internal_property_free(property_t *);
306 property_t *internal_property_find(pgroup_t *, const char *);
307 property_t *internal_property_create(const char *, scf_type_t, uint_t, ...);
308 value_t *internal_value_new(void);
309 
310 int internal_attach_service(bundle_t *, entity_t *);
311 int internal_attach_entity(entity_t *, entity_t *);
312 int internal_attach_pgroup(entity_t *, pgroup_t *);
313 int internal_attach_dependent(entity_t *, pgroup_t *);
314 int internal_attach_property(pgroup_t *, property_t *);
315 void internal_attach_value(property_t *, value_t *);
316 
317 int load_init(void);
318 void load_fini(void);
319 int load_pg_attrs(const scf_propertygroup_t *, pgroup_t **);
320 int load_pg(const scf_propertygroup_t *, pgroup_t **, const char *,
321     const char *);
322 int prop_equal(property_t *, property_t *, const char *, const char *, int);
323 int pg_attrs_equal(const pgroup_t *, const pgroup_t *, const char *, int);
324 int pg_equal(pgroup_t *, pgroup_t *);
325 
326 void lscf_cleanup(void);
327 void lscf_prep_hndl(void);
328 void lscf_init(void);
329 int lscf_bundle_import(bundle_t *, const char *, uint_t);
330 int lscf_bundle_apply(bundle_t *);
331 void lscf_delete(const char *, int);
332 void lscf_list(const char *);
333 void lscf_select(const char *);
334 void lscf_unselect();
335 void lscf_get_selection_str(char *, size_t);
336 void lscf_add(const char *);
337 void lscf_listpg(const char *);
338 void lscf_addpg(const char *, const char *, const char *);
339 void lscf_delpg(char *);
340 void lscf_listprop(const char *);
341 void lscf_addprop(char *, const char *, const uu_list_t *);
342 void lscf_delprop(char *);
343 void lscf_listsnap();
344 void lscf_selectsnap(const char *);
345 void lscf_revert(const char *);
346 char *filename_to_propname(const char *);
347 int lscf_retrieve_hash(const char *, unsigned char *);
348 int lscf_store_hash(const char *, unsigned char *);
349 CPL_MATCH_FN(complete_select);
350 CPL_MATCH_FN(complete_command);
351 
352 int lxml_init(void);
353 int lxml_get_bundle_file(bundle_t *, const char *, svccfg_op_t);
354 
355 void engine_init(void);
356 int engine_exec_cmd(void);
357 int engine_exec(char *);
358 int add_cmd_matches(WordCompletion *, const char *, int, uint32_t);
359 int engine_interp(void);
360 int engine_source(const char *, boolean_t);
361 int engine_import(uu_list_t *);
362 void help(int);
363 
364 int engine_cmd_getc(engine_state_t *);
365 int engine_cmd_ungetc(engine_state_t *, char);
366 void engine_cmd_nputs(engine_state_t *, char *, size_t);
367 
368 #ifdef	__cplusplus
369 }
370 #endif
371 
372 #endif	/* _CMD_SVCCFG_H */
373