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.
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.
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]
22 * Copyright (c) 2013 Gary Mills
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
27 * Portions Copyright 2009 Chad Mynhier
39 #include <libzonecfg.h>
46 static plwp_t
*plwp_tbl
[PLWP_TBL_SZ
];
51 (void) memset(&plwp_tbl
, 0, sizeof (plwp_t
*) * PLWP_TBL_SZ
);
59 if ((pwd
= getpwnam(name
)) == NULL
)
60 Die(gettext("invalid user name: %s\n"), name
);
65 pwd_getname(uid_t uid
, char *name
, size_t length
, int noresolve
,
66 int trunc
, size_t width
)
71 if (noresolve
|| (pwd
= getpwuid(uid
)) == NULL
) {
72 n
= snprintf(NULL
, 0, "%u", uid
);
73 if (trunc
&& n
> width
)
74 (void) snprintf(name
, length
, "%.*u%c",
77 (void) snprintf(name
, length
, "%u", uid
);
79 n
= mbstowcs(NULL
, pwd
->pw_name
, 0);
81 (void) snprintf(name
, length
, "%s", "ERROR");
82 else if (trunc
&& n
> width
)
83 (void) snprintf(name
, length
, "%.*s%c",
84 width
- 1, pwd
->pw_name
, '*');
86 (void) snprintf(name
, length
, "%s", pwd
->pw_name
);
91 add_uid(uidtbl_t
*tbl
, char *name
)
95 if (tbl
->n_size
== tbl
->n_nent
) { /* reallocation */
96 if ((tbl
->n_size
*= 2) == 0)
97 tbl
->n_size
= 4; /* first time */
98 tbl
->n_list
= Realloc(tbl
->n_list
, tbl
->n_size
*sizeof (uid_t
));
101 uid
= &tbl
->n_list
[tbl
->n_nent
++];
103 if (isdigit(name
[0])) {
106 *uid
= pwd_getid(name
);
111 has_uid(uidtbl_t
*tbl
, uid_t uid
)
115 if (tbl
->n_nent
) { /* do linear search if table is not empty */
116 for (i
= 0; i
< tbl
->n_nent
; i
++)
117 if (tbl
->n_list
[i
] == uid
)
120 return (1); /* if table is empty return true */
123 return (0); /* nothing has been found */
127 add_element(table_t
*table
, long element
)
129 if (table
->t_size
== table
->t_nent
) {
130 if ((table
->t_size
*= 2) == 0)
132 table
->t_list
= Realloc(table
->t_list
,
133 table
->t_size
* sizeof (long));
135 table
->t_list
[table
->t_nent
++] = element
;
139 has_element(table_t
*table
, long element
)
143 if (table
->t_nent
) { /* do linear search if table is not empty */
144 for (i
= 0; i
< table
->t_nent
; i
++)
145 if (table
->t_list
[i
] == element
)
147 } else { /* if table is empty then */
148 return (1); /* pretend that element was found */
151 return (0); /* element was not found */
155 foreach_element(table_t
*table
, void *buf
, void (*walker
)(long, void *))
160 for (i
= 0; i
< table
->t_nent
; i
++)
161 walker(table
->t_list
[i
], buf
);
169 add_zone(zonetbl_t
*tbl
, char *str
)
176 * str should be either the name of a configured zone, or the
177 * id of a running zone. If str is a zone name, store the name
178 * in the table; otherwise, just store the id.
180 if (zone_get_id(str
, &id
) != 0) {
181 Die(gettext("unknown zone -- %s\n"), str
);
185 /* was zone specified by name or id? */
187 if (id
== (zoneid_t
)strtol(str
, &cp
, 0) && errno
== 0 && cp
!= str
&&
189 /* found it by id, don't store the name */
193 if (tbl
->z_size
== tbl
->z_nent
) { /* reallocation */
194 if ((tbl
->z_size
*= 2) == 0)
195 tbl
->z_size
= 4; /* first time */
197 Realloc(tbl
->z_list
, tbl
->z_size
* sizeof (zonename_t
));
200 entp
= &tbl
->z_list
[tbl
->z_nent
++];
202 (void) strlcpy(entp
->z_name
, str
, ZONENAME_MAX
);
204 entp
->z_name
[0] = '\0';
209 has_zone(zonetbl_t
*tbl
, zoneid_t id
)
213 if (tbl
->z_nent
) { /* do linear search if table is not empty */
214 for (i
= 0; i
< tbl
->z_nent
; i
++)
215 if (tbl
->z_list
[i
].z_id
== id
)
217 return (0); /* nothing has been found */
220 return (1); /* if table is empty return true */
224 * Lookup ids for each zone name; this is done once each time /proc
225 * is scanned to avoid calling getzoneidbyname for each process.
228 convert_zone(zonetbl_t
*tbl
)
234 for (i
= 0; i
< tbl
->z_nent
; i
++) {
235 name
= tbl
->z_list
[i
].z_name
;
237 if ((id
= getzoneidbyname(name
)) != -1)
238 tbl
->z_list
[i
].z_id
= id
;
244 lwpid_add(lwp_info_t
*lwp
, pid_t pid
, id_t lwpid
)
246 plwp_t
*elm
= Zalloc(sizeof (plwp_t
));
247 int hash
= pid
% PLWP_TBL_SZ
;
250 elm
->l_lwpid
= lwpid
;
252 elm
->l_next
= plwp_tbl
[hash
]; /* add in front of chain */
253 plwp_tbl
[hash
] = elm
;
257 lwpid_del(pid_t pid
, id_t lwpid
)
259 plwp_t
*elm
, *elm_prev
;
260 int hash
= pid
% PLWP_TBL_SZ
;
262 elm
= plwp_tbl
[hash
];
266 if ((elm
->l_pid
== pid
) && (elm
->l_lwpid
== lwpid
)) {
267 if (!elm_prev
) /* first chain element */
268 plwp_tbl
[hash
] = elm
->l_next
;
270 elm_prev
->l_next
= elm
->l_next
;
281 lwpid_getptr(pid_t pid
, id_t lwpid
)
283 plwp_t
*elm
= plwp_tbl
[pid
% PLWP_TBL_SZ
];
285 if ((elm
->l_pid
== pid
) && (elm
->l_lwpid
== lwpid
))
294 lwpid_get(pid_t pid
, id_t lwpid
)
296 plwp_t
*elm
= lwpid_getptr(pid
, lwpid
);
304 lwpid_pidcheck(pid_t pid
)
307 elm
= plwp_tbl
[pid
% PLWP_TBL_SZ
];
309 if (elm
->l_pid
== pid
)
318 lwpid_is_active(pid_t pid
, id_t lwpid
)
320 plwp_t
*elm
= lwpid_getptr(pid
, lwpid
);
322 return (elm
->l_active
);
328 lwpid_set_active(pid_t pid
, id_t lwpid
)
330 plwp_t
*elm
= lwpid_getptr(pid
, lwpid
);
332 elm
->l_active
= LWP_ACTIVE
;