xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_rpc_adm.c (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <strings.h>
30 #include <limits.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <alloca.h>
34 
35 #include <fmd_rpc_adm.h>
36 #include <fmd_rpc.h>
37 #include <fmd_module.h>
38 #include <fmd_ustat.h>
39 #include <fmd_error.h>
40 #include <fmd_asru.h>
41 #include <fmd_ckpt.h>
42 #include <fmd_case.h>
43 #include <fmd_fmri.h>
44 #include <fmd_idspace.h>
45 #include <fmd_xprt.h>
46 
47 #include <fmd.h>
48 
49 bool_t
50 fmd_adm_modinfo_1_svc(struct fmd_rpc_modlist *rvp, struct svc_req *req)
51 {
52 	struct fmd_rpc_modinfo *rmi;
53 	fmd_module_t *mp;
54 
55 	rvp->rml_list = NULL;
56 	rvp->rml_err = 0;
57 	rvp->rml_len = 0;
58 
59 	if (fmd_rpc_deny(req)) {
60 		rvp->rml_err = FMD_ADM_ERR_PERM;
61 		return (TRUE);
62 	}
63 
64 	(void) pthread_mutex_lock(&fmd.d_mod_lock);
65 
66 	for (mp = fmd_list_next(&fmd.d_mod_list);
67 	    mp != NULL; mp = fmd_list_next(mp)) {
68 
69 		if ((rmi = malloc(sizeof (struct fmd_rpc_modinfo))) == NULL) {
70 			rvp->rml_err = FMD_ADM_ERR_NOMEM;
71 			break;
72 		}
73 
74 		fmd_module_lock(mp);
75 
76 		/*
77 		 * If mod_info is NULL, the module is in the middle of loading:
78 		 * do not report its presence to observability tools yet.
79 		 */
80 		if (mp->mod_info == NULL) {
81 			fmd_module_unlock(mp);
82 			free(rmi);
83 			continue;
84 		}
85 
86 		rmi->rmi_name = strdup(mp->mod_name);
87 		rmi->rmi_desc = strdup(mp->mod_info->fmdi_desc);
88 		rmi->rmi_vers = strdup(mp->mod_info->fmdi_vers);
89 		rmi->rmi_faulty = mp->mod_error != 0;
90 		rmi->rmi_next = rvp->rml_list;
91 
92 		fmd_module_unlock(mp);
93 		rvp->rml_list = rmi;
94 		rvp->rml_len++;
95 
96 		if (rmi->rmi_desc == NULL || rmi->rmi_vers == NULL) {
97 			rvp->rml_err = FMD_ADM_ERR_NOMEM;
98 			break;
99 		}
100 	}
101 
102 	(void) pthread_mutex_unlock(&fmd.d_mod_lock);
103 	return (TRUE);
104 }
105 
106 bool_t
107 fmd_adm_modcstat_1_svc(char *name,
108     struct fmd_rpc_modstat *rms, struct svc_req *req)
109 {
110 	fmd_ustat_snap_t snap;
111 	fmd_module_t *mp;
112 
113 	rms->rms_buf.rms_buf_val = NULL;
114 	rms->rms_buf.rms_buf_len = 0;
115 	rms->rms_err = 0;
116 
117 	if (fmd_rpc_deny(req)) {
118 		rms->rms_err = FMD_ADM_ERR_PERM;
119 		return (TRUE);
120 	}
121 
122 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL) {
123 		rms->rms_err = FMD_ADM_ERR_MODSRCH;
124 		return (TRUE);
125 	}
126 
127 	if (fmd_modstat_snapshot(mp, &snap) == 0) {
128 		rms->rms_buf.rms_buf_val = snap.uss_buf;
129 		rms->rms_buf.rms_buf_len = snap.uss_len;
130 	} else if (errno == EFMD_HDL_ABORT) {
131 		rms->rms_err = FMD_ADM_ERR_MODFAIL;
132 	} else
133 		rms->rms_err = FMD_ADM_ERR_NOMEM;
134 
135 	fmd_module_rele(mp);
136 	return (TRUE);
137 }
138 
139 bool_t
140 fmd_adm_moddstat_1_svc(char *name,
141     struct fmd_rpc_modstat *rms, struct svc_req *req)
142 {
143 	fmd_module_t *mp;
144 
145 	rms->rms_buf.rms_buf_val = NULL;
146 	rms->rms_buf.rms_buf_len = 0;
147 	rms->rms_err = 0;
148 
149 	if (fmd_rpc_deny(req)) {
150 		rms->rms_err = FMD_ADM_ERR_PERM;
151 		return (TRUE);
152 	}
153 
154 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL) {
155 		rms->rms_err = FMD_ADM_ERR_MODSRCH;
156 		return (TRUE);
157 	}
158 
159 	rms->rms_buf.rms_buf_val = malloc(sizeof (fmd_modstat_t));
160 	rms->rms_buf.rms_buf_len = sizeof (fmd_modstat_t) / sizeof (fmd_stat_t);
161 
162 	if (rms->rms_buf.rms_buf_val == NULL) {
163 		rms->rms_err = FMD_ADM_ERR_NOMEM;
164 		rms->rms_buf.rms_buf_len = 0;
165 		fmd_module_rele(mp);
166 		return (TRUE);
167 	}
168 
169 	/*
170 	 * Note: the bcopy() here is valid only if no FMD_TYPE_STRING stats
171 	 * are present in mp->mod_stats.  We don't use any for the daemon-
172 	 * maintained stats and provide this function in order to reduce the
173 	 * overhead of the fmstat(1M) default view, where these minimal stats
174 	 * must be retrieved for all of the active modules.
175 	 */
176 	(void) pthread_mutex_lock(&mp->mod_stats_lock);
177 
178 	if (mp->mod_stats != NULL) {
179 		mp->mod_stats->ms_snaptime.fmds_value.ui64 = gethrtime();
180 		bcopy(mp->mod_stats, rms->rms_buf.rms_buf_val,
181 		    sizeof (fmd_modstat_t));
182 	} else {
183 		free(rms->rms_buf.rms_buf_val);
184 		rms->rms_buf.rms_buf_val = NULL;
185 		rms->rms_buf.rms_buf_len = 0;
186 		rms->rms_err = FMD_ADM_ERR_MODFAIL;
187 	}
188 
189 	(void) pthread_mutex_unlock(&mp->mod_stats_lock);
190 	fmd_module_rele(mp);
191 	return (TRUE);
192 }
193 
194 bool_t
195 fmd_adm_modgstat_1_svc(struct fmd_rpc_modstat *rms, struct svc_req *req)
196 {
197 	const size_t size = sizeof (fmd_statistics_t);
198 
199 	if (fmd_rpc_deny(req)) {
200 		rms->rms_buf.rms_buf_val = NULL;
201 		rms->rms_buf.rms_buf_len = 0;
202 		rms->rms_err = FMD_ADM_ERR_PERM;
203 	} else if ((rms->rms_buf.rms_buf_val = malloc(size)) != NULL) {
204 		/*
205 		 * Note: the bcopy() here is valid only if no FMD_TYPE_STRING
206 		 * stats are present in fmd.d_stats (see definition in fmd.c).
207 		 */
208 		(void) pthread_mutex_lock(&fmd.d_stats_lock);
209 		bcopy(fmd.d_stats, rms->rms_buf.rms_buf_val, size);
210 		(void) pthread_mutex_unlock(&fmd.d_stats_lock);
211 		rms->rms_buf.rms_buf_len = size / sizeof (fmd_stat_t);
212 		rms->rms_err = 0;
213 	} else {
214 		rms->rms_buf.rms_buf_len = 0;
215 		rms->rms_err = FMD_ADM_ERR_NOMEM;
216 	}
217 
218 	return (TRUE);
219 }
220 
221 bool_t
222 fmd_adm_modload_1_svc(char *path, int *rvp, struct svc_req *req)
223 {
224 	fmd_module_t *mp;
225 	const char *p;
226 	int err = 0;
227 
228 	if (fmd_rpc_deny(req)) {
229 		*rvp = FMD_ADM_ERR_PERM;
230 		return (TRUE);
231 	}
232 
233 	/*
234 	 * Before we endure the expense of constructing a module and attempting
235 	 * to load it, do a quick check to see if the pathname is valid.
236 	 */
237 	if (access(path, F_OK) != 0) {
238 		*rvp = FMD_ADM_ERR_MODNOENT;
239 		return (TRUE);
240 	}
241 
242 	if ((p = strrchr(path, '.')) != NULL && strcmp(p, ".so") == 0)
243 		mp = fmd_modhash_load(fmd.d_mod_hash, path, &fmd_rtld_ops);
244 	else
245 		mp = fmd_modhash_load(fmd.d_mod_hash, path, &fmd_proc_ops);
246 
247 	if (mp == NULL) {
248 		switch (errno) {
249 		case EFMD_MOD_LOADED:
250 			err = FMD_ADM_ERR_MODEXIST;
251 			break;
252 		case EFMD_MOD_INIT:
253 			err = FMD_ADM_ERR_MODINIT;
254 			break;
255 		default:
256 			err = FMD_ADM_ERR_MODLOAD;
257 			break;
258 		}
259 	}
260 
261 	*rvp = err;
262 	return (TRUE);
263 }
264 
265 bool_t
266 fmd_adm_modunload_1_svc(char *name, int *rvp, struct svc_req *req)
267 {
268 	fmd_module_t *mp = NULL;
269 	int err = 0;
270 
271 	if (fmd_rpc_deny(req))
272 		err = FMD_ADM_ERR_PERM;
273 	else if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL)
274 		err = FMD_ADM_ERR_MODSRCH;
275 	else if (mp == fmd.d_self)
276 		err = FMD_ADM_ERR_MODBUSY;
277 	else if (fmd_modhash_unload(fmd.d_mod_hash, name) != 0)
278 		err = FMD_ADM_ERR_MODSRCH;
279 
280 	if (mp != NULL)
281 		fmd_module_rele(mp);
282 
283 	*rvp = err;
284 	return (TRUE);
285 }
286 
287 bool_t
288 fmd_adm_modreset_1_svc(char *name, int *rvp, struct svc_req *req)
289 {
290 	fmd_module_t *mp = NULL;
291 	int err = 0;
292 
293 	if (fmd_rpc_deny(req))
294 		err = FMD_ADM_ERR_PERM;
295 	else if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL)
296 		err = FMD_ADM_ERR_MODSRCH;
297 	else if (mp == fmd.d_self)
298 		err = FMD_ADM_ERR_MODBUSY;
299 	else if (fmd_modhash_unload(fmd.d_mod_hash, name) != 0)
300 		err = FMD_ADM_ERR_MODSRCH;
301 
302 	if (err == 0)
303 		fmd_ckpt_delete(mp); /* erase any saved checkpoints */
304 
305 	if (err == 0 && fmd_modhash_load(fmd.d_mod_hash,
306 	    mp->mod_path, mp->mod_ops) == NULL) {
307 		if (errno == EFMD_MOD_INIT)
308 			err = FMD_ADM_ERR_MODINIT;
309 		else
310 			err = FMD_ADM_ERR_MODLOAD;
311 	}
312 
313 	if (mp != NULL)
314 		fmd_module_rele(mp);
315 
316 	*rvp = err;
317 	return (TRUE);
318 }
319 
320 bool_t
321 fmd_adm_modgc_1_svc(char *name, int *rvp, struct svc_req *req)
322 {
323 	fmd_module_t *mp;
324 	int err = 0;
325 
326 	if (fmd_rpc_deny(req))
327 		err = FMD_ADM_ERR_PERM;
328 	else if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL)
329 		err = FMD_ADM_ERR_MODSRCH;
330 	else {
331 		fmd_module_gc(mp);
332 		fmd_module_rele(mp);
333 	}
334 
335 	*rvp = err;
336 	return (TRUE);
337 }
338 
339 /*
340  * Unlike our other RPC callbacks, fmd_adm_rsrclist_1 can return large amounts
341  * of data that may exceed the underlying RPC transport buffer size if the
342  * resource cache is heavily populated and/or all resources are requested.
343  * To minimize the likelihood of running out of RPC buffer space and having to
344  * fail the client request, fmd_adm_rsrclist_1 returns a snapshot of the
345  * relevant FMRI strings only: the client can use fmd_adm_rsrcinfo_1 on an
346  * individual FMRI if more information is needed.  To further reduce the XDR
347  * overhead, the string list is represented as XDR-opaque data where the
348  * entire list is returned as a string table (e.g. "fmriA\0fmriB\0...").
349  */
350 static void
351 fmd_adm_rsrclist_asru(fmd_asru_t *ap, void *arg)
352 {
353 	struct fmd_rpc_rsrclist *rrl = arg;
354 	size_t name_len, buf_len;
355 	void *p;
356 
357 	/*
358 	 * If rrl_all is false, we take a quick look at asru_flags with no lock
359 	 * held to see if the ASRU is either not faulty or invisible.  If so,
360 	 * we don't want to report it by default and can just skip this ASRU.
361 	 * This helps keep overhead low in the common case, as the call to
362 	 * fmd_asru_getstate() can be expensive depending on the scheme.
363 	 */
364 	if (rrl->rrl_all == B_FALSE && (ap->asru_flags & (FMD_ASRU_FAULTY |
365 	    FMD_ASRU_INVISIBLE)) != FMD_ASRU_FAULTY)
366 		return;
367 
368 	if (rrl->rrl_err != 0 || fmd_asru_getstate(ap) == 0)
369 		return; /* error has occurred or resource is in 'ok' state */
370 
371 	/*
372 	 * Lock the ASRU and reallocate rrl_buf[] to be large enough to hold
373 	 * another string, doubling it as needed.  Then copy the new string
374 	 * on to the end, and increment rrl_len to indicate the used space.
375 	 */
376 	(void) pthread_mutex_lock(&ap->asru_lock);
377 	name_len = strlen(ap->asru_name) + 1;
378 
379 	while (rrl->rrl_len + name_len > rrl->rrl_buf.rrl_buf_len) {
380 		if (rrl->rrl_buf.rrl_buf_len != 0)
381 			buf_len = rrl->rrl_buf.rrl_buf_len * 2;
382 		else
383 			buf_len = 1024; /* default buffer size */
384 
385 		if ((p = realloc(rrl->rrl_buf.rrl_buf_val, buf_len)) != NULL) {
386 			bzero((char *)p + rrl->rrl_buf.rrl_buf_len,
387 			    buf_len - rrl->rrl_buf.rrl_buf_len);
388 			rrl->rrl_buf.rrl_buf_val = p;
389 			rrl->rrl_buf.rrl_buf_len = buf_len;
390 		} else {
391 			rrl->rrl_err = FMD_ADM_ERR_NOMEM;
392 			break;
393 		}
394 	}
395 
396 	if (rrl->rrl_err == 0) {
397 		bcopy(ap->asru_name, (char *)rrl->rrl_buf.rrl_buf_val +
398 		    rrl->rrl_len, name_len);
399 		rrl->rrl_len += name_len;
400 		rrl->rrl_cnt++;
401 	}
402 
403 	(void) pthread_mutex_unlock(&ap->asru_lock);
404 }
405 
406 bool_t
407 fmd_adm_rsrclist_1_svc(bool_t all,
408     struct fmd_rpc_rsrclist *rvp, struct svc_req *req)
409 {
410 	rvp->rrl_buf.rrl_buf_len = 0;
411 	rvp->rrl_buf.rrl_buf_val = NULL;
412 	rvp->rrl_len = 0;
413 	rvp->rrl_cnt = 0;
414 	rvp->rrl_err = 0;
415 	rvp->rrl_all = all;
416 
417 	if (fmd_rpc_deny(req))
418 		rvp->rrl_err = FMD_ADM_ERR_PERM;
419 	else
420 		fmd_asru_hash_apply(fmd.d_asrus, fmd_adm_rsrclist_asru, rvp);
421 
422 	return (TRUE);
423 }
424 
425 bool_t
426 fmd_adm_rsrcinfo_1_svc(char *fmri,
427     struct fmd_rpc_rsrcinfo *rvp, struct svc_req *req)
428 {
429 	fmd_asru_t *ap;
430 	fmd_case_impl_t *cip;
431 	int state;
432 
433 	bzero(rvp, sizeof (struct fmd_rpc_rsrcinfo));
434 
435 	if (fmd_rpc_deny(req)) {
436 		rvp->rri_err = FMD_ADM_ERR_PERM;
437 		return (TRUE);
438 	}
439 
440 	if ((ap = fmd_asru_hash_lookup_name(fmd.d_asrus, fmri)) == NULL) {
441 		rvp->rri_err = FMD_ADM_ERR_RSRCSRCH;
442 		return (TRUE);
443 	}
444 
445 	state = fmd_asru_getstate(ap);
446 	(void) pthread_mutex_lock(&ap->asru_lock);
447 	cip = (fmd_case_impl_t *)ap->asru_case;
448 
449 	rvp->rri_fmri = strdup(ap->asru_name);
450 	rvp->rri_uuid = strdup(ap->asru_uuid);
451 	rvp->rri_case = cip ? strdup(cip->ci_uuid) : NULL;
452 	rvp->rri_faulty = (state & FMD_ASRU_FAULTY) != 0;
453 	rvp->rri_unusable = (state & FMD_ASRU_UNUSABLE) != 0;
454 	rvp->rri_invisible = (ap->asru_flags & FMD_ASRU_INVISIBLE) != 0;
455 
456 	(void) pthread_mutex_unlock(&ap->asru_lock);
457 	fmd_asru_hash_release(fmd.d_asrus, ap);
458 
459 	if (rvp->rri_fmri == NULL || rvp->rri_uuid == NULL)
460 		rvp->rri_err = FMD_ADM_ERR_NOMEM;
461 
462 	return (TRUE);
463 }
464 
465 bool_t
466 fmd_adm_rsrcflush_1_svc(char *name, int *rvp, struct svc_req *req)
467 {
468 	if (fmd_rpc_deny(req))
469 		*rvp = FMD_ADM_ERR_PERM;
470 	else if (fmd_asru_hash_delete_name(fmd.d_asrus, name) != 0)
471 		*rvp = FMD_ADM_ERR_RSRCSRCH;
472 	else
473 		*rvp = 0;
474 
475 	return (TRUE);
476 }
477 
478 static void
479 fmd_adm_repair_containee(fmd_asru_t *ee, void *er)
480 {
481 	if ((ee->asru_flags & FMD_ASRU_FAULTY) &&
482 	    fmd_fmri_contains(er, ee->asru_fmri) > 0)
483 		(void) fmd_asru_clrflags(ee, FMD_ASRU_FAULTY, NULL, NULL);
484 }
485 
486 bool_t
487 fmd_adm_rsrcrepair_1_svc(char *name, int *rvp, struct svc_req *req)
488 {
489 	fmd_asru_t *ap = NULL;
490 	int err = 0;
491 
492 	if (fmd_rpc_deny(req))
493 		err = FMD_ADM_ERR_PERM;
494 	else if ((ap = fmd_asru_hash_lookup_name(fmd.d_asrus, name)) == NULL)
495 		err = FMD_ADM_ERR_RSRCSRCH;
496 	else if (!fmd_asru_clrflags(ap, FMD_ASRU_FAULTY, NULL, NULL))
497 		err = FMD_ADM_ERR_RSRCNOTF;
498 	else {
499 		/*
500 		 * We've located the requested ASRU, and have repaired it.  Now
501 		 * traverse the ASRU cache, looking for any faulty entries that
502 		 * are contained by this one.  If we find any, repair them too.
503 		 */
504 		fmd_asru_hash_apply(fmd.d_asrus,
505 		    fmd_adm_repair_containee, ap->asru_fmri);
506 	}
507 
508 	if (ap != NULL)
509 		fmd_asru_hash_release(fmd.d_asrus, ap);
510 
511 	*rvp = err;
512 	return (TRUE);
513 }
514 
515 static void
516 fmd_adm_serdinfo_eng(fmd_serd_eng_t *sgp, void *arg)
517 {
518 	struct fmd_rpc_serdlist *rsl = arg;
519 	struct fmd_rpc_serdinfo *rsi = malloc(sizeof (struct fmd_rpc_serdinfo));
520 
521 	uint64_t old, now = fmd_time_gethrtime();
522 	const fmd_serd_elem_t *oep;
523 
524 	if (rsi == NULL || (rsi->rsi_name = strdup(sgp->sg_name)) == NULL) {
525 		rsl->rsl_err = FMD_ADM_ERR_NOMEM;
526 		free(rsi);
527 		return;
528 	}
529 
530 	if ((oep = fmd_list_next(&sgp->sg_list)) != NULL)
531 		old = fmd_event_hrtime(oep->se_event);
532 	else
533 		old = now;
534 
535 	rsi->rsi_delta = now >= old ? now - old : (UINT64_MAX - old) + now + 1;
536 	rsi->rsi_count = sgp->sg_count;
537 	rsi->rsi_fired = fmd_serd_eng_fired(sgp) != 0;
538 	rsi->rsi_n = sgp->sg_n;
539 	rsi->rsi_t = sgp->sg_t;
540 	rsi->rsi_next = rsl->rsl_list;
541 
542 	rsl->rsl_list = rsi;
543 	rsl->rsl_len++;
544 }
545 
546 bool_t
547 fmd_adm_serdinfo_1_svc(char *name,
548     struct fmd_rpc_serdlist *rvp, struct svc_req *req)
549 {
550 	fmd_module_t *mp;
551 
552 	rvp->rsl_list = NULL;
553 	rvp->rsl_err = 0;
554 	rvp->rsl_len = 0;
555 
556 	if (fmd_rpc_deny(req)) {
557 		rvp->rsl_err = FMD_ADM_ERR_PERM;
558 		return (TRUE);
559 	}
560 
561 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL) {
562 		rvp->rsl_err = FMD_ADM_ERR_MODSRCH;
563 		return (TRUE);
564 	}
565 
566 	fmd_module_lock(mp);
567 	fmd_serd_hash_apply(&mp->mod_serds, fmd_adm_serdinfo_eng, rvp);
568 	fmd_module_unlock(mp);
569 
570 	fmd_module_rele(mp);
571 	return (TRUE);
572 }
573 
574 bool_t
575 fmd_adm_serdreset_1_svc(char *mname, char *sname, int *rvp, struct svc_req *req)
576 {
577 	fmd_module_t *mp;
578 	fmd_serd_eng_t *sgp;
579 	int err = 0;
580 
581 	if (fmd_rpc_deny(req)) {
582 		*rvp = FMD_ADM_ERR_PERM;
583 		return (TRUE);
584 	}
585 
586 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, mname)) == NULL) {
587 		*rvp = FMD_ADM_ERR_MODSRCH;
588 		return (TRUE);
589 	}
590 
591 	fmd_module_lock(mp);
592 
593 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, sname)) != NULL) {
594 		if (fmd_serd_eng_fired(sgp)) {
595 			err = FMD_ADM_ERR_SERDFIRED;
596 		} else {
597 			fmd_serd_eng_reset(sgp);
598 			fmd_module_setdirty(mp);
599 		}
600 	} else
601 		err = FMD_ADM_ERR_SERDSRCH;
602 
603 	fmd_module_unlock(mp);
604 	fmd_module_rele(mp);
605 
606 	*rvp = err;
607 	return (TRUE);
608 }
609 
610 bool_t
611 fmd_adm_logrotate_1_svc(char *name, int *rvp, struct svc_req *req)
612 {
613 	fmd_log_t **lpp, *old, *new;
614 	int try = 1, trylimit = 1;
615 
616 	hrtime_t nsec = 0;
617 	timespec_t tv;
618 
619 	if (fmd_rpc_deny(req)) {
620 		*rvp = FMD_ADM_ERR_PERM;
621 		return (TRUE);
622 	}
623 
624 	if (strcmp(name, "errlog") == 0)
625 		lpp = &fmd.d_errlog;
626 	else if (strcmp(name, "fltlog") == 0)
627 		lpp = &fmd.d_fltlog;
628 	else {
629 		*rvp = FMD_ADM_ERR_ROTSRCH;
630 		return (TRUE);
631 	}
632 
633 	(void) fmd_conf_getprop(fmd.d_conf, "log.tryrotate", &trylimit);
634 	(void) fmd_conf_getprop(fmd.d_conf, "log.waitrotate", &nsec);
635 
636 	tv.tv_sec = nsec / NANOSEC;
637 	tv.tv_nsec = nsec % NANOSEC;
638 
639 	/*
640 	 * To rotate a log file, grab d_log_lock as writer to make sure no
641 	 * one else can discover the current log pointer.  Then try to rotate
642 	 * the log.  If we're successful, release the old log pointer.
643 	 */
644 	do {
645 		if (try > 1)
646 			(void) nanosleep(&tv, NULL); /* wait for checkpoints */
647 
648 		(void) pthread_rwlock_wrlock(&fmd.d_log_lock);
649 		old = *lpp;
650 
651 		if ((new = fmd_log_rotate(old)) != NULL) {
652 			fmd_log_rele(old);
653 			*lpp = new;
654 		}
655 
656 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
657 
658 	} while (new == NULL && errno == EFMD_LOG_ROTBUSY && try++ < trylimit);
659 
660 	if (new != NULL)
661 		*rvp = 0;
662 	else if (errno == EFMD_LOG_ROTBUSY)
663 		*rvp = FMD_ADM_ERR_ROTBUSY;
664 	else
665 		*rvp = FMD_ADM_ERR_ROTFAIL;
666 
667 	return (TRUE);
668 }
669 
670 /*
671  * If the case associated with this ASRU matches our input case, close all
672  * ASRUs contained by 'ap' and trigger appropriate case close events.
673  */
674 static void
675 fmd_adm_caserepair_asru(fmd_asru_t *ap, void *cp)
676 {
677 	if (ap->asru_case == cp) {
678 		fmd_asru_hash_apply(fmd.d_asrus,
679 		    fmd_adm_repair_containee, ap->asru_fmri);
680 		(void) fmd_asru_clrflags(ap, FMD_ASRU_FAULTY, NULL, NULL);
681 	}
682 }
683 
684 bool_t
685 fmd_adm_caserepair_1_svc(char *uuid, int *rvp, struct svc_req *req)
686 {
687 	fmd_case_t *cp = NULL;
688 	int err = 0;
689 
690 	if (fmd_rpc_deny(req))
691 		err = FMD_ADM_ERR_PERM;
692 	else if ((cp = fmd_case_hash_lookup(fmd.d_cases, uuid)) == NULL)
693 		err = FMD_ADM_ERR_CASESRCH;
694 	else if (fmd_case_repair(cp) == 0)
695 		fmd_asru_hash_apply(fmd.d_asrus, fmd_adm_caserepair_asru, cp);
696 	else {
697 		err = errno == EFMD_CASE_OWNER ?
698 		    FMD_ADM_ERR_CASEXPRT : FMD_ADM_ERR_CASEOPEN;
699 	}
700 
701 	if (cp != NULL)
702 		fmd_case_rele(cp);
703 
704 	*rvp = err;
705 	return (TRUE);
706 }
707 
708 void
709 fmd_adm_caselist_case(fmd_case_t *cp, void *arg)
710 {
711 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
712 	struct fmd_rpc_caselist *rcl = arg;
713 	size_t uuid_len, buf_len;
714 	void *p;
715 
716 	if (rcl->rcl_err != 0)
717 		return;
718 
719 	/*
720 	 * Lock the case and reallocate rcl_buf[] to be large enough to hold
721 	 * another string, doubling it as needed.  Then copy the new string
722 	 * on to the end, and increment rcl_len to indicate the used space.
723 	 */
724 	if (!(cip->ci_flags & FMD_CF_SOLVED))
725 		return;
726 
727 	(void) pthread_mutex_lock(&cip->ci_lock);
728 
729 	uuid_len = cip->ci_uuidlen + 1;
730 
731 	while (rcl->rcl_len + uuid_len > rcl->rcl_buf.rcl_buf_len) {
732 		if (rcl->rcl_buf.rcl_buf_len != 0)
733 			buf_len = rcl->rcl_buf.rcl_buf_len * 2;
734 		else
735 			buf_len = 1024; /* default buffer size */
736 
737 		if ((p = realloc(rcl->rcl_buf.rcl_buf_val, buf_len)) != NULL) {
738 			bzero((char *)p + rcl->rcl_buf.rcl_buf_len,
739 			    buf_len - rcl->rcl_buf.rcl_buf_len);
740 			rcl->rcl_buf.rcl_buf_val = p;
741 			rcl->rcl_buf.rcl_buf_len = buf_len;
742 		} else {
743 			rcl->rcl_err = FMD_ADM_ERR_NOMEM;
744 			break;
745 		}
746 	}
747 
748 	if (rcl->rcl_err == 0) {
749 		bcopy(cip->ci_uuid, (char *)rcl->rcl_buf.rcl_buf_val +
750 		    rcl->rcl_len, uuid_len);
751 		rcl->rcl_len += uuid_len;
752 		rcl->rcl_cnt++;
753 	}
754 
755 	(void) pthread_mutex_unlock(&cip->ci_lock);
756 }
757 
758 bool_t
759 fmd_adm_caselist_1_svc(struct fmd_rpc_caselist *rvp, struct svc_req *req)
760 {
761 	rvp->rcl_buf.rcl_buf_len = 0;
762 	rvp->rcl_buf.rcl_buf_val = NULL;
763 	rvp->rcl_len = 0;
764 	rvp->rcl_cnt = 0;
765 	rvp->rcl_err = 0;
766 
767 	if (fmd_rpc_deny(req))
768 		rvp->rcl_err = FMD_ADM_ERR_PERM;
769 	else
770 		fmd_case_hash_apply(fmd.d_cases, fmd_adm_caselist_case, rvp);
771 
772 	return (TRUE);
773 }
774 
775 bool_t
776 fmd_adm_caseinfo_1_svc(char *uuid, struct fmd_rpc_caseinfo *rvp,
777     struct svc_req *req)
778 {
779 	fmd_case_impl_t *cip;
780 	nvlist_t *nvl;
781 	int err = 0;
782 
783 	bzero(rvp, sizeof (struct fmd_rpc_caseinfo));
784 
785 	if (fmd_rpc_deny(req)) {
786 		rvp->rci_err = FMD_ADM_ERR_PERM;
787 		return (TRUE);
788 	}
789 
790 	if ((cip = (fmd_case_impl_t *)fmd_case_hash_lookup(fmd.d_cases, uuid))
791 	    == NULL) {
792 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
793 		return (TRUE);
794 	}
795 
796 	if (!(cip->ci_flags & FMD_CF_SOLVED)) {
797 		fmd_case_rele((fmd_case_t *)cip);
798 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
799 		return (TRUE);
800 	}
801 
802 	/*
803 	 * Avoid a race on cip->ci_diag; we can't call fmd_case_mkevent
804 	 * while holding the case lock, so we do this dance instead.
805 	 */
806 	(void) pthread_mutex_lock(&cip->ci_lock);
807 	nvl = cip->ci_diag;
808 	(void) pthread_mutex_unlock(&cip->ci_lock);
809 
810 	if (nvl == NULL)
811 		nvl = fmd_case_mkevent((fmd_case_t *)cip,
812 		    FM_LIST_SUSPECT_CLASS);
813 
814 	(void) pthread_mutex_lock(&cip->ci_lock);
815 	if (cip->ci_diag == NULL) {
816 		cip->ci_diag = nvl;
817 	} else if (nvl != cip->ci_diag) {
818 		nvlist_free(nvl);
819 		nvl = cip->ci_diag;
820 	}
821 	(void) pthread_mutex_unlock(&cip->ci_lock);
822 
823 	err = nvlist_pack(nvl, &rvp->rci_evbuf.rci_evbuf_val,
824 	    &rvp->rci_evbuf.rci_evbuf_len, NV_ENCODE_XDR, 0);
825 
826 	if (err != 0)
827 		rvp->rci_err = FMD_ADM_ERR_NOMEM;
828 
829 	fmd_case_rele((fmd_case_t *)cip);
830 
831 	return (TRUE);
832 }
833 
834 /*ARGSUSED*/
835 static void
836 fmd_adm_xprtlist_one(fmd_idspace_t *ids, id_t id, void *arg)
837 {
838 	struct fmd_rpc_xprtlist *rvp = arg;
839 
840 	if (rvp->rxl_len < rvp->rxl_buf.rxl_buf_len)
841 		rvp->rxl_buf.rxl_buf_val[rvp->rxl_len++] = id;
842 }
843 
844 bool_t
845 fmd_adm_xprtlist_1_svc(struct fmd_rpc_xprtlist *rvp, struct svc_req *req)
846 {
847 	if (fmd_rpc_deny(req)) {
848 		rvp->rxl_buf.rxl_buf_len = 0;
849 		rvp->rxl_buf.rxl_buf_val = NULL;
850 		rvp->rxl_len = 0;
851 		rvp->rxl_err = FMD_ADM_ERR_PERM;
852 		return (TRUE);
853 	}
854 
855 	/*
856 	 * Since we're taking a snapshot of the transports, and these could
857 	 * change after we return our result, there's no need to hold any kind
858 	 * of lock between retrieving ids_count and taking the snapshot.  We'll
859 	 * just capture up to a maximum of whatever ids_count value we sampled.
860 	 */
861 	rvp->rxl_buf.rxl_buf_len = fmd.d_xprt_ids->ids_count;
862 	rvp->rxl_buf.rxl_buf_val = malloc(sizeof (int32_t) *
863 	    rvp->rxl_buf.rxl_buf_len);
864 	rvp->rxl_len = 0;
865 	rvp->rxl_err = 0;
866 
867 	if (rvp->rxl_buf.rxl_buf_val == NULL) {
868 		rvp->rxl_err = FMD_ADM_ERR_NOMEM;
869 		return (TRUE);
870 	}
871 
872 	fmd_idspace_apply(fmd.d_xprt_ids, fmd_adm_xprtlist_one, rvp);
873 	return (TRUE);
874 }
875 
876 bool_t
877 fmd_adm_xprtstat_1_svc(int32_t id,
878     struct fmd_rpc_modstat *rms, struct svc_req *req)
879 {
880 	fmd_xprt_impl_t *xip;
881 	fmd_stat_t *sp, *ep, *cp;
882 
883 	if (fmd_rpc_deny(req)) {
884 		rms->rms_buf.rms_buf_val = NULL;
885 		rms->rms_buf.rms_buf_len = 0;
886 		rms->rms_err = FMD_ADM_ERR_PERM;
887 		return (TRUE);
888 	}
889 
890 	rms->rms_buf.rms_buf_val = malloc(sizeof (fmd_xprt_stat_t));
891 	rms->rms_buf.rms_buf_len = sizeof (fmd_xprt_stat_t) /
892 	    sizeof (fmd_stat_t);
893 	rms->rms_err = 0;
894 
895 	if (rms->rms_buf.rms_buf_val == NULL) {
896 		rms->rms_err = FMD_ADM_ERR_NOMEM;
897 		rms->rms_buf.rms_buf_len = 0;
898 		return (TRUE);
899 	}
900 
901 	if ((xip = fmd_idspace_hold(fmd.d_xprt_ids, id)) == NULL) {
902 		rms->rms_err = FMD_ADM_ERR_XPRTSRCH;
903 		return (TRUE);
904 	}
905 
906 	/*
907 	 * Grab the stats lock and bcopy the entire transport stats array in
908 	 * one shot. Then go back through and duplicate any string values.
909 	 */
910 	(void) pthread_mutex_lock(&xip->xi_stats_lock);
911 
912 	sp = (fmd_stat_t *)xip->xi_stats;
913 	ep = sp + rms->rms_buf.rms_buf_len;
914 	cp = rms->rms_buf.rms_buf_val;
915 
916 	bcopy(sp, cp, sizeof (fmd_xprt_stat_t));
917 
918 	for (; sp < ep; sp++, cp++) {
919 		if (sp->fmds_type == FMD_TYPE_STRING &&
920 		    sp->fmds_value.str != NULL)
921 			cp->fmds_value.str = strdup(sp->fmds_value.str);
922 	}
923 
924 	(void) pthread_mutex_unlock(&xip->xi_stats_lock);
925 	fmd_idspace_rele(fmd.d_xprt_ids, id);
926 
927 	return (TRUE);
928 }
929 
930 int
931 fmd_adm_1_freeresult(SVCXPRT *xprt, xdrproc_t proc, caddr_t data)
932 {
933 	xdr_free(proc, data);
934 	svc_done(xprt);
935 	return (TRUE);
936 }
937 
938 /*
939  * Custom XDR routine for our API structure fmd_stat_t.  This function must
940  * match the definition of fmd_stat_t in <fmd_api.h> and must also match
941  * the corresponding routine in usr/src/lib/fm/libfmd_adm/common/fmd_adm.c.
942  */
943 bool_t
944 xdr_fmd_stat(XDR *xp, fmd_stat_t *sp)
945 {
946 	bool_t rv = TRUE;
947 
948 	rv &= xdr_opaque(xp, sp->fmds_name, sizeof (sp->fmds_name));
949 	rv &= xdr_u_int(xp, &sp->fmds_type);
950 	rv &= xdr_opaque(xp, sp->fmds_desc, sizeof (sp->fmds_desc));
951 
952 	switch (sp->fmds_type) {
953 	case FMD_TYPE_BOOL:
954 		rv &= xdr_int(xp, &sp->fmds_value.bool);
955 		break;
956 	case FMD_TYPE_INT32:
957 		rv &= xdr_int32_t(xp, &sp->fmds_value.i32);
958 		break;
959 	case FMD_TYPE_UINT32:
960 		rv &= xdr_uint32_t(xp, &sp->fmds_value.ui32);
961 		break;
962 	case FMD_TYPE_INT64:
963 		rv &= xdr_int64_t(xp, &sp->fmds_value.i64);
964 		break;
965 	case FMD_TYPE_UINT64:
966 	case FMD_TYPE_TIME:
967 	case FMD_TYPE_SIZE:
968 		rv &= xdr_uint64_t(xp, &sp->fmds_value.ui64);
969 		break;
970 	case FMD_TYPE_STRING:
971 		rv &= xdr_string(xp, &sp->fmds_value.str, ~0);
972 		break;
973 	}
974 
975 	return (rv);
976 }
977