17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9acbbeafSnn35248 * Common Development and Distribution License (the "License").
6*9acbbeafSnn35248 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*9acbbeafSnn35248 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
33*9acbbeafSnn35248 #include <limits.h>
34*9acbbeafSnn35248
35*9acbbeafSnn35248 #include "Pcontrol.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * These several routines simply get the indicated /proc structures
397c478bd9Sstevel@tonic-gate * for a process identified by process ID. They are convenience
407c478bd9Sstevel@tonic-gate * functions for one-time operations. They do the mechanics of
417c478bd9Sstevel@tonic-gate * open() / read() / close() of the necessary /proc files so the
427c478bd9Sstevel@tonic-gate * caller's code can look relatively less cluttered.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * 'ngroups' is the number of supplementary group entries allocated in
477c478bd9Sstevel@tonic-gate * the caller's cred structure. It should equal zero or one unless extra
487c478bd9Sstevel@tonic-gate * space has been allocated for the group list by the caller, like this:
497c478bd9Sstevel@tonic-gate * credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate int
proc_get_cred(pid_t pid,prcred_t * credp,int ngroups)527c478bd9Sstevel@tonic-gate proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
537c478bd9Sstevel@tonic-gate {
54*9acbbeafSnn35248 char fname[PATH_MAX];
557c478bd9Sstevel@tonic-gate int fd;
567c478bd9Sstevel@tonic-gate int rv = -1;
577c478bd9Sstevel@tonic-gate ssize_t minsize = sizeof (*credp) - sizeof (gid_t);
587c478bd9Sstevel@tonic-gate size_t size = minsize + ngroups * sizeof (gid_t);
597c478bd9Sstevel@tonic-gate
60*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/cred",
61*9acbbeafSnn35248 procfs_path, (int)pid);
627c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
637c478bd9Sstevel@tonic-gate if (read(fd, credp, size) >= minsize)
647c478bd9Sstevel@tonic-gate rv = 0;
657c478bd9Sstevel@tonic-gate (void) close(fd);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate return (rv);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Malloc and return a properly sized structure.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate prpriv_t *
proc_get_priv(pid_t pid)747c478bd9Sstevel@tonic-gate proc_get_priv(pid_t pid)
757c478bd9Sstevel@tonic-gate {
76*9acbbeafSnn35248 char fname[PATH_MAX];
777c478bd9Sstevel@tonic-gate int fd;
787c478bd9Sstevel@tonic-gate struct stat statb;
797c478bd9Sstevel@tonic-gate prpriv_t *rv = NULL;
807c478bd9Sstevel@tonic-gate
81*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/priv",
82*9acbbeafSnn35248 procfs_path, (int)pid);
837c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
847c478bd9Sstevel@tonic-gate if (fstat(fd, &statb) != 0 ||
857c478bd9Sstevel@tonic-gate (rv = malloc(statb.st_size)) == NULL ||
867c478bd9Sstevel@tonic-gate read(fd, rv, statb.st_size) != statb.st_size) {
877c478bd9Sstevel@tonic-gate free(rv);
887c478bd9Sstevel@tonic-gate rv = NULL;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate (void) close(fd);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate return (rv);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * Fill in a pointer to a process LDT structure.
987c478bd9Sstevel@tonic-gate * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
997c478bd9Sstevel@tonic-gate * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1007c478bd9Sstevel@tonic-gate * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate int
proc_get_ldt(pid_t pid,struct ssd * pldt,int nldt)1037c478bd9Sstevel@tonic-gate proc_get_ldt(pid_t pid, struct ssd *pldt, int nldt)
1047c478bd9Sstevel@tonic-gate {
105*9acbbeafSnn35248 char fname[PATH_MAX];
1067c478bd9Sstevel@tonic-gate int fd;
1077c478bd9Sstevel@tonic-gate struct stat statb;
1087c478bd9Sstevel@tonic-gate size_t size;
1097c478bd9Sstevel@tonic-gate ssize_t ssize;
1107c478bd9Sstevel@tonic-gate
111*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
112*9acbbeafSnn35248 procfs_path, (int)pid);
1137c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) < 0)
1147c478bd9Sstevel@tonic-gate return (-1);
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (pldt == NULL || nldt == 0) {
1177c478bd9Sstevel@tonic-gate nldt = 0;
1187c478bd9Sstevel@tonic-gate if (fstat(fd, &statb) == 0)
1197c478bd9Sstevel@tonic-gate nldt = statb.st_size / sizeof (struct ssd);
1207c478bd9Sstevel@tonic-gate (void) close(fd);
1217c478bd9Sstevel@tonic-gate return (nldt);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate size = nldt * sizeof (struct ssd);
1257c478bd9Sstevel@tonic-gate if ((ssize = read(fd, pldt, size)) < 0)
1267c478bd9Sstevel@tonic-gate nldt = -1;
1277c478bd9Sstevel@tonic-gate else
1287c478bd9Sstevel@tonic-gate nldt = ssize / sizeof (struct ssd);
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate (void) close(fd);
1317c478bd9Sstevel@tonic-gate return (nldt);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate int
proc_get_psinfo(pid_t pid,psinfo_t * psp)1367c478bd9Sstevel@tonic-gate proc_get_psinfo(pid_t pid, psinfo_t *psp)
1377c478bd9Sstevel@tonic-gate {
138*9acbbeafSnn35248 char fname[PATH_MAX];
1397c478bd9Sstevel@tonic-gate int fd;
1407c478bd9Sstevel@tonic-gate int rv = -1;
1417c478bd9Sstevel@tonic-gate
142*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/psinfo",
143*9acbbeafSnn35248 procfs_path, (int)pid);
1447c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1457c478bd9Sstevel@tonic-gate if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1467c478bd9Sstevel@tonic-gate rv = 0;
1477c478bd9Sstevel@tonic-gate (void) close(fd);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate return (rv);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate int
proc_get_status(pid_t pid,pstatus_t * psp)1537c478bd9Sstevel@tonic-gate proc_get_status(pid_t pid, pstatus_t *psp)
1547c478bd9Sstevel@tonic-gate {
155*9acbbeafSnn35248 char fname[PATH_MAX];
1567c478bd9Sstevel@tonic-gate int fd;
1577c478bd9Sstevel@tonic-gate int rv = -1;
1587c478bd9Sstevel@tonic-gate
159*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/status",
160*9acbbeafSnn35248 procfs_path, (int)pid);
1617c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1627c478bd9Sstevel@tonic-gate if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1637c478bd9Sstevel@tonic-gate rv = 0;
1647c478bd9Sstevel@tonic-gate (void) close(fd);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate return (rv);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Get the process's aux vector.
1717c478bd9Sstevel@tonic-gate * 'naux' is the number of aux entries in the caller's buffer.
1727c478bd9Sstevel@tonic-gate * We return the number of aux entries actually fetched from
1737c478bd9Sstevel@tonic-gate * the process (less than or equal to 'naux') or -1 on failure.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate int
proc_get_auxv(pid_t pid,auxv_t * pauxv,int naux)1767c478bd9Sstevel@tonic-gate proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux)
1777c478bd9Sstevel@tonic-gate {
178*9acbbeafSnn35248 char fname[PATH_MAX];
1797c478bd9Sstevel@tonic-gate int fd;
1807c478bd9Sstevel@tonic-gate int rv = -1;
1817c478bd9Sstevel@tonic-gate
182*9acbbeafSnn35248 (void) snprintf(fname, sizeof (fname), "%s/%d/auxv",
183*9acbbeafSnn35248 procfs_path, (int)pid);
1847c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1857c478bd9Sstevel@tonic-gate if ((rv = read(fd, pauxv, naux * sizeof (auxv_t))) >= 0)
1867c478bd9Sstevel@tonic-gate rv /= sizeof (auxv_t);
1877c478bd9Sstevel@tonic-gate (void) close(fd);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate return (rv);
1907c478bd9Sstevel@tonic-gate }
191