]> git.saurik.com Git - apple/libc.git/blame - darwin/libproc.c
Libc-498.tar.gz
[apple/libc.git] / darwin / libproc.c
CommitLineData
224c7076
A
1/*
2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <sys/cdefs.h>
25#include <unistd.h>
26#include <errno.h>
27#include <string.h>
28#include <strings.h>
29#include <sys/errno.h>
30#include <sys/msgbuf.h>
31
32#include "libproc.h"
33
34int __proc_info(int callnum, int pid, int flavor, uint64_t arg, void * buffer, int buffersize);
35
36
37
38int
39proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize)
40{
41 int retval;
42
43 if ((type == PROC_ALL_PIDS) || (type == PROC_PGRP_ONLY) || (type == PROC_TTY_ONLY) || (type == PROC_UID_ONLY) || (type == PROC_RUID_ONLY)) {
44 if ((retval = __proc_info(1, type, typeinfo,(uint64_t)0, buffer, buffersize)) == -1)
45 return(0);
46 } else {
47 errno = EINVAL;
48 retval = 0;
49 }
50 return(retval);
51}
52
53
54int
55proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
56{
57 int retval;
58
59 if ((retval = __proc_info(2, pid, flavor, arg, buffer, buffersize)) == -1)
60 return(0);
61
62 return(retval);
63}
64
65
66int
67proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize)
68{
69 int retval;
70
71 if ((retval = __proc_info(3, pid, flavor, (uint64_t)fd, buffer, buffersize)) == -1)
72 return(0);
73
74 return (retval);
75}
76
77
78
79int
80proc_name(int pid, void * buffer, uint32_t buffersize)
81{
82 int retval = 0, len;
83 struct proc_bsdinfo pbsd;
84
85
86 if (buffersize < 2*MAXCOMLEN) {
87 errno = ENOMEM;
88 return(0);
89 }
90
91 retval = proc_pidinfo(pid, PROC_PIDTBSDINFO, (uint64_t)0, &pbsd, sizeof(struct proc_bsdinfo));
92 if (retval != -1) {
93 bcopy(&pbsd.pbi_name, buffer, 2* 2*MAXCOMLEN);
94 len = strlen(&pbsd.pbi_name[0]);
95 return(len);
96 }
97 return(0);
98}
99
100int
101proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize)
102{
103 int retval = 0, len;
104 struct proc_regionwithpathinfo reginfo;
105
106 if (buffersize < MAXPATHLEN) {
107 errno = ENOMEM;
108 return(0);
109 }
110
111 retval = proc_pidinfo(pid, PROC_PIDREGIONPATHINFO, (uint64_t)address, &reginfo, sizeof(struct proc_regionwithpathinfo));
112 if (retval != -1) {
113 len = strlen(&reginfo.prp_vip.vip_path[0]);
114 if (len != 0) {
115 if (len > MAXPATHLEN)
116 len = MAXPATHLEN;
117 bcopy(&reginfo.prp_vip.vip_path[0], buffer, len);
118 return(len);
119 }
120 return(0);
121 }
122 return(0);
123
124}
125
126int
127proc_kmsgbuf(void * buffer, uint32_t buffersize)
128{
129 int retval;
130
131 if ((retval = __proc_info(4, 0, 0, (uint64_t)0, buffer, buffersize)) == -1)
132 return(0);
133 return (retval);
134}
135
136int
137proc_pidpath(int pid, void * buffer, uint32_t buffersize)
138{
139 int retval, len;
140
141 if (buffersize < PROC_PIDPATHINFO_SIZE) {
142 errno = ENOMEM;
143 return(0);
144 }
145 if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
146 errno = EOVERFLOW;
147 return(0);
148 }
149
150 retval = __proc_info(2, pid, PROC_PIDPATHINFO, (uint64_t)0, buffer, buffersize);
151 if (retval != -1) {
152 len = strlen(buffer);
153 return(len);
154 }
155 return (0);
156}
157
158
159int proc_libversion(int *major, int * minor)
160{
161
162 if (major != NULL)
163 *major = 1;
164 if (minor != NULL)
165 *minor = 1;
166 return(0);
167}
168