xref: /onnv-gate/usr/src/lib/libproc/common/proc_get_info.c (revision 2712:f74a135872bc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2712Snn35248  * Common Development and Distribution License (the "License").
6*2712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2712Snn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <fcntl.h>
320Sstevel@tonic-gate #include <string.h>
33*2712Snn35248 #include <limits.h>
34*2712Snn35248 
35*2712Snn35248 #include "Pcontrol.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * These several routines simply get the indicated /proc structures
390Sstevel@tonic-gate  * for a process identified by process ID.  They are convenience
400Sstevel@tonic-gate  * functions for one-time operations.  They do the mechanics of
410Sstevel@tonic-gate  * open() / read() / close() of the necessary /proc files so the
420Sstevel@tonic-gate  * caller's code can look relatively less cluttered.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * 'ngroups' is the number of supplementary group entries allocated in
470Sstevel@tonic-gate  * the caller's cred structure.  It should equal zero or one unless extra
480Sstevel@tonic-gate  * space has been allocated for the group list by the caller, like this:
490Sstevel@tonic-gate  *    credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate int
proc_get_cred(pid_t pid,prcred_t * credp,int ngroups)520Sstevel@tonic-gate proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
530Sstevel@tonic-gate {
54*2712Snn35248 	char fname[PATH_MAX];
550Sstevel@tonic-gate 	int fd;
560Sstevel@tonic-gate 	int rv = -1;
570Sstevel@tonic-gate 	ssize_t minsize = sizeof (*credp) - sizeof (gid_t);
580Sstevel@tonic-gate 	size_t size = minsize + ngroups * sizeof (gid_t);
590Sstevel@tonic-gate 
60*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/cred",
61*2712Snn35248 	    procfs_path, (int)pid);
620Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
630Sstevel@tonic-gate 		if (read(fd, credp, size) >= minsize)
640Sstevel@tonic-gate 			rv = 0;
650Sstevel@tonic-gate 		(void) close(fd);
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 	return (rv);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Malloc and return a properly sized structure.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate prpriv_t *
proc_get_priv(pid_t pid)740Sstevel@tonic-gate proc_get_priv(pid_t pid)
750Sstevel@tonic-gate {
76*2712Snn35248 	char fname[PATH_MAX];
770Sstevel@tonic-gate 	int fd;
780Sstevel@tonic-gate 	struct stat statb;
790Sstevel@tonic-gate 	prpriv_t *rv = NULL;
800Sstevel@tonic-gate 
81*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/priv",
82*2712Snn35248 	    procfs_path, (int)pid);
830Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
840Sstevel@tonic-gate 		if (fstat(fd, &statb) != 0 ||
850Sstevel@tonic-gate 		    (rv = malloc(statb.st_size)) == NULL ||
860Sstevel@tonic-gate 		    read(fd, rv, statb.st_size) != statb.st_size) {
870Sstevel@tonic-gate 			free(rv);
880Sstevel@tonic-gate 			rv = NULL;
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 		(void) close(fd);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	return (rv);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Fill in a pointer to a process LDT structure.
980Sstevel@tonic-gate  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
990Sstevel@tonic-gate  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1000Sstevel@tonic-gate  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate int
proc_get_ldt(pid_t pid,struct ssd * pldt,int nldt)1030Sstevel@tonic-gate proc_get_ldt(pid_t pid, struct ssd *pldt, int nldt)
1040Sstevel@tonic-gate {
105*2712Snn35248 	char fname[PATH_MAX];
1060Sstevel@tonic-gate 	int fd;
1070Sstevel@tonic-gate 	struct stat statb;
1080Sstevel@tonic-gate 	size_t size;
1090Sstevel@tonic-gate 	ssize_t ssize;
1100Sstevel@tonic-gate 
111*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
112*2712Snn35248 	    procfs_path, (int)pid);
1130Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) < 0)
1140Sstevel@tonic-gate 		return (-1);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (pldt == NULL || nldt == 0) {
1170Sstevel@tonic-gate 		nldt = 0;
1180Sstevel@tonic-gate 		if (fstat(fd, &statb) == 0)
1190Sstevel@tonic-gate 			nldt = statb.st_size / sizeof (struct ssd);
1200Sstevel@tonic-gate 		(void) close(fd);
1210Sstevel@tonic-gate 		return (nldt);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	size = nldt * sizeof (struct ssd);
1250Sstevel@tonic-gate 	if ((ssize = read(fd, pldt, size)) < 0)
1260Sstevel@tonic-gate 		nldt = -1;
1270Sstevel@tonic-gate 	else
1280Sstevel@tonic-gate 		nldt = ssize / sizeof (struct ssd);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	(void) close(fd);
1310Sstevel@tonic-gate 	return (nldt);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate int
proc_get_psinfo(pid_t pid,psinfo_t * psp)1360Sstevel@tonic-gate proc_get_psinfo(pid_t pid, psinfo_t *psp)
1370Sstevel@tonic-gate {
138*2712Snn35248 	char fname[PATH_MAX];
1390Sstevel@tonic-gate 	int fd;
1400Sstevel@tonic-gate 	int rv = -1;
1410Sstevel@tonic-gate 
142*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/psinfo",
143*2712Snn35248 	    procfs_path, (int)pid);
1440Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
1450Sstevel@tonic-gate 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1460Sstevel@tonic-gate 			rv = 0;
1470Sstevel@tonic-gate 		(void) close(fd);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	return (rv);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate int
proc_get_status(pid_t pid,pstatus_t * psp)1530Sstevel@tonic-gate proc_get_status(pid_t pid, pstatus_t *psp)
1540Sstevel@tonic-gate {
155*2712Snn35248 	char fname[PATH_MAX];
1560Sstevel@tonic-gate 	int fd;
1570Sstevel@tonic-gate 	int rv = -1;
1580Sstevel@tonic-gate 
159*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/status",
160*2712Snn35248 	    procfs_path, (int)pid);
1610Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
1620Sstevel@tonic-gate 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1630Sstevel@tonic-gate 			rv = 0;
1640Sstevel@tonic-gate 		(void) close(fd);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	return (rv);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Get the process's aux vector.
1710Sstevel@tonic-gate  * 'naux' is the number of aux entries in the caller's buffer.
1720Sstevel@tonic-gate  * We return the number of aux entries actually fetched from
1730Sstevel@tonic-gate  * the process (less than or equal to 'naux') or -1 on failure.
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate int
proc_get_auxv(pid_t pid,auxv_t * pauxv,int naux)1760Sstevel@tonic-gate proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux)
1770Sstevel@tonic-gate {
178*2712Snn35248 	char fname[PATH_MAX];
1790Sstevel@tonic-gate 	int fd;
1800Sstevel@tonic-gate 	int rv = -1;
1810Sstevel@tonic-gate 
182*2712Snn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/auxv",
183*2712Snn35248 	    procfs_path, (int)pid);
1840Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
1850Sstevel@tonic-gate 		if ((rv = read(fd, pauxv, naux * sizeof (auxv_t))) >= 0)
1860Sstevel@tonic-gate 			rv /= sizeof (auxv_t);
1870Sstevel@tonic-gate 		(void) close(fd);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	return (rv);
1900Sstevel@tonic-gate }
191