xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_rpc_adm.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
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 bool_t
671 fmd_adm_caserepair_1_svc(char *uuid, int *rvp, struct svc_req *req)
672 {
673 	fmd_case_t *cp = NULL;
674 	int err = 0;
675 
676 	if (fmd_rpc_deny(req))
677 		err = FMD_ADM_ERR_PERM;
678 	else if ((cp = fmd_case_hash_lookup(fmd.d_cases, uuid)) == NULL)
679 		err = FMD_ADM_ERR_CASESRCH;
680 	else if (fmd_case_repair(cp) != 0) {
681 		err = errno == EFMD_CASE_OWNER ?
682 		    FMD_ADM_ERR_CASEXPRT : FMD_ADM_ERR_CASEOPEN;
683 	}
684 
685 	if (cp != NULL)
686 		fmd_case_rele(cp);
687 
688 	*rvp = err;
689 	return (TRUE);
690 }
691 
692 void
693 fmd_adm_caselist_case(fmd_case_t *cp, void *arg)
694 {
695 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
696 	struct fmd_rpc_caselist *rcl = arg;
697 	size_t uuid_len, buf_len;
698 	void *p;
699 
700 	if (rcl->rcl_err != 0)
701 		return;
702 
703 	/*
704 	 * Lock the case and reallocate rcl_buf[] to be large enough to hold
705 	 * another string, doubling it as needed.  Then copy the new string
706 	 * on to the end, and increment rcl_len to indicate the used space.
707 	 */
708 	if (!(cip->ci_flags & FMD_CF_SOLVED))
709 		return;
710 
711 	(void) pthread_mutex_lock(&cip->ci_lock);
712 
713 	uuid_len = cip->ci_uuidlen + 1;
714 
715 	while (rcl->rcl_len + uuid_len > rcl->rcl_buf.rcl_buf_len) {
716 		if (rcl->rcl_buf.rcl_buf_len != 0)
717 			buf_len = rcl->rcl_buf.rcl_buf_len * 2;
718 		else
719 			buf_len = 1024; /* default buffer size */
720 
721 		if ((p = realloc(rcl->rcl_buf.rcl_buf_val, buf_len)) != NULL) {
722 			bzero((char *)p + rcl->rcl_buf.rcl_buf_len,
723 			    buf_len - rcl->rcl_buf.rcl_buf_len);
724 			rcl->rcl_buf.rcl_buf_val = p;
725 			rcl->rcl_buf.rcl_buf_len = buf_len;
726 		} else {
727 			rcl->rcl_err = FMD_ADM_ERR_NOMEM;
728 			break;
729 		}
730 	}
731 
732 	if (rcl->rcl_err == 0) {
733 		bcopy(cip->ci_uuid, (char *)rcl->rcl_buf.rcl_buf_val +
734 		    rcl->rcl_len, uuid_len);
735 		rcl->rcl_len += uuid_len;
736 		rcl->rcl_cnt++;
737 	}
738 
739 	(void) pthread_mutex_unlock(&cip->ci_lock);
740 }
741 
742 bool_t
743 fmd_adm_caselist_1_svc(struct fmd_rpc_caselist *rvp, struct svc_req *req)
744 {
745 	rvp->rcl_buf.rcl_buf_len = 0;
746 	rvp->rcl_buf.rcl_buf_val = NULL;
747 	rvp->rcl_len = 0;
748 	rvp->rcl_cnt = 0;
749 	rvp->rcl_err = 0;
750 
751 	if (fmd_rpc_deny(req))
752 		rvp->rcl_err = FMD_ADM_ERR_PERM;
753 	else
754 		fmd_case_hash_apply(fmd.d_cases, fmd_adm_caselist_case, rvp);
755 
756 	return (TRUE);
757 }
758 
759 bool_t
760 fmd_adm_caseinfo_1_svc(char *uuid, struct fmd_rpc_caseinfo *rvp,
761     struct svc_req *req)
762 {
763 	fmd_case_impl_t *cip;
764 	nvlist_t *nvl;
765 	int err = 0;
766 
767 	bzero(rvp, sizeof (struct fmd_rpc_caseinfo));
768 
769 	if (fmd_rpc_deny(req)) {
770 		rvp->rci_err = FMD_ADM_ERR_PERM;
771 		return (TRUE);
772 	}
773 
774 	if ((cip = (fmd_case_impl_t *)fmd_case_hash_lookup(fmd.d_cases, uuid))
775 	    == NULL) {
776 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
777 		return (TRUE);
778 	}
779 
780 	if (!(cip->ci_flags & FMD_CF_SOLVED)) {
781 		fmd_case_rele((fmd_case_t *)cip);
782 		rvp->rci_err = FMD_ADM_ERR_CASESRCH;
783 		return (TRUE);
784 	}
785 
786 	/*
787 	 * Avoid a race on cip->ci_diag; we can't call fmd_case_mkevent
788 	 * while holding the case lock, so we do this dance instead.
789 	 */
790 	(void) pthread_mutex_lock(&cip->ci_lock);
791 	nvl = cip->ci_diag;
792 	(void) pthread_mutex_unlock(&cip->ci_lock);
793 
794 	if (nvl == NULL)
795 		nvl = fmd_case_mkevent((fmd_case_t *)cip,
796 		    FM_LIST_SUSPECT_CLASS);
797 
798 	(void) pthread_mutex_lock(&cip->ci_lock);
799 	if (cip->ci_diag == NULL) {
800 		cip->ci_diag = nvl;
801 	} else if (nvl != cip->ci_diag) {
802 		nvlist_free(nvl);
803 		nvl = cip->ci_diag;
804 	}
805 	(void) pthread_mutex_unlock(&cip->ci_lock);
806 
807 	err = nvlist_pack(nvl, &rvp->rci_evbuf.rci_evbuf_val,
808 	    &rvp->rci_evbuf.rci_evbuf_len, NV_ENCODE_XDR, 0);
809 
810 	if (err != 0)
811 		rvp->rci_err = FMD_ADM_ERR_NOMEM;
812 
813 	fmd_case_rele((fmd_case_t *)cip);
814 
815 	return (TRUE);
816 }
817 
818 /*ARGSUSED*/
819 static void
820 fmd_adm_xprtlist_one(fmd_idspace_t *ids, id_t id, void *arg)
821 {
822 	struct fmd_rpc_xprtlist *rvp = arg;
823 
824 	if (rvp->rxl_len < rvp->rxl_buf.rxl_buf_len)
825 		rvp->rxl_buf.rxl_buf_val[rvp->rxl_len++] = id;
826 }
827 
828 bool_t
829 fmd_adm_xprtlist_1_svc(struct fmd_rpc_xprtlist *rvp, struct svc_req *req)
830 {
831 	if (fmd_rpc_deny(req)) {
832 		rvp->rxl_buf.rxl_buf_len = 0;
833 		rvp->rxl_buf.rxl_buf_val = NULL;
834 		rvp->rxl_len = 0;
835 		rvp->rxl_err = FMD_ADM_ERR_PERM;
836 		return (TRUE);
837 	}
838 
839 	/*
840 	 * Since we're taking a snapshot of the transports, and these could
841 	 * change after we return our result, there's no need to hold any kind
842 	 * of lock between retrieving ids_count and taking the snapshot.  We'll
843 	 * just capture up to a maximum of whatever ids_count value we sampled.
844 	 */
845 	rvp->rxl_buf.rxl_buf_len = fmd.d_xprt_ids->ids_count;
846 	rvp->rxl_buf.rxl_buf_val = malloc(sizeof (int32_t) *
847 	    rvp->rxl_buf.rxl_buf_len);
848 	rvp->rxl_len = 0;
849 	rvp->rxl_err = 0;
850 
851 	if (rvp->rxl_buf.rxl_buf_val == NULL) {
852 		rvp->rxl_err = FMD_ADM_ERR_NOMEM;
853 		return (TRUE);
854 	}
855 
856 	fmd_idspace_apply(fmd.d_xprt_ids, fmd_adm_xprtlist_one, rvp);
857 	return (TRUE);
858 }
859 
860 bool_t
861 fmd_adm_xprtstat_1_svc(int32_t id,
862     struct fmd_rpc_modstat *rms, struct svc_req *req)
863 {
864 	fmd_xprt_impl_t *xip;
865 	fmd_stat_t *sp, *ep, *cp;
866 
867 	if (fmd_rpc_deny(req)) {
868 		rms->rms_buf.rms_buf_val = NULL;
869 		rms->rms_buf.rms_buf_len = 0;
870 		rms->rms_err = FMD_ADM_ERR_PERM;
871 		return (TRUE);
872 	}
873 
874 	rms->rms_buf.rms_buf_val = malloc(sizeof (fmd_xprt_stat_t));
875 	rms->rms_buf.rms_buf_len = sizeof (fmd_xprt_stat_t) /
876 	    sizeof (fmd_stat_t);
877 	rms->rms_err = 0;
878 
879 	if (rms->rms_buf.rms_buf_val == NULL) {
880 		rms->rms_err = FMD_ADM_ERR_NOMEM;
881 		rms->rms_buf.rms_buf_len = 0;
882 		return (TRUE);
883 	}
884 
885 	if ((xip = fmd_idspace_hold(fmd.d_xprt_ids, id)) == NULL) {
886 		rms->rms_err = FMD_ADM_ERR_XPRTSRCH;
887 		return (TRUE);
888 	}
889 
890 	/*
891 	 * Grab the stats lock and bcopy the entire transport stats array in
892 	 * one shot. Then go back through and duplicate any string values.
893 	 */
894 	(void) pthread_mutex_lock(&xip->xi_stats_lock);
895 
896 	sp = (fmd_stat_t *)xip->xi_stats;
897 	ep = sp + rms->rms_buf.rms_buf_len;
898 	cp = rms->rms_buf.rms_buf_val;
899 
900 	bcopy(sp, cp, sizeof (fmd_xprt_stat_t));
901 
902 	for (; sp < ep; sp++, cp++) {
903 		if (sp->fmds_type == FMD_TYPE_STRING &&
904 		    sp->fmds_value.str != NULL)
905 			cp->fmds_value.str = strdup(sp->fmds_value.str);
906 	}
907 
908 	(void) pthread_mutex_unlock(&xip->xi_stats_lock);
909 	fmd_idspace_rele(fmd.d_xprt_ids, id);
910 
911 	return (TRUE);
912 }
913 
914 int
915 fmd_adm_1_freeresult(SVCXPRT *xprt, xdrproc_t proc, caddr_t data)
916 {
917 	xdr_free(proc, data);
918 	svc_done(xprt);
919 	return (TRUE);
920 }
921 
922 /*
923  * Custom XDR routine for our API structure fmd_stat_t.  This function must
924  * match the definition of fmd_stat_t in <fmd_api.h> and must also match
925  * the corresponding routine in usr/src/lib/fm/libfmd_adm/common/fmd_adm.c.
926  */
927 bool_t
928 xdr_fmd_stat(XDR *xp, fmd_stat_t *sp)
929 {
930 	bool_t rv = TRUE;
931 
932 	rv &= xdr_opaque(xp, sp->fmds_name, sizeof (sp->fmds_name));
933 	rv &= xdr_u_int(xp, &sp->fmds_type);
934 	rv &= xdr_opaque(xp, sp->fmds_desc, sizeof (sp->fmds_desc));
935 
936 	switch (sp->fmds_type) {
937 	case FMD_TYPE_BOOL:
938 		rv &= xdr_int(xp, &sp->fmds_value.bool);
939 		break;
940 	case FMD_TYPE_INT32:
941 		rv &= xdr_int32_t(xp, &sp->fmds_value.i32);
942 		break;
943 	case FMD_TYPE_UINT32:
944 		rv &= xdr_uint32_t(xp, &sp->fmds_value.ui32);
945 		break;
946 	case FMD_TYPE_INT64:
947 		rv &= xdr_int64_t(xp, &sp->fmds_value.i64);
948 		break;
949 	case FMD_TYPE_UINT64:
950 	case FMD_TYPE_TIME:
951 	case FMD_TYPE_SIZE:
952 		rv &= xdr_uint64_t(xp, &sp->fmds_value.ui64);
953 		break;
954 	case FMD_TYPE_STRING:
955 		rv &= xdr_string(xp, &sp->fmds_value.str, ~0);
956 		break;
957 	}
958 
959 	return (rv);
960 }
961