]> git.saurik.com Git - apple/libc.git/blob - darwin/libproc.c
Libc-594.9.1.tar.gz
[apple/libc.git] / darwin / libproc.c
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
34 int __proc_info(int callnum, int pid, int flavor, uint64_t arg, void * buffer, int buffersize);
35
36
37
38 int
39 proc_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
54 int
55 proc_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
66 int
67 proc_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
79 int
80 proc_name(int pid, void * buffer, uint32_t buffersize)
81 {
82 int retval = 0, len;
83 struct proc_bsdinfo pbsd;
84
85
86 if (buffersize < sizeof(pbsd.pbi_name)) {
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 != 0) {
93 if (pbsd.pbi_name[0]) {
94 bcopy(&pbsd.pbi_name, buffer, sizeof(pbsd.pbi_name));
95 } else {
96 bcopy(&pbsd.pbi_comm, buffer, sizeof(pbsd.pbi_comm));
97 }
98 len = strlen(buffer);
99 return(len);
100 }
101 return(0);
102 }
103
104 int
105 proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize)
106 {
107 int retval = 0, len;
108 struct proc_regionwithpathinfo reginfo;
109
110 if (buffersize < MAXPATHLEN) {
111 errno = ENOMEM;
112 return(0);
113 }
114
115 retval = proc_pidinfo(pid, PROC_PIDREGIONPATHINFO, (uint64_t)address, &reginfo, sizeof(struct proc_regionwithpathinfo));
116 if (retval != -1) {
117 len = strlen(&reginfo.prp_vip.vip_path[0]);
118 if (len != 0) {
119 if (len > MAXPATHLEN)
120 len = MAXPATHLEN;
121 bcopy(&reginfo.prp_vip.vip_path[0], buffer, len);
122 return(len);
123 }
124 return(0);
125 }
126 return(0);
127
128 }
129
130 int
131 proc_kmsgbuf(void * buffer, uint32_t buffersize)
132 {
133 int retval;
134
135 if ((retval = __proc_info(4, 0, 0, (uint64_t)0, buffer, buffersize)) == -1)
136 return(0);
137 return (retval);
138 }
139
140 int
141 proc_pidpath(int pid, void * buffer, uint32_t buffersize)
142 {
143 int retval, len;
144
145 if (buffersize < PROC_PIDPATHINFO_SIZE) {
146 errno = ENOMEM;
147 return(0);
148 }
149 if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
150 errno = EOVERFLOW;
151 return(0);
152 }
153
154 retval = __proc_info(2, pid, PROC_PIDPATHINFO, (uint64_t)0, buffer, buffersize);
155 if (retval != -1) {
156 len = strlen(buffer);
157 return(len);
158 }
159 return (0);
160 }
161
162
163 int
164 proc_libversion(int *major, int * minor)
165 {
166
167 if (major != NULL)
168 *major = 1;
169 if (minor != NULL)
170 *minor = 1;
171 return(0);
172 }
173
174 int
175 proc_setpcontrol(const int control)
176 {
177 int retval ;
178
179 if (control < PROC_SETPC_NONE || control > PROC_SETPC_TERMINATE)
180 return(EINVAL);
181
182 if ((retval = __proc_info(5, getpid(), PROC_SELFSET_PCONTROL,(uint64_t)control, NULL, 0)) == -1)
183 return(errno);
184
185 return(0);
186 }
187
188