xref: /titanic_51/usr/src/lib/libproc/common/proc_get_info.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <limits.h>
34 
35 #include "Pcontrol.h"
36 
37 /*
38  * These several routines simply get the indicated /proc structures
39  * for a process identified by process ID.  They are convenience
40  * functions for one-time operations.  They do the mechanics of
41  * open() / read() / close() of the necessary /proc files so the
42  * caller's code can look relatively less cluttered.
43  */
44 
45 /*
46  * 'ngroups' is the number of supplementary group entries allocated in
47  * the caller's cred structure.  It should equal zero or one unless extra
48  * space has been allocated for the group list by the caller, like this:
49  *    credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
50  */
51 int
52 proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
53 {
54 	char fname[PATH_MAX];
55 	int fd;
56 	int rv = -1;
57 	ssize_t minsize = sizeof (*credp) - sizeof (gid_t);
58 	size_t size = minsize + ngroups * sizeof (gid_t);
59 
60 	(void) snprintf(fname, sizeof (fname), "%s/%d/cred",
61 	    procfs_path, (int)pid);
62 	if ((fd = open(fname, O_RDONLY)) >= 0) {
63 		if (read(fd, credp, size) >= minsize)
64 			rv = 0;
65 		(void) close(fd);
66 	}
67 	return (rv);
68 }
69 
70 /*
71  * Malloc and return a properly sized structure.
72  */
73 prpriv_t *
74 proc_get_priv(pid_t pid)
75 {
76 	char fname[PATH_MAX];
77 	int fd;
78 	struct stat statb;
79 	prpriv_t *rv = NULL;
80 
81 	(void) snprintf(fname, sizeof (fname), "%s/%d/priv",
82 	    procfs_path, (int)pid);
83 	if ((fd = open(fname, O_RDONLY)) >= 0) {
84 		if (fstat(fd, &statb) != 0 ||
85 		    (rv = malloc(statb.st_size)) == NULL ||
86 		    read(fd, rv, statb.st_size) != statb.st_size) {
87 			free(rv);
88 			rv = NULL;
89 		}
90 		(void) close(fd);
91 	}
92 	return (rv);
93 }
94 
95 #if defined(__i386) || defined(__amd64)
96 /*
97  * Fill in a pointer to a process LDT structure.
98  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
99  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
100  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
101  */
102 int
103 proc_get_ldt(pid_t pid, struct ssd *pldt, int nldt)
104 {
105 	char fname[PATH_MAX];
106 	int fd;
107 	struct stat statb;
108 	size_t size;
109 	ssize_t ssize;
110 
111 	(void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
112 	    procfs_path, (int)pid);
113 	if ((fd = open(fname, O_RDONLY)) < 0)
114 		return (-1);
115 
116 	if (pldt == NULL || nldt == 0) {
117 		nldt = 0;
118 		if (fstat(fd, &statb) == 0)
119 			nldt = statb.st_size / sizeof (struct ssd);
120 		(void) close(fd);
121 		return (nldt);
122 	}
123 
124 	size = nldt * sizeof (struct ssd);
125 	if ((ssize = read(fd, pldt, size)) < 0)
126 		nldt = -1;
127 	else
128 		nldt = ssize / sizeof (struct ssd);
129 
130 	(void) close(fd);
131 	return (nldt);
132 }
133 #endif	/* __i386 || __amd64 */
134 
135 int
136 proc_get_psinfo(pid_t pid, psinfo_t *psp)
137 {
138 	char fname[PATH_MAX];
139 	int fd;
140 	int rv = -1;
141 
142 	(void) snprintf(fname, sizeof (fname), "%s/%d/psinfo",
143 	    procfs_path, (int)pid);
144 	if ((fd = open(fname, O_RDONLY)) >= 0) {
145 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
146 			rv = 0;
147 		(void) close(fd);
148 	}
149 	return (rv);
150 }
151 
152 int
153 proc_get_status(pid_t pid, pstatus_t *psp)
154 {
155 	char fname[PATH_MAX];
156 	int fd;
157 	int rv = -1;
158 
159 	(void) snprintf(fname, sizeof (fname), "%s/%d/status",
160 	    procfs_path, (int)pid);
161 	if ((fd = open(fname, O_RDONLY)) >= 0) {
162 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
163 			rv = 0;
164 		(void) close(fd);
165 	}
166 	return (rv);
167 }
168 
169 /*
170  * Get the process's aux vector.
171  * 'naux' is the number of aux entries in the caller's buffer.
172  * We return the number of aux entries actually fetched from
173  * the process (less than or equal to 'naux') or -1 on failure.
174  */
175 int
176 proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux)
177 {
178 	char fname[PATH_MAX];
179 	int fd;
180 	int rv = -1;
181 
182 	(void) snprintf(fname, sizeof (fname), "%s/%d/auxv",
183 	    procfs_path, (int)pid);
184 	if ((fd = open(fname, O_RDONLY)) >= 0) {
185 		if ((rv = read(fd, pauxv, naux * sizeof (auxv_t))) >= 0)
186 			rv /= sizeof (auxv_t);
187 		(void) close(fd);
188 	}
189 	return (rv);
190 }
191