xref: /titanic_51/usr/src/lib/libscf/inc/libscf_priv.h (revision 9c2c14ab194d42014417b385d6bf226ba1a37995)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_LIBSCF_PRIV_H
27 #define	_LIBSCF_PRIV_H
28 
29 
30 #include <libscf.h>
31 #include <unistd.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * NOTE
39  *
40  * The contents of this file are private to the implementation of Solaris
41  * and are subject to change at any time without notice.
42  */
43 
44 #define	SCF_PG_GENERAL_TYPE		SCF_GROUP_FRAMEWORK
45 #define	SCF_PG_GENERAL_FLAGS		0
46 
47 #define	SCF_PG_GENERAL_OVR_TYPE		SCF_GROUP_FRAMEWORK
48 #define	SCF_PG_GENERAL_OVR_FLAGS	SCF_PG_FLAG_NONPERSISTENT
49 
50 #define	SCF_PG_DEATHROW_TYPE		SCF_GROUP_FRAMEWORK
51 #define	SCF_PG_DEATHROW_FLAGS		SCF_PG_FLAG_NONPERSISTENT
52 
53 #define	SCF_PG_OPTIONS_TYPE		SCF_GROUP_FRAMEWORK
54 #define	SCF_PG_OPTIONS_FLAGS		0
55 
56 #define	SCF_PG_OPTIONS_OVR_TYPE		SCF_GROUP_FRAMEWORK
57 #define	SCF_PG_OPTIONS_OVR_FLAGS	SCF_PG_FLAG_NONPERSISTENT
58 
59 #define	SCF_PG_RESTARTER_TYPE		SCF_GROUP_FRAMEWORK
60 #define	SCF_PG_RESTARTER_FLAGS		SCF_PG_FLAG_NONPERSISTENT
61 
62 #define	SCF_PG_RESTARTER_ACTIONS_TYPE	SCF_GROUP_FRAMEWORK
63 #define	SCF_PG_RESTARTER_ACTIONS_FLAGS	SCF_PG_FLAG_NONPERSISTENT
64 
65 #define	SCF_PROPERTY_CLEAR		((const char *)"maint_off")
66 #define	SCF_PROPERTY_MAINTENANCE	((const char *)"maint_on")
67 
68 #define	SCF_PROPERTY_LOGFILE		((const char *)"logfile")
69 #define	SCF_PROPERTY_ALT_LOGFILE	((const char *)"alt_logfile")
70 
71 #define	SCF_LEGACY_SERVICE		((const char *)"smf/legacy_run")
72 
73 #define	SCF_LEGACY_PROPERTY_NAME	((const char *)"name")
74 #define	SCF_LEGACY_PROPERTY_INODE	((const char *)"inode")
75 #define	SCF_LEGACY_PROPERTY_SUFFIX	((const char *)"suffix")
76 
77 #define	SCF_FMRI_TYPE_SVC		0x1
78 #define	SCF_FMRI_TYPE_FILE		0x2
79 
80 /*
81  * Strings for use in constructing FMRIs
82  */
83 #define	SCF_FMRI_SVC_PREFIX		"svc:"
84 #define	SCF_FMRI_FILE_PREFIX		"file:"
85 #define	SCF_FMRI_SCOPE_PREFIX		"//"
86 #define	SCF_FMRI_LOCAL_SCOPE		"localhost"
87 #define	SCF_FMRI_SCOPE_SUFFIX		"@localhost"
88 #define	SCF_FMRI_SERVICE_PREFIX		"/"
89 #define	SCF_FMRI_INSTANCE_PREFIX	":"
90 #define	SCF_FMRI_PROPERTYGRP_PREFIX	"/:properties/"
91 #define	SCF_FMRI_PROPERTY_PREFIX	"/"
92 #define	SCF_FMRI_LEGACY_PREFIX		"lrc:"
93 
94 /*
95  * sulogin Service FMRI
96  */
97 #define	SVC_SULOGIN_FMRI ((const char *)"svc:/system/sulogin")
98 
99 typedef struct scf_decoration_info {
100 	const char *sdi_name;
101 	scf_type_t sdi_type;
102 	scf_value_t *sdi_value;		/* can be SCF_DECORATE_CLEAR */
103 } scf_decoration_info_t;
104 
105 typedef int (*scf_decoration_func)(const scf_decoration_info_t *, void *);
106 
107 /*
108  * calls a callback function for each decoration on the handle.  If the
109  * callback returns 0, the iteration stops and returns 0.  If the callback
110  * returns a non-zero value, the iteration continues.  After full completion,
111  * 1 is returned.  On error, -1 is returned.
112  */
113 int _scf_handle_decorations(scf_handle_t *, scf_decoration_func *,
114     scf_value_t *, void *);
115 
116 /*
117  * wait for a change to the propertygroup -- may return early.
118  * For now, only one of these can be outstanding at a time.
119  *
120  * The second argument is how long, in seconds, to wait for a response.
121  *
122  * Returns SCF_COMPLETE on timeout, -1 on error, and SCF_SUCCESS in every
123  * other case.  You must call scf_pg_update() to see if the object has
124  * actually changed.
125  */
126 int _scf_pg_wait(scf_propertygroup_t *, int);
127 
128 /*
129  * set up notifications for changes to a class of property groups (by name
130  * and type)
131  *
132  * Only one thread can be sleeping in _scf_notify_wait() -- others will
133  * fail.  Deletions give an fmri in the output path.
134  *
135  * These do not survive unbind()->bind() -- in fact, that is currently the
136  * only way to clear them.
137  */
138 int _scf_notify_add_pgname(scf_handle_t *, const char *);
139 int _scf_notify_add_pgtype(scf_handle_t *, const char *);
140 int _scf_notify_wait(scf_propertygroup_t *, char *, size_t);
141 
142 /*
143  * Internal interfaces for snapshot creation:
144  *	_scf_snapshot_take_new(), _scf_snapshot_take_new_named(), and
145  *	_scf_snapshot_take_attach() create a set of snaplevels
146  *	containing frozen versions of both the instance's property groups and
147  *	its parent service's property groups. _scf_snapshot_take_new() and
148  *	_scf_snapshot_take_new_named() create a new snapshot to which the
149  *	new snaplevels are attached, while _scf_snapshot_take_attach()
150  *	attaches the new snaplevels to a pre-existing snapshot.
151  *
152  *	_scf_snapshot_take_new_named() records the passed in names into the
153  *	snaplevel instead of the instance and service name.  This creates
154  *	an inconsistency, which should be resolved by using
155  *	_scf_snapshot_attach() to attach the new snaplevels to a snapshot
156  *	underneath the appropriate instance.  The first snapshot can
157  *	then be deleted.
158  *
159  *	_scf_snapshot_attach(snap1, snap2) points snap2 at the snaplevels
160  *	pointed to by snap1.  After a call to either
161  *	_scf_snapshot_take_attach(snap1, snap2) or
162  *	_scf_snapshot_attach(inst, snap), scf_snapshot_update() will be
163  *	required for any open references to snap or snap2 to see the new
164  *	snaplevels.
165  *
166  *	_scf_snapshot_delete() deletes the snapshot object.  While
167  *	snaplevels, being only loosely connected to snapshots, stay
168  *	around until they are no longer referenced, any references *through
169  *	this snapshot object* will be invalidated.
170  *
171  * _scf_snapshot_take_new() can fail with at least _HANDLE_MISMATCH,
172  * _CONNECTION_BROKEN, _INVALID_ARGUMENT, _NO_RESOURCES, _PERMISSION_DENIED,
173  * _NOT_SET, _EXISTS.
174  *
175  * _scf_snapshot_take_new_named() can fail with at least _HANDLE_MISMATCH,
176  * _CONNECTION_BROKEN, _INVALID_ARGUMENT, _NO_RESOURCES, _PERMISSION_DENIED,
177  * _NOT_SET, _EXISTS.
178  *
179  * _scf_snapshot_take_attach() can fail with _CONNECTION_BROKEN, _NOT_SET,
180  * _PERMISSION_DENIED, _NO_RESOURCES, _INVALID_ARGUMENT.
181  *
182  * _scf_snapshot_attach() can fail with _HANDLE_MISMATCH, _CONNECTION_BROKEN,
183  * _NOT_SET, _NO_RESOURCES, _PERMISSION_DENIED.
184  */
185 int _scf_snapshot_take_new(scf_instance_t *, const char *, scf_snapshot_t *);
186 int _scf_snapshot_take_new_named(scf_instance_t *,
187     const char *, const char *, const char *, scf_snapshot_t *);
188 int _scf_snapshot_take_attach(scf_instance_t *, scf_snapshot_t *);
189 int _scf_snapshot_attach(scf_snapshot_t *, scf_snapshot_t *);
190 int _scf_snapshot_delete(scf_snapshot_t *);
191 
192 /*
193  * Destructively portions up the first argument into the different portions
194  * of a svc: fmri, and returns pointers to the applicable portions.  Omitted
195  * portions are set to NULL, except for the scope, which is set to the
196  * default local scope if not specified.
197  *
198  * Parsing is attempted in the order of: svc:, file:. The identified type
199  * of the service is returned in the second argument and may take a value
200  * of: SCF_FMRI_TYPE_SVC or SCF_FMRI_TYPE_FILE.
201  *
202  * Note that some of the returned pointers (in particular the scope) may not
203  * point into the passed buffer.
204  */
205 int scf_parse_fmri(char *, int *, const char **, const char **, const char **,
206     const char **, const char **);
207 
208 int scf_parse_svc_fmri(char *, const char **, const char **, const char **,
209     const char **, const char **);
210 
211 int scf_parse_file_fmri(char *fmri, const char **scope, const char **path);
212 
213 ssize_t scf_canonify_fmri(const char *, char *, size_t);
214 
215 const char *scf_type_to_string(scf_type_t);
216 scf_type_t scf_string_to_type(const char *);
217 
218 int _smf_refresh_instance_i(scf_instance_t *);
219 
220 typedef struct scf_simple_handle {
221 	scf_handle_t		*h;
222 	scf_snapshot_t		*snap;
223 	scf_instance_t		*inst;
224 	scf_propertygroup_t	*running_pg;
225 	scf_propertygroup_t	*editing_pg;
226 } scf_simple_handle_t;
227 
228 void scf_simple_handle_destroy(scf_simple_handle_t *);
229 scf_simple_handle_t *scf_general_pg_setup(const char *, const char *);
230 scf_transaction_t *scf_transaction_setup(scf_simple_handle_t *);
231 int scf_transaction_restart(scf_simple_handle_t *, scf_transaction_t *);
232 int scf_read_count_property(scf_simple_handle_t *, char *, uint64_t *);
233 int scf_set_count_property(scf_transaction_t *, char *, uint64_t, boolean_t);
234 
235 /*
236  * Walks all the instances matching a given fmri list.  Each fmri in the array
237  * can be one of the following:
238  *
239  * 	- Full instance name
240  * 	- Full service name
241  * 	- Full property group or property name
242  * 	- Partial service or instance name
243  * 	- A globbed pattern
244  *
245  * The matching rules for partial fmris are a slightly more complex.  We allow
246  * for any substring anchored at the end of the instance or service name,
247  * provided it begins with a complete element in the fmri.  For example, given
248  * the fmri "svc:/system/filesystem/local:default", any of the following would
249  * be acceptable matches: 'default', 'local', 'local:default',
250  * 'filesystem/local'.  The following would not be acceptable:
251  * 'system/filesystem', 'filesystem/loc', 'system/local'.  Possible flag values:
252  *
253  * 	SCF_WALK_MULTIPLE	Allow individual arguments to correspond to
254  * 				multiple instances.
255  *
256  * 	SCF_WALK_LEGACY		Walk legacy services (indicated by a non-NULL
257  * 				propery group).
258  *
259  * 	SCF_WALK_SERVICE	If the user specifies a service, pass the
260  * 				service to the callback without iterating over
261  * 				its instances.
262  *
263  * 	SCF_WALK_PROPERTY	Allow FMRIs which match property groups or
264  * 				individual properties.  Incompatible with
265  * 				SCF_WALK_LEGACY.
266  *
267  * 	SCF_WALK_NOINSTANCE	Walk only services.  Must be used in
268  * 				conjunction with SCF_WALK_SERVICE.
269  *
270  * 	SCF_WALK_EXPLICIT	Walk only services if the match is exact
271  *				else return instances. Must be used in
272  *				conjunction with SCF_WALK_SERVICE.
273  *
274  * If no arguments are given, then all instances in the service graph are
275  * walked.
276  *
277  * The second to last parameter is set to UU_EXIT_FATAL if one of the arguments
278  * is an invalid FMRI or matches multiple FMRIs when SCF_WALK_MULTIPLE is not
279  * set.
280  *
281  * The last parameter is a user-supplied error function that is called when
282  * reporting invalid arguments.
283  */
284 
285 #define	SCF_WALK_MULTIPLE	0x01
286 #define	SCF_WALK_LEGACY		0x02
287 #define	SCF_WALK_SERVICE	0x04
288 #define	SCF_WALK_PROPERTY	0x08
289 #define	SCF_WALK_NOINSTANCE	0x10
290 #define	SCF_WALK_EXPLICIT	0x20
291 
292 /*
293  * The default locations of the repository dbs
294  */
295 #define	REPOSITORY_DB		"/etc/svc/repository.db"
296 #define	NONPERSIST_DB		"/etc/svc/volatile/svc_nonpersist.db"
297 #define	FAST_REPOSITORY_DB	"/etc/svc/volatile/fast_repository.db"
298 
299 
300 typedef struct scf_walkinfo {
301 	const char		*fmri;
302 	scf_scope_t		*scope;
303 	scf_service_t		*svc;
304 	scf_instance_t		*inst;
305 	scf_propertygroup_t	*pg;
306 	scf_property_t		*prop;
307 	int			count;	/* svcprop special */
308 } scf_walkinfo_t;
309 
310 typedef int (*scf_walk_callback)(void *, scf_walkinfo_t *);
311 
312 scf_error_t scf_walk_fmri(scf_handle_t *, int, char **, int,
313     scf_walk_callback, void *, int *, void (*)(const char *, ...));
314 
315 /*
316  * Requests a backup of the repository with a particular name, which
317  * can be any alphabetic string.  Only privileged users can do this.
318  *
319  * Can fail with:
320  *	_NOT_BOUND, _CONNECTION_BROKEN, _PERMISSION_DENIED, _INVALID_ARGUMENT,
321  *	_INTERNAL (path too long, or the backup failed for an odd reason),
322  *	_BACKEND_READONLY (filesystem is still read-only)
323  */
324 int _scf_request_backup(scf_handle_t *, const char *);
325 
326 /*
327  * Repository switch client
328  */
329 int _scf_repository_switch(scf_handle_t *, int);
330 
331 /*
332  * Determines whether a property group requires authorization to read; this
333  * does not in any way reflect whether the caller has that authorization.
334  * To determine that, the caller must attempt to read the value of one of the
335  * group's properties.
336  *
337  * Can fail with:
338  *	_NOT_BOUND, _CONNECTION_BROKEN, _INVALID_ARGUMENT, _INTERNAL,
339  *	_NO_RESOURCES, _CONSTRAINT_VIOLATED, _DELETED.
340  */
341 int _scf_pg_is_read_protected(const scf_propertygroup_t *, boolean_t *);
342 
343 /*
344  * Sets annotation data for SMF audit logging.  Once this function has been
345  * set, the next audit record will be preceded by an ADT_smf_annotation
346  * with the information provided in this function.  This function is used
347  * to mark operations which comprise multiple primitive operations such as
348  * svccfg import.
349  */
350 int _scf_set_annotation(scf_handle_t *h, const char *operation,
351     const char *file);
352 
353 /*
354  * scf_pattern_t
355  */
356 typedef struct scf_pattern {
357 	enum	{
358 		PATTERN_INVALID,	/* Uninitialized state */
359 		PATTERN_EXACT,
360 		PATTERN_GLOB,
361 		PATTERN_PARTIAL
362 	} sp_type;
363 	char			*sp_arg;	/* Original argument */
364 	struct scf_match	*sp_matches;	/* List of matches */
365 	int			sp_matchcount;	/* # of matches */
366 } scf_pattern_t;
367 
368 int scf_cmp_pattern(char *, scf_pattern_t *);
369 
370 int gen_filenms_from_fmri(const char *, const char *, char *, char *);
371 
372 /*
373  * Interfaces for bulk access to SMF-stored configuration.
374  *
375  * Each scf_propvec_t represents a single property to be read (with
376  * scf_read_propvec) or written (with scf_write_propvec).
377  *
378  * The fields of a scf_propvec_t have the following meanings:
379  *
380  *   pv_prop - the name of the property
381  *   pv_desc - a description string (optional; to be consumed by the caller)
382  *   pv_type - the type of the property
383  *   pv_ptr  - where to store the data read, or a pointer to the data to
384  *             be written
385  *   pv_aux  - additional data influencing the interpretation of pv_ptr
386  *
387  * The meaning of pv_ptr and pv_aux depends on the type of property.  For:
388  *
389  *   boolean - if pv_aux is 0, pv_ptr is a pointer to a boolean_t
390  *             if pv_aux is non-0, pv_ptr is a pointer to a uint64_t,
391  *             where pv_aux indicates the bit holding the truth value.
392  *   count   - pv_ptr is a pointer to a uint64_t; pv_aux is unused
393  *   integer - pv_ptr is a pointer to an int64_t; pv_aux is unused
394  *   time    - pv_ptr is a pointer to an scf_time_t; pv_aux is unused
395  *   opaque  - pv_ptr is a pointer to an scf_opaque_t; pv_aux is unused
396  *   strings - (scf_read_propvec) pv_ptr is a pointer to a char *
397  *             (scf_write_propvec) pv_ptr is a pointer to an array of char
398  *             (both) pv_aux is unused
399  */
400 typedef struct {
401 	int64_t	st_sec;
402 	int32_t	st_nanosec;
403 } scf_time_t;
404 
405 typedef struct {
406 	void	*so_addr;
407 	size_t	so_size;
408 } scf_opaque_t;
409 
410 typedef struct {
411 	const char	*pv_prop;
412 	const char	*pv_desc;
413 	scf_type_t	pv_type;
414 	void		*pv_ptr;
415 	uint64_t	pv_aux;
416 } scf_propvec_t;
417 
418 void scf_clean_propvec(scf_propvec_t *);
419 int scf_read_propvec(const char *, const char *, boolean_t, scf_propvec_t *,
420     scf_propvec_t **);
421 int scf_write_propvec(const char *, const char *, scf_propvec_t *,
422     scf_propvec_t **);
423 
424 #ifdef	__cplusplus
425 }
426 #endif
427 
428 #endif	/* _LIBSCF_PRIV_H */
429