8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libproc / common / proc_get_info.c
blob19a84e060efc24efd7172b16fe972a64a6e6a1b2
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2015, Joyent, Inc.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <sys/secflags.h>
37 #include "Pcontrol.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));
53 int
54 proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
56 char fname[PATH_MAX];
57 int fd;
58 int rv = -1;
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)
66 rv = 0;
67 (void) close(fd);
69 return (rv);
72 int
73 proc_get_secflags(pid_t pid, prsecflags_t **psf)
75 char fname[PATH_MAX];
76 int fd;
77 int rv = -1;
79 if ((*psf = calloc(1, sizeof (prsecflags_t))) == NULL)
80 return (-1);
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))
87 rv = 0;
88 (void) close(fd);
90 return (rv);
93 void
94 proc_free_priv(prpriv_t *prv)
96 free(prv);
100 * Malloc and return a properly sized structure.
102 prpriv_t *
103 proc_get_priv(pid_t pid)
105 char fname[PATH_MAX];
106 int fd;
107 struct stat statb;
108 prpriv_t *rv = NULL;
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) {
116 free(rv);
117 rv = NULL;
119 (void) close(fd);
121 return (rv);
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];
135 int fd;
136 struct stat statb;
137 size_t size;
138 ssize_t ssize;
140 (void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
141 procfs_path, (int)pid);
142 if ((fd = open(fname, O_RDONLY)) < 0)
143 return (-1);
145 if (pldt == NULL || nldt == 0) {
146 nldt = 0;
147 if (fstat(fd, &statb) == 0)
148 nldt = statb.st_size / sizeof (struct ssd);
149 (void) close(fd);
150 return (nldt);
153 size = nldt * sizeof (struct ssd);
154 if ((ssize = read(fd, pldt, size)) < 0)
155 nldt = -1;
156 else
157 nldt = ssize / sizeof (struct ssd);
159 (void) close(fd);
160 return (nldt);
162 #endif /* __i386 || __amd64 */
165 proc_get_psinfo(pid_t pid, psinfo_t *psp)
167 char fname[PATH_MAX];
168 int fd;
169 int rv = -1;
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))
175 rv = 0;
176 (void) close(fd);
178 return (rv);
182 proc_get_status(pid_t pid, pstatus_t *psp)
184 char fname[PATH_MAX];
185 int fd;
186 int rv = -1;
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))
192 rv = 0;
193 (void) close(fd);
195 return (rv);
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];
208 int fd;
209 int rv = -1;
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);
216 (void) close(fd);
218 return (rv);