10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51712Srm88369 * Common Development and Distribution License (the "License").
61712Srm88369 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
23*12967Sgavin.maltby@oracle.com * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
26*12967Sgavin.maltby@oracle.com #include <libintl.h>
270Sstevel@tonic-gate #include <librestart.h>
280Sstevel@tonic-gate #include <librestart_priv.h>
290Sstevel@tonic-gate #include <libscf.h>
300Sstevel@tonic-gate #include <libscf_priv.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <dlfcn.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <exec_attr.h>
370Sstevel@tonic-gate #include <grp.h>
380Sstevel@tonic-gate #include <libsysevent.h>
390Sstevel@tonic-gate #include <libuutil.h>
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include <link.h>
420Sstevel@tonic-gate #include <malloc.h>
430Sstevel@tonic-gate #include <pool.h>
440Sstevel@tonic-gate #include <priv.h>
450Sstevel@tonic-gate #include <project.h>
460Sstevel@tonic-gate #include <pthread.h>
470Sstevel@tonic-gate #include <pwd.h>
480Sstevel@tonic-gate #include <secdb.h>
490Sstevel@tonic-gate #include <signal.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <syslog.h>
530Sstevel@tonic-gate #include <sys/corectl.h>
540Sstevel@tonic-gate #include <sys/machelf.h>
550Sstevel@tonic-gate #include <sys/task.h>
560Sstevel@tonic-gate #include <sys/types.h>
570Sstevel@tonic-gate #include <time.h>
580Sstevel@tonic-gate #include <unistd.h>
590Sstevel@tonic-gate #include <ucontext.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate #define min(a, b) ((a) > (b) ? (b) : (a))
620Sstevel@tonic-gate
630Sstevel@tonic-gate #define MKW_TRUE ":true"
640Sstevel@tonic-gate #define MKW_KILL ":kill"
650Sstevel@tonic-gate #define MKW_KILL_PROC ":kill_process"
660Sstevel@tonic-gate
670Sstevel@tonic-gate #define ALLOCFAIL ((char *)"Allocation failure.")
680Sstevel@tonic-gate #define RCBROKEN ((char *)"Repository connection broken.")
690Sstevel@tonic-gate
706045Srm88369 #define MAX_COMMIT_RETRIES 10
710Sstevel@tonic-gate #define MAX_COMMIT_RETRY_INT (5 * 1000000) /* 5 seconds */
720Sstevel@tonic-gate #define INITIAL_COMMIT_RETRY_INT (10000) /* 1/100th second */
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * bad_fail() catches bugs in this and lower layers by reporting supposedly
760Sstevel@tonic-gate * impossible function failures. The NDEBUG case keeps the strings out of the
770Sstevel@tonic-gate * library but still calls abort() so we can root-cause from the coredump.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate #ifndef NDEBUG
800Sstevel@tonic-gate #define bad_fail(func, err) { \
810Sstevel@tonic-gate (void) fprintf(stderr, \
820Sstevel@tonic-gate "At %s:%d, %s() failed with unexpected error %d. Aborting.\n", \
830Sstevel@tonic-gate __FILE__, __LINE__, (func), (err)); \
840Sstevel@tonic-gate abort(); \
850Sstevel@tonic-gate }
860Sstevel@tonic-gate #else
870Sstevel@tonic-gate #define bad_fail(func, err) abort()
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate
900Sstevel@tonic-gate struct restarter_event_handle {
910Sstevel@tonic-gate char *reh_restarter_name;
920Sstevel@tonic-gate char *reh_delegate_channel_name;
930Sstevel@tonic-gate evchan_t *reh_delegate_channel;
940Sstevel@tonic-gate char *reh_delegate_subscriber_id;
950Sstevel@tonic-gate char *reh_master_channel_name;
960Sstevel@tonic-gate evchan_t *reh_master_channel;
970Sstevel@tonic-gate char *reh_master_subscriber_id;
980Sstevel@tonic-gate int (*reh_handler)(restarter_event_t *);
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate struct restarter_event {
1020Sstevel@tonic-gate sysevent_t *re_sysevent;
1030Sstevel@tonic-gate restarter_event_type_t re_type;
1040Sstevel@tonic-gate char *re_instance_name;
1050Sstevel@tonic-gate restarter_event_handle_t *re_event_handle;
1060Sstevel@tonic-gate restarter_instance_state_t re_state;
1070Sstevel@tonic-gate restarter_instance_state_t re_next_state;
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate
1109765SSean.Wilcox@Sun.COM /*
111*12967Sgavin.maltby@oracle.com * Long reasons must all parse/read correctly in the following contexts:
112*12967Sgavin.maltby@oracle.com *
113*12967Sgavin.maltby@oracle.com * "A service instance transitioned state: %s."
114*12967Sgavin.maltby@oracle.com * "A service failed: %s."
115*12967Sgavin.maltby@oracle.com * "Reason: %s."
116*12967Sgavin.maltby@oracle.com * "The service transitioned state (%s) and ..."
117*12967Sgavin.maltby@oracle.com *
118*12967Sgavin.maltby@oracle.com * With the exception of restart_str_none they must also fit the following
119*12967Sgavin.maltby@oracle.com * moulds:
120*12967Sgavin.maltby@oracle.com *
121*12967Sgavin.maltby@oracle.com * "An instance transitioned because %s, and ..."
122*12967Sgavin.maltby@oracle.com * "An instance transitioned to <new-state> because %s, and ..."
123*12967Sgavin.maltby@oracle.com *
124*12967Sgavin.maltby@oracle.com * Note that whoever is rendering the long message must provide the
125*12967Sgavin.maltby@oracle.com * terminal punctuation - don't include it here. Similarly, do not
126*12967Sgavin.maltby@oracle.com * provide an initial capital letter in reason-long.
127*12967Sgavin.maltby@oracle.com *
128*12967Sgavin.maltby@oracle.com * The long reason strings are Volatile - within the grammatical constraints
129*12967Sgavin.maltby@oracle.com * above we may improve them as need be. The intention is that a consumer
130*12967Sgavin.maltby@oracle.com * may blindly render the string along the lines of the above examples,
131*12967Sgavin.maltby@oracle.com * but has no other guarantees as to the exact wording. Long reasons
132*12967Sgavin.maltby@oracle.com * are localized.
133*12967Sgavin.maltby@oracle.com *
134*12967Sgavin.maltby@oracle.com * We define revisions of the set of short reason strings in use. Within
135*12967Sgavin.maltby@oracle.com * a given revision, all short reasons are Committed. Consumers must check
136*12967Sgavin.maltby@oracle.com * the revision in use before relying on the semantics of the short reason
137*12967Sgavin.maltby@oracle.com * codes - if the version exceeds that which they are familiar with they should
138*12967Sgavin.maltby@oracle.com * fail gracefully. Having checked for version compatability, a consumer
139*12967Sgavin.maltby@oracle.com * is assured that
140*12967Sgavin.maltby@oracle.com *
141*12967Sgavin.maltby@oracle.com * "short_reason_A iff semantic_A", provided:
142*12967Sgavin.maltby@oracle.com *
143*12967Sgavin.maltby@oracle.com * . the restarter uses this short reason code at all,
144*12967Sgavin.maltby@oracle.com * . the short reason is not "none" (which a restarter could
145*12967Sgavin.maltby@oracle.com * specifiy for any transition semantics)
146*12967Sgavin.maltby@oracle.com *
147*12967Sgavin.maltby@oracle.com * To split/refine such a Committed semantic_A into further cases,
148*12967Sgavin.maltby@oracle.com * we are required to bump the revision number. This should be an
149*12967Sgavin.maltby@oracle.com * infrequent occurence. If you bump the revision number you may
150*12967Sgavin.maltby@oracle.com * need to make corresponding changes in any source that calls
151*12967Sgavin.maltby@oracle.com * restarter_str_version (e.g., FMA event generation).
152*12967Sgavin.maltby@oracle.com *
153*12967Sgavin.maltby@oracle.com * To add additional reasons to the set you must also bump the version
154*12967Sgavin.maltby@oracle.com * number.
155*12967Sgavin.maltby@oracle.com */
156*12967Sgavin.maltby@oracle.com
157*12967Sgavin.maltby@oracle.com /*
158*12967Sgavin.maltby@oracle.com * The following describes revision 0 of the set of transition reasons.
159*12967Sgavin.maltby@oracle.com * Read the preceding block comment before making any changes.
160*12967Sgavin.maltby@oracle.com */
161*12967Sgavin.maltby@oracle.com static const struct restarter_state_transition_reason restarter_str[] = {
162*12967Sgavin.maltby@oracle.com /*
163*12967Sgavin.maltby@oracle.com * Any transition for which the restarter has not provided a reason.
164*12967Sgavin.maltby@oracle.com */
165*12967Sgavin.maltby@oracle.com {
166*12967Sgavin.maltby@oracle.com restarter_str_none,
167*12967Sgavin.maltby@oracle.com "none",
168*12967Sgavin.maltby@oracle.com "the restarter gave no reason"
169*12967Sgavin.maltby@oracle.com },
170*12967Sgavin.maltby@oracle.com
171*12967Sgavin.maltby@oracle.com /*
172*12967Sgavin.maltby@oracle.com * A transition to maintenance state due to a
173*12967Sgavin.maltby@oracle.com * 'svcadm mark maintenance <fmri>'. *Not* used if the libscf
174*12967Sgavin.maltby@oracle.com * interface smf_maintain_instance(3SCF) is used to request maintenance.
175*12967Sgavin.maltby@oracle.com */
176*12967Sgavin.maltby@oracle.com {
177*12967Sgavin.maltby@oracle.com restarter_str_administrative_request,
178*12967Sgavin.maltby@oracle.com "administrative_request",
179*12967Sgavin.maltby@oracle.com "maintenance was requested by an administrator"
180*12967Sgavin.maltby@oracle.com },
181*12967Sgavin.maltby@oracle.com
182*12967Sgavin.maltby@oracle.com /*
183*12967Sgavin.maltby@oracle.com * A transition to maintenance state if a repository inconsistency
184*12967Sgavin.maltby@oracle.com * exists when the service/instance state is first read by startd
185*12967Sgavin.maltby@oracle.com * into the graph engine (this can also happen during startd restart).
186*12967Sgavin.maltby@oracle.com */
187*12967Sgavin.maltby@oracle.com {
188*12967Sgavin.maltby@oracle.com restarter_str_bad_repo_state,
189*12967Sgavin.maltby@oracle.com "bad_repo_state",
190*12967Sgavin.maltby@oracle.com "an SMF repository inconsistecy exists"
191*12967Sgavin.maltby@oracle.com },
192*12967Sgavin.maltby@oracle.com
193*12967Sgavin.maltby@oracle.com /*
194*12967Sgavin.maltby@oracle.com * A transition 'maintenance -> uninitialized' resulting always
195*12967Sgavin.maltby@oracle.com * from 'svcadm clear <fmri>'. *Not* used if the libscf interface
196*12967Sgavin.maltby@oracle.com * smf_restore_instance(3SCF) is used.
197*12967Sgavin.maltby@oracle.com */
198*12967Sgavin.maltby@oracle.com {
199*12967Sgavin.maltby@oracle.com restarter_str_clear_request,
200*12967Sgavin.maltby@oracle.com "clear_request",
201*12967Sgavin.maltby@oracle.com "maintenance clear was requested by an administrator"
202*12967Sgavin.maltby@oracle.com },
203*12967Sgavin.maltby@oracle.com
204*12967Sgavin.maltby@oracle.com /*
205*12967Sgavin.maltby@oracle.com * A transition 'online -> offline' due to a process core dump.
206*12967Sgavin.maltby@oracle.com */
207*12967Sgavin.maltby@oracle.com {
208*12967Sgavin.maltby@oracle.com restarter_str_ct_ev_core,
209*12967Sgavin.maltby@oracle.com "ct_ev_core",
210*12967Sgavin.maltby@oracle.com "a process dumped core"
211*12967Sgavin.maltby@oracle.com },
212*12967Sgavin.maltby@oracle.com
213*12967Sgavin.maltby@oracle.com /*
214*12967Sgavin.maltby@oracle.com * A transition 'online -> offline' due to an empty process contract,
215*12967Sgavin.maltby@oracle.com * i.e., the last process in a contract type service has exited.
216*12967Sgavin.maltby@oracle.com */
217*12967Sgavin.maltby@oracle.com {
218*12967Sgavin.maltby@oracle.com restarter_str_ct_ev_exit,
219*12967Sgavin.maltby@oracle.com "ct_ev_exit",
220*12967Sgavin.maltby@oracle.com "all processes in the service have exited"
221*12967Sgavin.maltby@oracle.com },
222*12967Sgavin.maltby@oracle.com
223*12967Sgavin.maltby@oracle.com /*
224*12967Sgavin.maltby@oracle.com * A transition 'online -> offline' due to a hardware error.
225*12967Sgavin.maltby@oracle.com */
226*12967Sgavin.maltby@oracle.com {
227*12967Sgavin.maltby@oracle.com restarter_str_ct_ev_hwerr,
228*12967Sgavin.maltby@oracle.com "ct_ev_hwerr",
229*12967Sgavin.maltby@oracle.com "a process was killed due to uncorrectable hardware error"
230*12967Sgavin.maltby@oracle.com },
231*12967Sgavin.maltby@oracle.com
232*12967Sgavin.maltby@oracle.com /*
233*12967Sgavin.maltby@oracle.com * A transition 'online -> offline' due to a process in the service
234*12967Sgavin.maltby@oracle.com * having received a fatal signal originating from outside the
235*12967Sgavin.maltby@oracle.com * service process contract.
236*12967Sgavin.maltby@oracle.com */
237*12967Sgavin.maltby@oracle.com {
238*12967Sgavin.maltby@oracle.com restarter_str_ct_ev_signal,
239*12967Sgavin.maltby@oracle.com "ct_ev_signal",
240*12967Sgavin.maltby@oracle.com "a process received a fatal signal from outside the service"
241*12967Sgavin.maltby@oracle.com },
242*12967Sgavin.maltby@oracle.com
243*12967Sgavin.maltby@oracle.com /*
244*12967Sgavin.maltby@oracle.com * A transition 'offline -> online' when all dependencies for the
245*12967Sgavin.maltby@oracle.com * service have been met.
246*12967Sgavin.maltby@oracle.com */
247*12967Sgavin.maltby@oracle.com {
248*12967Sgavin.maltby@oracle.com restarter_str_dependencies_satisfied,
249*12967Sgavin.maltby@oracle.com "dependencies_satisfied",
250*12967Sgavin.maltby@oracle.com "all dependencies have been satisfied"
251*12967Sgavin.maltby@oracle.com },
252*12967Sgavin.maltby@oracle.com
253*12967Sgavin.maltby@oracle.com /*
254*12967Sgavin.maltby@oracle.com * A transition 'online -> offline' because some dependency for the
255*12967Sgavin.maltby@oracle.com * service is no-longer met.
256*12967Sgavin.maltby@oracle.com */
257*12967Sgavin.maltby@oracle.com {
258*12967Sgavin.maltby@oracle.com restarter_str_dependency_activity,
259*12967Sgavin.maltby@oracle.com "dependency_activity",
260*12967Sgavin.maltby@oracle.com "a dependency activity required a stop"
261*12967Sgavin.maltby@oracle.com },
262*12967Sgavin.maltby@oracle.com
263*12967Sgavin.maltby@oracle.com /*
264*12967Sgavin.maltby@oracle.com * A transition to maintenance state due to a cycle in the
265*12967Sgavin.maltby@oracle.com * service dependencies.
266*12967Sgavin.maltby@oracle.com */
267*12967Sgavin.maltby@oracle.com {
268*12967Sgavin.maltby@oracle.com restarter_str_dependency_cycle,
269*12967Sgavin.maltby@oracle.com "dependency_cycle",
270*12967Sgavin.maltby@oracle.com "a dependency cycle exists"
271*12967Sgavin.maltby@oracle.com },
272*12967Sgavin.maltby@oracle.com
273*12967Sgavin.maltby@oracle.com /*
274*12967Sgavin.maltby@oracle.com * A transition 'online -> offline -> disabled' due to a
275*12967Sgavin.maltby@oracle.com * 'svcadm disable [-t] <fmri>' or smf_disable_instance(3SCF) call.
276*12967Sgavin.maltby@oracle.com */
277*12967Sgavin.maltby@oracle.com {
278*12967Sgavin.maltby@oracle.com restarter_str_disable_request,
279*12967Sgavin.maltby@oracle.com "disable_request",
280*12967Sgavin.maltby@oracle.com "a disable was requested"
281*12967Sgavin.maltby@oracle.com },
282*12967Sgavin.maltby@oracle.com
283*12967Sgavin.maltby@oracle.com /*
284*12967Sgavin.maltby@oracle.com * A transition 'disabled -> offline' due to a
285*12967Sgavin.maltby@oracle.com * 'svcadm enable [-t] <fmri>' or smf_enable_instance(3SCF) call.
286*12967Sgavin.maltby@oracle.com */
287*12967Sgavin.maltby@oracle.com {
288*12967Sgavin.maltby@oracle.com restarter_str_enable_request,
289*12967Sgavin.maltby@oracle.com "enable_request",
290*12967Sgavin.maltby@oracle.com "an enable was requested"
291*12967Sgavin.maltby@oracle.com },
292*12967Sgavin.maltby@oracle.com
293*12967Sgavin.maltby@oracle.com /*
294*12967Sgavin.maltby@oracle.com * A transition to maintenance state when a method fails
295*12967Sgavin.maltby@oracle.com * repeatedly for a retryable reason.
296*12967Sgavin.maltby@oracle.com */
297*12967Sgavin.maltby@oracle.com {
298*12967Sgavin.maltby@oracle.com restarter_str_fault_threshold_reached,
299*12967Sgavin.maltby@oracle.com "fault_threshold_reached",
300*12967Sgavin.maltby@oracle.com "a method is failing in a retryable manner but too often"
301*12967Sgavin.maltby@oracle.com },
302*12967Sgavin.maltby@oracle.com
303*12967Sgavin.maltby@oracle.com /*
304*12967Sgavin.maltby@oracle.com * A transition to uninitialized state when startd reads the service
305*12967Sgavin.maltby@oracle.com * configuration and inserts it into the graph engine.
306*12967Sgavin.maltby@oracle.com */
307*12967Sgavin.maltby@oracle.com {
308*12967Sgavin.maltby@oracle.com restarter_str_insert_in_graph,
309*12967Sgavin.maltby@oracle.com "insert_in_graph",
310*12967Sgavin.maltby@oracle.com "the instance was inserted in the graph"
311*12967Sgavin.maltby@oracle.com },
312*12967Sgavin.maltby@oracle.com
313*12967Sgavin.maltby@oracle.com /*
314*12967Sgavin.maltby@oracle.com * A transition to maintenance state due to an invalid dependency
315*12967Sgavin.maltby@oracle.com * declared for the service.
316*12967Sgavin.maltby@oracle.com */
317*12967Sgavin.maltby@oracle.com {
318*12967Sgavin.maltby@oracle.com restarter_str_invalid_dependency,
319*12967Sgavin.maltby@oracle.com "invalid_dependency",
320*12967Sgavin.maltby@oracle.com "a service has an invalid dependency"
321*12967Sgavin.maltby@oracle.com },
322*12967Sgavin.maltby@oracle.com
323*12967Sgavin.maltby@oracle.com /*
324*12967Sgavin.maltby@oracle.com * A transition to maintenance state because the service-declared
325*12967Sgavin.maltby@oracle.com * restarter is invalid.
326*12967Sgavin.maltby@oracle.com */
327*12967Sgavin.maltby@oracle.com {
328*12967Sgavin.maltby@oracle.com restarter_str_invalid_restarter,
329*12967Sgavin.maltby@oracle.com "invalid_restarter",
330*12967Sgavin.maltby@oracle.com "the service restarter is invalid"
331*12967Sgavin.maltby@oracle.com },
332*12967Sgavin.maltby@oracle.com
333*12967Sgavin.maltby@oracle.com /*
334*12967Sgavin.maltby@oracle.com * A transition to maintenance state because a restarter method
335*12967Sgavin.maltby@oracle.com * exited with one of SMF_EXIT_ERR_CONFIG, SMF_EXIT_ERR_NOSMF,
336*12967Sgavin.maltby@oracle.com * SMF_EXIT_ERR_PERM, or SMF_EXIT_ERR_FATAL.
337*12967Sgavin.maltby@oracle.com */
338*12967Sgavin.maltby@oracle.com {
339*12967Sgavin.maltby@oracle.com restarter_str_method_failed,
340*12967Sgavin.maltby@oracle.com "method_failed",
341*12967Sgavin.maltby@oracle.com "a start, stop or refresh method failed"
342*12967Sgavin.maltby@oracle.com },
343*12967Sgavin.maltby@oracle.com
344*12967Sgavin.maltby@oracle.com /*
345*12967Sgavin.maltby@oracle.com * A transition 'uninitialized -> {disabled|offline}' after
346*12967Sgavin.maltby@oracle.com * "insert_in_graph" to match the state configured in the
347*12967Sgavin.maltby@oracle.com * repository.
348*12967Sgavin.maltby@oracle.com */
349*12967Sgavin.maltby@oracle.com {
350*12967Sgavin.maltby@oracle.com restarter_str_per_configuration,
351*12967Sgavin.maltby@oracle.com "per_configuration",
352*12967Sgavin.maltby@oracle.com "the SMF repository configuration specifies this state"
353*12967Sgavin.maltby@oracle.com },
354*12967Sgavin.maltby@oracle.com
355*12967Sgavin.maltby@oracle.com /*
356*12967Sgavin.maltby@oracle.com * Refresh requested - no state change.
357*12967Sgavin.maltby@oracle.com */
358*12967Sgavin.maltby@oracle.com {
359*12967Sgavin.maltby@oracle.com restarter_str_refresh,
360*12967Sgavin.maltby@oracle.com NULL,
361*12967Sgavin.maltby@oracle.com "a refresh was requested (no change of state)"
362*12967Sgavin.maltby@oracle.com },
363*12967Sgavin.maltby@oracle.com
364*12967Sgavin.maltby@oracle.com /*
365*12967Sgavin.maltby@oracle.com * A transition 'online -> offline -> online' due to a
366*12967Sgavin.maltby@oracle.com * 'svcadm restart <fmri> or equivlaent libscf API call.
367*12967Sgavin.maltby@oracle.com * Both the 'online -> offline' and 'offline -> online' transtions
368*12967Sgavin.maltby@oracle.com * specify this reason.
369*12967Sgavin.maltby@oracle.com */
370*12967Sgavin.maltby@oracle.com {
371*12967Sgavin.maltby@oracle.com restarter_str_restart_request,
372*12967Sgavin.maltby@oracle.com "restart_request",
373*12967Sgavin.maltby@oracle.com "a restart was requested"
374*12967Sgavin.maltby@oracle.com },
375*12967Sgavin.maltby@oracle.com
376*12967Sgavin.maltby@oracle.com /*
377*12967Sgavin.maltby@oracle.com * A transition to maintenance state because the start method is
378*12967Sgavin.maltby@oracle.com * being executed successfully but too frequently.
379*12967Sgavin.maltby@oracle.com */
380*12967Sgavin.maltby@oracle.com {
381*12967Sgavin.maltby@oracle.com restarter_str_restarting_too_quickly,
382*12967Sgavin.maltby@oracle.com "restarting_too_quickly",
383*12967Sgavin.maltby@oracle.com "the instance is restarting too quickly"
384*12967Sgavin.maltby@oracle.com },
385*12967Sgavin.maltby@oracle.com
386*12967Sgavin.maltby@oracle.com /*
387*12967Sgavin.maltby@oracle.com * A transition to maintenance state due a service requesting
388*12967Sgavin.maltby@oracle.com * 'svcadm mark maintenance <fmri>' or equivalent libscf API call.
389*12967Sgavin.maltby@oracle.com * A command line 'svcadm mark maintenance <fmri>' does not produce
390*12967Sgavin.maltby@oracle.com * this reason - it produces administrative_request instead.
391*12967Sgavin.maltby@oracle.com */
392*12967Sgavin.maltby@oracle.com {
393*12967Sgavin.maltby@oracle.com restarter_str_service_request,
394*12967Sgavin.maltby@oracle.com "service_request",
395*12967Sgavin.maltby@oracle.com "maintenance was requested by another service"
396*12967Sgavin.maltby@oracle.com },
397*12967Sgavin.maltby@oracle.com
398*12967Sgavin.maltby@oracle.com /*
399*12967Sgavin.maltby@oracle.com * An instanced inserted into the graph at its existing state
400*12967Sgavin.maltby@oracle.com * during a startd restart - no state change.
401*12967Sgavin.maltby@oracle.com */
402*12967Sgavin.maltby@oracle.com {
403*12967Sgavin.maltby@oracle.com restarter_str_startd_restart,
404*12967Sgavin.maltby@oracle.com NULL,
405*12967Sgavin.maltby@oracle.com "the instance was inserted in the graph due to startd restart"
406*12967Sgavin.maltby@oracle.com }
407*12967Sgavin.maltby@oracle.com };
408*12967Sgavin.maltby@oracle.com
409*12967Sgavin.maltby@oracle.com uint32_t
restarter_str_version(void)410*12967Sgavin.maltby@oracle.com restarter_str_version(void)
411*12967Sgavin.maltby@oracle.com {
412*12967Sgavin.maltby@oracle.com return (RESTARTER_STRING_VERSION);
413*12967Sgavin.maltby@oracle.com }
414*12967Sgavin.maltby@oracle.com
415*12967Sgavin.maltby@oracle.com const char *
restarter_get_str_short(restarter_str_t key)416*12967Sgavin.maltby@oracle.com restarter_get_str_short(restarter_str_t key)
417*12967Sgavin.maltby@oracle.com {
418*12967Sgavin.maltby@oracle.com int i;
419*12967Sgavin.maltby@oracle.com for (i = 0; i < sizeof (restarter_str) /
420*12967Sgavin.maltby@oracle.com sizeof (struct restarter_state_transition_reason); i++)
421*12967Sgavin.maltby@oracle.com if (key == restarter_str[i].str_key)
422*12967Sgavin.maltby@oracle.com return (restarter_str[i].str_short);
423*12967Sgavin.maltby@oracle.com return (NULL);
424*12967Sgavin.maltby@oracle.com }
425*12967Sgavin.maltby@oracle.com
426*12967Sgavin.maltby@oracle.com const char *
restarter_get_str_long(restarter_str_t key)427*12967Sgavin.maltby@oracle.com restarter_get_str_long(restarter_str_t key)
428*12967Sgavin.maltby@oracle.com {
429*12967Sgavin.maltby@oracle.com int i;
430*12967Sgavin.maltby@oracle.com for (i = 0; i < sizeof (restarter_str) /
431*12967Sgavin.maltby@oracle.com sizeof (struct restarter_state_transition_reason); i++)
432*12967Sgavin.maltby@oracle.com if (key == restarter_str[i].str_key)
433*12967Sgavin.maltby@oracle.com return (dgettext(TEXT_DOMAIN,
434*12967Sgavin.maltby@oracle.com restarter_str[i].str_long));
435*12967Sgavin.maltby@oracle.com return (NULL);
436*12967Sgavin.maltby@oracle.com }
437*12967Sgavin.maltby@oracle.com
438*12967Sgavin.maltby@oracle.com /*
4399765SSean.Wilcox@Sun.COM * A static no memory error message mc_error_t structure
4409765SSean.Wilcox@Sun.COM * to be used in cases when memory errors are to be returned
4419765SSean.Wilcox@Sun.COM * This avoids the need to attempt to allocate memory for the
4429765SSean.Wilcox@Sun.COM * message, therefore getting into a cycle of no memory failures.
4439765SSean.Wilcox@Sun.COM */
4449765SSean.Wilcox@Sun.COM mc_error_t mc_nomem_err = {
4459765SSean.Wilcox@Sun.COM 0, ENOMEM, sizeof ("Out of memory") - 1, "Out of memory"
4469765SSean.Wilcox@Sun.COM };
4479765SSean.Wilcox@Sun.COM
4480Sstevel@tonic-gate static const char * const allocfail = "Allocation failure.\n";
4490Sstevel@tonic-gate static const char * const rcbroken = "Repository connection broken.\n";
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate static int method_context_safety = 0; /* Can safely call pools/projects. */
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate int ndebug = 1;
4540Sstevel@tonic-gate
4559765SSean.Wilcox@Sun.COM /* PRINTFLIKE3 */
4569765SSean.Wilcox@Sun.COM static mc_error_t *
mc_error_create(mc_error_t * e,int type,const char * format,...)4579765SSean.Wilcox@Sun.COM mc_error_create(mc_error_t *e, int type, const char *format, ...)
4589765SSean.Wilcox@Sun.COM {
4599765SSean.Wilcox@Sun.COM mc_error_t *le;
4609765SSean.Wilcox@Sun.COM va_list args;
4619765SSean.Wilcox@Sun.COM int size;
4629765SSean.Wilcox@Sun.COM
4639765SSean.Wilcox@Sun.COM /*
4649765SSean.Wilcox@Sun.COM * If the type is ENOMEM and format is NULL, then
4659765SSean.Wilcox@Sun.COM * go ahead and return the default nomem error.
4669765SSean.Wilcox@Sun.COM * Otherwise, attempt to allocate the memory and if
4679765SSean.Wilcox@Sun.COM * that fails then there is no reason to continue.
4689765SSean.Wilcox@Sun.COM */
4699765SSean.Wilcox@Sun.COM if (type == ENOMEM && format == NULL)
4709765SSean.Wilcox@Sun.COM return (&mc_nomem_err);
4719765SSean.Wilcox@Sun.COM
4729765SSean.Wilcox@Sun.COM if (e == NULL && (le = malloc(sizeof (mc_error_t))) == NULL)
4739765SSean.Wilcox@Sun.COM return (&mc_nomem_err);
4749765SSean.Wilcox@Sun.COM else
4759765SSean.Wilcox@Sun.COM le = e;
4769765SSean.Wilcox@Sun.COM
4779765SSean.Wilcox@Sun.COM le->type = type;
4789765SSean.Wilcox@Sun.COM le->destroy = 1;
4799765SSean.Wilcox@Sun.COM va_start(args, format);
4809765SSean.Wilcox@Sun.COM size = vsnprintf(NULL, 0, format, args) + 1;
4819765SSean.Wilcox@Sun.COM if (size >= RESTARTER_ERRMSGSZ) {
4829765SSean.Wilcox@Sun.COM if ((le = realloc(e, sizeof (mc_error_t) +
4839765SSean.Wilcox@Sun.COM (size - RESTARTER_ERRMSGSZ))) == NULL) {
4849765SSean.Wilcox@Sun.COM size = RESTARTER_ERRMSGSZ - 1;
4859765SSean.Wilcox@Sun.COM le = e;
4869765SSean.Wilcox@Sun.COM }
4879765SSean.Wilcox@Sun.COM }
4889765SSean.Wilcox@Sun.COM
4899765SSean.Wilcox@Sun.COM le->size = size;
4909765SSean.Wilcox@Sun.COM (void) vsnprintf(le->msg, le->size, format, args);
4919765SSean.Wilcox@Sun.COM va_end(args);
4929765SSean.Wilcox@Sun.COM
4939765SSean.Wilcox@Sun.COM return (le);
4949765SSean.Wilcox@Sun.COM }
4959765SSean.Wilcox@Sun.COM
4969765SSean.Wilcox@Sun.COM void
restarter_mc_error_destroy(mc_error_t * mc_err)4979765SSean.Wilcox@Sun.COM restarter_mc_error_destroy(mc_error_t *mc_err)
4989765SSean.Wilcox@Sun.COM {
4999765SSean.Wilcox@Sun.COM if (mc_err == NULL)
5009765SSean.Wilcox@Sun.COM return;
5019765SSean.Wilcox@Sun.COM
5029765SSean.Wilcox@Sun.COM /*
5039765SSean.Wilcox@Sun.COM * If the error messages was allocated then free.
5049765SSean.Wilcox@Sun.COM */
5059765SSean.Wilcox@Sun.COM if (mc_err->destroy) {
5069765SSean.Wilcox@Sun.COM free(mc_err);
5079765SSean.Wilcox@Sun.COM }
5089765SSean.Wilcox@Sun.COM }
5099765SSean.Wilcox@Sun.COM
5100Sstevel@tonic-gate static void
free_restarter_event_handle(struct restarter_event_handle * h)5110Sstevel@tonic-gate free_restarter_event_handle(struct restarter_event_handle *h)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate if (h == NULL)
5140Sstevel@tonic-gate return;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate * Just free the memory -- don't unbind the sysevent handle,
5180Sstevel@tonic-gate * as otherwise events may be lost if this is just a restarter
5190Sstevel@tonic-gate * restart.
5200Sstevel@tonic-gate */
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate if (h->reh_restarter_name != NULL)
5230Sstevel@tonic-gate free(h->reh_restarter_name);
5240Sstevel@tonic-gate if (h->reh_delegate_channel_name != NULL)
5250Sstevel@tonic-gate free(h->reh_delegate_channel_name);
5260Sstevel@tonic-gate if (h->reh_delegate_subscriber_id != NULL)
5270Sstevel@tonic-gate free(h->reh_delegate_subscriber_id);
5280Sstevel@tonic-gate if (h->reh_master_channel_name != NULL)
5290Sstevel@tonic-gate free(h->reh_master_channel_name);
5300Sstevel@tonic-gate if (h->reh_master_subscriber_id != NULL)
5310Sstevel@tonic-gate free(h->reh_master_subscriber_id);
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate free(h);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate char *
_restarter_get_channel_name(const char * fmri,int type)5370Sstevel@tonic-gate _restarter_get_channel_name(const char *fmri, int type)
5380Sstevel@tonic-gate {
5399263SSean.Wilcox@Sun.COM char *name;
5400Sstevel@tonic-gate char *chan_name = malloc(MAX_CHNAME_LEN);
5410Sstevel@tonic-gate char prefix_name[3];
5429263SSean.Wilcox@Sun.COM int i;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate if (chan_name == NULL)
5450Sstevel@tonic-gate return (NULL);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate if (type == RESTARTER_CHANNEL_DELEGATE)
5480Sstevel@tonic-gate (void) strcpy(prefix_name, "d_");
5490Sstevel@tonic-gate else if (type == RESTARTER_CHANNEL_MASTER)
5500Sstevel@tonic-gate (void) strcpy(prefix_name, "m_");
5510Sstevel@tonic-gate else {
5520Sstevel@tonic-gate free(chan_name);
5530Sstevel@tonic-gate return (NULL);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5569263SSean.Wilcox@Sun.COM /*
5579263SSean.Wilcox@Sun.COM * Create a unique name
5589263SSean.Wilcox@Sun.COM *
5599263SSean.Wilcox@Sun.COM * Use the entire name, using a replacement of the /
5609263SSean.Wilcox@Sun.COM * characters to get a better name.
5619263SSean.Wilcox@Sun.COM *
5629263SSean.Wilcox@Sun.COM * Remove the svc:/ from the beginning as this really
5639263SSean.Wilcox@Sun.COM * isn't going to provide any uniqueness...
5649263SSean.Wilcox@Sun.COM *
5659263SSean.Wilcox@Sun.COM * An fmri name greater than MAX_CHNAME_LEN is going
5669263SSean.Wilcox@Sun.COM * to be rejected as too long for the chan_name below
5679263SSean.Wilcox@Sun.COM * in the snprintf call.
5689263SSean.Wilcox@Sun.COM */
5699263SSean.Wilcox@Sun.COM if ((name = strdup(strchr(fmri, '/') + 1)) == NULL) {
5709263SSean.Wilcox@Sun.COM free(chan_name);
5719263SSean.Wilcox@Sun.COM return (NULL);
5729263SSean.Wilcox@Sun.COM }
5739263SSean.Wilcox@Sun.COM i = 0;
5749263SSean.Wilcox@Sun.COM while (name[i]) {
5759263SSean.Wilcox@Sun.COM if (name[i] == '/') {
5769263SSean.Wilcox@Sun.COM name[i] = '_';
5779263SSean.Wilcox@Sun.COM }
5789263SSean.Wilcox@Sun.COM
5799263SSean.Wilcox@Sun.COM i++;
5809263SSean.Wilcox@Sun.COM }
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate * Should check for [a-z],[A-Z],[0-9],.,_,-,:
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (snprintf(chan_name, MAX_CHNAME_LEN, "com.sun:scf:%s%s",
5870Sstevel@tonic-gate prefix_name, name) > MAX_CHNAME_LEN) {
5880Sstevel@tonic-gate free(chan_name);
5899263SSean.Wilcox@Sun.COM chan_name = NULL;
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate
5929263SSean.Wilcox@Sun.COM free(name);
5930Sstevel@tonic-gate return (chan_name);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate int
cb(sysevent_t * syse,void * cookie)5970Sstevel@tonic-gate cb(sysevent_t *syse, void *cookie)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate restarter_event_handle_t *h = (restarter_event_handle_t *)cookie;
6000Sstevel@tonic-gate restarter_event_t *e;
6010Sstevel@tonic-gate nvlist_t *attr_list = NULL;
6020Sstevel@tonic-gate int ret = 0;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate e = uu_zalloc(sizeof (restarter_event_t));
6050Sstevel@tonic-gate if (e == NULL)
6060Sstevel@tonic-gate uu_die(allocfail);
6070Sstevel@tonic-gate e->re_event_handle = h;
6080Sstevel@tonic-gate e->re_sysevent = syse;
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate if (sysevent_get_attr_list(syse, &attr_list) != 0)
6110Sstevel@tonic-gate uu_die(allocfail);
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate if ((nvlist_lookup_uint32(attr_list, RESTARTER_NAME_TYPE,
6140Sstevel@tonic-gate &(e->re_type)) != 0) ||
6150Sstevel@tonic-gate (nvlist_lookup_string(attr_list,
6160Sstevel@tonic-gate RESTARTER_NAME_INSTANCE, &(e->re_instance_name)) != 0)) {
6170Sstevel@tonic-gate uu_warn("%s: Can't decode nvlist for event %p\n",
6180Sstevel@tonic-gate h->reh_restarter_name, (void *)syse);
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate ret = 0;
6210Sstevel@tonic-gate } else {
6220Sstevel@tonic-gate ret = h->reh_handler(e);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate uu_free(e);
6260Sstevel@tonic-gate nvlist_free(attr_list);
6270Sstevel@tonic-gate return (ret);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate * restarter_bind_handle(uint32_t, char *, int (*)(restarter_event_t *), int,
6320Sstevel@tonic-gate * restarter_event_handle_t **)
6330Sstevel@tonic-gate *
6340Sstevel@tonic-gate * Bind to a delegated restarter event channel.
6350Sstevel@tonic-gate * Each delegated restarter gets its own channel for resource management.
6360Sstevel@tonic-gate *
6370Sstevel@tonic-gate * Returns 0 on success or
6380Sstevel@tonic-gate * ENOTSUP version mismatch
6390Sstevel@tonic-gate * EINVAL restarter_name or event_handle is NULL
6400Sstevel@tonic-gate * ENOMEM out of memory, too many channels, or too many subscriptions
6410Sstevel@tonic-gate * EBUSY sysevent_evc_bind() could not establish binding
6420Sstevel@tonic-gate * EFAULT internal sysevent_evc_bind()/sysevent_evc_subscribe() error
6430Sstevel@tonic-gate * EMFILE out of file descriptors
6440Sstevel@tonic-gate * EPERM insufficient privilege for sysevent_evc_bind()
6450Sstevel@tonic-gate * EEXIST already subscribed
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate int
restarter_bind_handle(uint32_t version,const char * restarter_name,int (* event_handler)(restarter_event_t *),int flags,restarter_event_handle_t ** rehp)6480Sstevel@tonic-gate restarter_bind_handle(uint32_t version, const char *restarter_name,
6490Sstevel@tonic-gate int (*event_handler)(restarter_event_t *), int flags,
6500Sstevel@tonic-gate restarter_event_handle_t **rehp)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate restarter_event_handle_t *h;
6530Sstevel@tonic-gate size_t sz;
6540Sstevel@tonic-gate int err;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate if (version != RESTARTER_EVENT_VERSION)
6570Sstevel@tonic-gate return (ENOTSUP);
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate if (restarter_name == NULL || event_handler == NULL)
6600Sstevel@tonic-gate return (EINVAL);
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate if (flags & RESTARTER_FLAG_DEBUG)
6630Sstevel@tonic-gate ndebug++;
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate if ((h = uu_zalloc(sizeof (restarter_event_handle_t))) == NULL)
6660Sstevel@tonic-gate return (ENOMEM);
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate h->reh_delegate_subscriber_id = malloc(MAX_SUBID_LEN);
6690Sstevel@tonic-gate h->reh_master_subscriber_id = malloc(MAX_SUBID_LEN);
6700Sstevel@tonic-gate h->reh_restarter_name = strdup(restarter_name);
6710Sstevel@tonic-gate if (h->reh_delegate_subscriber_id == NULL ||
6720Sstevel@tonic-gate h->reh_master_subscriber_id == NULL ||
6730Sstevel@tonic-gate h->reh_restarter_name == NULL) {
6740Sstevel@tonic-gate free_restarter_event_handle(h);
6750Sstevel@tonic-gate return (ENOMEM);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate sz = strlcpy(h->reh_delegate_subscriber_id, "del", MAX_SUBID_LEN);
6790Sstevel@tonic-gate assert(sz < MAX_SUBID_LEN);
6800Sstevel@tonic-gate sz = strlcpy(h->reh_master_subscriber_id, "master", MAX_SUBID_LEN);
6810Sstevel@tonic-gate assert(sz < MAX_SUBID_LEN);
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate h->reh_delegate_channel_name =
6840Sstevel@tonic-gate _restarter_get_channel_name(restarter_name,
6850Sstevel@tonic-gate RESTARTER_CHANNEL_DELEGATE);
6860Sstevel@tonic-gate h->reh_master_channel_name =
6870Sstevel@tonic-gate _restarter_get_channel_name(restarter_name,
6880Sstevel@tonic-gate RESTARTER_CHANNEL_MASTER);
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate if (h->reh_delegate_channel_name == NULL ||
6910Sstevel@tonic-gate h->reh_master_channel_name == NULL) {
6920Sstevel@tonic-gate free_restarter_event_handle(h);
6930Sstevel@tonic-gate return (ENOMEM);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate if (sysevent_evc_bind(h->reh_delegate_channel_name,
6970Sstevel@tonic-gate &h->reh_delegate_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
6980Sstevel@tonic-gate err = errno;
6990Sstevel@tonic-gate assert(err != EINVAL);
7000Sstevel@tonic-gate assert(err != ENOENT);
7010Sstevel@tonic-gate free_restarter_event_handle(h);
7020Sstevel@tonic-gate return (err);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate if (sysevent_evc_bind(h->reh_master_channel_name,
7060Sstevel@tonic-gate &h->reh_master_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
7070Sstevel@tonic-gate err = errno;
7080Sstevel@tonic-gate assert(err != EINVAL);
7090Sstevel@tonic-gate assert(err != ENOENT);
7100Sstevel@tonic-gate free_restarter_event_handle(h);
7110Sstevel@tonic-gate return (err);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate h->reh_handler = event_handler;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate assert(strlen(restarter_name) <= MAX_CLASS_LEN - 1);
7170Sstevel@tonic-gate assert(strlen(h->reh_delegate_subscriber_id) <= MAX_SUBID_LEN - 1);
7180Sstevel@tonic-gate assert(strlen(h->reh_master_subscriber_id) <= MAX_SUBID_LEN - 1);
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate if (sysevent_evc_subscribe(h->reh_delegate_channel,
7210Sstevel@tonic-gate h->reh_delegate_subscriber_id, EC_ALL, cb, h, EVCH_SUB_KEEP) != 0) {
7220Sstevel@tonic-gate err = errno;
7230Sstevel@tonic-gate assert(err != EINVAL);
7240Sstevel@tonic-gate free_restarter_event_handle(h);
7250Sstevel@tonic-gate return (err);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate *rehp = h;
7290Sstevel@tonic-gate return (0);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate restarter_event_handle_t *
restarter_event_get_handle(restarter_event_t * e)7330Sstevel@tonic-gate restarter_event_get_handle(restarter_event_t *e)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate assert(e != NULL && e->re_event_handle != NULL);
7360Sstevel@tonic-gate return (e->re_event_handle);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate restarter_event_type_t
restarter_event_get_type(restarter_event_t * e)7400Sstevel@tonic-gate restarter_event_get_type(restarter_event_t *e)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate assert(e != NULL);
7430Sstevel@tonic-gate return (e->re_type);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate ssize_t
restarter_event_get_instance(restarter_event_t * e,char * inst,size_t sz)7470Sstevel@tonic-gate restarter_event_get_instance(restarter_event_t *e, char *inst, size_t sz)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate assert(e != NULL && inst != NULL);
7500Sstevel@tonic-gate return ((ssize_t)strlcpy(inst, e->re_instance_name, sz));
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate int
restarter_event_get_current_states(restarter_event_t * e,restarter_instance_state_t * state,restarter_instance_state_t * next_state)7540Sstevel@tonic-gate restarter_event_get_current_states(restarter_event_t *e,
7550Sstevel@tonic-gate restarter_instance_state_t *state, restarter_instance_state_t *next_state)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate if (e == NULL)
7580Sstevel@tonic-gate return (-1);
7590Sstevel@tonic-gate *state = e->re_state;
7600Sstevel@tonic-gate *next_state = e->re_next_state;
7610Sstevel@tonic-gate return (0);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate /*
7656045Srm88369 * restarter_event_publish_retry() is a wrapper around sysevent_evc_publish().
7666045Srm88369 * In case, the event cannot be sent at the first attempt (sysevent_evc_publish
7676045Srm88369 * returned EAGAIN - sysevent queue full), this function retries a few time
7686045Srm88369 * and return ENOSPC if it reaches the retry limit.
7696045Srm88369 *
7706045Srm88369 * The arguments to this function map the arguments of sysevent_evc_publish().
7716045Srm88369 *
7726045Srm88369 * On success, return 0. On error, return
7736045Srm88369 *
7746045Srm88369 * EFAULT - internal sysevent_evc_publish() error
7756045Srm88369 * ENOMEM - internal sysevent_evc_publish() error
7766045Srm88369 * EBADF - scp is invalid (sysevent_evc_publish() returned EINVAL)
7776045Srm88369 * ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
7786045Srm88369 */
7796045Srm88369 int
restarter_event_publish_retry(evchan_t * scp,const char * class,const char * subclass,const char * vendor,const char * pub_name,nvlist_t * attr_list,uint32_t flags)7806045Srm88369 restarter_event_publish_retry(evchan_t *scp, const char *class,
7816045Srm88369 const char *subclass, const char *vendor, const char *pub_name,
7826045Srm88369 nvlist_t *attr_list, uint32_t flags)
7836045Srm88369 {
7846045Srm88369 int retries, ret;
7856045Srm88369 useconds_t retry_int = INITIAL_COMMIT_RETRY_INT;
7866045Srm88369
7876045Srm88369 for (retries = 0; retries < MAX_COMMIT_RETRIES; retries++) {
7886045Srm88369 ret = sysevent_evc_publish(scp, class, subclass, vendor,
7896045Srm88369 pub_name, attr_list, flags);
7906045Srm88369 if (ret == 0)
7916045Srm88369 break;
7926045Srm88369
7936045Srm88369 switch (ret) {
7946045Srm88369 case EAGAIN:
7956045Srm88369 /* Queue is full */
7966045Srm88369 (void) usleep(retry_int);
7976045Srm88369
7986045Srm88369 retry_int = min(retry_int * 2, MAX_COMMIT_RETRY_INT);
7996045Srm88369 break;
8006045Srm88369
8016045Srm88369 case EINVAL:
8026045Srm88369 ret = EBADF;
8036045Srm88369 /* FALLTHROUGH */
8046045Srm88369
8056045Srm88369 case EFAULT:
8066045Srm88369 case ENOMEM:
8076045Srm88369 return (ret);
8086045Srm88369
8096045Srm88369 case EOVERFLOW:
8106045Srm88369 default:
8116045Srm88369 /* internal error - abort */
8126045Srm88369 bad_fail("sysevent_evc_publish", ret);
8136045Srm88369 }
8146045Srm88369 }
8156045Srm88369
8166045Srm88369 if (retries == MAX_COMMIT_RETRIES)
8176045Srm88369 ret = ENOSPC;
8186045Srm88369
8196045Srm88369 return (ret);
8206045Srm88369 }
8216045Srm88369
8226045Srm88369 /*
8230Sstevel@tonic-gate * Commit the state, next state, and auxiliary state into the repository.
8240Sstevel@tonic-gate * Let the graph engine know about the state change and error. On success,
8250Sstevel@tonic-gate * return 0. On error, return
8260Sstevel@tonic-gate * EPROTO - librestart compiled against different libscf
8270Sstevel@tonic-gate * ENOMEM - out of memory
8280Sstevel@tonic-gate * - repository server out of resources
8290Sstevel@tonic-gate * ENOTACTIVE - repository server not running
8300Sstevel@tonic-gate * ECONNABORTED - repository connection established, but then broken
8310Sstevel@tonic-gate * - unknown libscf error
8320Sstevel@tonic-gate * ENOENT - inst does not exist in the repository
8330Sstevel@tonic-gate * EPERM - insufficient permissions
8340Sstevel@tonic-gate * EACCESS - backend access denied
8350Sstevel@tonic-gate * EROFS - backend is readonly
8360Sstevel@tonic-gate * EFAULT - internal sysevent_evc_publish() error
8370Sstevel@tonic-gate * EBADF - h is invalid (sysevent_evc_publish() returned EINVAL)
8386045Srm88369 * ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
8390Sstevel@tonic-gate */
8400Sstevel@tonic-gate int
restarter_set_states(restarter_event_handle_t * h,const char * inst,restarter_instance_state_t cur_state,restarter_instance_state_t new_cur_state,restarter_instance_state_t next_state,restarter_instance_state_t new_next_state,restarter_error_t e,restarter_str_t aux)8410Sstevel@tonic-gate restarter_set_states(restarter_event_handle_t *h, const char *inst,
8420Sstevel@tonic-gate restarter_instance_state_t cur_state,
8430Sstevel@tonic-gate restarter_instance_state_t new_cur_state,
8440Sstevel@tonic-gate restarter_instance_state_t next_state,
8450Sstevel@tonic-gate restarter_instance_state_t new_next_state, restarter_error_t e,
846*12967Sgavin.maltby@oracle.com restarter_str_t aux)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate nvlist_t *attr;
8490Sstevel@tonic-gate scf_handle_t *scf_h;
8500Sstevel@tonic-gate instance_data_t id;
8510Sstevel@tonic-gate int ret = 0;
852*12967Sgavin.maltby@oracle.com const char *p = restarter_get_str_short(aux);
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate assert(h->reh_master_channel != NULL);
8550Sstevel@tonic-gate assert(h->reh_master_channel_name != NULL);
8560Sstevel@tonic-gate assert(h->reh_master_subscriber_id != NULL);
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate if ((scf_h = scf_handle_create(SCF_VERSION)) == NULL) {
8590Sstevel@tonic-gate switch (scf_error()) {
8600Sstevel@tonic-gate case SCF_ERROR_VERSION_MISMATCH:
8610Sstevel@tonic-gate return (EPROTO);
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate case SCF_ERROR_NO_MEMORY:
8640Sstevel@tonic-gate return (ENOMEM);
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate default:
8670Sstevel@tonic-gate bad_fail("scf_handle_create", scf_error());
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate if (scf_handle_bind(scf_h) == -1) {
8720Sstevel@tonic-gate scf_handle_destroy(scf_h);
8730Sstevel@tonic-gate switch (scf_error()) {
8740Sstevel@tonic-gate case SCF_ERROR_NO_SERVER:
8750Sstevel@tonic-gate return (ENOTACTIVE);
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate case SCF_ERROR_NO_RESOURCES:
8780Sstevel@tonic-gate return (ENOMEM);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
8810Sstevel@tonic-gate case SCF_ERROR_IN_USE:
8820Sstevel@tonic-gate default:
8830Sstevel@tonic-gate bad_fail("scf_handle_bind", scf_error());
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
8880Sstevel@tonic-gate nvlist_add_int32(attr, RESTARTER_NAME_STATE, new_cur_state) != 0 ||
8890Sstevel@tonic-gate nvlist_add_int32(attr, RESTARTER_NAME_NEXT_STATE, new_next_state)
8900Sstevel@tonic-gate != 0 ||
8910Sstevel@tonic-gate nvlist_add_int32(attr, RESTARTER_NAME_ERROR, e) != 0 ||
892*12967Sgavin.maltby@oracle.com nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, inst) != 0 ||
893*12967Sgavin.maltby@oracle.com nvlist_add_int32(attr, RESTARTER_NAME_REASON, aux) != 0) {
8940Sstevel@tonic-gate ret = ENOMEM;
8956045Srm88369 } else {
8966045Srm88369 id.i_fmri = inst;
8976045Srm88369 id.i_state = cur_state;
8986045Srm88369 id.i_next_state = next_state;
8996045Srm88369
9006045Srm88369 ret = _restarter_commit_states(scf_h, &id, new_cur_state,
901*12967Sgavin.maltby@oracle.com new_next_state, p);
9026045Srm88369
9036045Srm88369 if (ret == 0) {
9046045Srm88369 ret = restarter_event_publish_retry(
9056045Srm88369 h->reh_master_channel, "master", "state_change",
9066045Srm88369 "com.sun", "librestart", attr, EVCH_NOSLEEP);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate nvlist_free(attr);
9110Sstevel@tonic-gate (void) scf_handle_unbind(scf_h);
9120Sstevel@tonic-gate scf_handle_destroy(scf_h);
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate return (ret);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate restarter_instance_state_t
restarter_string_to_state(char * string)9180Sstevel@tonic-gate restarter_string_to_state(char *string)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate assert(string != NULL);
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate if (strcmp(string, SCF_STATE_STRING_NONE) == 0)
9230Sstevel@tonic-gate return (RESTARTER_STATE_NONE);
9240Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_UNINIT) == 0)
9250Sstevel@tonic-gate return (RESTARTER_STATE_UNINIT);
9260Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_MAINT) == 0)
9270Sstevel@tonic-gate return (RESTARTER_STATE_MAINT);
9280Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_OFFLINE) == 0)
9290Sstevel@tonic-gate return (RESTARTER_STATE_OFFLINE);
9300Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_DISABLED) == 0)
9310Sstevel@tonic-gate return (RESTARTER_STATE_DISABLED);
9320Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_ONLINE) == 0)
9330Sstevel@tonic-gate return (RESTARTER_STATE_ONLINE);
9340Sstevel@tonic-gate else if (strcmp(string, SCF_STATE_STRING_DEGRADED) == 0)
9350Sstevel@tonic-gate return (RESTARTER_STATE_DEGRADED);
9360Sstevel@tonic-gate else {
9370Sstevel@tonic-gate return (RESTARTER_STATE_NONE);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate ssize_t
restarter_state_to_string(restarter_instance_state_t state,char * string,size_t len)9420Sstevel@tonic-gate restarter_state_to_string(restarter_instance_state_t state, char *string,
9430Sstevel@tonic-gate size_t len)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate assert(string != NULL);
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate if (state == RESTARTER_STATE_NONE)
9480Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_NONE, len));
9490Sstevel@tonic-gate else if (state == RESTARTER_STATE_UNINIT)
9500Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_UNINIT, len));
9510Sstevel@tonic-gate else if (state == RESTARTER_STATE_MAINT)
9520Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_MAINT, len));
9530Sstevel@tonic-gate else if (state == RESTARTER_STATE_OFFLINE)
9540Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_OFFLINE,
9550Sstevel@tonic-gate len));
9560Sstevel@tonic-gate else if (state == RESTARTER_STATE_DISABLED)
9570Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DISABLED,
9580Sstevel@tonic-gate len));
9590Sstevel@tonic-gate else if (state == RESTARTER_STATE_ONLINE)
9600Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_ONLINE, len));
9610Sstevel@tonic-gate else if (state == RESTARTER_STATE_DEGRADED)
9620Sstevel@tonic-gate return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DEGRADED,
9630Sstevel@tonic-gate len));
9640Sstevel@tonic-gate else
9650Sstevel@tonic-gate return ((ssize_t)strlcpy(string, "unknown", len));
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate * Sets pg to the name property group of s_inst. If it doesn't exist, it is
9700Sstevel@tonic-gate * added.
9710Sstevel@tonic-gate *
9720Sstevel@tonic-gate * Fails with
9730Sstevel@tonic-gate * ECONNABORTED - repository disconnection or unknown libscf error
9740Sstevel@tonic-gate * EBADF - inst is not set
9750Sstevel@tonic-gate * ECANCELED - inst is deleted
9760Sstevel@tonic-gate * EPERM - permission is denied
9770Sstevel@tonic-gate * EACCES - backend denied access
9780Sstevel@tonic-gate * EROFS - backend readonly
9790Sstevel@tonic-gate */
9800Sstevel@tonic-gate static int
instance_get_or_add_pg(scf_instance_t * inst,const char * name,const char * type,uint32_t flags,scf_propertygroup_t * pg)9810Sstevel@tonic-gate instance_get_or_add_pg(scf_instance_t *inst, const char *name,
9820Sstevel@tonic-gate const char *type, uint32_t flags, scf_propertygroup_t *pg)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate again:
9850Sstevel@tonic-gate if (scf_instance_get_pg(inst, name, pg) == 0)
9860Sstevel@tonic-gate return (0);
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate switch (scf_error()) {
9890Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
9900Sstevel@tonic-gate default:
9910Sstevel@tonic-gate return (ECONNABORTED);
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
9940Sstevel@tonic-gate return (EBADF);
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate case SCF_ERROR_DELETED:
9970Sstevel@tonic-gate return (ECANCELED);
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
10000Sstevel@tonic-gate break;
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
10030Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
10040Sstevel@tonic-gate bad_fail("scf_instance_get_pg", scf_error());
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
10080Sstevel@tonic-gate return (0);
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate switch (scf_error()) {
10110Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
10120Sstevel@tonic-gate default:
10130Sstevel@tonic-gate return (ECONNABORTED);
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate case SCF_ERROR_DELETED:
10160Sstevel@tonic-gate return (ECANCELED);
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate case SCF_ERROR_EXISTS:
10190Sstevel@tonic-gate goto again;
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
10220Sstevel@tonic-gate return (EPERM);
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
10250Sstevel@tonic-gate return (EACCES);
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
10280Sstevel@tonic-gate return (EROFS);
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
10310Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
10320Sstevel@tonic-gate case SCF_ERROR_NOT_SET: /* should be caught above */
10330Sstevel@tonic-gate bad_fail("scf_instance_add_pg", scf_error());
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate return (0);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate * Fails with
10410Sstevel@tonic-gate * ECONNABORTED
10420Sstevel@tonic-gate * ECANCELED - pg was deleted
10430Sstevel@tonic-gate */
10440Sstevel@tonic-gate static int
tx_set_value(scf_transaction_t * tx,scf_transaction_entry_t * ent,const char * pname,scf_type_t ty,scf_value_t * val)10450Sstevel@tonic-gate tx_set_value(scf_transaction_t *tx, scf_transaction_entry_t *ent,
10460Sstevel@tonic-gate const char *pname, scf_type_t ty, scf_value_t *val)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate int r;
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate for (;;) {
10510Sstevel@tonic-gate if (scf_transaction_property_change_type(tx, ent, pname,
10520Sstevel@tonic-gate ty) == 0)
10530Sstevel@tonic-gate break;
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate switch (scf_error()) {
10560Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
10570Sstevel@tonic-gate default:
10580Sstevel@tonic-gate return (ECONNABORTED);
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate case SCF_ERROR_DELETED:
10610Sstevel@tonic-gate return (ECANCELED);
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
10640Sstevel@tonic-gate break;
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
10670Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
10680Sstevel@tonic-gate case SCF_ERROR_IN_USE:
10690Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
10700Sstevel@tonic-gate bad_fail("scf_transaction_property_change_type",
10710Sstevel@tonic-gate scf_error());
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
10750Sstevel@tonic-gate break;
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate switch (scf_error()) {
10780Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
10790Sstevel@tonic-gate default:
10800Sstevel@tonic-gate return (ECONNABORTED);
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate case SCF_ERROR_DELETED:
10830Sstevel@tonic-gate return (ECANCELED);
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate case SCF_ERROR_EXISTS:
10860Sstevel@tonic-gate break;
10870Sstevel@tonic-gate
10880Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
10890Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
10900Sstevel@tonic-gate case SCF_ERROR_IN_USE:
10910Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
10920Sstevel@tonic-gate bad_fail("scf_transaction_property_new", scf_error());
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate r = scf_entry_add_value(ent, val);
10970Sstevel@tonic-gate assert(r == 0);
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate return (0);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate * Commit new_state, new_next_state, and aux to the repository for id. If
11040Sstevel@tonic-gate * successful, also set id's state and next-state as given, and return 0.
11050Sstevel@tonic-gate * Fails with
11060Sstevel@tonic-gate * ENOMEM - out of memory
11070Sstevel@tonic-gate * ECONNABORTED - repository connection broken
11080Sstevel@tonic-gate * - unknown libscf error
11090Sstevel@tonic-gate * EINVAL - id->i_fmri is invalid or not an instance FMRI
11100Sstevel@tonic-gate * ENOENT - id->i_fmri does not exist
11110Sstevel@tonic-gate * EPERM - insufficient permissions
11120Sstevel@tonic-gate * EACCES - backend access denied
11130Sstevel@tonic-gate * EROFS - backend is readonly
11140Sstevel@tonic-gate */
11150Sstevel@tonic-gate int
_restarter_commit_states(scf_handle_t * h,instance_data_t * id,restarter_instance_state_t new_state,restarter_instance_state_t new_state_next,const char * aux)11160Sstevel@tonic-gate _restarter_commit_states(scf_handle_t *h, instance_data_t *id,
11170Sstevel@tonic-gate restarter_instance_state_t new_state,
11180Sstevel@tonic-gate restarter_instance_state_t new_state_next, const char *aux)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate char str_state[MAX_SCF_STATE_STRING_SZ];
11210Sstevel@tonic-gate char str_new_state[MAX_SCF_STATE_STRING_SZ];
11220Sstevel@tonic-gate char str_state_next[MAX_SCF_STATE_STRING_SZ];
11230Sstevel@tonic-gate char str_new_state_next[MAX_SCF_STATE_STRING_SZ];
11240Sstevel@tonic-gate int ret = 0, r;
11250Sstevel@tonic-gate struct timeval now;
11260Sstevel@tonic-gate ssize_t sz;
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate scf_transaction_t *t = NULL;
11290Sstevel@tonic-gate scf_transaction_entry_t *t_state = NULL, *t_state_next = NULL;
11300Sstevel@tonic-gate scf_transaction_entry_t *t_stime = NULL, *t_aux = NULL;
11310Sstevel@tonic-gate scf_value_t *v_state = NULL, *v_state_next = NULL, *v_stime = NULL;
11320Sstevel@tonic-gate scf_value_t *v_aux = NULL;
11330Sstevel@tonic-gate scf_instance_t *s_inst = NULL;
11340Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
11350Sstevel@tonic-gate
11360Sstevel@tonic-gate assert(new_state != RESTARTER_STATE_NONE);
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate if ((s_inst = scf_instance_create(h)) == NULL ||
11390Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL ||
11400Sstevel@tonic-gate (t = scf_transaction_create(h)) == NULL ||
11410Sstevel@tonic-gate (t_state = scf_entry_create(h)) == NULL ||
11420Sstevel@tonic-gate (t_state_next = scf_entry_create(h)) == NULL ||
11430Sstevel@tonic-gate (t_stime = scf_entry_create(h)) == NULL ||
11440Sstevel@tonic-gate (t_aux = scf_entry_create(h)) == NULL ||
11450Sstevel@tonic-gate (v_state = scf_value_create(h)) == NULL ||
11460Sstevel@tonic-gate (v_state_next = scf_value_create(h)) == NULL ||
11470Sstevel@tonic-gate (v_stime = scf_value_create(h)) == NULL ||
11480Sstevel@tonic-gate (v_aux = scf_value_create(h)) == NULL) {
11490Sstevel@tonic-gate ret = ENOMEM;
11500Sstevel@tonic-gate goto out;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate sz = restarter_state_to_string(new_state, str_new_state,
11540Sstevel@tonic-gate sizeof (str_new_state));
11550Sstevel@tonic-gate assert(sz < sizeof (str_new_state));
11560Sstevel@tonic-gate sz = restarter_state_to_string(new_state_next, str_new_state_next,
11570Sstevel@tonic-gate sizeof (str_new_state_next));
11580Sstevel@tonic-gate assert(sz < sizeof (str_new_state_next));
11590Sstevel@tonic-gate sz = restarter_state_to_string(id->i_state, str_state,
11600Sstevel@tonic-gate sizeof (str_state));
11610Sstevel@tonic-gate assert(sz < sizeof (str_state));
11620Sstevel@tonic-gate sz = restarter_state_to_string(id->i_next_state, str_state_next,
11630Sstevel@tonic-gate sizeof (str_state_next));
11640Sstevel@tonic-gate assert(sz < sizeof (str_state_next));
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate ret = gettimeofday(&now, NULL);
11670Sstevel@tonic-gate assert(ret != -1);
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate if (scf_handle_decode_fmri(h, id->i_fmri, NULL, NULL, s_inst,
11700Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
11710Sstevel@tonic-gate switch (scf_error()) {
11720Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
11730Sstevel@tonic-gate default:
11740Sstevel@tonic-gate ret = ECONNABORTED;
11750Sstevel@tonic-gate break;
11760Sstevel@tonic-gate
11770Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
11780Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
11790Sstevel@tonic-gate ret = EINVAL;
11800Sstevel@tonic-gate break;
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
11830Sstevel@tonic-gate ret = ENOENT;
11840Sstevel@tonic-gate break;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
11870Sstevel@tonic-gate bad_fail("scf_handle_decode_fmri", scf_error());
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate goto out;
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate
11930Sstevel@tonic-gate if (scf_value_set_astring(v_state, str_new_state) != 0 ||
11948823STruong.Q.Nguyen@Sun.COM scf_value_set_astring(v_state_next, str_new_state_next) != 0)
11950Sstevel@tonic-gate bad_fail("scf_value_set_astring", scf_error());
11960Sstevel@tonic-gate
11978823STruong.Q.Nguyen@Sun.COM if (aux) {
11988823STruong.Q.Nguyen@Sun.COM if (scf_value_set_astring(v_aux, aux) != 0)
11998823STruong.Q.Nguyen@Sun.COM bad_fail("scf_value_set_astring", scf_error());
12008823STruong.Q.Nguyen@Sun.COM }
12018823STruong.Q.Nguyen@Sun.COM
12020Sstevel@tonic-gate if (scf_value_set_time(v_stime, now.tv_sec, now.tv_usec * 1000) != 0)
12030Sstevel@tonic-gate bad_fail("scf_value_set_time", scf_error());
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate add_pg:
12060Sstevel@tonic-gate switch (r = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
12070Sstevel@tonic-gate SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg)) {
12080Sstevel@tonic-gate case 0:
12090Sstevel@tonic-gate break;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate case ECONNABORTED:
12120Sstevel@tonic-gate case EPERM:
12130Sstevel@tonic-gate case EACCES:
12140Sstevel@tonic-gate case EROFS:
12150Sstevel@tonic-gate ret = r;
12160Sstevel@tonic-gate goto out;
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate case ECANCELED:
12190Sstevel@tonic-gate ret = ENOENT;
12200Sstevel@tonic-gate goto out;
12210Sstevel@tonic-gate
12220Sstevel@tonic-gate case EBADF:
12230Sstevel@tonic-gate default:
12240Sstevel@tonic-gate bad_fail("instance_get_or_add_pg", r);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate for (;;) {
12280Sstevel@tonic-gate if (scf_transaction_start(t, pg) != 0) {
12290Sstevel@tonic-gate switch (scf_error()) {
12300Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
12310Sstevel@tonic-gate default:
12320Sstevel@tonic-gate ret = ECONNABORTED;
12330Sstevel@tonic-gate goto out;
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
12360Sstevel@tonic-gate goto add_pg;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
12390Sstevel@tonic-gate ret = EPERM;
12400Sstevel@tonic-gate goto out;
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
12430Sstevel@tonic-gate ret = EACCES;
12440Sstevel@tonic-gate goto out;
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
12470Sstevel@tonic-gate ret = EROFS;
12480Sstevel@tonic-gate goto out;
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
12510Sstevel@tonic-gate case SCF_ERROR_IN_USE:
12520Sstevel@tonic-gate bad_fail("scf_transaction_start", scf_error());
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate if ((r = tx_set_value(t, t_state, SCF_PROPERTY_STATE,
12570Sstevel@tonic-gate SCF_TYPE_ASTRING, v_state)) != 0 ||
12580Sstevel@tonic-gate (r = tx_set_value(t, t_state_next, SCF_PROPERTY_NEXT_STATE,
12590Sstevel@tonic-gate SCF_TYPE_ASTRING, v_state_next)) != 0 ||
12600Sstevel@tonic-gate (r = tx_set_value(t, t_stime, SCF_PROPERTY_STATE_TIMESTAMP,
12610Sstevel@tonic-gate SCF_TYPE_TIME, v_stime)) != 0) {
12620Sstevel@tonic-gate switch (r) {
12630Sstevel@tonic-gate case ECONNABORTED:
12640Sstevel@tonic-gate ret = ECONNABORTED;
12650Sstevel@tonic-gate goto out;
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate case ECANCELED:
12680Sstevel@tonic-gate scf_transaction_reset(t);
12690Sstevel@tonic-gate goto add_pg;
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate default:
12720Sstevel@tonic-gate bad_fail("tx_set_value", r);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate
12768823STruong.Q.Nguyen@Sun.COM if (aux) {
12778823STruong.Q.Nguyen@Sun.COM if ((r = tx_set_value(t, t_aux, SCF_PROPERTY_AUX_STATE,
12788823STruong.Q.Nguyen@Sun.COM SCF_TYPE_ASTRING, v_aux)) != 0) {
12798823STruong.Q.Nguyen@Sun.COM switch (r) {
12808823STruong.Q.Nguyen@Sun.COM case ECONNABORTED:
12818823STruong.Q.Nguyen@Sun.COM ret = ECONNABORTED;
12828823STruong.Q.Nguyen@Sun.COM goto out;
12838823STruong.Q.Nguyen@Sun.COM
12848823STruong.Q.Nguyen@Sun.COM case ECANCELED:
12858823STruong.Q.Nguyen@Sun.COM scf_transaction_reset(t);
12868823STruong.Q.Nguyen@Sun.COM goto add_pg;
12878823STruong.Q.Nguyen@Sun.COM
12888823STruong.Q.Nguyen@Sun.COM default:
12898823STruong.Q.Nguyen@Sun.COM bad_fail("tx_set_value", r);
12908823STruong.Q.Nguyen@Sun.COM }
12918823STruong.Q.Nguyen@Sun.COM }
12928823STruong.Q.Nguyen@Sun.COM }
12938823STruong.Q.Nguyen@Sun.COM
12940Sstevel@tonic-gate ret = scf_transaction_commit(t);
12950Sstevel@tonic-gate if (ret == 1)
12960Sstevel@tonic-gate break;
12970Sstevel@tonic-gate if (ret == -1) {
12980Sstevel@tonic-gate switch (scf_error()) {
12990Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
13000Sstevel@tonic-gate default:
13010Sstevel@tonic-gate ret = ECONNABORTED;
13020Sstevel@tonic-gate goto out;
13030Sstevel@tonic-gate
13040Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
13050Sstevel@tonic-gate ret = EPERM;
13060Sstevel@tonic-gate goto out;
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
13090Sstevel@tonic-gate ret = EACCES;
13100Sstevel@tonic-gate goto out;
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
13130Sstevel@tonic-gate ret = EROFS;
13140Sstevel@tonic-gate goto out;
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
13170Sstevel@tonic-gate bad_fail("scf_transaction_commit", scf_error());
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate
13210Sstevel@tonic-gate scf_transaction_reset(t);
13220Sstevel@tonic-gate if (scf_pg_update(pg) == -1) {
13230Sstevel@tonic-gate switch (scf_error()) {
13240Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
13250Sstevel@tonic-gate default:
13260Sstevel@tonic-gate ret = ECONNABORTED;
13270Sstevel@tonic-gate goto out;
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
13300Sstevel@tonic-gate goto add_pg;
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate id->i_state = new_state;
13360Sstevel@tonic-gate id->i_next_state = new_state_next;
13370Sstevel@tonic-gate ret = 0;
13380Sstevel@tonic-gate
13390Sstevel@tonic-gate out:
13400Sstevel@tonic-gate scf_transaction_destroy(t);
13410Sstevel@tonic-gate scf_entry_destroy(t_state);
13420Sstevel@tonic-gate scf_entry_destroy(t_state_next);
13430Sstevel@tonic-gate scf_entry_destroy(t_stime);
13440Sstevel@tonic-gate scf_entry_destroy(t_aux);
13450Sstevel@tonic-gate scf_value_destroy(v_state);
13460Sstevel@tonic-gate scf_value_destroy(v_state_next);
13470Sstevel@tonic-gate scf_value_destroy(v_stime);
13480Sstevel@tonic-gate scf_value_destroy(v_aux);
13490Sstevel@tonic-gate scf_pg_destroy(pg);
13500Sstevel@tonic-gate scf_instance_destroy(s_inst);
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate return (ret);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate * Fails with
13570Sstevel@tonic-gate * EINVAL - type is invalid
13580Sstevel@tonic-gate * ENOMEM
13590Sstevel@tonic-gate * ECONNABORTED - repository connection broken
13600Sstevel@tonic-gate * EBADF - s_inst is not set
13610Sstevel@tonic-gate * ECANCELED - s_inst is deleted
13620Sstevel@tonic-gate * EPERM - permission denied
13630Sstevel@tonic-gate * EACCES - backend access denied
13640Sstevel@tonic-gate * EROFS - backend readonly
13650Sstevel@tonic-gate */
13660Sstevel@tonic-gate int
restarter_remove_contract(scf_instance_t * s_inst,ctid_t contract_id,restarter_contract_type_t type)13670Sstevel@tonic-gate restarter_remove_contract(scf_instance_t *s_inst, ctid_t contract_id,
13680Sstevel@tonic-gate restarter_contract_type_t type)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate scf_handle_t *h;
13710Sstevel@tonic-gate scf_transaction_t *t = NULL;
13720Sstevel@tonic-gate scf_transaction_entry_t *t_cid = NULL;
13730Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
13740Sstevel@tonic-gate scf_property_t *prop = NULL;
13750Sstevel@tonic-gate scf_value_t *val;
13760Sstevel@tonic-gate scf_iter_t *iter = NULL;
13770Sstevel@tonic-gate const char *pname;
13780Sstevel@tonic-gate int ret = 0, primary;
13790Sstevel@tonic-gate uint64_t c;
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate switch (type) {
13820Sstevel@tonic-gate case RESTARTER_CONTRACT_PRIMARY:
13830Sstevel@tonic-gate primary = 1;
13840Sstevel@tonic-gate break;
13850Sstevel@tonic-gate case RESTARTER_CONTRACT_TRANSIENT:
13860Sstevel@tonic-gate primary = 0;
13870Sstevel@tonic-gate break;
13880Sstevel@tonic-gate default:
13890Sstevel@tonic-gate return (EINVAL);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate
13920Sstevel@tonic-gate h = scf_instance_handle(s_inst);
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate pg = scf_pg_create(h);
13950Sstevel@tonic-gate prop = scf_property_create(h);
13960Sstevel@tonic-gate iter = scf_iter_create(h);
13970Sstevel@tonic-gate t = scf_transaction_create(h);
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
14000Sstevel@tonic-gate ret = ENOMEM;
14010Sstevel@tonic-gate goto remove_contract_cleanup;
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate add:
14050Sstevel@tonic-gate scf_transaction_destroy_children(t);
14060Sstevel@tonic-gate ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
14070Sstevel@tonic-gate SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
14080Sstevel@tonic-gate if (ret != 0)
14090Sstevel@tonic-gate goto remove_contract_cleanup;
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate pname = primary? SCF_PROPERTY_CONTRACT :
14120Sstevel@tonic-gate SCF_PROPERTY_TRANSIENT_CONTRACT;
14130Sstevel@tonic-gate
14140Sstevel@tonic-gate for (;;) {
14150Sstevel@tonic-gate if (scf_transaction_start(t, pg) != 0) {
14160Sstevel@tonic-gate switch (scf_error()) {
14170Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
14180Sstevel@tonic-gate default:
14190Sstevel@tonic-gate ret = ECONNABORTED;
14200Sstevel@tonic-gate goto remove_contract_cleanup;
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate case SCF_ERROR_DELETED:
14230Sstevel@tonic-gate goto add;
14240Sstevel@tonic-gate
14250Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
14260Sstevel@tonic-gate ret = EPERM;
14270Sstevel@tonic-gate goto remove_contract_cleanup;
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
14300Sstevel@tonic-gate ret = EACCES;
14310Sstevel@tonic-gate goto remove_contract_cleanup;
14320Sstevel@tonic-gate
14330Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
14340Sstevel@tonic-gate ret = EROFS;
14350Sstevel@tonic-gate goto remove_contract_cleanup;
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
14380Sstevel@tonic-gate case SCF_ERROR_IN_USE:
14390Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
14400Sstevel@tonic-gate bad_fail("scf_transaction_start", scf_error());
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate t_cid = scf_entry_create(h);
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate if (scf_pg_get_property(pg, pname, prop) == 0) {
14470Sstevel@tonic-gate replace:
14480Sstevel@tonic-gate if (scf_transaction_property_change_type(t, t_cid,
14490Sstevel@tonic-gate pname, SCF_TYPE_COUNT) != 0) {
14500Sstevel@tonic-gate switch (scf_error()) {
14510Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
14520Sstevel@tonic-gate default:
14530Sstevel@tonic-gate ret = ECONNABORTED;
14540Sstevel@tonic-gate goto remove_contract_cleanup;
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate case SCF_ERROR_DELETED:
14570Sstevel@tonic-gate scf_entry_destroy(t_cid);
14580Sstevel@tonic-gate goto add;
14590Sstevel@tonic-gate
14600Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
14610Sstevel@tonic-gate goto new;
14620Sstevel@tonic-gate
14630Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
14640Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
14650Sstevel@tonic-gate case SCF_ERROR_IN_USE:
14660Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
14670Sstevel@tonic-gate bad_fail(
14680Sstevel@tonic-gate "scf_transaction_property_changetype",
14690Sstevel@tonic-gate scf_error());
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
14740Sstevel@tonic-gate if (scf_iter_property_values(iter, prop) != 0) {
14750Sstevel@tonic-gate switch (scf_error()) {
14760Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
14770Sstevel@tonic-gate default:
14780Sstevel@tonic-gate ret = ECONNABORTED;
14790Sstevel@tonic-gate goto remove_contract_cleanup;
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
14820Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
14830Sstevel@tonic-gate bad_fail(
14840Sstevel@tonic-gate "scf_iter_property_values",
14850Sstevel@tonic-gate scf_error());
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate
14890Sstevel@tonic-gate next_val:
14900Sstevel@tonic-gate val = scf_value_create(h);
14910Sstevel@tonic-gate if (val == NULL) {
14920Sstevel@tonic-gate assert(scf_error() ==
14930Sstevel@tonic-gate SCF_ERROR_NO_MEMORY);
14940Sstevel@tonic-gate ret = ENOMEM;
14950Sstevel@tonic-gate goto remove_contract_cleanup;
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate ret = scf_iter_next_value(iter, val);
14990Sstevel@tonic-gate if (ret == -1) {
15000Sstevel@tonic-gate switch (scf_error()) {
15010Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
15020Sstevel@tonic-gate ret = ECONNABORTED;
15030Sstevel@tonic-gate goto remove_contract_cleanup;
15040Sstevel@tonic-gate
15050Sstevel@tonic-gate case SCF_ERROR_DELETED:
15060Sstevel@tonic-gate scf_value_destroy(val);
15070Sstevel@tonic-gate goto add;
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
15100Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
15115040Swesolows case SCF_ERROR_PERMISSION_DENIED:
15120Sstevel@tonic-gate default:
15130Sstevel@tonic-gate bad_fail("scf_iter_next_value",
15140Sstevel@tonic-gate scf_error());
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate if (ret == 1) {
15190Sstevel@tonic-gate ret = scf_value_get_count(val, &c);
15200Sstevel@tonic-gate assert(ret == 0);
15210Sstevel@tonic-gate
15220Sstevel@tonic-gate if (c != contract_id) {
15230Sstevel@tonic-gate ret = scf_entry_add_value(t_cid,
15240Sstevel@tonic-gate val);
15250Sstevel@tonic-gate assert(ret == 0);
15260Sstevel@tonic-gate } else {
15270Sstevel@tonic-gate scf_value_destroy(val);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate
15300Sstevel@tonic-gate goto next_val;
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate scf_value_destroy(val);
15340Sstevel@tonic-gate } else {
15350Sstevel@tonic-gate switch (scf_error()) {
15360Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
15370Sstevel@tonic-gate default:
15380Sstevel@tonic-gate ret = ECONNABORTED;
15390Sstevel@tonic-gate goto remove_contract_cleanup;
15400Sstevel@tonic-gate
15410Sstevel@tonic-gate case SCF_ERROR_TYPE_MISMATCH:
15420Sstevel@tonic-gate break;
15430Sstevel@tonic-gate
15440Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
15450Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
15460Sstevel@tonic-gate bad_fail("scf_property_is_type",
15470Sstevel@tonic-gate scf_error());
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate } else {
15510Sstevel@tonic-gate switch (scf_error()) {
15520Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
15530Sstevel@tonic-gate default:
15540Sstevel@tonic-gate ret = ECONNABORTED;
15550Sstevel@tonic-gate goto remove_contract_cleanup;
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate case SCF_ERROR_DELETED:
15580Sstevel@tonic-gate scf_entry_destroy(t_cid);
15590Sstevel@tonic-gate goto add;
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
15620Sstevel@tonic-gate break;
15630Sstevel@tonic-gate
15640Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
15650Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
15660Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
15670Sstevel@tonic-gate bad_fail("scf_pg_get_property", scf_error());
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate
15700Sstevel@tonic-gate new:
15710Sstevel@tonic-gate if (scf_transaction_property_new(t, t_cid, pname,
15720Sstevel@tonic-gate SCF_TYPE_COUNT) != 0) {
15730Sstevel@tonic-gate switch (scf_error()) {
15740Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
15750Sstevel@tonic-gate default:
15760Sstevel@tonic-gate ret = ECONNABORTED;
15770Sstevel@tonic-gate goto remove_contract_cleanup;
15780Sstevel@tonic-gate
15790Sstevel@tonic-gate case SCF_ERROR_DELETED:
15800Sstevel@tonic-gate scf_entry_destroy(t_cid);
15810Sstevel@tonic-gate goto add;
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate case SCF_ERROR_EXISTS:
15840Sstevel@tonic-gate goto replace;
15850Sstevel@tonic-gate
15860Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
15870Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
15880Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
15890Sstevel@tonic-gate bad_fail("scf_transaction_property_new",
15900Sstevel@tonic-gate scf_error());
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate ret = scf_transaction_commit(t);
15960Sstevel@tonic-gate if (ret == -1) {
15970Sstevel@tonic-gate switch (scf_error()) {
15980Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
15990Sstevel@tonic-gate default:
16000Sstevel@tonic-gate ret = ECONNABORTED;
16010Sstevel@tonic-gate goto remove_contract_cleanup;
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate case SCF_ERROR_DELETED:
16040Sstevel@tonic-gate goto add;
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
16070Sstevel@tonic-gate ret = EPERM;
16080Sstevel@tonic-gate goto remove_contract_cleanup;
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
16110Sstevel@tonic-gate ret = EACCES;
16120Sstevel@tonic-gate goto remove_contract_cleanup;
16130Sstevel@tonic-gate
16140Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
16150Sstevel@tonic-gate ret = EROFS;
16160Sstevel@tonic-gate goto remove_contract_cleanup;
16170Sstevel@tonic-gate
16180Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
16190Sstevel@tonic-gate bad_fail("scf_transaction_commit", scf_error());
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate if (ret == 1) {
16230Sstevel@tonic-gate ret = 0;
16240Sstevel@tonic-gate break;
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate
16270Sstevel@tonic-gate scf_transaction_destroy_children(t);
16280Sstevel@tonic-gate if (scf_pg_update(pg) == -1) {
16290Sstevel@tonic-gate switch (scf_error()) {
16300Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
16310Sstevel@tonic-gate default:
16320Sstevel@tonic-gate ret = ECONNABORTED;
16330Sstevel@tonic-gate goto remove_contract_cleanup;
16340Sstevel@tonic-gate
16350Sstevel@tonic-gate case SCF_ERROR_DELETED:
16360Sstevel@tonic-gate goto add;
16370Sstevel@tonic-gate
16380Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
16390Sstevel@tonic-gate bad_fail("scf_pg_update", scf_error());
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate remove_contract_cleanup:
16450Sstevel@tonic-gate scf_transaction_destroy_children(t);
16460Sstevel@tonic-gate scf_transaction_destroy(t);
16470Sstevel@tonic-gate scf_iter_destroy(iter);
16480Sstevel@tonic-gate scf_property_destroy(prop);
16490Sstevel@tonic-gate scf_pg_destroy(pg);
16500Sstevel@tonic-gate
16510Sstevel@tonic-gate return (ret);
16520Sstevel@tonic-gate }
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate /*
16550Sstevel@tonic-gate * Fails with
16560Sstevel@tonic-gate * EINVAL - type is invalid
16570Sstevel@tonic-gate * ENOMEM
16580Sstevel@tonic-gate * ECONNABORTED - repository disconnection
16590Sstevel@tonic-gate * EBADF - s_inst is not set
16600Sstevel@tonic-gate * ECANCELED - s_inst is deleted
16610Sstevel@tonic-gate * EPERM
16620Sstevel@tonic-gate * EACCES
16630Sstevel@tonic-gate * EROFS
16640Sstevel@tonic-gate */
16650Sstevel@tonic-gate int
restarter_store_contract(scf_instance_t * s_inst,ctid_t contract_id,restarter_contract_type_t type)16660Sstevel@tonic-gate restarter_store_contract(scf_instance_t *s_inst, ctid_t contract_id,
16670Sstevel@tonic-gate restarter_contract_type_t type)
16680Sstevel@tonic-gate {
16690Sstevel@tonic-gate scf_handle_t *h;
16700Sstevel@tonic-gate scf_transaction_t *t = NULL;
16710Sstevel@tonic-gate scf_transaction_entry_t *t_cid = NULL;
16720Sstevel@tonic-gate scf_value_t *val;
16730Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
16740Sstevel@tonic-gate scf_property_t *prop = NULL;
16750Sstevel@tonic-gate scf_iter_t *iter = NULL;
16760Sstevel@tonic-gate const char *pname;
16770Sstevel@tonic-gate int ret = 0, primary;
16780Sstevel@tonic-gate
16790Sstevel@tonic-gate if (type == RESTARTER_CONTRACT_PRIMARY)
16800Sstevel@tonic-gate primary = 1;
16810Sstevel@tonic-gate else if (type == RESTARTER_CONTRACT_TRANSIENT)
16820Sstevel@tonic-gate primary = 0;
16830Sstevel@tonic-gate else
16840Sstevel@tonic-gate return (EINVAL);
16850Sstevel@tonic-gate
16860Sstevel@tonic-gate h = scf_instance_handle(s_inst);
16870Sstevel@tonic-gate
16880Sstevel@tonic-gate pg = scf_pg_create(h);
16890Sstevel@tonic-gate prop = scf_property_create(h);
16900Sstevel@tonic-gate iter = scf_iter_create(h);
16910Sstevel@tonic-gate t = scf_transaction_create(h);
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
16940Sstevel@tonic-gate ret = ENOMEM;
16950Sstevel@tonic-gate goto out;
16960Sstevel@tonic-gate }
16970Sstevel@tonic-gate
16980Sstevel@tonic-gate add:
16990Sstevel@tonic-gate scf_transaction_destroy_children(t);
17000Sstevel@tonic-gate ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
17010Sstevel@tonic-gate SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
17020Sstevel@tonic-gate if (ret != 0)
17030Sstevel@tonic-gate goto out;
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate pname = primary ? SCF_PROPERTY_CONTRACT :
17060Sstevel@tonic-gate SCF_PROPERTY_TRANSIENT_CONTRACT;
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate for (;;) {
17090Sstevel@tonic-gate if (scf_transaction_start(t, pg) != 0) {
17100Sstevel@tonic-gate switch (scf_error()) {
17110Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
17120Sstevel@tonic-gate default:
17130Sstevel@tonic-gate ret = ECONNABORTED;
17140Sstevel@tonic-gate goto out;
17150Sstevel@tonic-gate
17160Sstevel@tonic-gate case SCF_ERROR_DELETED:
17170Sstevel@tonic-gate goto add;
17180Sstevel@tonic-gate
17190Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
17200Sstevel@tonic-gate ret = EPERM;
17210Sstevel@tonic-gate goto out;
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
17240Sstevel@tonic-gate ret = EACCES;
17250Sstevel@tonic-gate goto out;
17260Sstevel@tonic-gate
17270Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
17280Sstevel@tonic-gate ret = EROFS;
17290Sstevel@tonic-gate goto out;
17300Sstevel@tonic-gate
17310Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
17320Sstevel@tonic-gate case SCF_ERROR_IN_USE:
17330Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
17340Sstevel@tonic-gate bad_fail("scf_transaction_start", scf_error());
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate }
17370Sstevel@tonic-gate
17380Sstevel@tonic-gate t_cid = scf_entry_create(h);
17390Sstevel@tonic-gate if (t_cid == NULL) {
17400Sstevel@tonic-gate ret = ENOMEM;
17410Sstevel@tonic-gate goto out;
17420Sstevel@tonic-gate }
17430Sstevel@tonic-gate
17440Sstevel@tonic-gate if (scf_pg_get_property(pg, pname, prop) == 0) {
17450Sstevel@tonic-gate replace:
17460Sstevel@tonic-gate if (scf_transaction_property_change_type(t, t_cid,
17470Sstevel@tonic-gate pname, SCF_TYPE_COUNT) != 0) {
17480Sstevel@tonic-gate switch (scf_error()) {
17490Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
17500Sstevel@tonic-gate default:
17510Sstevel@tonic-gate ret = ECONNABORTED;
17520Sstevel@tonic-gate goto out;
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate case SCF_ERROR_DELETED:
17550Sstevel@tonic-gate scf_entry_destroy(t_cid);
17560Sstevel@tonic-gate goto add;
17570Sstevel@tonic-gate
17580Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
17590Sstevel@tonic-gate goto new;
17600Sstevel@tonic-gate
17610Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
17620Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
17630Sstevel@tonic-gate case SCF_ERROR_IN_USE:
17640Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
17650Sstevel@tonic-gate bad_fail(
17660Sstevel@tonic-gate "scf_transaction_propert_change_type",
17670Sstevel@tonic-gate scf_error());
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate
17710Sstevel@tonic-gate if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
17720Sstevel@tonic-gate if (scf_iter_property_values(iter, prop) != 0) {
17730Sstevel@tonic-gate switch (scf_error()) {
17740Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
17750Sstevel@tonic-gate default:
17760Sstevel@tonic-gate ret = ECONNABORTED;
17770Sstevel@tonic-gate goto out;
17780Sstevel@tonic-gate
17790Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
17800Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
17810Sstevel@tonic-gate bad_fail(
17820Sstevel@tonic-gate "scf_iter_property_values",
17830Sstevel@tonic-gate scf_error());
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate
17870Sstevel@tonic-gate next_val:
17880Sstevel@tonic-gate val = scf_value_create(h);
17890Sstevel@tonic-gate if (val == NULL) {
17900Sstevel@tonic-gate assert(scf_error() ==
17910Sstevel@tonic-gate SCF_ERROR_NO_MEMORY);
17920Sstevel@tonic-gate ret = ENOMEM;
17930Sstevel@tonic-gate goto out;
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate
17960Sstevel@tonic-gate ret = scf_iter_next_value(iter, val);
17970Sstevel@tonic-gate if (ret == -1) {
17980Sstevel@tonic-gate switch (scf_error()) {
17990Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
18000Sstevel@tonic-gate default:
18010Sstevel@tonic-gate ret = ECONNABORTED;
18020Sstevel@tonic-gate goto out;
18030Sstevel@tonic-gate
18040Sstevel@tonic-gate case SCF_ERROR_DELETED:
18050Sstevel@tonic-gate scf_value_destroy(val);
18060Sstevel@tonic-gate goto add;
18070Sstevel@tonic-gate
18080Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
18090Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
18105040Swesolows case SCF_ERROR_PERMISSION_DENIED:
18110Sstevel@tonic-gate bad_fail(
18120Sstevel@tonic-gate "scf_iter_next_value",
18130Sstevel@tonic-gate scf_error());
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate
18170Sstevel@tonic-gate if (ret == 1) {
18180Sstevel@tonic-gate ret = scf_entry_add_value(t_cid, val);
18190Sstevel@tonic-gate assert(ret == 0);
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate goto next_val;
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate scf_value_destroy(val);
18250Sstevel@tonic-gate } else {
18260Sstevel@tonic-gate switch (scf_error()) {
18270Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
18280Sstevel@tonic-gate default:
18290Sstevel@tonic-gate ret = ECONNABORTED;
18300Sstevel@tonic-gate goto out;
18310Sstevel@tonic-gate
18320Sstevel@tonic-gate case SCF_ERROR_TYPE_MISMATCH:
18330Sstevel@tonic-gate break;
18340Sstevel@tonic-gate
18350Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
18360Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
18370Sstevel@tonic-gate bad_fail("scf_property_is_type",
18380Sstevel@tonic-gate scf_error());
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate } else {
18420Sstevel@tonic-gate switch (scf_error()) {
18430Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
18440Sstevel@tonic-gate default:
18450Sstevel@tonic-gate ret = ECONNABORTED;
18460Sstevel@tonic-gate goto out;
18470Sstevel@tonic-gate
18480Sstevel@tonic-gate case SCF_ERROR_DELETED:
18490Sstevel@tonic-gate scf_entry_destroy(t_cid);
18500Sstevel@tonic-gate goto add;
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
18530Sstevel@tonic-gate break;
18540Sstevel@tonic-gate
18550Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
18560Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
18570Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
18580Sstevel@tonic-gate bad_fail("scf_pg_get_property", scf_error());
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate new:
18620Sstevel@tonic-gate if (scf_transaction_property_new(t, t_cid, pname,
18630Sstevel@tonic-gate SCF_TYPE_COUNT) != 0) {
18640Sstevel@tonic-gate switch (scf_error()) {
18650Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
18660Sstevel@tonic-gate default:
18670Sstevel@tonic-gate ret = ECONNABORTED;
18680Sstevel@tonic-gate goto out;
18690Sstevel@tonic-gate
18700Sstevel@tonic-gate case SCF_ERROR_DELETED:
18710Sstevel@tonic-gate scf_entry_destroy(t_cid);
18720Sstevel@tonic-gate goto add;
18730Sstevel@tonic-gate
18740Sstevel@tonic-gate case SCF_ERROR_EXISTS:
18750Sstevel@tonic-gate goto replace;
18760Sstevel@tonic-gate
18770Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
18780Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
18790Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
18800Sstevel@tonic-gate bad_fail("scf_transaction_property_new",
18810Sstevel@tonic-gate scf_error());
18820Sstevel@tonic-gate }
18830Sstevel@tonic-gate }
18840Sstevel@tonic-gate }
18850Sstevel@tonic-gate
18860Sstevel@tonic-gate val = scf_value_create(h);
18870Sstevel@tonic-gate if (val == NULL) {
18880Sstevel@tonic-gate assert(scf_error() == SCF_ERROR_NO_MEMORY);
18890Sstevel@tonic-gate ret = ENOMEM;
18900Sstevel@tonic-gate goto out;
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate
18930Sstevel@tonic-gate scf_value_set_count(val, contract_id);
18940Sstevel@tonic-gate ret = scf_entry_add_value(t_cid, val);
18950Sstevel@tonic-gate assert(ret == 0);
18960Sstevel@tonic-gate
18970Sstevel@tonic-gate ret = scf_transaction_commit(t);
18980Sstevel@tonic-gate if (ret == -1) {
18990Sstevel@tonic-gate switch (scf_error()) {
19000Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
19010Sstevel@tonic-gate default:
19020Sstevel@tonic-gate ret = ECONNABORTED;
19030Sstevel@tonic-gate goto out;
19040Sstevel@tonic-gate
19050Sstevel@tonic-gate case SCF_ERROR_DELETED:
19060Sstevel@tonic-gate goto add;
19070Sstevel@tonic-gate
19080Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
19090Sstevel@tonic-gate ret = EPERM;
19100Sstevel@tonic-gate goto out;
19110Sstevel@tonic-gate
19120Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
19130Sstevel@tonic-gate ret = EACCES;
19140Sstevel@tonic-gate goto out;
19150Sstevel@tonic-gate
19160Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
19170Sstevel@tonic-gate ret = EROFS;
19180Sstevel@tonic-gate goto out;
19190Sstevel@tonic-gate
19200Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
19210Sstevel@tonic-gate bad_fail("scf_transaction_commit", scf_error());
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate }
19240Sstevel@tonic-gate if (ret == 1) {
19250Sstevel@tonic-gate ret = 0;
19260Sstevel@tonic-gate break;
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate
19290Sstevel@tonic-gate scf_transaction_destroy_children(t);
19300Sstevel@tonic-gate if (scf_pg_update(pg) == -1) {
19310Sstevel@tonic-gate switch (scf_error()) {
19320Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
19330Sstevel@tonic-gate default:
19340Sstevel@tonic-gate ret = ECONNABORTED;
19350Sstevel@tonic-gate goto out;
19360Sstevel@tonic-gate
19370Sstevel@tonic-gate case SCF_ERROR_DELETED:
19380Sstevel@tonic-gate goto add;
19390Sstevel@tonic-gate
19400Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
19410Sstevel@tonic-gate bad_fail("scf_pg_update", scf_error());
19420Sstevel@tonic-gate }
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate
19460Sstevel@tonic-gate out:
19470Sstevel@tonic-gate scf_transaction_destroy_children(t);
19480Sstevel@tonic-gate scf_transaction_destroy(t);
19490Sstevel@tonic-gate scf_iter_destroy(iter);
19500Sstevel@tonic-gate scf_property_destroy(prop);
19510Sstevel@tonic-gate scf_pg_destroy(pg);
19520Sstevel@tonic-gate
19530Sstevel@tonic-gate return (ret);
19540Sstevel@tonic-gate }
19550Sstevel@tonic-gate
19560Sstevel@tonic-gate int
restarter_rm_libs_loadable()19570Sstevel@tonic-gate restarter_rm_libs_loadable()
19580Sstevel@tonic-gate {
19590Sstevel@tonic-gate void *libhndl;
19600Sstevel@tonic-gate
19610Sstevel@tonic-gate if (method_context_safety)
19620Sstevel@tonic-gate return (1);
19630Sstevel@tonic-gate
19640Sstevel@tonic-gate if ((libhndl = dlopen("libpool.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
19650Sstevel@tonic-gate return (0);
19660Sstevel@tonic-gate
19670Sstevel@tonic-gate (void) dlclose(libhndl);
19680Sstevel@tonic-gate
19690Sstevel@tonic-gate if ((libhndl = dlopen("libproject.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
19700Sstevel@tonic-gate return (0);
19710Sstevel@tonic-gate
19720Sstevel@tonic-gate (void) dlclose(libhndl);
19730Sstevel@tonic-gate
19740Sstevel@tonic-gate method_context_safety = 1;
19750Sstevel@tonic-gate
19760Sstevel@tonic-gate return (1);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate
19790Sstevel@tonic-gate static int
get_astring_val(scf_propertygroup_t * pg,const char * name,char * buf,size_t bufsz,scf_property_t * prop,scf_value_t * val)19800Sstevel@tonic-gate get_astring_val(scf_propertygroup_t *pg, const char *name, char *buf,
19810Sstevel@tonic-gate size_t bufsz, scf_property_t *prop, scf_value_t *val)
19820Sstevel@tonic-gate {
19830Sstevel@tonic-gate ssize_t szret;
19840Sstevel@tonic-gate
19859263SSean.Wilcox@Sun.COM if (pg == NULL)
19869263SSean.Wilcox@Sun.COM return (-1);
19879263SSean.Wilcox@Sun.COM
19880Sstevel@tonic-gate if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
19890Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
19900Sstevel@tonic-gate uu_die(rcbroken);
19910Sstevel@tonic-gate return (-1);
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate
19940Sstevel@tonic-gate if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
19950Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
19960Sstevel@tonic-gate uu_die(rcbroken);
19970Sstevel@tonic-gate return (-1);
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate szret = scf_value_get_astring(val, buf, bufsz);
20010Sstevel@tonic-gate
20020Sstevel@tonic-gate return (szret >= 0 ? 0 : -1);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate
20058823STruong.Q.Nguyen@Sun.COM static int
get_boolean_val(scf_propertygroup_t * pg,const char * name,uint8_t * b,scf_property_t * prop,scf_value_t * val)20068823STruong.Q.Nguyen@Sun.COM get_boolean_val(scf_propertygroup_t *pg, const char *name, uint8_t *b,
20078823STruong.Q.Nguyen@Sun.COM scf_property_t *prop, scf_value_t *val)
20088823STruong.Q.Nguyen@Sun.COM {
20098823STruong.Q.Nguyen@Sun.COM if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
20108823STruong.Q.Nguyen@Sun.COM if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
20118823STruong.Q.Nguyen@Sun.COM uu_die(rcbroken);
20128823STruong.Q.Nguyen@Sun.COM return (-1);
20138823STruong.Q.Nguyen@Sun.COM }
20148823STruong.Q.Nguyen@Sun.COM
20158823STruong.Q.Nguyen@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
20168823STruong.Q.Nguyen@Sun.COM if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
20178823STruong.Q.Nguyen@Sun.COM uu_die(rcbroken);
20188823STruong.Q.Nguyen@Sun.COM return (-1);
20198823STruong.Q.Nguyen@Sun.COM }
20208823STruong.Q.Nguyen@Sun.COM
20218823STruong.Q.Nguyen@Sun.COM if (scf_value_get_boolean(val, b))
20228823STruong.Q.Nguyen@Sun.COM return (-1);
20238823STruong.Q.Nguyen@Sun.COM
20248823STruong.Q.Nguyen@Sun.COM return (0);
20258823STruong.Q.Nguyen@Sun.COM }
20268823STruong.Q.Nguyen@Sun.COM
20270Sstevel@tonic-gate /*
20280Sstevel@tonic-gate * Try to load mcp->pwd, if it isn't already.
20290Sstevel@tonic-gate * Fails with
20300Sstevel@tonic-gate * ENOMEM - malloc() failed
20310Sstevel@tonic-gate * ENOENT - no entry found
20320Sstevel@tonic-gate * EIO - I/O error
20330Sstevel@tonic-gate * EMFILE - process out of file descriptors
20340Sstevel@tonic-gate * ENFILE - system out of file handles
20350Sstevel@tonic-gate */
20360Sstevel@tonic-gate static int
lookup_pwd(struct method_context * mcp)20370Sstevel@tonic-gate lookup_pwd(struct method_context *mcp)
20380Sstevel@tonic-gate {
20390Sstevel@tonic-gate struct passwd *pwdp;
20400Sstevel@tonic-gate
20410Sstevel@tonic-gate if (mcp->pwbuf != NULL && mcp->pwd.pw_uid == mcp->uid)
20420Sstevel@tonic-gate return (0);
20430Sstevel@tonic-gate
20440Sstevel@tonic-gate if (mcp->pwbuf == NULL) {
20450Sstevel@tonic-gate mcp->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
20460Sstevel@tonic-gate assert(mcp->pwbufsz >= 0);
20470Sstevel@tonic-gate mcp->pwbuf = malloc(mcp->pwbufsz);
20480Sstevel@tonic-gate if (mcp->pwbuf == NULL)
20490Sstevel@tonic-gate return (ENOMEM);
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate
20520Sstevel@tonic-gate do {
20530Sstevel@tonic-gate errno = 0;
20540Sstevel@tonic-gate pwdp = getpwuid_r(mcp->uid, &mcp->pwd, mcp->pwbuf,
20550Sstevel@tonic-gate mcp->pwbufsz);
20560Sstevel@tonic-gate } while (pwdp == NULL && errno == EINTR);
20570Sstevel@tonic-gate if (pwdp != NULL)
20580Sstevel@tonic-gate return (0);
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate free(mcp->pwbuf);
20610Sstevel@tonic-gate mcp->pwbuf = NULL;
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate switch (errno) {
20640Sstevel@tonic-gate case 0:
20650Sstevel@tonic-gate default:
20660Sstevel@tonic-gate /*
20670Sstevel@tonic-gate * Until bug 5065780 is fixed, getpwuid_r() can fail with
20680Sstevel@tonic-gate * ENOENT, particularly on the miniroot. Since the
20690Sstevel@tonic-gate * documentation is inaccurate, we'll return ENOENT for unknown
20700Sstevel@tonic-gate * errors.
20710Sstevel@tonic-gate */
20720Sstevel@tonic-gate return (ENOENT);
20730Sstevel@tonic-gate
20740Sstevel@tonic-gate case EIO:
20750Sstevel@tonic-gate case EMFILE:
20760Sstevel@tonic-gate case ENFILE:
20770Sstevel@tonic-gate return (errno);
20780Sstevel@tonic-gate
20790Sstevel@tonic-gate case ERANGE:
20800Sstevel@tonic-gate bad_fail("getpwuid_r", errno);
20810Sstevel@tonic-gate /* NOTREACHED */
20820Sstevel@tonic-gate }
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate
20850Sstevel@tonic-gate /*
20860Sstevel@tonic-gate * Get the user id for str. Returns 0 on success or
20870Sstevel@tonic-gate * ERANGE the uid is too big
20880Sstevel@tonic-gate * EINVAL the string starts with a digit, but is not a valid uid
20890Sstevel@tonic-gate * ENOMEM out of memory
20900Sstevel@tonic-gate * ENOENT no passwd entry for str
20910Sstevel@tonic-gate * EIO an I/O error has occurred
20920Sstevel@tonic-gate * EMFILE/ENFILE out of file descriptors
20930Sstevel@tonic-gate */
20940Sstevel@tonic-gate int
get_uid(const char * str,struct method_context * ci,uid_t * uidp)20950Sstevel@tonic-gate get_uid(const char *str, struct method_context *ci, uid_t *uidp)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate if (isdigit(str[0])) {
20980Sstevel@tonic-gate uid_t uid;
20990Sstevel@tonic-gate char *cp;
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate errno = 0;
21020Sstevel@tonic-gate uid = strtol(str, &cp, 10);
21030Sstevel@tonic-gate
21040Sstevel@tonic-gate if (uid == 0 && errno != 0) {
21050Sstevel@tonic-gate assert(errno != EINVAL);
21060Sstevel@tonic-gate return (errno);
21070Sstevel@tonic-gate }
21080Sstevel@tonic-gate
21090Sstevel@tonic-gate for (; *cp != '\0'; ++cp)
21100Sstevel@tonic-gate if (*cp != ' ' || *cp != '\t')
21110Sstevel@tonic-gate return (EINVAL);
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate if (uid > UID_MAX)
21140Sstevel@tonic-gate return (EINVAL);
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate *uidp = uid;
21170Sstevel@tonic-gate return (0);
21180Sstevel@tonic-gate } else {
21190Sstevel@tonic-gate struct passwd *pwdp;
21200Sstevel@tonic-gate
21210Sstevel@tonic-gate if (ci->pwbuf == NULL) {
21220Sstevel@tonic-gate ci->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
21230Sstevel@tonic-gate ci->pwbuf = malloc(ci->pwbufsz);
21240Sstevel@tonic-gate if (ci->pwbuf == NULL)
21250Sstevel@tonic-gate return (ENOMEM);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate
21280Sstevel@tonic-gate do {
21290Sstevel@tonic-gate errno = 0;
21300Sstevel@tonic-gate pwdp =
21310Sstevel@tonic-gate getpwnam_r(str, &ci->pwd, ci->pwbuf, ci->pwbufsz);
21320Sstevel@tonic-gate } while (pwdp == NULL && errno == EINTR);
21330Sstevel@tonic-gate
21340Sstevel@tonic-gate if (pwdp != NULL) {
21350Sstevel@tonic-gate *uidp = ci->pwd.pw_uid;
21360Sstevel@tonic-gate return (0);
21370Sstevel@tonic-gate } else {
21380Sstevel@tonic-gate free(ci->pwbuf);
21390Sstevel@tonic-gate ci->pwbuf = NULL;
21400Sstevel@tonic-gate switch (errno) {
21410Sstevel@tonic-gate case 0:
21420Sstevel@tonic-gate return (ENOENT);
21430Sstevel@tonic-gate
21440Sstevel@tonic-gate case ENOENT:
21450Sstevel@tonic-gate case EIO:
21460Sstevel@tonic-gate case EMFILE:
21470Sstevel@tonic-gate case ENFILE:
21480Sstevel@tonic-gate return (errno);
21490Sstevel@tonic-gate
21500Sstevel@tonic-gate case ERANGE:
21510Sstevel@tonic-gate default:
21520Sstevel@tonic-gate bad_fail("getpwnam_r", errno);
21530Sstevel@tonic-gate /* NOTREACHED */
21540Sstevel@tonic-gate }
21550Sstevel@tonic-gate }
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate }
21580Sstevel@tonic-gate
21590Sstevel@tonic-gate gid_t
get_gid(const char * str)21600Sstevel@tonic-gate get_gid(const char *str)
21610Sstevel@tonic-gate {
21620Sstevel@tonic-gate if (isdigit(str[0])) {
21630Sstevel@tonic-gate gid_t gid;
21640Sstevel@tonic-gate char *cp;
21650Sstevel@tonic-gate
21660Sstevel@tonic-gate errno = 0;
21670Sstevel@tonic-gate gid = strtol(str, &cp, 10);
21680Sstevel@tonic-gate
21690Sstevel@tonic-gate if (gid == 0 && errno != 0)
21704321Scasper return ((gid_t)-1);
21710Sstevel@tonic-gate
21720Sstevel@tonic-gate for (; *cp != '\0'; ++cp)
21730Sstevel@tonic-gate if (*cp != ' ' || *cp != '\t')
21744321Scasper return ((gid_t)-1);
21750Sstevel@tonic-gate
21760Sstevel@tonic-gate return (gid);
21770Sstevel@tonic-gate } else {
21780Sstevel@tonic-gate struct group grp, *ret;
21790Sstevel@tonic-gate char *buffer;
21800Sstevel@tonic-gate size_t buflen;
21810Sstevel@tonic-gate
21820Sstevel@tonic-gate buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
21830Sstevel@tonic-gate buffer = malloc(buflen);
21840Sstevel@tonic-gate if (buffer == NULL)
21850Sstevel@tonic-gate uu_die(allocfail);
21860Sstevel@tonic-gate
21870Sstevel@tonic-gate errno = 0;
21880Sstevel@tonic-gate ret = getgrnam_r(str, &grp, buffer, buflen);
21890Sstevel@tonic-gate free(buffer);
21900Sstevel@tonic-gate
21914321Scasper return (ret == NULL ? (gid_t)-1 : grp.gr_gid);
21920Sstevel@tonic-gate }
21930Sstevel@tonic-gate }
21940Sstevel@tonic-gate
21950Sstevel@tonic-gate /*
21960Sstevel@tonic-gate * Fails with
21970Sstevel@tonic-gate * ENOMEM - out of memory
21980Sstevel@tonic-gate * ENOENT - no passwd entry
21990Sstevel@tonic-gate * no project entry
22000Sstevel@tonic-gate * EIO - an I/O error occurred
22010Sstevel@tonic-gate * EMFILE - the process is out of file descriptors
22020Sstevel@tonic-gate * ENFILE - the system is out of file handles
22030Sstevel@tonic-gate * ERANGE - the project id is out of range
22040Sstevel@tonic-gate * EINVAL - str is invalid
22050Sstevel@tonic-gate * E2BIG - the project entry was too big
22060Sstevel@tonic-gate * -1 - the name service switch is misconfigured
22070Sstevel@tonic-gate */
22080Sstevel@tonic-gate int
get_projid(const char * str,struct method_context * cip)22090Sstevel@tonic-gate get_projid(const char *str, struct method_context *cip)
22100Sstevel@tonic-gate {
22110Sstevel@tonic-gate int ret;
22120Sstevel@tonic-gate void *buf;
22130Sstevel@tonic-gate const size_t bufsz = PROJECT_BUFSZ;
22140Sstevel@tonic-gate struct project proj, *pp;
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate if (strcmp(str, ":default") == 0) {
22170Sstevel@tonic-gate if (cip->uid == 0) {
22180Sstevel@tonic-gate /* Don't change project for root services */
22190Sstevel@tonic-gate cip->project = NULL;
22200Sstevel@tonic-gate return (0);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate
22230Sstevel@tonic-gate switch (ret = lookup_pwd(cip)) {
22240Sstevel@tonic-gate case 0:
22250Sstevel@tonic-gate break;
22260Sstevel@tonic-gate
22270Sstevel@tonic-gate case ENOMEM:
22280Sstevel@tonic-gate case ENOENT:
22290Sstevel@tonic-gate case EIO:
22300Sstevel@tonic-gate case EMFILE:
22310Sstevel@tonic-gate case ENFILE:
22320Sstevel@tonic-gate return (ret);
22330Sstevel@tonic-gate
22340Sstevel@tonic-gate default:
22350Sstevel@tonic-gate bad_fail("lookup_pwd", ret);
22360Sstevel@tonic-gate }
22370Sstevel@tonic-gate
22380Sstevel@tonic-gate buf = malloc(bufsz);
22390Sstevel@tonic-gate if (buf == NULL)
22400Sstevel@tonic-gate return (ENOMEM);
22410Sstevel@tonic-gate
22420Sstevel@tonic-gate do {
22430Sstevel@tonic-gate errno = 0;
22440Sstevel@tonic-gate pp = getdefaultproj(cip->pwd.pw_name, &proj, buf,
22450Sstevel@tonic-gate bufsz);
22460Sstevel@tonic-gate } while (pp == NULL && errno == EINTR);
22470Sstevel@tonic-gate
22480Sstevel@tonic-gate /* to be continued ... */
22490Sstevel@tonic-gate } else {
22500Sstevel@tonic-gate projid_t projid;
22510Sstevel@tonic-gate char *cp;
22520Sstevel@tonic-gate
22530Sstevel@tonic-gate if (!isdigit(str[0])) {
22540Sstevel@tonic-gate cip->project = strdup(str);
22550Sstevel@tonic-gate return (cip->project != NULL ? 0 : ENOMEM);
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate errno = 0;
22590Sstevel@tonic-gate projid = strtol(str, &cp, 10);
22600Sstevel@tonic-gate
22610Sstevel@tonic-gate if (projid == 0 && errno != 0) {
22620Sstevel@tonic-gate assert(errno == ERANGE);
22630Sstevel@tonic-gate return (errno);
22640Sstevel@tonic-gate }
22650Sstevel@tonic-gate
22660Sstevel@tonic-gate for (; *cp != '\0'; ++cp)
22670Sstevel@tonic-gate if (*cp != ' ' || *cp != '\t')
22680Sstevel@tonic-gate return (EINVAL);
22690Sstevel@tonic-gate
22700Sstevel@tonic-gate if (projid > MAXPROJID)
22710Sstevel@tonic-gate return (ERANGE);
22720Sstevel@tonic-gate
22730Sstevel@tonic-gate buf = malloc(bufsz);
22740Sstevel@tonic-gate if (buf == NULL)
22750Sstevel@tonic-gate return (ENOMEM);
22760Sstevel@tonic-gate
22770Sstevel@tonic-gate do {
22780Sstevel@tonic-gate errno = 0;
22790Sstevel@tonic-gate pp = getprojbyid(projid, &proj, buf, bufsz);
22800Sstevel@tonic-gate } while (pp == NULL && errno == EINTR);
22810Sstevel@tonic-gate }
22820Sstevel@tonic-gate
22830Sstevel@tonic-gate if (pp) {
22840Sstevel@tonic-gate cip->project = strdup(pp->pj_name);
22850Sstevel@tonic-gate free(buf);
22860Sstevel@tonic-gate return (cip->project != NULL ? 0 : ENOMEM);
22870Sstevel@tonic-gate }
22880Sstevel@tonic-gate
22890Sstevel@tonic-gate free(buf);
22900Sstevel@tonic-gate
22910Sstevel@tonic-gate switch (errno) {
22920Sstevel@tonic-gate case 0:
22930Sstevel@tonic-gate return (ENOENT);
22940Sstevel@tonic-gate
22950Sstevel@tonic-gate case EIO:
22960Sstevel@tonic-gate case EMFILE:
22970Sstevel@tonic-gate case ENFILE:
22980Sstevel@tonic-gate return (errno);
22990Sstevel@tonic-gate
23000Sstevel@tonic-gate case ERANGE:
23010Sstevel@tonic-gate return (E2BIG);
23020Sstevel@tonic-gate
23030Sstevel@tonic-gate default:
23040Sstevel@tonic-gate return (-1);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate }
23070Sstevel@tonic-gate
23080Sstevel@tonic-gate /*
23090Sstevel@tonic-gate * Parse the supp_groups property value and populate ci->groups. Returns
23100Sstevel@tonic-gate * EINVAL (get_gid() failed for one of the components), E2BIG (the property has
23110Sstevel@tonic-gate * more than NGROUPS_MAX-1 groups), or 0 on success.
23120Sstevel@tonic-gate */
23130Sstevel@tonic-gate int
get_groups(char * str,struct method_context * ci)23140Sstevel@tonic-gate get_groups(char *str, struct method_context *ci)
23150Sstevel@tonic-gate {
23160Sstevel@tonic-gate char *cp, *end, *next;
23170Sstevel@tonic-gate uint_t i;
23180Sstevel@tonic-gate
23190Sstevel@tonic-gate const char * const whitespace = " \t";
23200Sstevel@tonic-gate const char * const illegal = ", \t";
23210Sstevel@tonic-gate
23220Sstevel@tonic-gate if (str[0] == '\0') {
23230Sstevel@tonic-gate ci->ngroups = 0;
23240Sstevel@tonic-gate return (0);
23250Sstevel@tonic-gate }
23260Sstevel@tonic-gate
23270Sstevel@tonic-gate for (cp = str, i = 0; *cp != '\0'; ) {
23280Sstevel@tonic-gate /* skip whitespace */
23290Sstevel@tonic-gate cp += strspn(cp, whitespace);
23300Sstevel@tonic-gate
23310Sstevel@tonic-gate /* find the end */
23320Sstevel@tonic-gate end = cp + strcspn(cp, illegal);
23330Sstevel@tonic-gate
23340Sstevel@tonic-gate /* skip whitespace after end */
23350Sstevel@tonic-gate next = end + strspn(end, whitespace);
23360Sstevel@tonic-gate
23370Sstevel@tonic-gate /* if there's a comma, it separates the fields */
23380Sstevel@tonic-gate if (*next == ',')
23390Sstevel@tonic-gate ++next;
23400Sstevel@tonic-gate
23410Sstevel@tonic-gate *end = '\0';
23420Sstevel@tonic-gate
23434321Scasper if ((ci->groups[i] = get_gid(cp)) == (gid_t)-1) {
23440Sstevel@tonic-gate ci->ngroups = 0;
23450Sstevel@tonic-gate return (EINVAL);
23460Sstevel@tonic-gate }
23470Sstevel@tonic-gate
23480Sstevel@tonic-gate ++i;
23490Sstevel@tonic-gate if (i > NGROUPS_MAX - 1) {
23500Sstevel@tonic-gate ci->ngroups = 0;
23510Sstevel@tonic-gate return (E2BIG);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate
23540Sstevel@tonic-gate cp = next;
23550Sstevel@tonic-gate }
23560Sstevel@tonic-gate
23570Sstevel@tonic-gate ci->ngroups = i;
23580Sstevel@tonic-gate return (0);
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate
23619765SSean.Wilcox@Sun.COM
23620Sstevel@tonic-gate /*
23639765SSean.Wilcox@Sun.COM * Return an error message structure containing the error message
23649765SSean.Wilcox@Sun.COM * with context, and the error so the caller can make a decision
23659765SSean.Wilcox@Sun.COM * on what to do next.
23669765SSean.Wilcox@Sun.COM *
23679765SSean.Wilcox@Sun.COM * Because get_ids uses the mc_error_create() function which can
23689765SSean.Wilcox@Sun.COM * reallocate the merr, this function must return the merr pointer
23699765SSean.Wilcox@Sun.COM * in case it was reallocated.
23700Sstevel@tonic-gate */
23719765SSean.Wilcox@Sun.COM static mc_error_t *
get_profile(scf_propertygroup_t * methpg,scf_propertygroup_t * instpg,scf_property_t * prop,scf_value_t * val,const char * cmdline,struct method_context * ci,mc_error_t * merr)23729263SSean.Wilcox@Sun.COM get_profile(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
23739263SSean.Wilcox@Sun.COM scf_property_t *prop, scf_value_t *val, const char *cmdline,
23749765SSean.Wilcox@Sun.COM struct method_context *ci, mc_error_t *merr)
23750Sstevel@tonic-gate {
23760Sstevel@tonic-gate char *buf = ci->vbuf;
23770Sstevel@tonic-gate ssize_t buf_sz = ci->vbuf_sz;
23780Sstevel@tonic-gate char cmd[PATH_MAX];
23790Sstevel@tonic-gate char *cp, *value;
23800Sstevel@tonic-gate const char *cmdp;
23810Sstevel@tonic-gate execattr_t *eap;
23829765SSean.Wilcox@Sun.COM mc_error_t *err = merr;
23839765SSean.Wilcox@Sun.COM int r;
23849263SSean.Wilcox@Sun.COM
23859263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_PROFILE, buf, buf_sz, prop,
23869263SSean.Wilcox@Sun.COM val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PROFILE, buf,
23879263SSean.Wilcox@Sun.COM buf_sz, prop, val) == 0))
23889765SSean.Wilcox@Sun.COM return (mc_error_create(merr, scf_error(),
23899765SSean.Wilcox@Sun.COM "Method context requires a profile, but the \"%s\" "
23909765SSean.Wilcox@Sun.COM "property could not be read. scf_error is %s",
23919765SSean.Wilcox@Sun.COM SCF_PROPERTY_PROFILE, scf_strerror(scf_error())));
23920Sstevel@tonic-gate
23930Sstevel@tonic-gate /* Extract the command from the command line. */
23940Sstevel@tonic-gate cp = strpbrk(cmdline, " \t");
23950Sstevel@tonic-gate
23960Sstevel@tonic-gate if (cp == NULL) {
23970Sstevel@tonic-gate cmdp = cmdline;
23980Sstevel@tonic-gate } else {
23990Sstevel@tonic-gate (void) strncpy(cmd, cmdline, cp - cmdline);
24000Sstevel@tonic-gate cmd[cp - cmdline] = '\0';
24010Sstevel@tonic-gate cmdp = cmd;
24020Sstevel@tonic-gate }
24030Sstevel@tonic-gate
24040Sstevel@tonic-gate /* Require that cmdp[0] == '/'? */
24050Sstevel@tonic-gate
24060Sstevel@tonic-gate eap = getexecprof(buf, KV_COMMAND, cmdp, GET_ONE);
24070Sstevel@tonic-gate if (eap == NULL)
24089765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
24099765SSean.Wilcox@Sun.COM "Could not find the execution profile \"%s\", "
24109765SSean.Wilcox@Sun.COM "command %s.", buf, cmdp));
24110Sstevel@tonic-gate
24120Sstevel@tonic-gate /* Based on pfexec.c */
24130Sstevel@tonic-gate
24140Sstevel@tonic-gate /* Get the euid first so we don't override ci->pwd for the uid. */
24150Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_EUID_KW)) != NULL) {
24169765SSean.Wilcox@Sun.COM if ((r = get_uid(value, ci, &ci->euid)) != 0) {
24174321Scasper ci->euid = (uid_t)-1;
24189765SSean.Wilcox@Sun.COM err = mc_error_create(merr, r,
24199765SSean.Wilcox@Sun.COM "Could not interpret profile euid value \"%s\", "
24209765SSean.Wilcox@Sun.COM "from the execution profile \"%s\", error %d.",
24219765SSean.Wilcox@Sun.COM value, buf, r);
24220Sstevel@tonic-gate goto out;
24230Sstevel@tonic-gate }
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate
24260Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_UID_KW)) != NULL) {
24279765SSean.Wilcox@Sun.COM if ((r = get_uid(value, ci, &ci->uid)) != 0) {
24284321Scasper ci->euid = ci->uid = (uid_t)-1;
24299765SSean.Wilcox@Sun.COM err = mc_error_create(merr, r,
24309765SSean.Wilcox@Sun.COM "Could not interpret profile uid value \"%s\", "
24319765SSean.Wilcox@Sun.COM "from the execution profile \"%s\", error %d.",
24329765SSean.Wilcox@Sun.COM value, buf, r);
24330Sstevel@tonic-gate goto out;
24340Sstevel@tonic-gate }
24350Sstevel@tonic-gate ci->euid = ci->uid;
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate
24380Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_GID_KW)) != NULL) {
24390Sstevel@tonic-gate ci->egid = ci->gid = get_gid(value);
24404321Scasper if (ci->gid == (gid_t)-1) {
24419765SSean.Wilcox@Sun.COM err = mc_error_create(merr, EINVAL,
24429765SSean.Wilcox@Sun.COM "Could not interpret profile gid value \"%s\", "
24439765SSean.Wilcox@Sun.COM "from the execution profile \"%s\".", value, buf);
24440Sstevel@tonic-gate goto out;
24450Sstevel@tonic-gate }
24460Sstevel@tonic-gate }
24470Sstevel@tonic-gate
24480Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_EGID_KW)) != NULL) {
24490Sstevel@tonic-gate ci->egid = get_gid(value);
24504321Scasper if (ci->egid == (gid_t)-1) {
24519765SSean.Wilcox@Sun.COM err = mc_error_create(merr, EINVAL,
24529765SSean.Wilcox@Sun.COM "Could not interpret profile egid value \"%s\", "
24539765SSean.Wilcox@Sun.COM "from the execution profile \"%s\".", value, buf);
24540Sstevel@tonic-gate goto out;
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate }
24570Sstevel@tonic-gate
24580Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_LPRIV_KW)) != NULL) {
24590Sstevel@tonic-gate ci->lpriv_set = priv_str_to_set(value, ",", NULL);
24600Sstevel@tonic-gate if (ci->lpriv_set == NULL) {
24610Sstevel@tonic-gate if (errno != EINVAL)
24629765SSean.Wilcox@Sun.COM err = mc_error_create(merr, ENOMEM,
24639765SSean.Wilcox@Sun.COM ALLOCFAIL);
24640Sstevel@tonic-gate else
24659765SSean.Wilcox@Sun.COM err = mc_error_create(merr, EINVAL,
24669765SSean.Wilcox@Sun.COM "Could not interpret profile "
24679765SSean.Wilcox@Sun.COM "limitprivs value \"%s\", from "
24689765SSean.Wilcox@Sun.COM "the execution profile \"%s\".",
24699765SSean.Wilcox@Sun.COM value, buf);
24700Sstevel@tonic-gate goto out;
24710Sstevel@tonic-gate }
24720Sstevel@tonic-gate }
24730Sstevel@tonic-gate
24740Sstevel@tonic-gate if ((value = kva_match(eap->attr, EXECATTR_IPRIV_KW)) != NULL) {
24750Sstevel@tonic-gate ci->priv_set = priv_str_to_set(value, ",", NULL);
24760Sstevel@tonic-gate if (ci->priv_set == NULL) {
24770Sstevel@tonic-gate if (errno != EINVAL)
24789765SSean.Wilcox@Sun.COM err = mc_error_create(merr, ENOMEM,
24799765SSean.Wilcox@Sun.COM ALLOCFAIL);
24800Sstevel@tonic-gate else
24819765SSean.Wilcox@Sun.COM err = mc_error_create(merr, EINVAL,
24829765SSean.Wilcox@Sun.COM "Could not interpret profile privs value "
24839765SSean.Wilcox@Sun.COM "\"%s\", from the execution profile "
24849765SSean.Wilcox@Sun.COM "\"%s\".", value, buf);
24850Sstevel@tonic-gate goto out;
24860Sstevel@tonic-gate }
24870Sstevel@tonic-gate }
24880Sstevel@tonic-gate
24890Sstevel@tonic-gate out:
24900Sstevel@tonic-gate free_execattr(eap);
24910Sstevel@tonic-gate
24929765SSean.Wilcox@Sun.COM return (err);
24930Sstevel@tonic-gate }
24940Sstevel@tonic-gate
24950Sstevel@tonic-gate /*
24969765SSean.Wilcox@Sun.COM * Return an error message structure containing the error message
24979765SSean.Wilcox@Sun.COM * with context, and the error so the caller can make a decision
24989765SSean.Wilcox@Sun.COM * on what to do next.
24999765SSean.Wilcox@Sun.COM *
25009765SSean.Wilcox@Sun.COM * Because get_ids uses the mc_error_create() function which can
25019765SSean.Wilcox@Sun.COM * reallocate the merr, this function must return the merr pointer
25029765SSean.Wilcox@Sun.COM * in case it was reallocated.
25030Sstevel@tonic-gate */
25049765SSean.Wilcox@Sun.COM static mc_error_t *
get_ids(scf_propertygroup_t * methpg,scf_propertygroup_t * instpg,scf_property_t * prop,scf_value_t * val,struct method_context * ci,mc_error_t * merr)25059263SSean.Wilcox@Sun.COM get_ids(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
25069765SSean.Wilcox@Sun.COM scf_property_t *prop, scf_value_t *val, struct method_context *ci,
25079765SSean.Wilcox@Sun.COM mc_error_t *merr)
25080Sstevel@tonic-gate {
25090Sstevel@tonic-gate char *vbuf = ci->vbuf;
25100Sstevel@tonic-gate ssize_t vbuf_sz = ci->vbuf_sz;
25110Sstevel@tonic-gate int r;
25120Sstevel@tonic-gate
25139263SSean.Wilcox@Sun.COM /*
25149263SSean.Wilcox@Sun.COM * This should never happen because the caller should fall through
25159263SSean.Wilcox@Sun.COM * another path of just setting the ids to defaults, instead of
25169263SSean.Wilcox@Sun.COM * attempting to get the ids here.
25179263SSean.Wilcox@Sun.COM */
25189765SSean.Wilcox@Sun.COM if (methpg == NULL && instpg == NULL)
25199765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25209765SSean.Wilcox@Sun.COM "No property groups to get ids from."));
25219263SSean.Wilcox@Sun.COM
25229263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_USER,
25239263SSean.Wilcox@Sun.COM vbuf, vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
25249263SSean.Wilcox@Sun.COM SCF_PROPERTY_USER, vbuf, vbuf_sz, prop,
25259765SSean.Wilcox@Sun.COM val) == 0))
25269765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25279765SSean.Wilcox@Sun.COM "Could not get \"%s\" property.", SCF_PROPERTY_USER));
25289765SSean.Wilcox@Sun.COM
25299765SSean.Wilcox@Sun.COM if ((r = get_uid(vbuf, ci, &ci->uid)) != 0) {
25304321Scasper ci->uid = (uid_t)-1;
25319765SSean.Wilcox@Sun.COM return (mc_error_create(merr, r,
25329765SSean.Wilcox@Sun.COM "Could not interpret \"%s\" property value \"%s\", "
25339765SSean.Wilcox@Sun.COM "error %d.", SCF_PROPERTY_USER, vbuf, r));
25340Sstevel@tonic-gate }
25350Sstevel@tonic-gate
25369263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_GROUP, vbuf, vbuf_sz, prop,
25379263SSean.Wilcox@Sun.COM val) == 0 || get_astring_val(instpg, SCF_PROPERTY_GROUP, vbuf,
25389263SSean.Wilcox@Sun.COM vbuf_sz, prop, val) == 0)) {
25399263SSean.Wilcox@Sun.COM if (scf_error() == SCF_ERROR_NOT_FOUND) {
25409263SSean.Wilcox@Sun.COM (void) strcpy(vbuf, ":default");
25419263SSean.Wilcox@Sun.COM } else {
25429765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25439765SSean.Wilcox@Sun.COM "Could not get \"%s\" property.",
25449765SSean.Wilcox@Sun.COM SCF_PROPERTY_GROUP));
25459263SSean.Wilcox@Sun.COM }
25460Sstevel@tonic-gate }
25470Sstevel@tonic-gate
25480Sstevel@tonic-gate if (strcmp(vbuf, ":default") != 0) {
25490Sstevel@tonic-gate ci->gid = get_gid(vbuf);
25504321Scasper if (ci->gid == (gid_t)-1) {
25519765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25529765SSean.Wilcox@Sun.COM "Could not interpret \"%s\" property value \"%s\".",
25539765SSean.Wilcox@Sun.COM SCF_PROPERTY_GROUP, vbuf));
25540Sstevel@tonic-gate }
25550Sstevel@tonic-gate } else {
25560Sstevel@tonic-gate switch (r = lookup_pwd(ci)) {
25570Sstevel@tonic-gate case 0:
25580Sstevel@tonic-gate ci->gid = ci->pwd.pw_gid;
25590Sstevel@tonic-gate break;
25600Sstevel@tonic-gate
25610Sstevel@tonic-gate case ENOENT:
25624321Scasper ci->gid = (gid_t)-1;
25639765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25649765SSean.Wilcox@Sun.COM "No passwd entry for uid \"%d\".", ci->uid));
25650Sstevel@tonic-gate
25660Sstevel@tonic-gate case ENOMEM:
25679765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOMEM,
25689765SSean.Wilcox@Sun.COM "Out of memory."));
25690Sstevel@tonic-gate
25700Sstevel@tonic-gate case EIO:
25710Sstevel@tonic-gate case EMFILE:
25720Sstevel@tonic-gate case ENFILE:
25739765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENFILE,
25749765SSean.Wilcox@Sun.COM "getpwuid_r() failed, error %d.", r));
25750Sstevel@tonic-gate
25760Sstevel@tonic-gate default:
25770Sstevel@tonic-gate bad_fail("lookup_pwd", r);
25780Sstevel@tonic-gate }
25790Sstevel@tonic-gate }
25800Sstevel@tonic-gate
25819263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz,
25829263SSean.Wilcox@Sun.COM prop, val) == 0 || get_astring_val(instpg,
25839263SSean.Wilcox@Sun.COM SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz, prop, val) == 0)) {
25849263SSean.Wilcox@Sun.COM if (scf_error() == SCF_ERROR_NOT_FOUND) {
25859263SSean.Wilcox@Sun.COM (void) strcpy(vbuf, ":default");
25869263SSean.Wilcox@Sun.COM } else {
25879765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
25889765SSean.Wilcox@Sun.COM "Could not get supplemental groups (\"%s\") "
25899765SSean.Wilcox@Sun.COM "property.", SCF_PROPERTY_SUPP_GROUPS));
25909263SSean.Wilcox@Sun.COM }
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate
25930Sstevel@tonic-gate if (strcmp(vbuf, ":default") != 0) {
25940Sstevel@tonic-gate switch (r = get_groups(vbuf, ci)) {
25950Sstevel@tonic-gate case 0:
25960Sstevel@tonic-gate break;
25970Sstevel@tonic-gate
25980Sstevel@tonic-gate case EINVAL:
25999765SSean.Wilcox@Sun.COM return (mc_error_create(merr, EINVAL,
26009765SSean.Wilcox@Sun.COM "Could not interpret supplemental groups (\"%s\") "
26019765SSean.Wilcox@Sun.COM "property value \"%s\".", SCF_PROPERTY_SUPP_GROUPS,
26029765SSean.Wilcox@Sun.COM vbuf));
26030Sstevel@tonic-gate
26040Sstevel@tonic-gate case E2BIG:
26059765SSean.Wilcox@Sun.COM return (mc_error_create(merr, E2BIG,
26069765SSean.Wilcox@Sun.COM "Too many supplemental groups values in \"%s\".",
26079765SSean.Wilcox@Sun.COM vbuf));
26080Sstevel@tonic-gate
26090Sstevel@tonic-gate default:
26100Sstevel@tonic-gate bad_fail("get_groups", r);
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate } else {
26130Sstevel@tonic-gate ci->ngroups = -1;
26140Sstevel@tonic-gate }
26150Sstevel@tonic-gate
26169263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_PRIVILEGES, vbuf, vbuf_sz,
26179263SSean.Wilcox@Sun.COM prop, val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PRIVILEGES,
26189263SSean.Wilcox@Sun.COM vbuf, vbuf_sz, prop, val) == 0)) {
26199263SSean.Wilcox@Sun.COM if (scf_error() == SCF_ERROR_NOT_FOUND) {
26209263SSean.Wilcox@Sun.COM (void) strcpy(vbuf, ":default");
26219263SSean.Wilcox@Sun.COM } else {
26229765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
26239765SSean.Wilcox@Sun.COM "Could not get \"%s\" property.",
26249765SSean.Wilcox@Sun.COM SCF_PROPERTY_PRIVILEGES));
26259263SSean.Wilcox@Sun.COM }
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate
26280Sstevel@tonic-gate /*
26290Sstevel@tonic-gate * For default privs, we need to keep priv_set == NULL, as
26300Sstevel@tonic-gate * we use this test elsewhere.
26310Sstevel@tonic-gate */
26320Sstevel@tonic-gate if (strcmp(vbuf, ":default") != 0) {
26330Sstevel@tonic-gate ci->priv_set = priv_str_to_set(vbuf, ",", NULL);
26340Sstevel@tonic-gate if (ci->priv_set == NULL) {
26350Sstevel@tonic-gate if (errno != EINVAL) {
26369765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOMEM,
26379765SSean.Wilcox@Sun.COM ALLOCFAIL));
26380Sstevel@tonic-gate } else {
26399765SSean.Wilcox@Sun.COM return (mc_error_create(merr, EINVAL,
26409765SSean.Wilcox@Sun.COM "Could not interpret \"%s\" "
26419765SSean.Wilcox@Sun.COM "property value \"%s\".",
26429765SSean.Wilcox@Sun.COM SCF_PROPERTY_PRIVILEGES, vbuf));
26430Sstevel@tonic-gate }
26440Sstevel@tonic-gate }
26450Sstevel@tonic-gate }
26460Sstevel@tonic-gate
26479263SSean.Wilcox@Sun.COM if (!(get_astring_val(methpg, SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf,
26489263SSean.Wilcox@Sun.COM vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
26499263SSean.Wilcox@Sun.COM SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf, vbuf_sz, prop, val) == 0)) {
26509263SSean.Wilcox@Sun.COM if (scf_error() == SCF_ERROR_NOT_FOUND) {
26519263SSean.Wilcox@Sun.COM (void) strcpy(vbuf, ":default");
26529263SSean.Wilcox@Sun.COM } else {
26539765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOENT,
26549765SSean.Wilcox@Sun.COM "Could not get \"%s\" property.",
26559765SSean.Wilcox@Sun.COM SCF_PROPERTY_LIMIT_PRIVILEGES));
26569263SSean.Wilcox@Sun.COM }
26570Sstevel@tonic-gate }
26580Sstevel@tonic-gate
26590Sstevel@tonic-gate if (strcmp(vbuf, ":default") == 0)
26600Sstevel@tonic-gate /*
26610Sstevel@tonic-gate * L must default to all privileges so root NPA services see
26620Sstevel@tonic-gate * iE = all. "zone" is all privileges available in the current
26630Sstevel@tonic-gate * zone, equivalent to "all" in the global zone.
26640Sstevel@tonic-gate */
26650Sstevel@tonic-gate (void) strcpy(vbuf, "zone");
26660Sstevel@tonic-gate
26670Sstevel@tonic-gate ci->lpriv_set = priv_str_to_set(vbuf, ",", NULL);
26680Sstevel@tonic-gate if (ci->lpriv_set == NULL) {
26699765SSean.Wilcox@Sun.COM if (errno != EINVAL) {
26709765SSean.Wilcox@Sun.COM return (mc_error_create(merr, ENOMEM, ALLOCFAIL));
26719765SSean.Wilcox@Sun.COM } else {
26729765SSean.Wilcox@Sun.COM return (mc_error_create(merr, EINVAL,
26739765SSean.Wilcox@Sun.COM "Could not interpret \"%s\" property value \"%s\".",
26749765SSean.Wilcox@Sun.COM SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf));
26750Sstevel@tonic-gate }
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate
26789765SSean.Wilcox@Sun.COM return (merr);
26790Sstevel@tonic-gate }
26800Sstevel@tonic-gate
26810Sstevel@tonic-gate static int
get_environment(scf_handle_t * h,scf_propertygroup_t * pg,struct method_context * mcp,scf_property_t * prop,scf_value_t * val)26820Sstevel@tonic-gate get_environment(scf_handle_t *h, scf_propertygroup_t *pg,
26830Sstevel@tonic-gate struct method_context *mcp, scf_property_t *prop, scf_value_t *val)
26840Sstevel@tonic-gate {
26850Sstevel@tonic-gate scf_iter_t *iter;
26860Sstevel@tonic-gate scf_type_t type;
26870Sstevel@tonic-gate size_t i = 0;
26880Sstevel@tonic-gate int ret;
26890Sstevel@tonic-gate
26900Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_PROPERTY_ENVIRONMENT, prop) != 0) {
26910Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_FOUND)
26920Sstevel@tonic-gate return (ENOENT);
26930Sstevel@tonic-gate return (scf_error());
26940Sstevel@tonic-gate }
26950Sstevel@tonic-gate if (scf_property_type(prop, &type) != 0)
26960Sstevel@tonic-gate return (scf_error());
26970Sstevel@tonic-gate if (type != SCF_TYPE_ASTRING)
26980Sstevel@tonic-gate return (EINVAL);
26990Sstevel@tonic-gate if ((iter = scf_iter_create(h)) == NULL)
27000Sstevel@tonic-gate return (scf_error());
27010Sstevel@tonic-gate
27020Sstevel@tonic-gate if (scf_iter_property_values(iter, prop) != 0) {
27030Sstevel@tonic-gate ret = scf_error();
27040Sstevel@tonic-gate scf_iter_destroy(iter);
27050Sstevel@tonic-gate return (ret);
27060Sstevel@tonic-gate }
27070Sstevel@tonic-gate
27080Sstevel@tonic-gate mcp->env_sz = 10;
27090Sstevel@tonic-gate
27100Sstevel@tonic-gate if ((mcp->env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz)) == NULL) {
27110Sstevel@tonic-gate ret = ENOMEM;
27120Sstevel@tonic-gate goto out;
27130Sstevel@tonic-gate }
27140Sstevel@tonic-gate
27150Sstevel@tonic-gate while ((ret = scf_iter_next_value(iter, val)) == 1) {
27160Sstevel@tonic-gate ret = scf_value_get_as_string(val, mcp->vbuf, mcp->vbuf_sz);
27170Sstevel@tonic-gate if (ret == -1) {
27180Sstevel@tonic-gate ret = scf_error();
27190Sstevel@tonic-gate goto out;
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate
27220Sstevel@tonic-gate if ((mcp->env[i] = strdup(mcp->vbuf)) == NULL) {
27230Sstevel@tonic-gate ret = ENOMEM;
27240Sstevel@tonic-gate goto out;
27250Sstevel@tonic-gate }
27260Sstevel@tonic-gate
27270Sstevel@tonic-gate if (++i == mcp->env_sz) {
27280Sstevel@tonic-gate char **env;
27290Sstevel@tonic-gate mcp->env_sz *= 2;
27300Sstevel@tonic-gate env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz);
27310Sstevel@tonic-gate if (env == NULL) {
27320Sstevel@tonic-gate ret = ENOMEM;
27330Sstevel@tonic-gate goto out;
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate (void) memcpy(env, mcp->env,
27360Sstevel@tonic-gate sizeof (*mcp->env) * (mcp->env_sz / 2));
27370Sstevel@tonic-gate free(mcp->env);
27380Sstevel@tonic-gate mcp->env = env;
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate }
27410Sstevel@tonic-gate
27420Sstevel@tonic-gate if (ret == -1)
27430Sstevel@tonic-gate ret = scf_error();
27440Sstevel@tonic-gate
27450Sstevel@tonic-gate out:
27460Sstevel@tonic-gate scf_iter_destroy(iter);
27470Sstevel@tonic-gate return (ret);
27480Sstevel@tonic-gate }
27490Sstevel@tonic-gate
27500Sstevel@tonic-gate /*
27510Sstevel@tonic-gate * Fetch method context information from the repository, allocate and fill
27529765SSean.Wilcox@Sun.COM * a method_context structure, return it in *mcpp, and return NULL.
27530Sstevel@tonic-gate *
27549263SSean.Wilcox@Sun.COM * If no method_context is defined, original init context is provided, where
27559263SSean.Wilcox@Sun.COM * the working directory is '/', and uid/gid are 0/0. But if a method_context
27569263SSean.Wilcox@Sun.COM * is defined at any level the smf_method(5) method_context defaults are used.
27579263SSean.Wilcox@Sun.COM *
27589765SSean.Wilcox@Sun.COM * Return an error message structure containing the error message
27599765SSean.Wilcox@Sun.COM * with context, and the error so the caller can make a decision
27609765SSean.Wilcox@Sun.COM * on what to do next.
27619765SSean.Wilcox@Sun.COM *
27629765SSean.Wilcox@Sun.COM * Error Types :
27639765SSean.Wilcox@Sun.COM * E2BIG Too many values or entry is too big
27649765SSean.Wilcox@Sun.COM * EINVAL Invalid value
27659765SSean.Wilcox@Sun.COM * EIO an I/O error has occured
27669765SSean.Wilcox@Sun.COM * ENOENT no entry for value
27679765SSean.Wilcox@Sun.COM * ENOMEM out of memory
27689765SSean.Wilcox@Sun.COM * ENOTSUP Version mismatch
27699765SSean.Wilcox@Sun.COM * ERANGE value is out of range
27709765SSean.Wilcox@Sun.COM * EMFILE/ENFILE out of file descriptors
27719765SSean.Wilcox@Sun.COM *
27729765SSean.Wilcox@Sun.COM * SCF_ERROR_BACKEND_ACCESS
27739765SSean.Wilcox@Sun.COM * SCF_ERROR_CONNECTION_BROKEN
27749765SSean.Wilcox@Sun.COM * SCF_ERROR_DELETED
27759765SSean.Wilcox@Sun.COM * SCF_ERROR_CONSTRAINT_VIOLATED
27769765SSean.Wilcox@Sun.COM * SCF_ERROR_HANDLE_DESTROYED
27779765SSean.Wilcox@Sun.COM * SCF_ERROR_INTERNAL
27789765SSean.Wilcox@Sun.COM * SCF_ERROR_INVALID_ARGUMENT
27799765SSean.Wilcox@Sun.COM * SCF_ERROR_NO_MEMORY
27809765SSean.Wilcox@Sun.COM * SCF_ERROR_NO_RESOURCES
27819765SSean.Wilcox@Sun.COM * SCF_ERROR_NOT_BOUND
27829765SSean.Wilcox@Sun.COM * SCF_ERROR_NOT_FOUND
27839765SSean.Wilcox@Sun.COM * SCF_ERROR_NOT_SET
27849765SSean.Wilcox@Sun.COM * SCF_ERROR_TYPE_MISMATCH
27859765SSean.Wilcox@Sun.COM *
27860Sstevel@tonic-gate */
27879765SSean.Wilcox@Sun.COM mc_error_t *
restarter_get_method_context(uint_t version,scf_instance_t * inst,scf_snapshot_t * snap,const char * mname,const char * cmdline,struct method_context ** mcpp)27880Sstevel@tonic-gate restarter_get_method_context(uint_t version, scf_instance_t *inst,
27890Sstevel@tonic-gate scf_snapshot_t *snap, const char *mname, const char *cmdline,
27900Sstevel@tonic-gate struct method_context **mcpp)
27910Sstevel@tonic-gate {
27920Sstevel@tonic-gate scf_handle_t *h;
27930Sstevel@tonic-gate scf_propertygroup_t *methpg = NULL;
27940Sstevel@tonic-gate scf_propertygroup_t *instpg = NULL;
27950Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
27960Sstevel@tonic-gate scf_property_t *prop = NULL;
27970Sstevel@tonic-gate scf_value_t *val = NULL;
27980Sstevel@tonic-gate scf_type_t ty;
27990Sstevel@tonic-gate uint8_t use_profile;
28009765SSean.Wilcox@Sun.COM int ret = 0;
28019263SSean.Wilcox@Sun.COM int mc_used = 0;
28029765SSean.Wilcox@Sun.COM mc_error_t *err = NULL;
28030Sstevel@tonic-gate struct method_context *cip;
28040Sstevel@tonic-gate
28059765SSean.Wilcox@Sun.COM if ((err = malloc(sizeof (mc_error_t))) == NULL)
28069765SSean.Wilcox@Sun.COM return (mc_error_create(NULL, ENOMEM, NULL));
28079765SSean.Wilcox@Sun.COM
28089765SSean.Wilcox@Sun.COM /* Set the type to zero to track if an error occured. */
28099765SSean.Wilcox@Sun.COM err->type = 0;
28109765SSean.Wilcox@Sun.COM
28110Sstevel@tonic-gate if (version != RESTARTER_METHOD_CONTEXT_VERSION)
28129765SSean.Wilcox@Sun.COM return (mc_error_create(err, ENOTSUP,
28139765SSean.Wilcox@Sun.COM "Invalid client version %d. (Expected %d)",
28149765SSean.Wilcox@Sun.COM version, RESTARTER_METHOD_CONTEXT_VERSION));
28150Sstevel@tonic-gate
28160Sstevel@tonic-gate /* Get the handle before we allocate anything. */
28170Sstevel@tonic-gate h = scf_instance_handle(inst);
28180Sstevel@tonic-gate if (h == NULL)
28199765SSean.Wilcox@Sun.COM return (mc_error_create(err, scf_error(),
28209765SSean.Wilcox@Sun.COM scf_strerror(scf_error())));
28210Sstevel@tonic-gate
28220Sstevel@tonic-gate cip = malloc(sizeof (*cip));
28230Sstevel@tonic-gate if (cip == NULL)
28249765SSean.Wilcox@Sun.COM return (mc_error_create(err, ENOMEM, ALLOCFAIL));
28250Sstevel@tonic-gate
28260Sstevel@tonic-gate (void) memset(cip, 0, sizeof (*cip));
28274321Scasper cip->uid = (uid_t)-1;
28284321Scasper cip->euid = (uid_t)-1;
28294321Scasper cip->gid = (gid_t)-1;
28304321Scasper cip->egid = (gid_t)-1;
28310Sstevel@tonic-gate
28320Sstevel@tonic-gate cip->vbuf_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
28330Sstevel@tonic-gate assert(cip->vbuf_sz >= 0);
28340Sstevel@tonic-gate cip->vbuf = malloc(cip->vbuf_sz);
28350Sstevel@tonic-gate if (cip->vbuf == NULL) {
28360Sstevel@tonic-gate free(cip);
28379765SSean.Wilcox@Sun.COM return (mc_error_create(err, ENOMEM, ALLOCFAIL));
28380Sstevel@tonic-gate }
28390Sstevel@tonic-gate
28400Sstevel@tonic-gate if ((instpg = scf_pg_create(h)) == NULL ||
28410Sstevel@tonic-gate (methpg = scf_pg_create(h)) == NULL ||
28420Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL ||
28430Sstevel@tonic-gate (val = scf_value_create(h)) == NULL) {
28449765SSean.Wilcox@Sun.COM err = mc_error_create(err, scf_error(),
28459765SSean.Wilcox@Sun.COM "Failed to create repository object: %s\n",
28469765SSean.Wilcox@Sun.COM scf_strerror(scf_error()));
28470Sstevel@tonic-gate goto out;
28480Sstevel@tonic-gate }
28490Sstevel@tonic-gate
28500Sstevel@tonic-gate /*
28510Sstevel@tonic-gate * The method environment, and the credentials/profile data,
28520Sstevel@tonic-gate * may be found either in the pg for the method (methpg),
28530Sstevel@tonic-gate * or in the instance/service SCF_PG_METHOD_CONTEXT pg (named
28540Sstevel@tonic-gate * instpg below).
28550Sstevel@tonic-gate */
28560Sstevel@tonic-gate
28570Sstevel@tonic-gate if (scf_instance_get_pg_composed(inst, snap, mname, methpg) !=
28580Sstevel@tonic-gate SCF_SUCCESS) {
28599765SSean.Wilcox@Sun.COM err = mc_error_create(err, scf_error(), "Unable to get the "
28609765SSean.Wilcox@Sun.COM "\"%s\" method, %s", mname, scf_strerror(scf_error()));
28610Sstevel@tonic-gate goto out;
28620Sstevel@tonic-gate }
28630Sstevel@tonic-gate
28640Sstevel@tonic-gate if (scf_instance_get_pg_composed(inst, snap, SCF_PG_METHOD_CONTEXT,
28650Sstevel@tonic-gate instpg) != SCF_SUCCESS) {
28660Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) {
28679765SSean.Wilcox@Sun.COM err = mc_error_create(err, scf_error(),
28689765SSean.Wilcox@Sun.COM "Unable to retrieve the \"%s\" property group, %s",
28699765SSean.Wilcox@Sun.COM SCF_PG_METHOD_CONTEXT, scf_strerror(scf_error()));
28700Sstevel@tonic-gate goto out;
28710Sstevel@tonic-gate }
28720Sstevel@tonic-gate scf_pg_destroy(instpg);
28730Sstevel@tonic-gate instpg = NULL;
28749263SSean.Wilcox@Sun.COM } else {
28759263SSean.Wilcox@Sun.COM mc_used++;
28760Sstevel@tonic-gate }
28770Sstevel@tonic-gate
28780Sstevel@tonic-gate ret = get_environment(h, methpg, cip, prop, val);
28790Sstevel@tonic-gate if (ret == ENOENT && instpg != NULL) {
28800Sstevel@tonic-gate ret = get_environment(h, instpg, cip, prop, val);
28810Sstevel@tonic-gate }
28820Sstevel@tonic-gate
28830Sstevel@tonic-gate switch (ret) {
28840Sstevel@tonic-gate case 0:
28859263SSean.Wilcox@Sun.COM mc_used++;
28869263SSean.Wilcox@Sun.COM break;
28870Sstevel@tonic-gate case ENOENT:
28880Sstevel@tonic-gate break;
28890Sstevel@tonic-gate case ENOMEM:
28909765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, "Out of memory.");
28910Sstevel@tonic-gate goto out;
28920Sstevel@tonic-gate case EINVAL:
28939765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, "Invalid method environment.");
28940Sstevel@tonic-gate goto out;
28950Sstevel@tonic-gate default:
28969765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
28979765SSean.Wilcox@Sun.COM "Get method environment failed : %s\n", scf_strerror(ret));
28980Sstevel@tonic-gate goto out;
28990Sstevel@tonic-gate }
29000Sstevel@tonic-gate
29010Sstevel@tonic-gate pg = methpg;
29020Sstevel@tonic-gate
29030Sstevel@tonic-gate ret = scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, prop);
29040Sstevel@tonic-gate if (ret && scf_error() == SCF_ERROR_NOT_FOUND && instpg != NULL) {
29059263SSean.Wilcox@Sun.COM pg = NULL;
29069263SSean.Wilcox@Sun.COM ret = scf_pg_get_property(instpg, SCF_PROPERTY_USE_PROFILE,
29079263SSean.Wilcox@Sun.COM prop);
29080Sstevel@tonic-gate }
29090Sstevel@tonic-gate
29100Sstevel@tonic-gate if (ret) {
29110Sstevel@tonic-gate switch (scf_error()) {
29120Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
29139263SSean.Wilcox@Sun.COM /* No profile context: use default credentials */
29140Sstevel@tonic-gate cip->uid = 0;
29150Sstevel@tonic-gate cip->gid = 0;
29169263SSean.Wilcox@Sun.COM break;
29170Sstevel@tonic-gate
29180Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
29199765SSean.Wilcox@Sun.COM err = mc_error_create(err, SCF_ERROR_CONNECTION_BROKEN,
29209765SSean.Wilcox@Sun.COM RCBROKEN);
29210Sstevel@tonic-gate goto out;
29220Sstevel@tonic-gate
29230Sstevel@tonic-gate case SCF_ERROR_DELETED:
29249765SSean.Wilcox@Sun.COM err = mc_error_create(err, SCF_ERROR_NOT_FOUND,
29259765SSean.Wilcox@Sun.COM "Could not find property group \"%s\"",
29269765SSean.Wilcox@Sun.COM pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
29270Sstevel@tonic-gate goto out;
29280Sstevel@tonic-gate
29290Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
29300Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
29310Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
29320Sstevel@tonic-gate default:
29330Sstevel@tonic-gate bad_fail("scf_pg_get_property", scf_error());
29340Sstevel@tonic-gate }
29359263SSean.Wilcox@Sun.COM } else {
29369263SSean.Wilcox@Sun.COM if (scf_property_type(prop, &ty) != SCF_SUCCESS) {
29379765SSean.Wilcox@Sun.COM ret = scf_error();
29389765SSean.Wilcox@Sun.COM switch (ret) {
29399263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
29409765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29419765SSean.Wilcox@Sun.COM SCF_ERROR_CONNECTION_BROKEN, RCBROKEN);
29429263SSean.Wilcox@Sun.COM break;
29439263SSean.Wilcox@Sun.COM
29449263SSean.Wilcox@Sun.COM case SCF_ERROR_DELETED:
29459765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29469765SSean.Wilcox@Sun.COM SCF_ERROR_NOT_FOUND,
29479765SSean.Wilcox@Sun.COM "Could not find property group \"%s\"",
29489765SSean.Wilcox@Sun.COM pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
29499263SSean.Wilcox@Sun.COM break;
29509263SSean.Wilcox@Sun.COM
29519263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_SET:
29529263SSean.Wilcox@Sun.COM default:
29539765SSean.Wilcox@Sun.COM bad_fail("scf_property_type", ret);
29549263SSean.Wilcox@Sun.COM }
29559263SSean.Wilcox@Sun.COM
29569263SSean.Wilcox@Sun.COM goto out;
29579263SSean.Wilcox@Sun.COM }
29589263SSean.Wilcox@Sun.COM
29599263SSean.Wilcox@Sun.COM if (ty != SCF_TYPE_BOOLEAN) {
29609765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29619765SSean.Wilcox@Sun.COM SCF_ERROR_TYPE_MISMATCH,
29629765SSean.Wilcox@Sun.COM "\"%s\" property is not boolean in property group "
29639765SSean.Wilcox@Sun.COM "\"%s\".", SCF_PROPERTY_USE_PROFILE,
29649765SSean.Wilcox@Sun.COM pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
29659263SSean.Wilcox@Sun.COM goto out;
29669263SSean.Wilcox@Sun.COM }
29679263SSean.Wilcox@Sun.COM
29689263SSean.Wilcox@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
29699765SSean.Wilcox@Sun.COM ret = scf_error();
29709765SSean.Wilcox@Sun.COM switch (ret) {
29719263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
29729765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29739765SSean.Wilcox@Sun.COM SCF_ERROR_CONNECTION_BROKEN, RCBROKEN);
29749263SSean.Wilcox@Sun.COM break;
29759263SSean.Wilcox@Sun.COM
29769263SSean.Wilcox@Sun.COM case SCF_ERROR_CONSTRAINT_VIOLATED:
29779765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29789765SSean.Wilcox@Sun.COM SCF_ERROR_CONSTRAINT_VIOLATED,
29799765SSean.Wilcox@Sun.COM "\"%s\" property has multiple values.",
29809765SSean.Wilcox@Sun.COM SCF_PROPERTY_USE_PROFILE);
29819263SSean.Wilcox@Sun.COM break;
29829263SSean.Wilcox@Sun.COM
29839263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
29849765SSean.Wilcox@Sun.COM err = mc_error_create(err,
29859765SSean.Wilcox@Sun.COM SCF_ERROR_NOT_FOUND,
29869765SSean.Wilcox@Sun.COM "\"%s\" property has no values.",
29879765SSean.Wilcox@Sun.COM SCF_PROPERTY_USE_PROFILE);
29889263SSean.Wilcox@Sun.COM break;
29899263SSean.Wilcox@Sun.COM default:
29909765SSean.Wilcox@Sun.COM bad_fail("scf_property_get_value", ret);
29919263SSean.Wilcox@Sun.COM }
29929263SSean.Wilcox@Sun.COM
29939263SSean.Wilcox@Sun.COM goto out;
29949263SSean.Wilcox@Sun.COM }
29959263SSean.Wilcox@Sun.COM
29969263SSean.Wilcox@Sun.COM mc_used++;
29979263SSean.Wilcox@Sun.COM ret = scf_value_get_boolean(val, &use_profile);
29989263SSean.Wilcox@Sun.COM assert(ret == SCF_SUCCESS);
29999263SSean.Wilcox@Sun.COM
30009263SSean.Wilcox@Sun.COM /* get ids & privileges */
30019263SSean.Wilcox@Sun.COM if (use_profile)
30029765SSean.Wilcox@Sun.COM err = get_profile(pg, instpg, prop, val, cmdline,
30039765SSean.Wilcox@Sun.COM cip, err);
30049263SSean.Wilcox@Sun.COM else
30059765SSean.Wilcox@Sun.COM err = get_ids(pg, instpg, prop, val, cip, err);
30069765SSean.Wilcox@Sun.COM
30079765SSean.Wilcox@Sun.COM if (err->type != 0)
30089263SSean.Wilcox@Sun.COM goto out;
30090Sstevel@tonic-gate }
30100Sstevel@tonic-gate
30119263SSean.Wilcox@Sun.COM /* get working directory */
30129263SSean.Wilcox@Sun.COM if ((methpg != NULL && scf_pg_get_property(methpg,
30139263SSean.Wilcox@Sun.COM SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS) ||
30149263SSean.Wilcox@Sun.COM (instpg != NULL && scf_pg_get_property(instpg,
30159263SSean.Wilcox@Sun.COM SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS)) {
30169263SSean.Wilcox@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
30179765SSean.Wilcox@Sun.COM ret = scf_error();
30189765SSean.Wilcox@Sun.COM switch (ret) {
30199263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
30209765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, RCBROKEN);
30219263SSean.Wilcox@Sun.COM break;
30229263SSean.Wilcox@Sun.COM
30239263SSean.Wilcox@Sun.COM case SCF_ERROR_CONSTRAINT_VIOLATED:
30249765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
30259765SSean.Wilcox@Sun.COM "\"%s\" property has multiple values.",
30269765SSean.Wilcox@Sun.COM SCF_PROPERTY_WORKING_DIRECTORY);
30279263SSean.Wilcox@Sun.COM break;
30289263SSean.Wilcox@Sun.COM
30299263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
30309765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
30319765SSean.Wilcox@Sun.COM "\"%s\" property has no values.",
30329765SSean.Wilcox@Sun.COM SCF_PROPERTY_WORKING_DIRECTORY);
30339263SSean.Wilcox@Sun.COM break;
30349263SSean.Wilcox@Sun.COM
30359263SSean.Wilcox@Sun.COM default:
30369765SSean.Wilcox@Sun.COM bad_fail("scf_property_get_value", ret);
30379263SSean.Wilcox@Sun.COM }
30389263SSean.Wilcox@Sun.COM
30399263SSean.Wilcox@Sun.COM goto out;
30409263SSean.Wilcox@Sun.COM }
30419263SSean.Wilcox@Sun.COM
30429263SSean.Wilcox@Sun.COM mc_used++;
30439263SSean.Wilcox@Sun.COM ret = scf_value_get_astring(val, cip->vbuf, cip->vbuf_sz);
30449263SSean.Wilcox@Sun.COM assert(ret != -1);
30459263SSean.Wilcox@Sun.COM } else {
30469765SSean.Wilcox@Sun.COM ret = scf_error();
30479765SSean.Wilcox@Sun.COM switch (ret) {
30489263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
30499263SSean.Wilcox@Sun.COM /* okay if missing. */
30509263SSean.Wilcox@Sun.COM (void) strcpy(cip->vbuf, ":default");
30510Sstevel@tonic-gate break;
30520Sstevel@tonic-gate
30530Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
30549765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, RCBROKEN);
30559263SSean.Wilcox@Sun.COM goto out;
30569263SSean.Wilcox@Sun.COM
30579263SSean.Wilcox@Sun.COM case SCF_ERROR_DELETED:
30589765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
30599765SSean.Wilcox@Sun.COM "Property group could not be found");
30609263SSean.Wilcox@Sun.COM goto out;
30619263SSean.Wilcox@Sun.COM
30629263SSean.Wilcox@Sun.COM case SCF_ERROR_HANDLE_MISMATCH:
30639263SSean.Wilcox@Sun.COM case SCF_ERROR_INVALID_ARGUMENT:
30649263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_SET:
30650Sstevel@tonic-gate default:
30669765SSean.Wilcox@Sun.COM bad_fail("scf_pg_get_property", ret);
30670Sstevel@tonic-gate }
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate
30700Sstevel@tonic-gate if (strcmp(cip->vbuf, ":default") == 0 ||
30710Sstevel@tonic-gate strcmp(cip->vbuf, ":home") == 0) {
30720Sstevel@tonic-gate switch (ret = lookup_pwd(cip)) {
30730Sstevel@tonic-gate case 0:
30740Sstevel@tonic-gate break;
30750Sstevel@tonic-gate
30760Sstevel@tonic-gate case ENOMEM:
30779765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, "Out of memory.");
30780Sstevel@tonic-gate goto out;
30790Sstevel@tonic-gate
30800Sstevel@tonic-gate case ENOENT:
30810Sstevel@tonic-gate case EIO:
30820Sstevel@tonic-gate case EMFILE:
30830Sstevel@tonic-gate case ENFILE:
30849765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
30859765SSean.Wilcox@Sun.COM "Could not get passwd entry.");
30860Sstevel@tonic-gate goto out;
30870Sstevel@tonic-gate
30880Sstevel@tonic-gate default:
30890Sstevel@tonic-gate bad_fail("lookup_pwd", ret);
30900Sstevel@tonic-gate }
30910Sstevel@tonic-gate
30920Sstevel@tonic-gate cip->working_dir = strdup(cip->pwd.pw_dir);
30930Sstevel@tonic-gate if (cip->working_dir == NULL) {
30949765SSean.Wilcox@Sun.COM err = mc_error_create(err, ENOMEM, ALLOCFAIL);
30950Sstevel@tonic-gate goto out;
30960Sstevel@tonic-gate }
30970Sstevel@tonic-gate } else {
30980Sstevel@tonic-gate cip->working_dir = strdup(cip->vbuf);
30990Sstevel@tonic-gate if (cip->working_dir == NULL) {
31009765SSean.Wilcox@Sun.COM err = mc_error_create(err, ENOMEM, ALLOCFAIL);
31010Sstevel@tonic-gate goto out;
31020Sstevel@tonic-gate }
31030Sstevel@tonic-gate }
31040Sstevel@tonic-gate
31050Sstevel@tonic-gate /* get (optional) corefile pattern */
31069263SSean.Wilcox@Sun.COM if ((methpg != NULL && scf_pg_get_property(methpg,
31079263SSean.Wilcox@Sun.COM SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS) ||
31089263SSean.Wilcox@Sun.COM (instpg != NULL && scf_pg_get_property(instpg,
31099263SSean.Wilcox@Sun.COM SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS)) {
31109263SSean.Wilcox@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
31119765SSean.Wilcox@Sun.COM ret = scf_error();
31129765SSean.Wilcox@Sun.COM switch (ret) {
31139263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
31149765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, RCBROKEN);
31159263SSean.Wilcox@Sun.COM break;
31169263SSean.Wilcox@Sun.COM
31179263SSean.Wilcox@Sun.COM case SCF_ERROR_CONSTRAINT_VIOLATED:
31189765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31199765SSean.Wilcox@Sun.COM "\"%s\" property has multiple values.",
31209765SSean.Wilcox@Sun.COM SCF_PROPERTY_COREFILE_PATTERN);
31219263SSean.Wilcox@Sun.COM break;
31229263SSean.Wilcox@Sun.COM
31239263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
31249765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31259765SSean.Wilcox@Sun.COM "\"%s\" property has no values.",
31269765SSean.Wilcox@Sun.COM SCF_PROPERTY_COREFILE_PATTERN);
31279263SSean.Wilcox@Sun.COM break;
31289263SSean.Wilcox@Sun.COM
31299263SSean.Wilcox@Sun.COM default:
31309765SSean.Wilcox@Sun.COM bad_fail("scf_property_get_value", ret);
31319263SSean.Wilcox@Sun.COM }
31329263SSean.Wilcox@Sun.COM
31339263SSean.Wilcox@Sun.COM } else {
31349263SSean.Wilcox@Sun.COM
31359263SSean.Wilcox@Sun.COM ret = scf_value_get_astring(val, cip->vbuf,
31369263SSean.Wilcox@Sun.COM cip->vbuf_sz);
31379263SSean.Wilcox@Sun.COM assert(ret != -1);
31389263SSean.Wilcox@Sun.COM
31399263SSean.Wilcox@Sun.COM cip->corefile_pattern = strdup(cip->vbuf);
31409263SSean.Wilcox@Sun.COM if (cip->corefile_pattern == NULL) {
31419765SSean.Wilcox@Sun.COM err = mc_error_create(err, ENOMEM, ALLOCFAIL);
31429263SSean.Wilcox@Sun.COM goto out;
31439263SSean.Wilcox@Sun.COM }
31440Sstevel@tonic-gate }
31450Sstevel@tonic-gate
31469263SSean.Wilcox@Sun.COM mc_used++;
31470Sstevel@tonic-gate } else {
31489765SSean.Wilcox@Sun.COM ret = scf_error();
31499765SSean.Wilcox@Sun.COM switch (ret) {
31500Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
31510Sstevel@tonic-gate /* okay if missing. */
31520Sstevel@tonic-gate break;
31530Sstevel@tonic-gate
31540Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
31559765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, RCBROKEN);
31560Sstevel@tonic-gate goto out;
31570Sstevel@tonic-gate
31580Sstevel@tonic-gate case SCF_ERROR_DELETED:
31599765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31609765SSean.Wilcox@Sun.COM "Property group could not be found");
31610Sstevel@tonic-gate goto out;
31620Sstevel@tonic-gate
31630Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
31640Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
31650Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
31660Sstevel@tonic-gate default:
31679765SSean.Wilcox@Sun.COM bad_fail("scf_pg_get_property", ret);
31680Sstevel@tonic-gate }
31690Sstevel@tonic-gate }
31700Sstevel@tonic-gate
31710Sstevel@tonic-gate if (restarter_rm_libs_loadable()) {
31720Sstevel@tonic-gate /* get project */
31739263SSean.Wilcox@Sun.COM if ((methpg != NULL && scf_pg_get_property(methpg,
31749263SSean.Wilcox@Sun.COM SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS) ||
31759263SSean.Wilcox@Sun.COM (instpg != NULL && scf_pg_get_property(instpg,
31769263SSean.Wilcox@Sun.COM SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS)) {
31779263SSean.Wilcox@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
31789765SSean.Wilcox@Sun.COM ret = scf_error();
31799765SSean.Wilcox@Sun.COM switch (ret) {
31809263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
31819765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31829765SSean.Wilcox@Sun.COM RCBROKEN);
31839263SSean.Wilcox@Sun.COM break;
31849263SSean.Wilcox@Sun.COM
31859263SSean.Wilcox@Sun.COM case SCF_ERROR_CONSTRAINT_VIOLATED:
31869765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31879765SSean.Wilcox@Sun.COM "\"%s\" property has multiple "
31889765SSean.Wilcox@Sun.COM "values.", SCF_PROPERTY_PROJECT);
31899263SSean.Wilcox@Sun.COM break;
31909263SSean.Wilcox@Sun.COM
31919263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
31929765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
31939765SSean.Wilcox@Sun.COM "\"%s\" property has no values.",
31949765SSean.Wilcox@Sun.COM SCF_PROPERTY_PROJECT);
31959263SSean.Wilcox@Sun.COM break;
31969263SSean.Wilcox@Sun.COM
31979263SSean.Wilcox@Sun.COM default:
31989765SSean.Wilcox@Sun.COM bad_fail("scf_property_get_value", ret);
31999263SSean.Wilcox@Sun.COM }
32009263SSean.Wilcox@Sun.COM
32019263SSean.Wilcox@Sun.COM (void) strcpy(cip->vbuf, ":default");
32029263SSean.Wilcox@Sun.COM } else {
32039263SSean.Wilcox@Sun.COM ret = scf_value_get_astring(val, cip->vbuf,
32049263SSean.Wilcox@Sun.COM cip->vbuf_sz);
32059263SSean.Wilcox@Sun.COM assert(ret != -1);
32069263SSean.Wilcox@Sun.COM }
32079263SSean.Wilcox@Sun.COM
32089263SSean.Wilcox@Sun.COM mc_used++;
32099263SSean.Wilcox@Sun.COM } else {
32109263SSean.Wilcox@Sun.COM (void) strcpy(cip->vbuf, ":default");
32110Sstevel@tonic-gate }
32120Sstevel@tonic-gate
32130Sstevel@tonic-gate switch (ret = get_projid(cip->vbuf, cip)) {
32140Sstevel@tonic-gate case 0:
32150Sstevel@tonic-gate break;
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate case ENOMEM:
32189765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, "Out of memory.");
32190Sstevel@tonic-gate goto out;
32200Sstevel@tonic-gate
32210Sstevel@tonic-gate case ENOENT:
32229765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32239765SSean.Wilcox@Sun.COM "Missing passwd or project entry for \"%s\".",
32249765SSean.Wilcox@Sun.COM cip->vbuf);
32250Sstevel@tonic-gate goto out;
32260Sstevel@tonic-gate
32270Sstevel@tonic-gate case EIO:
32289765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, "I/O error.");
32290Sstevel@tonic-gate goto out;
32300Sstevel@tonic-gate
32310Sstevel@tonic-gate case EMFILE:
32320Sstevel@tonic-gate case ENFILE:
32339765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32349765SSean.Wilcox@Sun.COM "Out of file descriptors.");
32350Sstevel@tonic-gate goto out;
32360Sstevel@tonic-gate
32370Sstevel@tonic-gate case -1:
32389765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32399765SSean.Wilcox@Sun.COM "Name service switch is misconfigured.");
32400Sstevel@tonic-gate goto out;
32410Sstevel@tonic-gate
32420Sstevel@tonic-gate case ERANGE:
32439765SSean.Wilcox@Sun.COM case E2BIG:
32449765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32459765SSean.Wilcox@Sun.COM "Project ID \"%s\" too big.", cip->vbuf);
32460Sstevel@tonic-gate goto out;
32470Sstevel@tonic-gate
32480Sstevel@tonic-gate case EINVAL:
32499765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32509765SSean.Wilcox@Sun.COM "Project ID \"%s\" is invalid.", cip->vbuf);
32510Sstevel@tonic-gate goto out;
32520Sstevel@tonic-gate
32530Sstevel@tonic-gate default:
32540Sstevel@tonic-gate bad_fail("get_projid", ret);
32550Sstevel@tonic-gate }
32560Sstevel@tonic-gate
32570Sstevel@tonic-gate /* get resource pool */
32589263SSean.Wilcox@Sun.COM if ((methpg != NULL && scf_pg_get_property(methpg,
32599263SSean.Wilcox@Sun.COM SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS) ||
32609263SSean.Wilcox@Sun.COM (instpg != NULL && scf_pg_get_property(instpg,
32619263SSean.Wilcox@Sun.COM SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS)) {
32629263SSean.Wilcox@Sun.COM if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
32639765SSean.Wilcox@Sun.COM ret = scf_error();
32649765SSean.Wilcox@Sun.COM switch (ret) {
32659263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
32669765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32679765SSean.Wilcox@Sun.COM RCBROKEN);
32689263SSean.Wilcox@Sun.COM break;
32699263SSean.Wilcox@Sun.COM
32709263SSean.Wilcox@Sun.COM case SCF_ERROR_CONSTRAINT_VIOLATED:
32719765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32729765SSean.Wilcox@Sun.COM "\"%s\" property has multiple "
32739765SSean.Wilcox@Sun.COM "values.",
32749765SSean.Wilcox@Sun.COM SCF_PROPERTY_RESOURCE_POOL);
32759263SSean.Wilcox@Sun.COM break;
32769263SSean.Wilcox@Sun.COM
32779263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
32789765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
32799765SSean.Wilcox@Sun.COM "\"%s\" property has no "
32809765SSean.Wilcox@Sun.COM "values.",
32819765SSean.Wilcox@Sun.COM SCF_PROPERTY_RESOURCE_POOL);
32829263SSean.Wilcox@Sun.COM break;
32839263SSean.Wilcox@Sun.COM
32849263SSean.Wilcox@Sun.COM default:
32859765SSean.Wilcox@Sun.COM bad_fail("scf_property_get_value", ret);
32869263SSean.Wilcox@Sun.COM }
32879263SSean.Wilcox@Sun.COM
32889263SSean.Wilcox@Sun.COM (void) strcpy(cip->vbuf, ":default");
32899263SSean.Wilcox@Sun.COM } else {
32909263SSean.Wilcox@Sun.COM ret = scf_value_get_astring(val, cip->vbuf,
32919263SSean.Wilcox@Sun.COM cip->vbuf_sz);
32929263SSean.Wilcox@Sun.COM assert(ret != -1);
32939263SSean.Wilcox@Sun.COM }
32949263SSean.Wilcox@Sun.COM
32959263SSean.Wilcox@Sun.COM mc_used++;
32969263SSean.Wilcox@Sun.COM } else {
32979765SSean.Wilcox@Sun.COM ret = scf_error();
32989765SSean.Wilcox@Sun.COM switch (ret) {
32999263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_FOUND:
33009263SSean.Wilcox@Sun.COM /* okay if missing. */
33019263SSean.Wilcox@Sun.COM (void) strcpy(cip->vbuf, ":default");
33029263SSean.Wilcox@Sun.COM break;
33039263SSean.Wilcox@Sun.COM
33049263SSean.Wilcox@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
33059765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret, RCBROKEN);
33069263SSean.Wilcox@Sun.COM goto out;
33079263SSean.Wilcox@Sun.COM
33089263SSean.Wilcox@Sun.COM case SCF_ERROR_DELETED:
33099765SSean.Wilcox@Sun.COM err = mc_error_create(err, ret,
33109765SSean.Wilcox@Sun.COM "property group could not be found.");
33119263SSean.Wilcox@Sun.COM goto out;
33129263SSean.Wilcox@Sun.COM
33139263SSean.Wilcox@Sun.COM case SCF_ERROR_HANDLE_MISMATCH:
33149263SSean.Wilcox@Sun.COM case SCF_ERROR_INVALID_ARGUMENT:
33159263SSean.Wilcox@Sun.COM case SCF_ERROR_NOT_SET:
33169263SSean.Wilcox@Sun.COM default:
33179765SSean.Wilcox@Sun.COM bad_fail("scf_pg_get_property", ret);
33189263SSean.Wilcox@Sun.COM }
33190Sstevel@tonic-gate }
33200Sstevel@tonic-gate
33210Sstevel@tonic-gate if (strcmp(cip->vbuf, ":default") != 0) {
33220Sstevel@tonic-gate cip->resource_pool = strdup(cip->vbuf);
33230Sstevel@tonic-gate if (cip->resource_pool == NULL) {
33249765SSean.Wilcox@Sun.COM err = mc_error_create(err, ENOMEM, ALLOCFAIL);
33250Sstevel@tonic-gate goto out;
33260Sstevel@tonic-gate }
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate }
33290Sstevel@tonic-gate
33309263SSean.Wilcox@Sun.COM /*
33319263SSean.Wilcox@Sun.COM * A method_context was not used for any configurable
33329263SSean.Wilcox@Sun.COM * elements or attributes, so reset and use the simple
33339263SSean.Wilcox@Sun.COM * defaults that provide historic init behavior.
33349263SSean.Wilcox@Sun.COM */
33359263SSean.Wilcox@Sun.COM if (mc_used == 0) {
33369263SSean.Wilcox@Sun.COM (void) memset(cip, 0, sizeof (*cip));
33379263SSean.Wilcox@Sun.COM cip->uid = 0;
33389263SSean.Wilcox@Sun.COM cip->gid = 0;
33399263SSean.Wilcox@Sun.COM cip->euid = (uid_t)-1;
33409263SSean.Wilcox@Sun.COM cip->egid = (gid_t)-1;
33419263SSean.Wilcox@Sun.COM }
33429263SSean.Wilcox@Sun.COM
33430Sstevel@tonic-gate *mcpp = cip;
33440Sstevel@tonic-gate
33450Sstevel@tonic-gate out:
33460Sstevel@tonic-gate (void) scf_value_destroy(val);
33470Sstevel@tonic-gate scf_property_destroy(prop);
33480Sstevel@tonic-gate scf_pg_destroy(instpg);
33490Sstevel@tonic-gate scf_pg_destroy(methpg);
33500Sstevel@tonic-gate
33510Sstevel@tonic-gate if (cip->pwbuf != NULL)
33520Sstevel@tonic-gate free(cip->pwbuf);
33530Sstevel@tonic-gate free(cip->vbuf);
33540Sstevel@tonic-gate
33559765SSean.Wilcox@Sun.COM if (err->type != 0) {
33560Sstevel@tonic-gate restarter_free_method_context(cip);
33579765SSean.Wilcox@Sun.COM } else {
33589765SSean.Wilcox@Sun.COM restarter_mc_error_destroy(err);
33599765SSean.Wilcox@Sun.COM err = NULL;
33609765SSean.Wilcox@Sun.COM }
33619765SSean.Wilcox@Sun.COM
33629765SSean.Wilcox@Sun.COM return (err);
33630Sstevel@tonic-gate }
33640Sstevel@tonic-gate
33650Sstevel@tonic-gate /*
33660Sstevel@tonic-gate * Modify the current process per the given method_context. On success, returns
33670Sstevel@tonic-gate * 0. Note that the environment is not modified by this function to include the
33680Sstevel@tonic-gate * environment variables in cip->env.
33690Sstevel@tonic-gate *
33700Sstevel@tonic-gate * On failure, sets *fp to NULL or the name of the function which failed,
33710Sstevel@tonic-gate * and returns one of the following error codes. The words in parentheses are
33720Sstevel@tonic-gate * the values to which *fp may be set for the error case.
33730Sstevel@tonic-gate * ENOMEM - malloc() failed
33740Sstevel@tonic-gate * EIO - an I/O error occurred (getpwuid_r, chdir)
33750Sstevel@tonic-gate * EMFILE - process is out of file descriptors (getpwuid_r)
33760Sstevel@tonic-gate * ENFILE - system is out of file handles (getpwuid_r)
33770Sstevel@tonic-gate * EINVAL - gid or egid is out of range (setregid)
33780Sstevel@tonic-gate * ngroups is too big (setgroups)
33790Sstevel@tonic-gate * project's project id is bad (setproject)
33800Sstevel@tonic-gate * uid or euid is out of range (setreuid)
33811712Srm88369 * poolname is invalid (pool_set_binding)
33820Sstevel@tonic-gate * EPERM - insufficient privilege (setregid, initgroups, setgroups, setppriv,
33830Sstevel@tonic-gate * setproject, setreuid, settaskid)
33840Sstevel@tonic-gate * ENOENT - uid has a passwd entry but no shadow entry
33850Sstevel@tonic-gate * working_dir does not exist (chdir)
33860Sstevel@tonic-gate * uid has no passwd entry
33870Sstevel@tonic-gate * the pool could not be found (pool_set_binding)
33880Sstevel@tonic-gate * EFAULT - lpriv_set or priv_set has a bad address (setppriv)
33890Sstevel@tonic-gate * working_dir has a bad address (chdir)
33900Sstevel@tonic-gate * EACCES - could not access working_dir (chdir)
33910Sstevel@tonic-gate * in a TASK_FINAL task (setproject, settaskid)
33920Sstevel@tonic-gate * no resource pool accepting default binding exists (setproject)
33930Sstevel@tonic-gate * ELOOP - too many symbolic links in working_dir (chdir)
33940Sstevel@tonic-gate * ENAMETOOLONG - working_dir is too long (chdir)
33950Sstevel@tonic-gate * ENOLINK - working_dir is on an inaccessible remote machine (chdir)
33960Sstevel@tonic-gate * ENOTDIR - working_dir is not a directory (chdir)
33970Sstevel@tonic-gate * ESRCH - uid is not a user of project (setproject)
33980Sstevel@tonic-gate * project is invalid (setproject)
33990Sstevel@tonic-gate * the resource pool specified for project is unknown (setproject)
34000Sstevel@tonic-gate * EBADF - the configuration for the pool is invalid (pool_set_binding)
34010Sstevel@tonic-gate * -1 - core_set_process_path() failed (core_set_process_path)
34020Sstevel@tonic-gate * a resource control assignment failed (setproject)
34030Sstevel@tonic-gate * a system error occurred during pool_set_binding (pool_set_binding)
34040Sstevel@tonic-gate */
34050Sstevel@tonic-gate int
restarter_set_method_context(struct method_context * cip,const char ** fp)34060Sstevel@tonic-gate restarter_set_method_context(struct method_context *cip, const char **fp)
34070Sstevel@tonic-gate {
34080Sstevel@tonic-gate pid_t mypid = -1;
34090Sstevel@tonic-gate int r, ret;
34100Sstevel@tonic-gate
34110Sstevel@tonic-gate cip->pwbuf = NULL;
34120Sstevel@tonic-gate *fp = NULL;
34130Sstevel@tonic-gate
34144321Scasper if (cip->gid != (gid_t)-1) {
34150Sstevel@tonic-gate if (setregid(cip->gid,
34164321Scasper cip->egid != (gid_t)-1 ? cip->egid : cip->gid) != 0) {
34170Sstevel@tonic-gate *fp = "setregid";
34180Sstevel@tonic-gate
34190Sstevel@tonic-gate ret = errno;
34200Sstevel@tonic-gate assert(ret == EINVAL || ret == EPERM);
34210Sstevel@tonic-gate goto out;
34220Sstevel@tonic-gate }
34230Sstevel@tonic-gate } else {
34240Sstevel@tonic-gate if (cip->pwbuf == NULL) {
34250Sstevel@tonic-gate switch (ret = lookup_pwd(cip)) {
34260Sstevel@tonic-gate case 0:
34270Sstevel@tonic-gate break;
34280Sstevel@tonic-gate
34290Sstevel@tonic-gate case ENOMEM:
34300Sstevel@tonic-gate case ENOENT:
34310Sstevel@tonic-gate *fp = NULL;
34320Sstevel@tonic-gate goto out;
34330Sstevel@tonic-gate
34340Sstevel@tonic-gate case EIO:
34350Sstevel@tonic-gate case EMFILE:
34360Sstevel@tonic-gate case ENFILE:
34370Sstevel@tonic-gate *fp = "getpwuid_r";
34380Sstevel@tonic-gate goto out;
34390Sstevel@tonic-gate
34400Sstevel@tonic-gate default:
34410Sstevel@tonic-gate bad_fail("lookup_pwd", ret);
34420Sstevel@tonic-gate }
34430Sstevel@tonic-gate }
34440Sstevel@tonic-gate
34450Sstevel@tonic-gate if (setregid(cip->pwd.pw_gid,
34464321Scasper cip->egid != (gid_t)-1 ?
34474321Scasper cip->egid : cip->pwd.pw_gid) != 0) {
34480Sstevel@tonic-gate *fp = "setregid";
34490Sstevel@tonic-gate
34500Sstevel@tonic-gate ret = errno;
34510Sstevel@tonic-gate assert(ret == EINVAL || ret == EPERM);
34520Sstevel@tonic-gate goto out;
34530Sstevel@tonic-gate }
34540Sstevel@tonic-gate }
34550Sstevel@tonic-gate
34560Sstevel@tonic-gate if (cip->ngroups == -1) {
34570Sstevel@tonic-gate if (cip->pwbuf == NULL) {
34580Sstevel@tonic-gate switch (ret = lookup_pwd(cip)) {
34590Sstevel@tonic-gate case 0:
34600Sstevel@tonic-gate break;
34610Sstevel@tonic-gate
34620Sstevel@tonic-gate case ENOMEM:
34630Sstevel@tonic-gate case ENOENT:
34640Sstevel@tonic-gate *fp = NULL;
34650Sstevel@tonic-gate goto out;
34660Sstevel@tonic-gate
34670Sstevel@tonic-gate case EIO:
34680Sstevel@tonic-gate case EMFILE:
34690Sstevel@tonic-gate case ENFILE:
34700Sstevel@tonic-gate *fp = "getpwuid_r";
34710Sstevel@tonic-gate goto out;
34720Sstevel@tonic-gate
34730Sstevel@tonic-gate default:
34740Sstevel@tonic-gate bad_fail("lookup_pwd", ret);
34750Sstevel@tonic-gate }
34760Sstevel@tonic-gate }
34770Sstevel@tonic-gate
34780Sstevel@tonic-gate /* Ok if cip->gid == -1 */
34790Sstevel@tonic-gate if (initgroups(cip->pwd.pw_name, cip->gid) != 0) {
34800Sstevel@tonic-gate *fp = "initgroups";
34810Sstevel@tonic-gate ret = errno;
34820Sstevel@tonic-gate assert(ret == EPERM);
34830Sstevel@tonic-gate goto out;
34840Sstevel@tonic-gate }
34850Sstevel@tonic-gate } else if (cip->ngroups > 0 &&
34860Sstevel@tonic-gate setgroups(cip->ngroups, cip->groups) != 0) {
34870Sstevel@tonic-gate *fp = "setgroups";
34880Sstevel@tonic-gate
34890Sstevel@tonic-gate ret = errno;
34900Sstevel@tonic-gate assert(ret == EINVAL || ret == EPERM);
34910Sstevel@tonic-gate goto out;
34920Sstevel@tonic-gate }
34930Sstevel@tonic-gate
34940Sstevel@tonic-gate if (cip->corefile_pattern != NULL) {
34950Sstevel@tonic-gate mypid = getpid();
34960Sstevel@tonic-gate
34970Sstevel@tonic-gate if (core_set_process_path(cip->corefile_pattern,
34980Sstevel@tonic-gate strlen(cip->corefile_pattern) + 1, mypid) != 0) {
34990Sstevel@tonic-gate *fp = "core_set_process_path";
35000Sstevel@tonic-gate ret = -1;
35010Sstevel@tonic-gate goto out;
35020Sstevel@tonic-gate }
35030Sstevel@tonic-gate }
35040Sstevel@tonic-gate
35050Sstevel@tonic-gate if (restarter_rm_libs_loadable()) {
35060Sstevel@tonic-gate if (cip->project == NULL) {
35070Sstevel@tonic-gate if (settaskid(getprojid(), TASK_NORMAL) == -1) {
35080Sstevel@tonic-gate switch (errno) {
35090Sstevel@tonic-gate case EACCES:
35100Sstevel@tonic-gate case EPERM:
35110Sstevel@tonic-gate *fp = "settaskid";
35120Sstevel@tonic-gate ret = errno;
35130Sstevel@tonic-gate goto out;
35140Sstevel@tonic-gate
35150Sstevel@tonic-gate case EINVAL:
35160Sstevel@tonic-gate default:
35170Sstevel@tonic-gate bad_fail("settaskid", errno);
35180Sstevel@tonic-gate }
35190Sstevel@tonic-gate }
35200Sstevel@tonic-gate } else {
35210Sstevel@tonic-gate switch (ret = lookup_pwd(cip)) {
35220Sstevel@tonic-gate case 0:
35230Sstevel@tonic-gate break;
35240Sstevel@tonic-gate
35250Sstevel@tonic-gate case ENOMEM:
35260Sstevel@tonic-gate case ENOENT:
35270Sstevel@tonic-gate *fp = NULL;
35280Sstevel@tonic-gate goto out;
35290Sstevel@tonic-gate
35300Sstevel@tonic-gate case EIO:
35310Sstevel@tonic-gate case EMFILE:
35320Sstevel@tonic-gate case ENFILE:
35330Sstevel@tonic-gate *fp = "getpwuid_r";
35340Sstevel@tonic-gate goto out;
35350Sstevel@tonic-gate
35360Sstevel@tonic-gate default:
35370Sstevel@tonic-gate bad_fail("lookup_pwd", ret);
35380Sstevel@tonic-gate }
35390Sstevel@tonic-gate
35400Sstevel@tonic-gate *fp = "setproject";
35410Sstevel@tonic-gate
35420Sstevel@tonic-gate switch (setproject(cip->project, cip->pwd.pw_name,
35430Sstevel@tonic-gate TASK_NORMAL)) {
35440Sstevel@tonic-gate case 0:
35450Sstevel@tonic-gate break;
35460Sstevel@tonic-gate
35470Sstevel@tonic-gate case SETPROJ_ERR_TASK:
35480Sstevel@tonic-gate case SETPROJ_ERR_POOL:
35490Sstevel@tonic-gate ret = errno;
35500Sstevel@tonic-gate goto out;
35510Sstevel@tonic-gate
35520Sstevel@tonic-gate default:
35530Sstevel@tonic-gate ret = -1;
35540Sstevel@tonic-gate goto out;
35550Sstevel@tonic-gate }
35560Sstevel@tonic-gate }
35570Sstevel@tonic-gate
35580Sstevel@tonic-gate if (cip->resource_pool != NULL) {
35590Sstevel@tonic-gate if (mypid == -1)
35600Sstevel@tonic-gate mypid = getpid();
35610Sstevel@tonic-gate
35620Sstevel@tonic-gate *fp = "pool_set_binding";
35630Sstevel@tonic-gate
35640Sstevel@tonic-gate if (pool_set_binding(cip->resource_pool, P_PID,
35650Sstevel@tonic-gate mypid) != PO_SUCCESS) {
35660Sstevel@tonic-gate switch (pool_error()) {
35671712Srm88369 case POE_INVALID_SEARCH:
35681712Srm88369 ret = ENOENT;
35691712Srm88369 break;
35701712Srm88369
35710Sstevel@tonic-gate case POE_BADPARAM:
35721712Srm88369 ret = EINVAL;
35730Sstevel@tonic-gate break;
35740Sstevel@tonic-gate
35750Sstevel@tonic-gate case POE_INVALID_CONF:
35760Sstevel@tonic-gate ret = EBADF;
35770Sstevel@tonic-gate break;
35780Sstevel@tonic-gate
35790Sstevel@tonic-gate case POE_SYSTEM:
35800Sstevel@tonic-gate ret = -1;
35810Sstevel@tonic-gate break;
35820Sstevel@tonic-gate
35830Sstevel@tonic-gate default:
35840Sstevel@tonic-gate bad_fail("pool_set_binding",
35850Sstevel@tonic-gate pool_error());
35860Sstevel@tonic-gate }
35870Sstevel@tonic-gate
35880Sstevel@tonic-gate goto out;
35890Sstevel@tonic-gate }
35900Sstevel@tonic-gate }
35910Sstevel@tonic-gate }
35920Sstevel@tonic-gate
35930Sstevel@tonic-gate /*
35943879Svp157776 * Now, we have to assume our ID. If the UID is 0, we want it to be
35953879Svp157776 * privilege-aware, otherwise the limit set gets used instead of E/P.
35960Sstevel@tonic-gate * We can do this by setting P as well, which keeps
35970Sstevel@tonic-gate * PA status (see priv_can_clear_PA()).
35980Sstevel@tonic-gate */
35990Sstevel@tonic-gate
36009263SSean.Wilcox@Sun.COM *fp = "setppriv";
36019263SSean.Wilcox@Sun.COM
36029263SSean.Wilcox@Sun.COM if (cip->lpriv_set != NULL) {
36039263SSean.Wilcox@Sun.COM if (setppriv(PRIV_SET, PRIV_LIMIT, cip->lpriv_set) != 0) {
36049263SSean.Wilcox@Sun.COM ret = errno;
36059263SSean.Wilcox@Sun.COM assert(ret == EFAULT || ret == EPERM);
36069263SSean.Wilcox@Sun.COM goto out;
36079263SSean.Wilcox@Sun.COM }
36089263SSean.Wilcox@Sun.COM }
36099263SSean.Wilcox@Sun.COM if (cip->priv_set != NULL) {
36109263SSean.Wilcox@Sun.COM if (setppriv(PRIV_SET, PRIV_INHERITABLE, cip->priv_set) != 0) {
36119263SSean.Wilcox@Sun.COM ret = errno;
36129263SSean.Wilcox@Sun.COM assert(ret == EFAULT || ret == EPERM);
36139263SSean.Wilcox@Sun.COM goto out;
36149263SSean.Wilcox@Sun.COM }
36159263SSean.Wilcox@Sun.COM }
36169263SSean.Wilcox@Sun.COM
36179263SSean.Wilcox@Sun.COM /*
36189263SSean.Wilcox@Sun.COM * If the limit privset is already set, then must be privilege
36199263SSean.Wilcox@Sun.COM * aware. Otherwise, don't assume anything, and force privilege
36209263SSean.Wilcox@Sun.COM * aware status.
36219263SSean.Wilcox@Sun.COM */
36229263SSean.Wilcox@Sun.COM
36239263SSean.Wilcox@Sun.COM if (cip->lpriv_set == NULL && cip->priv_set != NULL) {
36249263SSean.Wilcox@Sun.COM ret = setpflags(PRIV_AWARE, 1);
36259263SSean.Wilcox@Sun.COM assert(ret == 0);
36269263SSean.Wilcox@Sun.COM }
36279263SSean.Wilcox@Sun.COM
36280Sstevel@tonic-gate *fp = "setreuid";
36294321Scasper if (setreuid(cip->uid,
36304321Scasper cip->euid != (uid_t)-1 ? cip->euid : cip->uid) != 0) {
36310Sstevel@tonic-gate ret = errno;
36320Sstevel@tonic-gate assert(ret == EINVAL || ret == EPERM);
36330Sstevel@tonic-gate goto out;
36340Sstevel@tonic-gate }
36350Sstevel@tonic-gate
36360Sstevel@tonic-gate *fp = "setppriv";
36370Sstevel@tonic-gate if (cip->priv_set != NULL) {
36380Sstevel@tonic-gate if (setppriv(PRIV_SET, PRIV_PERMITTED, cip->priv_set) != 0) {
36390Sstevel@tonic-gate ret = errno;
36400Sstevel@tonic-gate assert(ret == EFAULT || ret == EPERM);
36410Sstevel@tonic-gate goto out;
36420Sstevel@tonic-gate }
36430Sstevel@tonic-gate }
36440Sstevel@tonic-gate
36453879Svp157776 /*
36463879Svp157776 * The last thing to do is chdir to the specified working directory.
36473879Svp157776 * This should come after the uid switching as only the user might
36483879Svp157776 * have access to the specified directory.
36493879Svp157776 */
36503879Svp157776 if (cip->working_dir != NULL) {
36515040Swesolows do {
36523879Svp157776 r = chdir(cip->working_dir);
36535040Swesolows } while (r != 0 && errno == EINTR);
36543879Svp157776 if (r != 0) {
36553879Svp157776 *fp = "chdir";
36563879Svp157776 ret = errno;
36573879Svp157776 goto out;
36583879Svp157776 }
36593879Svp157776 }
36603879Svp157776
36610Sstevel@tonic-gate ret = 0;
36620Sstevel@tonic-gate out:
36630Sstevel@tonic-gate free(cip->pwbuf);
36640Sstevel@tonic-gate cip->pwbuf = NULL;
36650Sstevel@tonic-gate return (ret);
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate
36680Sstevel@tonic-gate void
restarter_free_method_context(struct method_context * mcp)36690Sstevel@tonic-gate restarter_free_method_context(struct method_context *mcp)
36700Sstevel@tonic-gate {
36710Sstevel@tonic-gate size_t i;
36720Sstevel@tonic-gate
36730Sstevel@tonic-gate if (mcp->lpriv_set != NULL)
36740Sstevel@tonic-gate priv_freeset(mcp->lpriv_set);
36750Sstevel@tonic-gate if (mcp->priv_set != NULL)
36760Sstevel@tonic-gate priv_freeset(mcp->priv_set);
36770Sstevel@tonic-gate
36780Sstevel@tonic-gate if (mcp->env != NULL) {
36790Sstevel@tonic-gate for (i = 0; i < mcp->env_sz; i++)
36800Sstevel@tonic-gate free(mcp->env[i]);
36810Sstevel@tonic-gate free(mcp->env);
36820Sstevel@tonic-gate }
36830Sstevel@tonic-gate
36840Sstevel@tonic-gate free(mcp->working_dir);
36850Sstevel@tonic-gate free(mcp->corefile_pattern);
36860Sstevel@tonic-gate free(mcp->project);
36870Sstevel@tonic-gate free(mcp->resource_pool);
36880Sstevel@tonic-gate free(mcp);
36890Sstevel@tonic-gate }
36900Sstevel@tonic-gate
36910Sstevel@tonic-gate /*
36920Sstevel@tonic-gate * Method keyword functions
36930Sstevel@tonic-gate */
36940Sstevel@tonic-gate
36950Sstevel@tonic-gate int
restarter_is_null_method(const char * meth)36960Sstevel@tonic-gate restarter_is_null_method(const char *meth)
36970Sstevel@tonic-gate {
36980Sstevel@tonic-gate return (strcmp(meth, MKW_TRUE) == 0);
36990Sstevel@tonic-gate }
37000Sstevel@tonic-gate
37010Sstevel@tonic-gate static int
is_kill_method(const char * method,const char * kill_str,size_t kill_str_len)37020Sstevel@tonic-gate is_kill_method(const char *method, const char *kill_str,
37030Sstevel@tonic-gate size_t kill_str_len)
37040Sstevel@tonic-gate {
37050Sstevel@tonic-gate const char *cp;
37060Sstevel@tonic-gate int sig;
37070Sstevel@tonic-gate
37080Sstevel@tonic-gate if (strncmp(method, kill_str, kill_str_len) != 0 ||
37090Sstevel@tonic-gate (method[kill_str_len] != '\0' &&
37100Sstevel@tonic-gate !isspace(method[kill_str_len])))
37110Sstevel@tonic-gate return (-1);
37120Sstevel@tonic-gate
37130Sstevel@tonic-gate cp = method + kill_str_len;
37140Sstevel@tonic-gate while (*cp != '\0' && isspace(*cp))
37150Sstevel@tonic-gate ++cp;
37160Sstevel@tonic-gate
37170Sstevel@tonic-gate if (*cp == '\0')
37180Sstevel@tonic-gate return (SIGTERM);
37190Sstevel@tonic-gate
37200Sstevel@tonic-gate if (*cp != '-')
37210Sstevel@tonic-gate return (-1);
37220Sstevel@tonic-gate
37230Sstevel@tonic-gate return (str2sig(cp + 1, &sig) == 0 ? sig : -1);
37240Sstevel@tonic-gate }
37250Sstevel@tonic-gate
37260Sstevel@tonic-gate int
restarter_is_kill_proc_method(const char * method)37270Sstevel@tonic-gate restarter_is_kill_proc_method(const char *method)
37280Sstevel@tonic-gate {
37290Sstevel@tonic-gate return (is_kill_method(method, MKW_KILL_PROC,
37300Sstevel@tonic-gate sizeof (MKW_KILL_PROC) - 1));
37310Sstevel@tonic-gate }
37320Sstevel@tonic-gate
37330Sstevel@tonic-gate int
restarter_is_kill_method(const char * method)37340Sstevel@tonic-gate restarter_is_kill_method(const char *method)
37350Sstevel@tonic-gate {
37360Sstevel@tonic-gate return (is_kill_method(method, MKW_KILL, sizeof (MKW_KILL) - 1));
37370Sstevel@tonic-gate }
37380Sstevel@tonic-gate
37390Sstevel@tonic-gate /*
37400Sstevel@tonic-gate * Stubs for now.
37410Sstevel@tonic-gate */
37420Sstevel@tonic-gate
37430Sstevel@tonic-gate /* ARGSUSED */
37440Sstevel@tonic-gate int
restarter_event_get_enabled(restarter_event_t * e)37450Sstevel@tonic-gate restarter_event_get_enabled(restarter_event_t *e)
37460Sstevel@tonic-gate {
37470Sstevel@tonic-gate return (-1);
37480Sstevel@tonic-gate }
37490Sstevel@tonic-gate
37500Sstevel@tonic-gate /* ARGSUSED */
37510Sstevel@tonic-gate uint64_t
restarter_event_get_seq(restarter_event_t * e)37520Sstevel@tonic-gate restarter_event_get_seq(restarter_event_t *e)
37530Sstevel@tonic-gate {
37540Sstevel@tonic-gate return (-1);
37550Sstevel@tonic-gate }
37560Sstevel@tonic-gate
37570Sstevel@tonic-gate /* ARGSUSED */
37580Sstevel@tonic-gate void
restarter_event_get_time(restarter_event_t * e,hrtime_t * time)37590Sstevel@tonic-gate restarter_event_get_time(restarter_event_t *e, hrtime_t *time)
37600Sstevel@tonic-gate {
37610Sstevel@tonic-gate }
37628823STruong.Q.Nguyen@Sun.COM
37638823STruong.Q.Nguyen@Sun.COM /*
37648823STruong.Q.Nguyen@Sun.COM * Check for and validate fmri specified in restarter_actions/auxiliary_fmri
37658823STruong.Q.Nguyen@Sun.COM * 0 - Success
37668823STruong.Q.Nguyen@Sun.COM * 1 - Failure
37678823STruong.Q.Nguyen@Sun.COM */
37688823STruong.Q.Nguyen@Sun.COM int
restarter_inst_validate_ractions_aux_fmri(scf_instance_t * inst)37698823STruong.Q.Nguyen@Sun.COM restarter_inst_validate_ractions_aux_fmri(scf_instance_t *inst)
37708823STruong.Q.Nguyen@Sun.COM {
37718823STruong.Q.Nguyen@Sun.COM scf_handle_t *h;
37728823STruong.Q.Nguyen@Sun.COM scf_propertygroup_t *pg;
37738823STruong.Q.Nguyen@Sun.COM scf_property_t *prop;
37748823STruong.Q.Nguyen@Sun.COM scf_value_t *val;
37758823STruong.Q.Nguyen@Sun.COM char *aux_fmri;
37768823STruong.Q.Nguyen@Sun.COM size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
37778823STruong.Q.Nguyen@Sun.COM int ret = 1;
37788823STruong.Q.Nguyen@Sun.COM
37798823STruong.Q.Nguyen@Sun.COM if ((aux_fmri = malloc(size)) == NULL)
37808823STruong.Q.Nguyen@Sun.COM return (1);
37818823STruong.Q.Nguyen@Sun.COM
37828823STruong.Q.Nguyen@Sun.COM h = scf_instance_handle(inst);
37838823STruong.Q.Nguyen@Sun.COM
37848823STruong.Q.Nguyen@Sun.COM pg = scf_pg_create(h);
37858823STruong.Q.Nguyen@Sun.COM prop = scf_property_create(h);
37868823STruong.Q.Nguyen@Sun.COM val = scf_value_create(h);
37878823STruong.Q.Nguyen@Sun.COM if (pg == NULL || prop == NULL || val == NULL)
37888823STruong.Q.Nguyen@Sun.COM goto out;
37898823STruong.Q.Nguyen@Sun.COM
37908823STruong.Q.Nguyen@Sun.COM if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
37918823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
37928823STruong.Q.Nguyen@Sun.COM pg) != SCF_SUCCESS)
37938823STruong.Q.Nguyen@Sun.COM goto out;
37948823STruong.Q.Nguyen@Sun.COM
37958823STruong.Q.Nguyen@Sun.COM if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
37968823STruong.Q.Nguyen@Sun.COM prop, val) != SCF_SUCCESS)
37978823STruong.Q.Nguyen@Sun.COM goto out;
37988823STruong.Q.Nguyen@Sun.COM
37998823STruong.Q.Nguyen@Sun.COM if (scf_parse_fmri(aux_fmri, NULL, NULL, NULL, NULL, NULL,
38008823STruong.Q.Nguyen@Sun.COM NULL) != SCF_SUCCESS)
38018823STruong.Q.Nguyen@Sun.COM goto out;
38028823STruong.Q.Nguyen@Sun.COM
38038823STruong.Q.Nguyen@Sun.COM ret = 0;
38048823STruong.Q.Nguyen@Sun.COM
38058823STruong.Q.Nguyen@Sun.COM out:
38068823STruong.Q.Nguyen@Sun.COM free(aux_fmri);
38078823STruong.Q.Nguyen@Sun.COM scf_value_destroy(val);
38088823STruong.Q.Nguyen@Sun.COM scf_property_destroy(prop);
38098823STruong.Q.Nguyen@Sun.COM scf_pg_destroy(pg);
38108823STruong.Q.Nguyen@Sun.COM return (ret);
38118823STruong.Q.Nguyen@Sun.COM }
38128823STruong.Q.Nguyen@Sun.COM
38138823STruong.Q.Nguyen@Sun.COM /*
38148823STruong.Q.Nguyen@Sun.COM * Get instance's boolean value in restarter_actions/auxiliary_tty
38158823STruong.Q.Nguyen@Sun.COM * Return -1 on failure
38168823STruong.Q.Nguyen@Sun.COM */
38178823STruong.Q.Nguyen@Sun.COM int
restarter_inst_ractions_from_tty(scf_instance_t * inst)38188823STruong.Q.Nguyen@Sun.COM restarter_inst_ractions_from_tty(scf_instance_t *inst)
38198823STruong.Q.Nguyen@Sun.COM {
38208823STruong.Q.Nguyen@Sun.COM scf_handle_t *h;
38218823STruong.Q.Nguyen@Sun.COM scf_propertygroup_t *pg;
38228823STruong.Q.Nguyen@Sun.COM scf_property_t *prop;
38238823STruong.Q.Nguyen@Sun.COM scf_value_t *val;
38248823STruong.Q.Nguyen@Sun.COM uint8_t has_tty;
38258823STruong.Q.Nguyen@Sun.COM int ret = -1;
38268823STruong.Q.Nguyen@Sun.COM
38278823STruong.Q.Nguyen@Sun.COM h = scf_instance_handle(inst);
38288823STruong.Q.Nguyen@Sun.COM pg = scf_pg_create(h);
38298823STruong.Q.Nguyen@Sun.COM prop = scf_property_create(h);
38308823STruong.Q.Nguyen@Sun.COM val = scf_value_create(h);
38318823STruong.Q.Nguyen@Sun.COM if (pg == NULL || prop == NULL || val == NULL)
38328823STruong.Q.Nguyen@Sun.COM goto out;
38338823STruong.Q.Nguyen@Sun.COM
38348823STruong.Q.Nguyen@Sun.COM if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
38358823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
38368823STruong.Q.Nguyen@Sun.COM pg) != SCF_SUCCESS)
38378823STruong.Q.Nguyen@Sun.COM goto out;
38388823STruong.Q.Nguyen@Sun.COM
38398823STruong.Q.Nguyen@Sun.COM if (get_boolean_val(pg, SCF_PROPERTY_AUX_TTY, &has_tty, prop,
38408823STruong.Q.Nguyen@Sun.COM val) != SCF_SUCCESS)
38418823STruong.Q.Nguyen@Sun.COM goto out;
38428823STruong.Q.Nguyen@Sun.COM
38438823STruong.Q.Nguyen@Sun.COM ret = has_tty;
38448823STruong.Q.Nguyen@Sun.COM
38458823STruong.Q.Nguyen@Sun.COM out:
38468823STruong.Q.Nguyen@Sun.COM scf_value_destroy(val);
38478823STruong.Q.Nguyen@Sun.COM scf_property_destroy(prop);
38488823STruong.Q.Nguyen@Sun.COM scf_pg_destroy(pg);
38498823STruong.Q.Nguyen@Sun.COM return (ret);
38508823STruong.Q.Nguyen@Sun.COM }
38518823STruong.Q.Nguyen@Sun.COM
38528823STruong.Q.Nguyen@Sun.COM static int
restarter_inst_set_astring_prop(scf_instance_t * inst,const char * pgname,const char * pgtype,uint32_t pgflags,const char * pname,const char * str)38538823STruong.Q.Nguyen@Sun.COM restarter_inst_set_astring_prop(scf_instance_t *inst, const char *pgname,
38548823STruong.Q.Nguyen@Sun.COM const char *pgtype, uint32_t pgflags, const char *pname, const char *str)
38558823STruong.Q.Nguyen@Sun.COM {
38568823STruong.Q.Nguyen@Sun.COM scf_handle_t *h;
38578823STruong.Q.Nguyen@Sun.COM scf_propertygroup_t *pg;
38588823STruong.Q.Nguyen@Sun.COM scf_transaction_t *t;
38598823STruong.Q.Nguyen@Sun.COM scf_transaction_entry_t *e;
38608823STruong.Q.Nguyen@Sun.COM scf_value_t *v;
38618823STruong.Q.Nguyen@Sun.COM int ret = 1, r;
38628823STruong.Q.Nguyen@Sun.COM
38638823STruong.Q.Nguyen@Sun.COM h = scf_instance_handle(inst);
38648823STruong.Q.Nguyen@Sun.COM
38658823STruong.Q.Nguyen@Sun.COM pg = scf_pg_create(h);
38668823STruong.Q.Nguyen@Sun.COM t = scf_transaction_create(h);
38678823STruong.Q.Nguyen@Sun.COM e = scf_entry_create(h);
38688823STruong.Q.Nguyen@Sun.COM v = scf_value_create(h);
38698823STruong.Q.Nguyen@Sun.COM if (pg == NULL || t == NULL || e == NULL || v == NULL)
38708823STruong.Q.Nguyen@Sun.COM goto out;
38718823STruong.Q.Nguyen@Sun.COM
38728823STruong.Q.Nguyen@Sun.COM if (instance_get_or_add_pg(inst, pgname, pgtype, pgflags, pg))
38738823STruong.Q.Nguyen@Sun.COM goto out;
38748823STruong.Q.Nguyen@Sun.COM
38758823STruong.Q.Nguyen@Sun.COM if (scf_value_set_astring(v, str) != SCF_SUCCESS)
38768823STruong.Q.Nguyen@Sun.COM goto out;
38778823STruong.Q.Nguyen@Sun.COM
38788823STruong.Q.Nguyen@Sun.COM for (;;) {
38798823STruong.Q.Nguyen@Sun.COM if (scf_transaction_start(t, pg) != 0)
38808823STruong.Q.Nguyen@Sun.COM goto out;
38818823STruong.Q.Nguyen@Sun.COM
38828823STruong.Q.Nguyen@Sun.COM if (tx_set_value(t, e, pname, SCF_TYPE_ASTRING, v) != 0)
38838823STruong.Q.Nguyen@Sun.COM goto out;
38848823STruong.Q.Nguyen@Sun.COM
38858823STruong.Q.Nguyen@Sun.COM if ((r = scf_transaction_commit(t)) == 1)
38868823STruong.Q.Nguyen@Sun.COM break;
38878823STruong.Q.Nguyen@Sun.COM
38888823STruong.Q.Nguyen@Sun.COM if (r == -1)
38898823STruong.Q.Nguyen@Sun.COM goto out;
38908823STruong.Q.Nguyen@Sun.COM
38918823STruong.Q.Nguyen@Sun.COM scf_transaction_reset(t);
38928823STruong.Q.Nguyen@Sun.COM if (scf_pg_update(pg) == -1)
38938823STruong.Q.Nguyen@Sun.COM goto out;
38948823STruong.Q.Nguyen@Sun.COM }
38958823STruong.Q.Nguyen@Sun.COM ret = 0;
38968823STruong.Q.Nguyen@Sun.COM
38978823STruong.Q.Nguyen@Sun.COM out:
38988823STruong.Q.Nguyen@Sun.COM scf_transaction_destroy(t);
38998823STruong.Q.Nguyen@Sun.COM scf_entry_destroy(e);
39008823STruong.Q.Nguyen@Sun.COM scf_value_destroy(v);
39018823STruong.Q.Nguyen@Sun.COM scf_pg_destroy(pg);
39028823STruong.Q.Nguyen@Sun.COM
39038823STruong.Q.Nguyen@Sun.COM return (ret);
39048823STruong.Q.Nguyen@Sun.COM }
39058823STruong.Q.Nguyen@Sun.COM
39068823STruong.Q.Nguyen@Sun.COM int
restarter_inst_set_aux_fmri(scf_instance_t * inst)39078823STruong.Q.Nguyen@Sun.COM restarter_inst_set_aux_fmri(scf_instance_t *inst)
39088823STruong.Q.Nguyen@Sun.COM {
39098823STruong.Q.Nguyen@Sun.COM scf_handle_t *h;
39108823STruong.Q.Nguyen@Sun.COM scf_propertygroup_t *pg;
39118823STruong.Q.Nguyen@Sun.COM scf_property_t *prop;
39128823STruong.Q.Nguyen@Sun.COM scf_value_t *val;
39138823STruong.Q.Nguyen@Sun.COM char *aux_fmri;
39148823STruong.Q.Nguyen@Sun.COM size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
39158823STruong.Q.Nguyen@Sun.COM int ret = 1;
39168823STruong.Q.Nguyen@Sun.COM
39178823STruong.Q.Nguyen@Sun.COM if ((aux_fmri = malloc(size)) == NULL)
39188823STruong.Q.Nguyen@Sun.COM return (1);
39198823STruong.Q.Nguyen@Sun.COM
39208823STruong.Q.Nguyen@Sun.COM h = scf_instance_handle(inst);
39218823STruong.Q.Nguyen@Sun.COM
39228823STruong.Q.Nguyen@Sun.COM pg = scf_pg_create(h);
39238823STruong.Q.Nguyen@Sun.COM prop = scf_property_create(h);
39248823STruong.Q.Nguyen@Sun.COM val = scf_value_create(h);
39258823STruong.Q.Nguyen@Sun.COM if (pg == NULL || prop == NULL || val == NULL)
39268823STruong.Q.Nguyen@Sun.COM goto out;
39278823STruong.Q.Nguyen@Sun.COM
39288823STruong.Q.Nguyen@Sun.COM /*
39298823STruong.Q.Nguyen@Sun.COM * Get auxiliary_fmri value from restarter_actions pg
39308823STruong.Q.Nguyen@Sun.COM */
39318823STruong.Q.Nguyen@Sun.COM if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
39328823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
39338823STruong.Q.Nguyen@Sun.COM pg) != SCF_SUCCESS)
39348823STruong.Q.Nguyen@Sun.COM goto out;
39358823STruong.Q.Nguyen@Sun.COM
39368823STruong.Q.Nguyen@Sun.COM if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
39378823STruong.Q.Nguyen@Sun.COM prop, val) != SCF_SUCCESS)
39388823STruong.Q.Nguyen@Sun.COM goto out;
39398823STruong.Q.Nguyen@Sun.COM
39408823STruong.Q.Nguyen@Sun.COM /*
39418823STruong.Q.Nguyen@Sun.COM * Populate restarter/auxiliary_fmri with the obtained fmri.
39428823STruong.Q.Nguyen@Sun.COM */
39438823STruong.Q.Nguyen@Sun.COM ret = restarter_inst_set_astring_prop(inst, SCF_PG_RESTARTER,
39448823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS,
39458823STruong.Q.Nguyen@Sun.COM SCF_PROPERTY_AUX_FMRI, aux_fmri);
39468823STruong.Q.Nguyen@Sun.COM
39478823STruong.Q.Nguyen@Sun.COM out:
39488823STruong.Q.Nguyen@Sun.COM free(aux_fmri);
39498823STruong.Q.Nguyen@Sun.COM scf_value_destroy(val);
39508823STruong.Q.Nguyen@Sun.COM scf_property_destroy(prop);
39518823STruong.Q.Nguyen@Sun.COM scf_pg_destroy(pg);
39528823STruong.Q.Nguyen@Sun.COM return (ret);
39538823STruong.Q.Nguyen@Sun.COM }
39548823STruong.Q.Nguyen@Sun.COM
39558823STruong.Q.Nguyen@Sun.COM int
restarter_inst_reset_aux_fmri(scf_instance_t * inst)39568823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_aux_fmri(scf_instance_t *inst)
39578823STruong.Q.Nguyen@Sun.COM {
39588823STruong.Q.Nguyen@Sun.COM return (scf_instance_delete_prop(inst,
39598823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER, SCF_PROPERTY_AUX_FMRI));
39608823STruong.Q.Nguyen@Sun.COM }
39618823STruong.Q.Nguyen@Sun.COM
39628823STruong.Q.Nguyen@Sun.COM int
restarter_inst_reset_ractions_aux_fmri(scf_instance_t * inst)39638823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_ractions_aux_fmri(scf_instance_t *inst)
39648823STruong.Q.Nguyen@Sun.COM {
39658823STruong.Q.Nguyen@Sun.COM return (scf_instance_delete_prop(inst,
39668823STruong.Q.Nguyen@Sun.COM SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_FMRI));
39678823STruong.Q.Nguyen@Sun.COM }
3968