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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2015, Joyent, Inc.
35 #include <sys/secflags.h>
40 * These several routines simply get the indicated /proc structures
41 * for a process identified by process ID. They are convenience
42 * functions for one-time operations. They do the mechanics of
43 * open() / read() / close() of the necessary /proc files so the
44 * caller's code can look relatively less cluttered.
48 * 'ngroups' is the number of supplementary group entries allocated in
49 * the caller's cred structure. It should equal zero or one unless extra
50 * space has been allocated for the group list by the caller, like this:
51 * credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
54 proc_get_cred(pid_t pid
, prcred_t
*credp
, int ngroups
)
59 ssize_t minsize
= sizeof (*credp
) - sizeof (gid_t
);
60 size_t size
= minsize
+ ngroups
* sizeof (gid_t
);
62 (void) snprintf(fname
, sizeof (fname
), "%s/%d/cred",
63 procfs_path
, (int)pid
);
64 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
65 if (read(fd
, credp
, size
) >= minsize
)
73 proc_get_secflags(pid_t pid
, prsecflags_t
**psf
)
79 if ((*psf
= calloc(1, sizeof (prsecflags_t
))) == NULL
)
82 (void) snprintf(fname
, sizeof (fname
), "%s/%d/secflags",
83 procfs_path
, (int)pid
);
84 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
85 if (read(fd
, *psf
, sizeof (prsecflags_t
)) ==
86 sizeof (prsecflags_t
))
94 proc_free_priv(prpriv_t
*prv
)
100 * Malloc and return a properly sized structure.
103 proc_get_priv(pid_t pid
)
105 char fname
[PATH_MAX
];
110 (void) snprintf(fname
, sizeof (fname
), "%s/%d/priv",
111 procfs_path
, (int)pid
);
112 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
113 if (fstat(fd
, &statb
) != 0 ||
114 (rv
= malloc(statb
.st_size
)) == NULL
||
115 read(fd
, rv
, statb
.st_size
) != statb
.st_size
) {
124 #if defined(__i386) || defined(__amd64)
126 * Fill in a pointer to a process LDT structure.
127 * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
128 * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
129 * Otherwise we return the actual number of LDT entries fetched (<= nldt).
132 proc_get_ldt(pid_t pid
, struct ssd
*pldt
, int nldt
)
134 char fname
[PATH_MAX
];
140 (void) snprintf(fname
, sizeof (fname
), "%s/%d/ldt",
141 procfs_path
, (int)pid
);
142 if ((fd
= open(fname
, O_RDONLY
)) < 0)
145 if (pldt
== NULL
|| nldt
== 0) {
147 if (fstat(fd
, &statb
) == 0)
148 nldt
= statb
.st_size
/ sizeof (struct ssd
);
153 size
= nldt
* sizeof (struct ssd
);
154 if ((ssize
= read(fd
, pldt
, size
)) < 0)
157 nldt
= ssize
/ sizeof (struct ssd
);
162 #endif /* __i386 || __amd64 */
165 proc_get_psinfo(pid_t pid
, psinfo_t
*psp
)
167 char fname
[PATH_MAX
];
171 (void) snprintf(fname
, sizeof (fname
), "%s/%d/psinfo",
172 procfs_path
, (int)pid
);
173 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
174 if (read(fd
, psp
, sizeof (*psp
)) == sizeof (*psp
))
182 proc_get_status(pid_t pid
, pstatus_t
*psp
)
184 char fname
[PATH_MAX
];
188 (void) snprintf(fname
, sizeof (fname
), "%s/%d/status",
189 procfs_path
, (int)pid
);
190 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
191 if (read(fd
, psp
, sizeof (*psp
)) == sizeof (*psp
))
199 * Get the process's aux vector.
200 * 'naux' is the number of aux entries in the caller's buffer.
201 * We return the number of aux entries actually fetched from
202 * the process (less than or equal to 'naux') or -1 on failure.
205 proc_get_auxv(pid_t pid
, auxv_t
*pauxv
, int naux
)
207 char fname
[PATH_MAX
];
211 (void) snprintf(fname
, sizeof (fname
), "%s/%d/auxv",
212 procfs_path
, (int)pid
);
213 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
214 if ((rv
= read(fd
, pauxv
, naux
* sizeof (auxv_t
))) >= 0)
215 rv
/= sizeof (auxv_t
);