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