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