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