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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 *
25 * Portions Copyright 2009 Chad Mynhier
26 */
27
28 #include <procfs.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <pwd.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <libintl.h>
35 #include <errno.h>
36 #include <zone.h>
37 #include <libzonecfg.h>
38
39 #include "prstat.h"
40 #include "prutil.h"
41 #include "prtable.h"
42
43 static plwp_t *plwp_tbl[PLWP_TBL_SZ];
44
45 void
46 lwpid_init()
47 {
48 (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
49 }
50
51 static uid_t
52 pwd_getid(char *name)
53 {
54 struct passwd *pwd;
55
56 if ((pwd = getpwnam(name)) == NULL)
57 Die(gettext("invalid user name: %s\n"), name);
58 return (pwd->pw_uid);
59 }
60
61 void
62 pwd_getname(uid_t uid, char *name, int length, int noresolve)
63 {
64 struct passwd *pwd;
65
66 if (noresolve || (pwd = getpwuid(uid)) == NULL) {
67 (void) snprintf(name, length, "%u", uid);
68 } else {
69 (void) snprintf(name, length, "%s", pwd->pw_name);
70 }
71 }
72
73 void
74 add_uid(uidtbl_t *tbl, char *name)
75 {
76 uid_t *uid;
77
78 if (tbl->n_size == tbl->n_nent) { /* reallocation */
79 if ((tbl->n_size *= 2) == 0)
80 tbl->n_size = 4; /* first time */
81 tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
82 }
83
84 uid = &tbl->n_list[tbl->n_nent++];
85
86 if (isdigit(name[0])) {
87 *uid = Atoi(name);
88 } else {
|
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 (c) 2013 Gary Mills
23 *
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 *
27 * Portions Copyright 2009 Chad Mynhier
28 */
29
30 #include <procfs.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <pwd.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <libintl.h>
37 #include <errno.h>
38 #include <zone.h>
39 #include <libzonecfg.h>
40
41 #include "prstat.h"
42 #include "prutil.h"
43 #include "prtable.h"
44
45 static plwp_t *plwp_tbl[PLWP_TBL_SZ];
46
47 void
48 lwpid_init()
49 {
50 (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
51 }
52
53 static uid_t
54 pwd_getid(char *name)
55 {
56 struct passwd *pwd;
57
58 if ((pwd = getpwnam(name)) == NULL)
59 Die(gettext("invalid user name: %s\n"), name);
60 return (pwd->pw_uid);
61 }
62
63 void
64 pwd_getname(uid_t uid, char *name, size_t length, int noresolve,
65 int trunc, size_t width)
66 {
67 struct passwd *pwd;
68 size_t n;
69
70 if (noresolve || (pwd = getpwuid(uid)) == NULL) {
71 n = snprintf(NULL, 0, "%u", uid);
72 if (trunc && n > width)
73 (void) snprintf(name, length, "%.*u%c",
74 width - 1, uid, '*');
75 else
76 (void) snprintf(name, length, "%u", uid);
77 } else {
78 n = strlen(pwd->pw_name);
79 if (trunc && n > width)
80 (void) snprintf(name, length, "%.*s%c",
81 width - 1, pwd->pw_name, '*');
82 else
83 (void) snprintf(name, length, "%s", pwd->pw_name);
84 }
85 }
86
87 void
88 add_uid(uidtbl_t *tbl, char *name)
89 {
90 uid_t *uid;
91
92 if (tbl->n_size == tbl->n_nent) { /* reallocation */
93 if ((tbl->n_size *= 2) == 0)
94 tbl->n_size = 4; /* first time */
95 tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
96 }
97
98 uid = &tbl->n_list[tbl->n_nent++];
99
100 if (isdigit(name[0])) {
101 *uid = Atoi(name);
102 } else {
|