xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_rpc_adm.c (revision 45391f50095088ce9d0b1362d78cb4d9d049e411)
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 2008 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 	 * Skip the ASRU if this fault is marked as invisible.
359 	 * If rrl_all is false, we take a quick look at asru_flags with no lock
360 	 * held to see if the ASRU is not faulty.  If so,
361 	 * we don't want to report it by default and can just skip this ASRU.
362 	 * This helps keep overhead low in the common case, as the call to
363 	 * fmd_asru_getstate() can be expensive depending on the scheme.
364 	 */
365 
366 	if (ap->asru_flags & FMD_ASRU_INVISIBLE)
367 		return;
368 	if (rrl->rrl_all == B_FALSE && !(ap->asru_flags & FMD_ASRU_FAULTY))
369 		return;
370 
371 	if (rrl->rrl_err != 0 || fmd_asru_getstate(ap) == 0)
372 		return; /* error has occurred or resource is in 'ok' state */
373 
374 	/*
375 	 * Lock the ASRU and reallocate rrl_buf[] to be large enough to hold
376 	 * another string, doubling it as needed.  Then copy the new string
377 	 * on to the end, and increment rrl_len to indicate the used space.
378 	 */
379 	(void) pthread_mutex_lock(&ap->asru_lock);
380 	name_len = strlen(ap->asru_name) + 1;
381 
382 	while (rrl->rrl_len + name_len > rrl->rrl_buf.rrl_buf_len) {
383 		if (rrl->rrl_buf.rrl_buf_len != 0)
384 			buf_len = rrl->rrl_buf.rrl_buf_len * 2;
385 		else
386 			buf_len = 1024; /* default buffer size */
387 
388 		if ((p = realloc(rrl->rrl_buf.rrl_buf_val, buf_len)) != NULL) {
389 			bzero((char *)p + rrl->rrl_buf.rrl_buf_len,
390 			    buf_len - rrl->rrl_buf.rrl_buf_len);
391 			rrl->rrl_buf.rrl_buf_val = p;
392 			rrl->rrl_buf.rrl_buf_len = buf_len;
393 		} else {
394 			rrl->rrl_err = FMD_ADM_ERR_NOMEM;
395 			break;
396 		}
397 	}
398 
399 	if (rrl->rrl_err == 0) {
400 		bcopy(ap->asru_name, (char *)rrl->rrl_buf.rrl_buf_val +
401 		    rrl->rrl_len, name_len);
402 		rrl->rrl_len += name_len;
403 		rrl->rrl_cnt++;
404 	}
405 
406 	(void) pthread_mutex_unlock(&ap->asru_lock);
407 }
408 
409 bool_t
410 fmd_adm_rsrclist_1_svc(bool_t all,
411     struct fmd_rpc_rsrclist *rvp, struct svc_req *req)
412 {
413 	rvp->rrl_buf.rrl_buf_len = 0;
414 	rvp->rrl_buf.rrl_buf_val = NULL;
415 	rvp->rrl_len = 0;
416 	rvp->rrl_cnt = 0;
417 	rvp->rrl_err = 0;
418 	rvp->rrl_all = all;
419 
420 	if (fmd_rpc_deny(req))
421 		rvp->rrl_err = FMD_ADM_ERR_PERM;
422 	else
423 		fmd_asru_hash_apply(fmd.d_asrus, fmd_adm_rsrclist_asru, rvp);
424 
425 	return (TRUE);
426 }
427 
428 bool_t
429 fmd_adm_rsrcinfo_1_svc(char *fmri,
430     struct fmd_rpc_rsrcinfo *rvp, struct svc_req *req)
431 {
432 	fmd_asru_t *ap;
433 	fmd_case_impl_t *cip;
434 	int state;
435 
436 	bzero(rvp, sizeof (struct fmd_rpc_rsrcinfo));
437 
438 	if (fmd_rpc_deny(req)) {
439 		rvp->rri_err = FMD_ADM_ERR_PERM;
440 		return (TRUE);
441 	}
442 
443 	if ((ap = fmd_asru_hash_lookup_name(fmd.d_asrus, fmri)) == NULL) {
444 		rvp->rri_err = FMD_ADM_ERR_RSRCSRCH;
445 		return (TRUE);
446 	}
447 
448 	state = fmd_asru_getstate(ap);
449 	(void) pthread_mutex_lock(&ap->asru_lock);
450 	cip = (fmd_case_impl_t *)ap->asru_case;
451 
452 	rvp->rri_fmri = strdup(ap->asru_name);
453 	rvp->rri_uuid = strdup(ap->asru_uuid);
454 	rvp->rri_case = cip ? strdup(cip->ci_uuid) : NULL;
455 	rvp->rri_faulty = (state & FMD_ASRU_FAULTY) != 0;
456 	rvp->rri_unusable = (state & FMD_ASRU_UNUSABLE) != 0;
457 	rvp->rri_invisible = (ap->asru_flags & FMD_ASRU_INVISIBLE) != 0;
458 
459 	(void) pthread_mutex_unlock(&ap->asru_lock);
460 	fmd_asru_hash_release(fmd.d_asrus, ap);
461 
462 	if (rvp->rri_fmri == NULL || rvp->rri_uuid == NULL)
463 		rvp->rri_err = FMD_ADM_ERR_NOMEM;
464 
465 	return (TRUE);
466 }
467 
468 bool_t
469 fmd_adm_rsrcflush_1_svc(char *name, int *rvp, struct svc_req *req)
470 {
471 	return (fmd_adm_rsrcrepair_1_svc(name, rvp, req));
472 }
473 
474 bool_t
475 fmd_adm_rsrcrepair_1_svc(char *name, int *rvp, struct svc_req *req)
476 {
477 	int err = FMD_ADM_ERR_RSRCNOTF;
478 
479 	if (fmd_rpc_deny(req))
480 		err = FMD_ADM_ERR_PERM;
481 	else {
482 		fmd_asru_hash_apply_by_asru(fmd.d_asrus, name,
483 		    fmd_asru_repair, &err);
484 		fmd_asru_hash_apply_by_label(fmd.d_asrus, name,
485 		    fmd_asru_repair, &err);
486 		fmd_asru_hash_apply_by_fru(fmd.d_asrus, name,
487 		    fmd_asru_repair, &err);
488 		fmd_asru_hash_apply_by_rsrc(fmd.d_asrus, name,
489 		    fmd_asru_repair, &err);
490 	}
491 	*rvp = err;
492 	return (TRUE);
493 }
494 
495 static void
496 fmd_adm_serdinfo_eng(fmd_serd_eng_t *sgp, void *arg)
497 {
498 	struct fmd_rpc_serdlist *rsl = arg;
499 	struct fmd_rpc_serdinfo *rsi = malloc(sizeof (struct fmd_rpc_serdinfo));
500 
501 	uint64_t old, now = fmd_time_gethrtime();
502 	const fmd_serd_elem_t *oep;
503 
504 	if (rsi == NULL || (rsi->rsi_name = strdup(sgp->sg_name)) == NULL) {
505 		rsl->rsl_err = FMD_ADM_ERR_NOMEM;
506 		free(rsi);
507 		return;
508 	}
509 
510 	if ((oep = fmd_list_next(&sgp->sg_list)) != NULL)
511 		old = fmd_event_hrtime(oep->se_event);
512 	else
513 		old = now;
514 
515 	rsi->rsi_delta = now >= old ? now - old : (UINT64_MAX - old) + now + 1;
516 	rsi->rsi_count = sgp->sg_count;
517 	rsi->rsi_fired = fmd_serd_eng_fired(sgp) != 0;
518 	rsi->rsi_n = sgp->sg_n;
519 	rsi->rsi_t = sgp->sg_t;
520 	rsi->rsi_next = rsl->rsl_list;
521 
522 	rsl->rsl_list = rsi;
523 	rsl->rsl_len++;
524 }
525 
526 bool_t
527 fmd_adm_serdinfo_1_svc(char *name,
528     struct fmd_rpc_serdlist *rvp, struct svc_req *req)
529 {
530 	fmd_module_t *mp;
531 
532 	rvp->rsl_list = NULL;
533 	rvp->rsl_err = 0;
534 	rvp->rsl_len = 0;
535 
536 	if (fmd_rpc_deny(req)) {
537 		rvp->rsl_err = FMD_ADM_ERR_PERM;
538 		return (TRUE);
539 	}
540 
541 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, name)) == NULL) {
542 		rvp->rsl_err = FMD_ADM_ERR_MODSRCH;
543 		return (TRUE);
544 	}
545 
546 	fmd_module_lock(mp);
547 	fmd_serd_hash_apply(&mp->mod_serds, fmd_adm_serdinfo_eng, rvp);
548 	fmd_module_unlock(mp);
549 
550 	fmd_module_rele(mp);
551 	return (TRUE);
552 }
553 
554 bool_t
555 fmd_adm_serdreset_1_svc(char *mname, char *sname, int *rvp, struct svc_req *req)
556 {
557 	fmd_module_t *mp;
558 	fmd_serd_eng_t *sgp;
559 	int err = 0;
560 
561 	if (fmd_rpc_deny(req)) {
562 		*rvp = FMD_ADM_ERR_PERM;
563 		return (TRUE);
564 	}
565 
566 	if ((mp = fmd_modhash_lookup(fmd.d_mod_hash, mname)) == NULL) {
567 		*rvp = FMD_ADM_ERR_MODSRCH;
568 		return (TRUE);
569 	}
570 
571 	fmd_module_lock(mp);
572 
573 	if ((sgp = fmd_serd_eng_lookup(&mp->mod_serds, sname)) != NULL) {
574 		if (fmd_serd_eng_fired(sgp)) {
575 			err = FMD_ADM_ERR_SERDFIRED;
576 		} else {
577 			fmd_serd_eng_reset(sgp);
578 			fmd_module_setdirty(mp);
579 		}
580 	} else
581 		err = FMD_ADM_ERR_SERDSRCH;
582 
583 	fmd_module_unlock(mp);
584 	fmd_module_rele(mp);
585 
586 	*rvp = err;
587 	return (TRUE);
588 }
589 
590 bool_t
591 fmd_adm_logrotate_1_svc(char *name, int *rvp, struct svc_req *req)
592 {
593 	fmd_log_t **lpp, *old, *new;
594 	int try = 1, trylimit = 1;
595 
596 	hrtime_t nsec = 0;
597 	timespec_t tv;
598 
599 	if (fmd_rpc_deny(req)) {
600 		*rvp = FMD_ADM_ERR_PERM;
601 		return (TRUE);
602 	}
603 
604 	if (strcmp(name, "errlog") == 0)
605 		lpp = &fmd.d_errlog;
606 	else if (strcmp(name, "fltlog") == 0)
607 		lpp = &fmd.d_fltlog;
608 	else {
609 		*rvp = FMD_ADM_ERR_ROTSRCH;
610 		return (TRUE);
611 	}
612 
613 	(void) fmd_conf_getprop(fmd.d_conf, "log.tryrotate", &trylimit);
614 	(void) fmd_conf_getprop(fmd.d_conf, "log.waitrotate", &nsec);
615 
616 	tv.tv_sec = nsec / NANOSEC;
617 	tv.tv_nsec = nsec % NANOSEC;
618 
619 	/*
620 	 * To rotate a log file, grab d_log_lock as writer to make sure no
621 	 * one else can discover the current log pointer.  Then try to rotate
622 	 * the log.  If we're successful, release the old log pointer.
623 	 */
624 	do {
625 		if (try > 1)
626 			(void) nanosleep(&tv, NULL); /* wait for checkpoints */
627 
628 		(void) pthread_rwlock_wrlock(&fmd.d_log_lock);
629 		old = *lpp;
630 
631 		if ((new = fmd_log_rotate(old)) != NULL) {
632 			fmd_log_rele(old);
633 			*lpp = new;
634 		}
635 
636 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
637 
638 	} while (new == NULL && errno == EFMD_LOG_ROTBUSY && try++ < trylimit);
639 
640 	if (new != NULL)
641 		*rvp = 0;
642 	else if (errno == EFMD_LOG_ROTBUSY)
643 		*rvp = FMD_ADM_ERR_ROTBUSY;
644 	else
645 		*rvp = FMD_ADM_ERR_ROTFAIL;
646 
647 	return (TRUE);
648 }
649 
650 bool_t
651 fmd_adm_caserepair_1_svc(char *uuid, int *rvp, struct svc_req *req)
652 {
653 	fmd_case_t *cp = NULL;
654 	int err = 0;
655 
656 	if (fmd_rpc_deny(req))
657 		err = FMD_ADM_ERR_PERM;
658 	else if ((cp = fmd_case_hash_lookup(fmd.d_cases, uuid)) == NULL)
659 		err = FMD_ADM_ERR_CASESRCH;
660 	else if (fmd_case_repair(cp) != 0) {
661 		err = errno == EFMD_CASE_OWNER ?
662 		    FMD_ADM_ERR_CASEXPRT : FMD_ADM_ERR_CASEOPEN;
663 	}
664 
665 	if (cp != NULL)
666 		fmd_case_rele(cp);
667 
668 	*rvp = err;
669 	return (TRUE);
670 }
671 
672 void
673 fmd_adm_caselist_case(fmd_case_t *cp, void *arg)
674 {
675 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
676 	struct fmd_rpc_caselist *rcl = arg;
677 	size_t uuid_len, buf_len;
678 	void *p;
679 
680 	if (rcl->rcl_err != 0)
681 		return;
682 
683 	/*
684 	 * skip invisible cases
685 	 */
686 	if (cip->ci_flags & FMD_CF_INVISIBLE)
687 		return;
688 
689 	/*
690 	 * Lock the case and reallocate rcl_buf[] to be large enough to hold
691 	 * another string, doubling it as needed.  Then copy the new string
692 	 * on to the end, and increment rcl_len to indicate the used space.
693 	 */
694 	if (!(cip->ci_flags & FMD_CF_SOLVED))
695 		return;
696 
697 	(void) pthread_mutex_lock(&cip->ci_lock);
698 
699 	uuid_len = cip->ci_uuidlen + 1;
700 
701 	while (rcl->rcl_len + uuid_len > rcl->rcl_buf.rcl_buf_len) {
702 		if (rcl->rcl_buf.rcl_buf_len != 0)
703 			buf_len = rcl->rcl_buf.rcl_buf_len * 2;
704 		else
705 			buf_len = 1024; /* default buffer size */
706 
707 		if ((p = realloc(rcl->rcl_buf.rcl_buf_val, buf_len)) != NULL) {
708 			bzero((char *)p + rcl->rcl_buf.rcl_buf_len,
709 			    buf_len - rcl->rcl_buf.rcl_buf_len);
710 			rcl->rcl_buf.rcl_buf_val = p;
711 			rcl->rcl_buf.rcl_buf_len = buf_len;
712 		} else {
713 			rcl->rcl_err = FMD_ADM_ERR_NOMEM;
714 			break;
715 		}
716 	}
717 
718 	if (rcl->rcl_err == 0) {
719 		bcopy(cip->ci_uuid, (char *)rcl->rcl_buf.rcl_buf_val +
720 		    rcl->rcl_len, uuid_len);
721 		rcl->rcl_len += uuid_len;
722 		rcl->rcl_cnt++;
723 	}
724 
725 	(void) pthread_mutex_unlock(&cip->ci_lock);
726 }
727 
728 bool_t
729 fmd_adm_caselist_1_svc(struct fmd_rpc_caselist *rvp, struct svc_req *req)
730 {
731 	rvp->rcl_buf.rcl_buf_len = 0;
732 	rvp->rcl_buf.rcl_buf_val = NULL;
733 	rvp->rcl_len = 0;
734 	rvp->rcl_cnt = 0;
735 	rvp->rcl_err = 0;
736 
737 	if (fmd_rpc_deny(req))
738 		rvp->rcl_err = FMD_ADM_ERR_PERM;
739 	else
740 		fmd_case_hash_apply(fmd.d_cases, fmd_adm_caselist_case, rvp);
741 
742 	return (TRUE);
743 }
744 
745 bool_t
746 fmd_adm_caseinfo_1_svc(char *uuid, struct fmd_rpc_caseinfo *rvp,
747     struct svc_req *req)
748 {
749 	fmd_case_t *cp;
750 	nvlist_t *nvl;
751 	int err = 0;
752 
753 	bzero(rvp, sizeof (struct fmd_rpc_caseinfo));
754 
755 	if (fmd_rpc_deny(req)) {
756 		rvp->rci_err = FMD_ADM_ERR_PERM;
757 		return (TRUE);
758 	}
759 
760 	if ((cp = fmd_case_hash_lookup(fmd.d_cases, uuid)) == NULL) {
761 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
762 		return (TRUE);
763 	}
764 
765 	if (!(((fmd_case_impl_t *)cp)->ci_flags & FMD_CF_SOLVED)) {
766 		fmd_case_rele(cp);
767 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
768 		return (TRUE);
769 	}
770 
771 	nvl = fmd_case_mkevent(cp, FM_LIST_SUSPECT_CLASS);
772 
773 	err = nvlist_pack(nvl, &rvp->rci_evbuf.rci_evbuf_val,
774 	    &rvp->rci_evbuf.rci_evbuf_len, NV_ENCODE_XDR, 0);
775 
776 	nvlist_free(nvl);
777 
778 	if (err != 0)
779 		rvp->rci_err = FMD_ADM_ERR_NOMEM;
780 
781 	fmd_case_rele(cp);
782 
783 	return (TRUE);
784 }
785 
786 /*ARGSUSED*/
787 static void
788 fmd_adm_xprtlist_one(fmd_idspace_t *ids, id_t id, void *arg)
789 {
790 	struct fmd_rpc_xprtlist *rvp = arg;
791 
792 	if (rvp->rxl_len < rvp->rxl_buf.rxl_buf_len)
793 		rvp->rxl_buf.rxl_buf_val[rvp->rxl_len++] = id;
794 }
795 
796 bool_t
797 fmd_adm_xprtlist_1_svc(struct fmd_rpc_xprtlist *rvp, struct svc_req *req)
798 {
799 	if (fmd_rpc_deny(req)) {
800 		rvp->rxl_buf.rxl_buf_len = 0;
801 		rvp->rxl_buf.rxl_buf_val = NULL;
802 		rvp->rxl_len = 0;
803 		rvp->rxl_err = FMD_ADM_ERR_PERM;
804 		return (TRUE);
805 	}
806 
807 	/*
808 	 * Since we're taking a snapshot of the transports, and these could
809 	 * change after we return our result, there's no need to hold any kind
810 	 * of lock between retrieving ids_count and taking the snapshot.  We'll
811 	 * just capture up to a maximum of whatever ids_count value we sampled.
812 	 */
813 	rvp->rxl_buf.rxl_buf_len = fmd.d_xprt_ids->ids_count;
814 	rvp->rxl_buf.rxl_buf_val = malloc(sizeof (int32_t) *
815 	    rvp->rxl_buf.rxl_buf_len);
816 	rvp->rxl_len = 0;
817 	rvp->rxl_err = 0;
818 
819 	if (rvp->rxl_buf.rxl_buf_val == NULL) {
820 		rvp->rxl_err = FMD_ADM_ERR_NOMEM;
821 		return (TRUE);
822 	}
823 
824 	fmd_idspace_apply(fmd.d_xprt_ids, fmd_adm_xprtlist_one, rvp);
825 	return (TRUE);
826 }
827 
828 bool_t
829 fmd_adm_xprtstat_1_svc(int32_t id,
830     struct fmd_rpc_modstat *rms, struct svc_req *req)
831 {
832 	fmd_xprt_impl_t *xip;
833 	fmd_stat_t *sp, *ep, *cp;
834 
835 	if (fmd_rpc_deny(req)) {
836 		rms->rms_buf.rms_buf_val = NULL;
837 		rms->rms_buf.rms_buf_len = 0;
838 		rms->rms_err = FMD_ADM_ERR_PERM;
839 		return (TRUE);
840 	}
841 
842 	rms->rms_buf.rms_buf_val = malloc(sizeof (fmd_xprt_stat_t));
843 	rms->rms_buf.rms_buf_len = sizeof (fmd_xprt_stat_t) /
844 	    sizeof (fmd_stat_t);
845 	rms->rms_err = 0;
846 
847 	if (rms->rms_buf.rms_buf_val == NULL) {
848 		rms->rms_err = FMD_ADM_ERR_NOMEM;
849 		rms->rms_buf.rms_buf_len = 0;
850 		return (TRUE);
851 	}
852 
853 	if ((xip = fmd_idspace_hold(fmd.d_xprt_ids, id)) == NULL) {
854 		rms->rms_err = FMD_ADM_ERR_XPRTSRCH;
855 		return (TRUE);
856 	}
857 
858 	/*
859 	 * Grab the stats lock and bcopy the entire transport stats array in
860 	 * one shot. Then go back through and duplicate any string values.
861 	 */
862 	(void) pthread_mutex_lock(&xip->xi_stats_lock);
863 
864 	sp = (fmd_stat_t *)xip->xi_stats;
865 	ep = sp + rms->rms_buf.rms_buf_len;
866 	cp = rms->rms_buf.rms_buf_val;
867 
868 	bcopy(sp, cp, sizeof (fmd_xprt_stat_t));
869 
870 	for (; sp < ep; sp++, cp++) {
871 		if (sp->fmds_type == FMD_TYPE_STRING &&
872 		    sp->fmds_value.str != NULL)
873 			cp->fmds_value.str = strdup(sp->fmds_value.str);
874 	}
875 
876 	(void) pthread_mutex_unlock(&xip->xi_stats_lock);
877 	fmd_idspace_rele(fmd.d_xprt_ids, id);
878 
879 	return (TRUE);
880 }
881 
882 int
883 fmd_adm_1_freeresult(SVCXPRT *xprt, xdrproc_t proc, caddr_t data)
884 {
885 	xdr_free(proc, data);
886 	svc_done(xprt);
887 	return (TRUE);
888 }
889 
890 /*
891  * Custom XDR routine for our API structure fmd_stat_t.  This function must
892  * match the definition of fmd_stat_t in <fmd_api.h> and must also match
893  * the corresponding routine in usr/src/lib/fm/libfmd_adm/common/fmd_adm.c.
894  */
895 bool_t
896 xdr_fmd_stat(XDR *xp, fmd_stat_t *sp)
897 {
898 	bool_t rv = TRUE;
899 
900 	rv &= xdr_opaque(xp, sp->fmds_name, sizeof (sp->fmds_name));
901 	rv &= xdr_u_int(xp, &sp->fmds_type);
902 	rv &= xdr_opaque(xp, sp->fmds_desc, sizeof (sp->fmds_desc));
903 
904 	switch (sp->fmds_type) {
905 	case FMD_TYPE_BOOL:
906 		rv &= xdr_int(xp, &sp->fmds_value.bool);
907 		break;
908 	case FMD_TYPE_INT32:
909 		rv &= xdr_int32_t(xp, &sp->fmds_value.i32);
910 		break;
911 	case FMD_TYPE_UINT32:
912 		rv &= xdr_uint32_t(xp, &sp->fmds_value.ui32);
913 		break;
914 	case FMD_TYPE_INT64:
915 		rv &= xdr_int64_t(xp, &sp->fmds_value.i64);
916 		break;
917 	case FMD_TYPE_UINT64:
918 	case FMD_TYPE_TIME:
919 	case FMD_TYPE_SIZE:
920 		rv &= xdr_uint64_t(xp, &sp->fmds_value.ui64);
921 		break;
922 	case FMD_TYPE_STRING:
923 		rv &= xdr_string(xp, &sp->fmds_value.str, ~0);
924 		break;
925 	}
926 
927 	return (rv);
928 }
929