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 #include <sys/contract/process.h>
29 #include <sys/types.h>
33 #include <libcontract.h>
34 #include <libcontract_priv.h>
45 contract_abandon(ctid_t ctid
)
51 err
= contract_abandon_id(ctid
);
54 log_framework(LOG_NOTICE
,
55 "failed to abandon contract %ld: %s\n", ctid
,
60 contract_kill(ctid_t ctid
, int sig
, const char *fmri
)
62 if (sigsend(P_CTID
, ctid
, sig
) == -1 && errno
!= ESRCH
) {
63 log_error(LOG_WARNING
,
64 "%s: Could not signal all contract members: %s\n", fmri
,
76 ctid_t ctid
, configd_ctid
= -1;
85 * 2. Acquire any contracts we should have inherited. First, find the
86 * contract we belong to, then get its status.
88 if ((psfd
= open("/proc/self/psinfo", O_RDONLY
)) < 0) {
89 log_error(LOG_WARNING
, "Can not open /proc/self/psinfo; unable "
90 "to check to adopt contracts: %s\n", strerror(errno
));
94 if (read(psfd
, &psi
, sizeof (psinfo_t
)) != sizeof (psinfo_t
)) {
95 log_error(LOG_WARNING
, "Can not read from /proc/self/psinfo; "
96 "unable to adopt contracts: %s\n",
102 ctid
= psi
.pr_contract
;
106 if ((csfd
= contract_open(ctid
, "process", "status", O_RDONLY
)) < 0) {
107 log_error(LOG_WARNING
, "Can not open containing contract "
108 "status; unable to adopt contracts: %s\n", strerror(errno
));
112 /* 3. Go about adopting our member list. */
114 err
= ct_status_read(csfd
, CTD_ALL
, &s
);
117 log_error(LOG_WARNING
, "Can not read containing contract "
118 "status; unable to adopt: %s\n", strerror(err
));
122 if (err
= ct_pr_status_get_contracts(s
, &ctids
, &nctids
)) {
123 log_error(LOG_WARNING
, "Can not get my inherited contracts; "
124 "unable to adopt: %s\n", strerror(err
));
131 * We're booting, as a svc.startd which managed to fork a
132 * child will always have a svc.configd contract to adopt.
140 * We're restarting after an interruption of some kind.
142 log_framework(LOG_NOTICE
, "restarting after interruption\n");
146 * 3'. Loop through the array, adopting them all where possible, and
147 * noting which one contains svc.configd (via a cookie vlaue of
150 for (n
= 0; n
< nctids
; n
++) {
154 if ((ccfd
= contract_open(ctids
[n
], "process", "ctl",
156 log_error(LOG_WARNING
, "Can not open contract %ld ctl "
157 "for adoption: %s\n", ctids
[n
], strerror(err
));
162 if ((csfd
= contract_open(ctids
[n
], "process", "status",
164 log_error(LOG_WARNING
, "Can not open contract %ld "
165 "status for cookie: %s\n", ctids
[n
], strerror(err
));
171 if (err
= ct_ctl_adopt(ccfd
)) {
172 log_error(LOG_WARNING
, "Can not adopt contract %ld: "
173 "%s\n", ctids
[n
], strerror(err
));
182 if (err
= ct_status_read(csfd
, CTD_COMMON
, &cs
)) {
183 log_error(LOG_WARNING
, "Can not read contract %ld"
184 "status; unable to fetch cookie: %s\n", ctids
[n
],
193 if (ct_status_get_cookie(cs
) == CONFIGD_COOKIE
)
194 configd_ctid
= ctids
[n
];
203 return (configd_ctid
);
207 contract_is_empty(ctid_t ctid
)
215 fd
= contract_open(ctid
, "process", "status", O_RDONLY
);
219 ret
= ct_status_read(fd
, CTD_ALL
, &ctstat
);
224 ret
= ct_pr_status_get_members(ctstat
, &members
, &num
);
225 ct_status_free(ctstat
);
235 typedef struct contract_bucket
{
236 pthread_mutex_t cb_lock
;
240 #define CI_HASH_SIZE 64
241 #define CI_HASH_MASK (CI_HASH_SIZE - 1);
244 * contract_hash is a hash table of contract ids to restarter instance
245 * IDs. It can be used for quick lookups when processing contract events,
246 * because the restarter instance lock doesn't need to be held to access
249 static contract_bucket_t contract_hash
[CI_HASH_SIZE
];
251 static contract_bucket_t
*
252 contract_hold_bucket(ctid_t ctid
)
254 contract_bucket_t
*bp
;
257 hash
= ctid
& CI_HASH_MASK
;
259 bp
= &contract_hash
[hash
];
260 MUTEX_LOCK(&bp
->cb_lock
);
265 contract_release_bucket(contract_bucket_t
*bp
)
267 assert(MUTEX_HELD(&bp
->cb_lock
));
268 MUTEX_UNLOCK(&bp
->cb_lock
);
271 static contract_entry_t
*
272 contract_lookup(contract_bucket_t
*bp
, ctid_t ctid
)
274 contract_entry_t
*ce
;
276 assert(MUTEX_HELD(&bp
->cb_lock
));
278 if (bp
->cb_list
== NULL
)
281 for (ce
= uu_list_first(bp
->cb_list
); ce
!= NULL
;
282 ce
= uu_list_next(bp
->cb_list
, ce
)) {
283 if (ce
->ce_ctid
== ctid
)
291 contract_insert(contract_bucket_t
*bp
, contract_entry_t
*ce
)
295 if (bp
->cb_list
== NULL
)
296 bp
->cb_list
= startd_list_create(contract_list_pool
, bp
, 0);
298 uu_list_node_init(ce
, &ce
->ce_link
, contract_list_pool
);
299 r
= uu_list_insert_before(bp
->cb_list
, NULL
, ce
);
308 for (i
= 0; i
< CI_HASH_SIZE
; i
++)
309 (void) pthread_mutex_init(&contract_hash
[i
].cb_lock
,
314 contract_hash_store(ctid_t ctid
, int instid
)
316 contract_bucket_t
*bp
;
317 contract_entry_t
*ce
;
319 bp
= contract_hold_bucket(ctid
);
320 assert(contract_lookup(bp
, ctid
) == NULL
);
321 ce
= startd_alloc(sizeof (contract_entry_t
));
323 ce
->ce_instid
= instid
;
325 contract_insert(bp
, ce
);
327 contract_release_bucket(bp
);
331 contract_hash_remove(ctid_t ctid
)
333 contract_bucket_t
*bp
;
334 contract_entry_t
*ce
;
336 bp
= contract_hold_bucket(ctid
);
338 ce
= contract_lookup(bp
, ctid
);
340 uu_list_remove(bp
->cb_list
, ce
);
341 startd_free(ce
, sizeof (contract_entry_t
));
344 contract_release_bucket(bp
);
348 * int lookup_inst_by_contract()
349 * Lookup the instance id in the hash table by the contract id.
350 * Returns instid if found, -1 if not. Doesn't do a hold on the
351 * instance, so a check for continued existence is required.
354 lookup_inst_by_contract(ctid_t ctid
)
356 contract_bucket_t
*bp
;
357 contract_entry_t
*ce
;
360 bp
= contract_hold_bucket(ctid
);
361 ce
= contract_lookup(bp
, ctid
);
364 contract_release_bucket(bp
);