proc_get_info.c revision 2712:f74a135872bc
15828Smseledtsov/* 213013Smseledtsov * CDDL HEADER START 35828Smseledtsov * 45828Smseledtsov * The contents of this file are subject to the terms of the 55828Smseledtsov * Common Development and Distribution License (the "License"). 65828Smseledtsov * You may not use this file except in compliance with the License. 75828Smseledtsov * 85828Smseledtsov * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95828Smseledtsov * or http://www.opensolaris.org/os/licensing. 105828Smseledtsov * See the License for the specific language governing permissions 115828Smseledtsov * and limitations under the License. 125828Smseledtsov * 135828Smseledtsov * When distributing Covered Code, include this CDDL HEADER in each 145828Smseledtsov * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155828Smseledtsov * If applicable, add the following below this CDDL HEADER, with the 165828Smseledtsov * fields enclosed by brackets "[]" replaced with your own identifying 175828Smseledtsov * information: Portions Copyright [yyyy] [name of copyright owner] 185828Smseledtsov * 195828Smseledtsov * CDDL HEADER END 205828Smseledtsov */ 215828Smseledtsov/* 225828Smseledtsov * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 235828Smseledtsov * Use is subject to license terms. 245828Smseledtsov */ 255828Smseledtsov 265828Smseledtsov#pragma ident "%Z%%M% %I% %E% SMI" 2713041Smseledtsov 2811833Sctornqvi#include <stdio.h> 2910551Schegar#include <stdlib.h> 308013Sykantser#include <unistd.h> 315828Smseledtsov#include <fcntl.h> 326030Sgtriantafill#include <string.h> 335828Smseledtsov#include <limits.h> 345828Smseledtsov 3513013Smseledtsov#include "Pcontrol.h" 3611833Sctornqvi 3711833Sctornqvi/* 385828Smseledtsov * These several routines simply get the indicated /proc structures 395828Smseledtsov * for a process identified by process ID. They are convenience 405828Smseledtsov * functions for one-time operations. They do the mechanics of 415828Smseledtsov * open() / read() / close() of the necessary /proc files so the 425828Smseledtsov * caller's code can look relatively less cluttered. 435828Smseledtsov */ 445828Smseledtsov 455828Smseledtsov/* 465828Smseledtsov * 'ngroups' is the number of supplementary group entries allocated in 475828Smseledtsov * the caller's cred structure. It should equal zero or one unless extra 485828Smseledtsov * space has been allocated for the group list by the caller, like this: 495828Smseledtsov * credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t)); 5013013Smseledtsov */ 5113146Srprotacioint 525828Smseledtsovproc_get_cred(pid_t pid, prcred_t *credp, int ngroups) 535828Smseledtsov{ 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 */ 73prpriv_t * 74proc_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 */ 102int 103proc_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 135int 136proc_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 152int 153proc_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 */ 175int 176proc_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