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 (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2015, Joyent, Inc. All rights reserved. 25 * Copyright 2019, Carlos Neira <cneirabustos@gmail.com> 26 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association. 27 */ 28 29 #include <stdio.h> 30 #define __EXTENSIONS__ 31 #include <string.h> 32 #undef __EXTENSIONS__ 33 #include <signal.h> 34 #include <errno.h> 35 #include "libproc.h" 36 #include <sys/procfs_isa.h> 37 38 static const char * 39 rawfltname(int flt) 40 { 41 const char *name; 42 43 switch (flt) { 44 case FLTILL: name = "FLTILL"; break; 45 case FLTPRIV: name = "FLTPRIV"; break; 46 case FLTBPT: name = "FLTBPT"; break; 47 case FLTTRACE: name = "FLTTRACE"; break; 48 case FLTACCESS: name = "FLTACCESS"; break; 49 case FLTBOUNDS: name = "FLTBOUNDS"; break; 50 case FLTIOVF: name = "FLTIOVF"; break; 51 case FLTIZDIV: name = "FLTIZDIV"; break; 52 case FLTFPE: name = "FLTFPE"; break; 53 case FLTSTACK: name = "FLTSTACK"; break; 54 case FLTPAGE: name = "FLTPAGE"; break; 55 case FLTWATCH: name = "FLTWATCH"; break; 56 case FLTCPCOVF: name = "FLTCPCOVF"; break; 57 default: name = NULL; break; 58 } 59 60 return (name); 61 } 62 63 /* 64 * Return the name of a fault. 65 * Manufacture a name for unknown fault. 66 */ 67 char * 68 proc_fltname(int flt, char *buf, size_t bufsz) 69 { 70 const char *name = rawfltname(flt); 71 size_t len; 72 73 if (bufsz == 0) /* force a program failure */ 74 return (NULL); 75 76 if (name != NULL) { 77 len = strlen(name); 78 (void) strncpy(buf, name, bufsz); 79 } else { 80 len = snprintf(buf, bufsz, "FLT#%d", flt); 81 } 82 83 if (len >= bufsz) /* ensure null-termination */ 84 buf[bufsz-1] = '\0'; 85 86 return (buf); 87 } 88 89 /* 90 * Return the name of a signal. 91 * Manufacture a name for unknown signal. 92 */ 93 char * 94 proc_signame(int sig, char *buf, size_t bufsz) 95 { 96 char name[SIG2STR_MAX+4]; 97 size_t len; 98 99 if (bufsz == 0) /* force a program failure */ 100 return (NULL); 101 102 /* sig2str() omits the leading "SIG" */ 103 (void) strcpy(name, "SIG"); 104 105 if (sig2str(sig, name+3) == 0) { 106 len = strlen(name); 107 (void) strncpy(buf, name, bufsz); 108 } else { 109 len = snprintf(buf, bufsz, "SIG#%d", sig); 110 } 111 112 if (len >= bufsz) /* ensure null-termination */ 113 buf[bufsz-1] = '\0'; 114 115 return (buf); 116 } 117 118 static const char *const systable[] = { 119 NULL, /* 0 */ 120 "_exit", /* 1 */ 121 NULL, /* 2 */ 122 "read", /* 3 */ 123 "write", /* 4 */ 124 "open", /* 5 */ 125 "close", /* 6 */ 126 "linkat", /* 7 */ 127 NULL, /* 8 */ 128 "link", /* 9 */ 129 "unlink", /* 10 */ 130 "symlinkat", /* 11 */ 131 "chdir", /* 12 */ 132 "time", /* 13 */ 133 "mknod", /* 14 */ 134 "chmod", /* 15 */ 135 "chown", /* 16 */ 136 "brk", /* 17 */ 137 "stat", /* 18 */ 138 "lseek", /* 19 */ 139 "getpid", /* 20 */ 140 "mount", /* 21 */ 141 "readlinkat", /* 22 */ 142 "setuid", /* 23 */ 143 "getuid", /* 24 */ 144 "stime", /* 25 */ 145 "ptrace", /* 26 */ 146 "alarm", /* 27 */ 147 "fstat", /* 28 */ 148 "pause", /* 29 */ 149 NULL, /* 30 */ 150 "stty", /* 31 */ 151 "gtty", /* 32 */ 152 "access", /* 33 */ 153 "nice", /* 34 */ 154 "statfs", /* 35 */ 155 "sync", /* 36 */ 156 "kill", /* 37 */ 157 "fstatfs", /* 38 */ 158 "pgrpsys", /* 39 */ 159 "uucopystr", /* 40 */ 160 NULL, /* 41 */ 161 "pipe", /* 42 */ 162 "times", /* 43 */ 163 "profil", /* 44 */ 164 "faccessat", /* 45 */ 165 "setgid", /* 46 */ 166 "getgid", /* 47 */ 167 "mknodat", /* 48 */ 168 "msgsys", /* 49 */ 169 "sysi86", /* 50 */ 170 "acct", /* 51 */ 171 "shmsys", /* 52 */ 172 "semsys", /* 53 */ 173 "ioctl", /* 54 */ 174 "uadmin", /* 55 */ 175 "fchownat", /* 56 */ 176 "utssys", /* 57 */ 177 "fdsync", /* 58 */ 178 "execve", /* 59 */ 179 "umask", /* 60 */ 180 "chroot", /* 61 */ 181 "fcntl", /* 62 */ 182 "ulimit", /* 63 */ 183 "renameat", /* 64 */ 184 "unlinkat", /* 65 */ 185 "fstatat", /* 66 */ 186 "fstatat64", /* 67 */ 187 "openat", /* 68 */ 188 "openat64", /* 69 */ 189 "tasksys", /* 70 */ 190 "acctctl", /* 71 */ 191 "exacctsys", /* 72 */ 192 "getpagesizes", /* 73 */ 193 "rctlsys", /* 74 */ 194 "issetugid", /* 75 */ 195 "fsat", /* 76 */ 196 "lwp_park", /* 77 */ 197 "sendfilev", /* 78 */ 198 "rmdir", /* 79 */ 199 "mkdir", /* 80 */ 200 "getdents", /* 81 */ 201 "privsys", /* 82 */ 202 "ucredsys", /* 83 */ 203 "sysfs", /* 84 */ 204 "getmsg", /* 85 */ 205 "putmsg", /* 86 */ 206 NULL, /* 87 */ 207 "lstat", /* 88 */ 208 "symlink", /* 89 */ 209 "readlink", /* 90 */ 210 "setgroups", /* 91 */ 211 "getgroups", /* 92 */ 212 "fchmod", /* 93 */ 213 "fchown", /* 94 */ 214 "sigprocmask", /* 95 */ 215 "sigsuspend", /* 96 */ 216 "sigaltstack", /* 97 */ 217 "sigaction", /* 98 */ 218 "sigpending", /* 99 */ 219 "context", /* 100 */ 220 "fchmodat", /* 101 */ 221 "mkdirat", /* 102 */ 222 "statvfs", /* 103 */ 223 "fstatvfs", /* 104 */ 224 "getloadavg", /* 105 */ 225 "nfssys", /* 106 */ 226 "waitid", /* 107 */ 227 "sigsendsys", /* 108 */ 228 "hrtsys", /* 109 */ 229 "acancel", /* 110 */ 230 "async", /* 111 */ 231 "priocntlsys", /* 112 */ 232 "pathconf", /* 113 */ 233 "mincore", /* 114 */ 234 "mmap", /* 115 */ 235 "mprotect", /* 116 */ 236 "munmap", /* 117 */ 237 "fpathconf", /* 118 */ 238 "vfork", /* 119 */ 239 "fchdir", /* 120 */ 240 "readv", /* 121 */ 241 "writev", /* 122 */ 242 "preadv", /* 123 */ 243 "pwritev", /* 124 */ 244 NULL, /* 125 */ 245 "getrandom", /* 126 */ 246 "mmapobj", /* 127 */ 247 "setrlimit", /* 128 */ 248 "getrlimit", /* 129 */ 249 "lchown", /* 130 */ 250 "memcntl", /* 131 */ 251 "getpmsg", /* 132 */ 252 "putpmsg", /* 133 */ 253 "rename", /* 134 */ 254 "uname", /* 135 */ 255 "setegid", /* 136 */ 256 "sysconfig", /* 137 */ 257 "adjtime", /* 138 */ 258 "systeminfo", /* 139 */ 259 "sharefs", /* 140 */ 260 "seteuid", /* 141 */ 261 "forksys", /* 142 */ 262 NULL, /* 143 */ 263 "sigtimedwait", /* 144 */ 264 "lwp_info", /* 145 */ 265 "yield", /* 146 */ 266 NULL, /* 147 */ 267 "lwp_sema_post", /* 148 */ 268 "lwp_sema_trywait", /* 149 */ 269 "lwp_detatch", /* 150 */ 270 "corectl", /* 151 */ 271 "modctl", /* 152 */ 272 "fchroot", /* 153 */ 273 NULL, /* 154 */ 274 "vhangup", /* 155 */ 275 "gettimeofday", /* 156 */ 276 "getitimer", /* 157 */ 277 "setitimer", /* 158 */ 278 "lwp_create", /* 159 */ 279 "lwp_exit", /* 160 */ 280 "lwp_suspend", /* 161 */ 281 "lwp_continue", /* 162 */ 282 "lwp_kill", /* 163 */ 283 "lwp_self", /* 164 */ 284 "lwp_sigmask", /* 165 */ 285 "lwp_private", /* 166 */ 286 "lwp_wait", /* 167 */ 287 "lwp_mutex_wakeup", /* 168 */ 288 NULL, /* 169 */ 289 "lwp_cond_wait", /* 170 */ 290 "lwp_cond_signal", /* 171 */ 291 "lwp_cond_broadcast", /* 172 */ 292 "pread", /* 173 */ 293 "pwrite", /* 174 */ 294 "llseek", /* 175 */ 295 "inst_sync", /* 176 */ 296 "brand", /* 177 */ 297 "kaio", /* 178 */ 298 "cpc", /* 179 */ 299 "lgrpsys", /* 180 */ 300 "rusagesys", /* 181 */ 301 "portfs", /* 182 */ 302 "pollsys", /* 183 */ 303 "labelsys", /* 184 */ 304 "acl", /* 185 */ 305 "auditsys", /* 186 */ 306 "processor_bind", /* 187 */ 307 "processor_info", /* 188 */ 308 "p_online", /* 189 */ 309 "sigqueue", /* 190 */ 310 "clock_gettime", /* 191 */ 311 "clock_settime", /* 192 */ 312 "clock_getres", /* 193 */ 313 "timer_create", /* 194 */ 314 "timer_delete", /* 195 */ 315 "timer_settime", /* 196 */ 316 "timer_gettime", /* 197 */ 317 "timer_getoverrun", /* 198 */ 318 "nanosleep", /* 199 */ 319 "facl", /* 200 */ 320 "door", /* 201 */ 321 "setreuid", /* 202 */ 322 "setregid", /* 203 */ 323 "install_utrap", /* 204 */ 324 "signotify", /* 205 */ 325 "schedctl", /* 206 */ 326 "pset", /* 207 */ 327 "sparc_utrap_install", /* 208 */ 328 "resolvepath", /* 209 */ 329 "lwp_mutex_timedlock", /* 210 */ 330 "lwp_sema_timedwait", /* 211 */ 331 "lwp_rwlock_sys", /* 212 */ 332 "getdents64", /* 213 */ 333 "mmap64", /* 214 */ 334 "stat64", /* 215 */ 335 "lstat64", /* 216 */ 336 "fstat64", /* 217 */ 337 "statvfs64", /* 218 */ 338 "fstatvfs64", /* 219 */ 339 "setrlimit64", /* 220 */ 340 "getrlimit64", /* 221 */ 341 "pread64", /* 222 */ 342 "pwrite64", /* 223 */ 343 NULL, /* 224 */ 344 "open64", /* 225 */ 345 "rpcmod", /* 226 */ 346 "zone", /* 227 */ 347 "autofssys", /* 228 */ 348 "getcwd", /* 229 */ 349 "so_socket", /* 230 */ 350 "so_socketpair", /* 231 */ 351 "bind", /* 232 */ 352 "listen", /* 233 */ 353 "accept", /* 234 */ 354 "connect", /* 235 */ 355 "shutdown", /* 236 */ 356 "recv", /* 237 */ 357 "recvfrom", /* 238 */ 358 "recvmsg", /* 239 */ 359 "send", /* 240 */ 360 "sendmsg", /* 241 */ 361 "sendto", /* 242 */ 362 "getpeername", /* 243 */ 363 "getsockname", /* 244 */ 364 "getsockopt", /* 245 */ 365 "setsockopt", /* 246 */ 366 "sockconfig", /* 247 */ 367 "ntp_gettime", /* 248 */ 368 "ntp_adjtime", /* 249 */ 369 "lwp_mutex_unlock", /* 250 */ 370 "lwp_mutex_trylock", /* 251 */ 371 "lwp_mutex_register", /* 252 */ 372 "cladm", /* 253 */ 373 "uucopy", /* 254 */ 374 "umount2" /* 255 */ 375 }; 376 377 /* SYSEND == max syscall number + 1 */ 378 #define SYSEND (sizeof (systable) / sizeof (systable[0])) 379 380 /* 381 * Return the name of a system call. 382 * Manufacture a name for unknown system call. 383 */ 384 char * 385 proc_sysname(int sys, char *buf, size_t bufsz) 386 { 387 const char *name; 388 size_t len; 389 390 if (bufsz == 0) /* force a program failure */ 391 return (NULL); 392 393 if (sys >= 0 && sys < SYSEND) 394 name = systable[sys]; 395 else 396 name = NULL; 397 398 if (name != NULL) { 399 len = strlen(name); 400 (void) strncpy(buf, name, bufsz); 401 } else { 402 len = snprintf(buf, bufsz, "SYS#%d", sys); 403 } 404 405 if (len >= bufsz) /* ensure null-termination */ 406 buf[bufsz-1] = '\0'; 407 408 return (buf); 409 } 410 411 /* 412 * Convert a string representation of a fault to the corresponding number. 413 */ 414 int 415 proc_str2flt(const char *str, int *fltnum) 416 { 417 char *next; 418 int i; 419 420 i = strtol(str, &next, 0); 421 if (i > 0 && i <= PRMAXFAULT && *next == '\0') { 422 *fltnum = i; 423 return (0); 424 } 425 426 for (i = 1; i <= PRMAXFAULT; i++) { 427 const char *s = rawfltname(i); 428 429 if (s && (strcasecmp(s, str) == 0 || 430 strcasecmp(s + 3, str) == 0)) { 431 *fltnum = i; 432 return (0); 433 } 434 } 435 436 return (-1); 437 } 438 439 /* 440 * Convert a string representation of a signal to the signal number. This 441 * functionality is already available in libc, but the interface doesn't 442 * optionally accept a "SIG" prefix. We strip that first, and then call libc. 443 */ 444 int 445 proc_str2sig(const char *str, int *signum) 446 { 447 if (strncasecmp(str, "SIG", 3) == 0) 448 str += 3; /* skip prefix */ 449 450 return (str2sig(str, signum)); 451 } 452 453 /* 454 * Convert a string representation of a system call to the corresponding number. 455 * We do this by performing a simple linear search of the table above. 456 */ 457 int 458 proc_str2sys(const char *str, int *sysnum) 459 { 460 char *next; 461 int i; 462 463 i = strtol(str, &next, 0); 464 if (i > 0 && i <= PRMAXSYS && *next == '\0') { 465 *sysnum = i; 466 return (0); 467 } 468 469 for (i = 1; i < SYSEND; i++) { 470 if (systable[i] != NULL && strcmp(systable[i], str) == 0) { 471 *sysnum = i; 472 return (0); 473 } 474 } 475 476 return (-1); 477 } 478 479 /* 480 * Convert a fltset_t to a string representation consisting of canonical 481 * machine fault names separated by the given delimeter string. If 482 * m is non-zero (TRUE), set members are printed. If m is zero (FALSE), set 483 * non-members are printed. If the specified buf is too small to hold the 484 * complete formatted set, NULL is returned; otherwise buf is returned. 485 */ 486 char * 487 proc_fltset2str(const fltset_t *set, const char *delim, int m, 488 char *buf, size_t len) 489 { 490 char name[FLT2STR_MAX], *p = buf; 491 size_t n; 492 int i; 493 494 if (buf == NULL || len < 1) { 495 errno = EINVAL; 496 return (NULL); 497 } 498 499 buf[0] = '\0'; /* Set first byte to \0 */ 500 501 for (i = 1; i <= PRMAXFAULT; i++) { 502 if ((prismember(set, i) != 0) ^ (m == 0)) { 503 (void) proc_fltname(i, name, sizeof (name)); 504 505 if (buf[0] != '\0') 506 n = snprintf(p, len, "%s%s", delim, name); 507 else 508 n = snprintf(p, len, "%s", name); 509 510 if (n != strlen(p)) { 511 errno = ENAMETOOLONG; /* Output was truncated */ 512 return (NULL); 513 } 514 len -= n; 515 p += n; 516 } 517 } 518 return (buf); 519 } 520 521 /* 522 * Convert a sigset_t to a string representation consisting of canonical signal 523 * names (without the SIG prefix). Parameters and return values analogous to 524 * proc_fltset2str(). 525 */ 526 char * 527 proc_sigset2str(const sigset_t *set, const char *delim, int m, 528 char *buf, size_t len) 529 { 530 char name[SIG2STR_MAX], *p = buf; 531 size_t n; 532 int i; 533 534 if (buf == NULL || len < 1) { 535 errno = EINVAL; 536 return (NULL); 537 } 538 539 m = (m != 0); /* Make sure m is 0 or 1 */ 540 buf[0] = '\0'; /* Set first byte to \0 */ 541 542 /* 543 * Unlike proc_fltset2str() and proc_sysset2str(), we don't loop 544 * until i <= NSIG here, because sigismember() rejects i == NSIG. 545 */ 546 for (i = 1; i < NSIG; i++) { 547 if (sigismember(set, i) == m) { 548 (void) sig2str(i, name); 549 550 if (buf[0] != '\0') 551 n = snprintf(p, len, "%s%s", delim, name); 552 else 553 n = snprintf(p, len, "%s", name); 554 555 if (n != strlen(p)) { 556 errno = ENAMETOOLONG; /* Output was truncated */ 557 return (NULL); 558 } 559 560 len -= n; 561 p += n; 562 } 563 } 564 565 return (buf); 566 } 567 568 /* 569 * Convert a sysset_t to a string representation consisting of canonical system 570 * call names. Parameters and return values analogous to proc_fltset2str(). 571 */ 572 char * 573 proc_sysset2str(const sysset_t *set, const char *delim, int m, 574 char *buf, size_t len) 575 { 576 char name[SYS2STR_MAX], *p = buf; 577 size_t n; 578 int i; 579 580 if (buf == NULL || len < 1) { 581 errno = EINVAL; 582 return (NULL); 583 } 584 585 buf[0] = '\0'; /* Set first byte to \0 */ 586 587 for (i = 1; i <= PRMAXSYS; i++) { 588 if ((prismember(set, i) != 0) ^ (m == 0)) { 589 (void) proc_sysname(i, name, sizeof (name)); 590 591 if (buf[0] != '\0') 592 n = snprintf(p, len, "%s%s", delim, name); 593 else 594 n = snprintf(p, len, "%s", name); 595 596 if (n != strlen(p)) { 597 errno = ENAMETOOLONG; /* Output was truncated */ 598 return (NULL); 599 } 600 len -= n; 601 p += n; 602 } 603 } 604 return (buf); 605 } 606 607 /* 608 * Convert a string representation of a fault set (names separated by 609 * one or more of the given delimeters) to a fltset_t. 610 * If m is non-zero (TRUE), members of the string representation are set. 611 * If m is zero (FALSE), non-members of the string representation are set. 612 * This function returns NULL for success. Otherwise it returns a pointer 613 * to the token of the string that couldn't be identified as a string 614 * representation of a fault. 615 */ 616 char * 617 proc_str2fltset(const char *s, const char *delim, int m, fltset_t *set) 618 { 619 char *p, *q, *t; 620 int flt; 621 622 if (m) { 623 premptyset(set); 624 } else { 625 prfillset(set); 626 } 627 628 t = strdupa(s); 629 630 for (p = strtok_r(t, delim, &q); p != NULL; 631 p = strtok_r(NULL, delim, &q)) { 632 if (proc_str2flt(p, &flt) == -1) { 633 errno = EINVAL; 634 return ((char *)s + (p - t)); 635 } 636 if (m) 637 praddset(set, flt); 638 else 639 prdelset(set, flt); 640 } 641 return (NULL); 642 } 643 644 /* 645 * Convert a string representation of a signal set (names with or without the 646 * SIG prefix separated by one or more of the given delimeters) to a sigset_t. 647 * Parameters and return values analogous to proc_str2fltset(). 648 */ 649 char * 650 proc_str2sigset(const char *s, const char *delim, int m, sigset_t *set) 651 { 652 char *p, *q, *t; 653 int sig; 654 655 if (m) { 656 premptyset(set); 657 } else { 658 prfillset(set); 659 } 660 661 t = strdupa(s); 662 663 for (p = strtok_r(t, delim, &q); p != NULL; 664 p = strtok_r(NULL, delim, &q)) { 665 if (proc_str2sig(p, &sig) == -1) { 666 errno = EINVAL; 667 return ((char *)s + (p - t)); 668 } 669 if (m) 670 praddset(set, sig); 671 else 672 prdelset(set, sig); 673 } 674 return (NULL); 675 } 676 677 /* 678 * Convert a string representation of a system call set (names separated by 679 * one or more of the given delimeters) to a sysset_t. Parameters and return 680 * values analogous to proc_str2fltset(). 681 */ 682 char * 683 proc_str2sysset(const char *s, const char *delim, int m, sysset_t *set) 684 { 685 char *p, *q, *t; 686 int sys; 687 688 if (m) { 689 premptyset(set); 690 } else { 691 prfillset(set); 692 } 693 694 t = strdupa(s); 695 696 for (p = strtok_r(t, delim, &q); p != NULL; 697 p = strtok_r(NULL, delim, &q)) { 698 if (proc_str2sys(p, &sys) == -1) { 699 errno = EINVAL; 700 return ((char *)s + (p - t)); 701 } 702 if (m) 703 praddset(set, sys); 704 else 705 prdelset(set, sys); 706 } 707 return (NULL); 708 } 709 710 /* 711 * Returns a string representation of a process data model. 712 * See <sys/procfs_isa.h> for possible values. 713 */ 714 char * 715 proc_dmodelname(int dmodel, char *buf, size_t bufsz) 716 { 717 static const char *const dmdls[] = { 718 "PR_MODEL_UNKNOWN", 719 "PR_MODEL_ILP32", 720 "PR_MODEL_LP64", 721 NULL 722 }; 723 size_t len; 724 725 if (bufsz == 0) 726 return (NULL); 727 728 if (dmodel > PR_MODEL_LP64 || dmodel < PR_MODEL_UNKNOWN) { 729 len = snprintf(buf, bufsz, "DMODEL#%d", dmodel); 730 } else { 731 len = strlen(dmdls[dmodel]); 732 (void) strncpy(buf, dmdls[dmodel], bufsz); 733 } 734 735 if (len >= bufsz) 736 buf[bufsz-1] = '\0'; 737 738 return (buf); 739 } 740