xref: /titanic_51/usr/src/lib/libproc/common/proc_names.c (revision 53a7b6b6763f5865522a76e5e887390a8f4777d7)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #define	__EXTENSIONS__
28 #include <string.h>
29 #undef  __EXTENSIONS__
30 #include <signal.h>
31 #include <alloca.h>
32 #include <errno.h>
33 #include "libproc.h"
34 
35 static const char *
36 rawfltname(int flt)
37 {
38 	const char *name;
39 
40 	switch (flt) {
41 	case FLTILL:	name = "FLTILL";	break;
42 	case FLTPRIV:	name = "FLTPRIV";	break;
43 	case FLTBPT:	name = "FLTBPT";	break;
44 	case FLTTRACE:	name = "FLTTRACE";	break;
45 	case FLTACCESS:	name = "FLTACCESS";	break;
46 	case FLTBOUNDS:	name = "FLTBOUNDS";	break;
47 	case FLTIOVF:	name = "FLTIOVF";	break;
48 	case FLTIZDIV:	name = "FLTIZDIV";	break;
49 	case FLTFPE:	name = "FLTFPE";	break;
50 	case FLTSTACK:	name = "FLTSTACK";	break;
51 	case FLTPAGE:	name = "FLTPAGE";	break;
52 	case FLTWATCH:	name = "FLTWATCH";	break;
53 	case FLTCPCOVF:	name = "FLTCPCOVF";	break;
54 	default:	name = NULL;		break;
55 	}
56 
57 	return (name);
58 }
59 
60 /*
61  * Return the name of a fault.
62  * Manufacture a name for unknown fault.
63  */
64 char *
65 proc_fltname(int flt, char *buf, size_t bufsz)
66 {
67 	const char *name = rawfltname(flt);
68 	size_t len;
69 
70 	if (bufsz == 0)		/* force a program failure */
71 		return (NULL);
72 
73 	if (name != NULL) {
74 		len = strlen(name);
75 		(void) strncpy(buf, name, bufsz);
76 	} else {
77 		len = snprintf(buf, bufsz, "FLT#%d", flt);
78 	}
79 
80 	if (len >= bufsz)	/* ensure null-termination */
81 		buf[bufsz-1] = '\0';
82 
83 	return (buf);
84 }
85 
86 /*
87  * Return the name of a signal.
88  * Manufacture a name for unknown signal.
89  */
90 char *
91 proc_signame(int sig, char *buf, size_t bufsz)
92 {
93 	char name[SIG2STR_MAX+4];
94 	size_t len;
95 
96 	if (bufsz == 0)		/* force a program failure */
97 		return (NULL);
98 
99 	/* sig2str() omits the leading "SIG" */
100 	(void) strcpy(name, "SIG");
101 
102 	if (sig2str(sig, name+3) == 0) {
103 		len = strlen(name);
104 		(void) strncpy(buf, name, bufsz);
105 	} else {
106 		len = snprintf(buf, bufsz, "SIG#%d", sig);
107 	}
108 
109 	if (len >= bufsz)	/* ensure null-termination */
110 		buf[bufsz-1] = '\0';
111 
112 	return (buf);
113 }
114 
115 static const char *const systable[] = {
116 	NULL,			/*  0 */
117 	"_exit",		/*  1 */
118 	"forkall",		/*  2 */
119 	"read",			/*  3 */
120 	"write",		/*  4 */
121 	"open",			/*  5 */
122 	"close",		/*  6 */
123 	"wait",			/*  7 */
124 	"creat",		/*  8 */
125 	"link",			/*  9 */
126 	"unlink",		/* 10 */
127 	"exec",			/* 11 */
128 	"chdir",		/* 12 */
129 	"time",			/* 13 */
130 	"mknod",		/* 14 */
131 	"chmod",		/* 15 */
132 	"chown",		/* 16 */
133 	"brk",			/* 17 */
134 	"stat",			/* 18 */
135 	"lseek",		/* 19 */
136 	"getpid",		/* 20 */
137 	"mount",		/* 21 */
138 	"umount",		/* 22 */
139 	"setuid",		/* 23 */
140 	"getuid",		/* 24 */
141 	"stime",		/* 25 */
142 	"ptrace",		/* 26 */
143 	"alarm",		/* 27 */
144 	"fstat",		/* 28 */
145 	"pause",		/* 29 */
146 	"utime",		/* 30 */
147 	"stty",			/* 31 */
148 	"gtty",			/* 32 */
149 	"access",		/* 33 */
150 	"nice",			/* 34 */
151 	"statfs",		/* 35 */
152 	"sync",			/* 36 */
153 	"kill",			/* 37 */
154 	"fstatfs",		/* 38 */
155 	"pgrpsys",		/* 39 */
156 	"uucopystr",		/* 40 */
157 	"dup",			/* 41 */
158 	"pipe",			/* 42 */
159 	"times",		/* 43 */
160 	"profil",		/* 44 */
161 	"plock",		/* 45 */
162 	"setgid",		/* 46 */
163 	"getgid",		/* 47 */
164 	"signal",		/* 48 */
165 	"msgsys",		/* 49 */
166 	"sysi86",		/* 50 */
167 	"acct",			/* 51 */
168 	"shmsys",		/* 52 */
169 	"semsys",		/* 53 */
170 	"ioctl",		/* 54 */
171 	"uadmin",		/* 55 */
172 	NULL,			/* 56 */
173 	"utssys",		/* 57 */
174 	"fdsync",		/* 58 */
175 	"execve",		/* 59 */
176 	"umask",		/* 60 */
177 	"chroot",		/* 61 */
178 	"fcntl",		/* 62 */
179 	"ulimit",		/* 63 */
180 
181 	/* The following 6 entries were reserved for the UNIX PC */
182 	NULL,			/* 64 */
183 	NULL,			/* 65 */
184 	NULL,			/* 66 */
185 	NULL,			/* 67 */
186 	NULL,			/* 68 */
187 	NULL,			/* 69 */
188 
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 	"poll",			/* 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 	"evsys",		/* 101 */
221 	"evtrapret",		/* 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 	"xstat",		/* 123 */
243 	"lxstat",		/* 124 */
244 	"fxstat",		/* 125 */
245 	"xmknod",		/* 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 	"fork1",		/* 143 */
263 	"sigtimedwait",		/* 144 */
264 	"lwp_info",		/* 145 */
265 	"yield",		/* 146 */
266 	"lwp_sema_wait",	/* 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 	"utimes",		/* 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 	"lwp_mutex_lock",	/* 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 	NULL,			/* 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 	"creat64",		/* 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 = alloca(strlen(s) + 1);
620 	int flt;
621 
622 	if (m) {
623 		premptyset(set);
624 	} else {
625 		prfillset(set);
626 	}
627 
628 	(void) strcpy(t, 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 = alloca(strlen(s) + 1);
653 	int sig;
654 
655 	if (m) {
656 		premptyset(set);
657 	} else {
658 		prfillset(set);
659 	}
660 
661 	(void) strcpy(t, 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 = alloca(strlen(s) + 1);
686 	int sys;
687 
688 	if (m) {
689 		premptyset(set);
690 	} else {
691 		prfillset(set);
692 	}
693 
694 	(void) strcpy(t, 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