4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #ifdef _FILE_OFFSET_BITS
28 #undef _FILE_OFFSET_BITS
29 #endif /* _FILE_OFFSET_BITS */
31 #include <sys/contract/process.h>
33 #include <sys/types.h>
37 #include <libcontract.h>
38 #include <libcontract_priv.h>
49 contract_abandon(ctid_t ctid
)
55 err
= contract_abandon_id(ctid
);
58 log_framework(LOG_NOTICE
,
59 "failed to abandon contract %ld: %s\n", ctid
,
64 contract_kill(ctid_t ctid
, int sig
, const char *fmri
)
66 if (sigsend(P_CTID
, ctid
, sig
) == -1 && errno
!= ESRCH
) {
67 log_error(LOG_WARNING
,
68 "%s: Could not signal all contract members: %s\n", fmri
,
80 ctid_t ctid
, configd_ctid
= -1;
89 * 2. Acquire any contracts we should have inherited. First, find the
90 * contract we belong to, then get its status.
92 if ((psfd
= open("/proc/self/psinfo", O_RDONLY
)) < 0) {
93 log_error(LOG_WARNING
, "Can not open /proc/self/psinfo; unable "
94 "to check to adopt contracts: %s\n", strerror(errno
));
98 if (read(psfd
, &psi
, sizeof (psinfo_t
)) != sizeof (psinfo_t
)) {
99 log_error(LOG_WARNING
, "Can not read from /proc/self/psinfo; "
100 "unable to adopt contracts: %s\n",
106 ctid
= psi
.pr_contract
;
110 if ((csfd
= contract_open(ctid
, "process", "status", O_RDONLY
)) < 0) {
111 log_error(LOG_WARNING
, "Can not open containing contract "
112 "status; unable to adopt contracts: %s\n", strerror(errno
));
116 /* 3. Go about adopting our member list. */
118 err
= ct_status_read(csfd
, CTD_ALL
, &s
);
121 log_error(LOG_WARNING
, "Can not read containing contract "
122 "status; unable to adopt: %s\n", strerror(err
));
126 if (err
= ct_pr_status_get_contracts(s
, &ctids
, &nctids
)) {
127 log_error(LOG_WARNING
, "Can not get my inherited contracts; "
128 "unable to adopt: %s\n", strerror(err
));
135 * We're booting, as a svc.startd which managed to fork a
136 * child will always have a svc.configd contract to adopt.
144 * We're restarting after an interruption of some kind.
146 log_framework(LOG_NOTICE
, "restarting after interruption\n");
150 * 3'. Loop through the array, adopting them all where possible, and
151 * noting which one contains svc.configd (via a cookie vlaue of
154 for (n
= 0; n
< nctids
; n
++) {
158 if ((ccfd
= contract_open(ctids
[n
], "process", "ctl",
160 log_error(LOG_WARNING
, "Can not open contract %ld ctl "
161 "for adoption: %s\n", ctids
[n
], strerror(err
));
166 if ((csfd
= contract_open(ctids
[n
], "process", "status",
168 log_error(LOG_WARNING
, "Can not open contract %ld "
169 "status for cookie: %s\n", ctids
[n
], strerror(err
));
175 if (err
= ct_ctl_adopt(ccfd
)) {
176 log_error(LOG_WARNING
, "Can not adopt contract %ld: "
177 "%s\n", ctids
[n
], strerror(err
));
186 if (err
= ct_status_read(csfd
, CTD_COMMON
, &cs
)) {
187 log_error(LOG_WARNING
, "Can not read contract %ld"
188 "status; unable to fetch cookie: %s\n", ctids
[n
],
197 if (ct_status_get_cookie(cs
) == CONFIGD_COOKIE
)
198 configd_ctid
= ctids
[n
];
207 return (configd_ctid
);
211 contract_is_empty(ctid_t ctid
)
219 fd
= contract_open(ctid
, "process", "status", O_RDONLY
);
223 ret
= ct_status_read(fd
, CTD_ALL
, &ctstat
);
228 ret
= ct_pr_status_get_members(ctstat
, &members
, &num
);
229 ct_status_free(ctstat
);
239 typedef struct contract_bucket
{
240 pthread_mutex_t cb_lock
;
244 #define CI_HASH_SIZE 64
245 #define CI_HASH_MASK (CI_HASH_SIZE - 1);
248 * contract_hash is a hash table of contract ids to restarter instance
249 * IDs. It can be used for quick lookups when processing contract events,
250 * because the restarter instance lock doesn't need to be held to access
253 static contract_bucket_t contract_hash
[CI_HASH_SIZE
];
255 static contract_bucket_t
*
256 contract_hold_bucket(ctid_t ctid
)
258 contract_bucket_t
*bp
;
261 hash
= ctid
& CI_HASH_MASK
;
263 bp
= &contract_hash
[hash
];
264 MUTEX_LOCK(&bp
->cb_lock
);
269 contract_release_bucket(contract_bucket_t
*bp
)
271 assert(MUTEX_HELD(&bp
->cb_lock
));
272 MUTEX_UNLOCK(&bp
->cb_lock
);
275 static contract_entry_t
*
276 contract_lookup(contract_bucket_t
*bp
, ctid_t ctid
)
278 contract_entry_t
*ce
;
280 assert(MUTEX_HELD(&bp
->cb_lock
));
282 if (bp
->cb_list
== NULL
)
285 for (ce
= uu_list_first(bp
->cb_list
); ce
!= NULL
;
286 ce
= uu_list_next(bp
->cb_list
, ce
)) {
287 if (ce
->ce_ctid
== ctid
)
295 contract_insert(contract_bucket_t
*bp
, contract_entry_t
*ce
)
299 if (bp
->cb_list
== NULL
)
300 bp
->cb_list
= startd_list_create(contract_list_pool
, bp
, 0);
302 uu_list_node_init(ce
, &ce
->ce_link
, contract_list_pool
);
303 r
= uu_list_insert_before(bp
->cb_list
, NULL
, ce
);
312 for (i
= 0; i
< CI_HASH_SIZE
; i
++)
313 (void) pthread_mutex_init(&contract_hash
[i
].cb_lock
,
318 contract_hash_store(ctid_t ctid
, int instid
)
320 contract_bucket_t
*bp
;
321 contract_entry_t
*ce
;
323 bp
= contract_hold_bucket(ctid
);
324 assert(contract_lookup(bp
, ctid
) == NULL
);
325 ce
= startd_alloc(sizeof (contract_entry_t
));
327 ce
->ce_instid
= instid
;
329 contract_insert(bp
, ce
);
331 contract_release_bucket(bp
);
335 contract_hash_remove(ctid_t ctid
)
337 contract_bucket_t
*bp
;
338 contract_entry_t
*ce
;
340 bp
= contract_hold_bucket(ctid
);
342 ce
= contract_lookup(bp
, ctid
);
344 uu_list_remove(bp
->cb_list
, ce
);
345 startd_free(ce
, sizeof (contract_entry_t
));
348 contract_release_bucket(bp
);
352 * int lookup_inst_by_contract()
353 * Lookup the instance id in the hash table by the contract id.
354 * Returns instid if found, -1 if not. Doesn't do a hold on the
355 * instance, so a check for continued existence is required.
358 lookup_inst_by_contract(ctid_t ctid
)
360 contract_bucket_t
*bp
;
361 contract_entry_t
*ce
;
364 bp
= contract_hold_bucket(ctid
);
365 ce
= contract_lookup(bp
, ctid
);
368 contract_release_bucket(bp
);