|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24:
25: /*
26: * Mach Operating System
27: * Copyright (c) 1987 Carnegie-Mellon University
28: * All rights reserved. The CMU software License Agreement specifies
29: * the terms and conditions for use and redistribution.
30: */
31:
32: /*
33: */
34: #import <cputypes.h>
35: #import <mach_nbc.h>
36: #import <mach/features.h>
37:
38: #import <kern/task.h>
39: #import <kern/thread.h>
40: #import <mach/time_value.h>
41: #import <mach/vm_param.h>
42: #import <vm/vm_map.h>
43: #import <vm/vm_page.h>
44: #import <kern/parallel.h>
45:
46: #import <sys/param.h>
47: #import <sys/systm.h>
48: #import <sys/dir.h>
49: #import <sys/proc.h>
50: #import <sys/vm.h>
51: #import <sys/file.h>
52: #import <sys/vnode.h>
53: #import <sys/buf.h>
54: #import <sys/mount.h>
55: #import <sys/trace.h>
56: #import <sys/kernel.h>
57:
58: #import <kern/kalloc.h>
59: #import <mach/port.h>
60:
61: #import <machine/spl.h>
62:
63: useracc(addr, len, prot)
64: caddr_t addr;
65: u_int len;
66: int prot;
67: {
68: return (vm_map_check_protection(
69: current_task()->map,
70: trunc_page(addr), round_page(addr+len),
71: prot == B_READ ? VM_PROT_READ : VM_PROT_WRITE));
72: }
73:
74: vslock(addr, len)
75: caddr_t addr;
76: int len;
77: {
78: vm_map_pageable(current_task()->map, trunc_page(addr),
79: round_page(addr+len), FALSE);
80: }
81:
82: vsunlock(addr, len, dirtied)
83: caddr_t addr;
84: int len;
85: int dirtied;
86: {
87: #if NeXT
88: pmap_t pmap;
89: vm_page_t pg;
90: vm_offset_t vaddr, paddr;
91:
92: if (dirtied) {
93: pmap = current_task()->map->pmap;
94: for (vaddr = trunc_page(addr); vaddr < round_page(addr+len);
95: vaddr += PAGE_SIZE) {
96: paddr = pmap_extract(pmap, vaddr);
97: pg = PHYS_TO_VM_PAGE(paddr);
98: vm_page_set_modified(pg);
99: }
100: }
101: #endif /* NeXT */
102: #ifdef lint
103: dirtied++;
104: #endif /* lint */
105: vm_map_pageable(current_task()->map, trunc_page(addr),
106: round_page(addr+len), TRUE);
107: }
108:
109: #if defined(sun) || BALANCE || defined(m88k) || defined(i386)
110: #else defined(sun) || BALANCE || defined(m88k) || defined(i386)
111: subyte(addr, byte)
112: void * addr;
113: int byte;
114: {
115: char character;
116:
117: character = (char)byte;
118: return (copyout((void *)&(character), addr, sizeof(char)) == 0 ? 0 : -1);
119: }
120:
121: suibyte(addr, byte)
122: void * addr;
123: int byte;
124: {
125: char character;
126:
127: character = (char)byte;
128: return (copyout((void *) &(character), addr, sizeof(char)) == 0 ? 0 : -1);
129: }
130:
131: int fubyte(addr)
132: void * addr;
133: {
134: unsigned char byte;
135:
136: if (copyin(addr, (void *) &byte, sizeof(char)))
137: return(-1);
138: return(byte);
139: }
140:
141: int fuibyte(addr)
142: void * addr;
143: {
144: unsigned char byte;
145:
146: if (copyin(addr, (void *) &(byte), sizeof(char)))
147: return(-1);
148: return(byte);
149: }
150:
151: suword(addr, word)
152: void * addr;
153: long word;
154: {
155: return (copyout((void *) &word, addr, sizeof(int)) == 0 ? 0 : -1);
156: }
157:
158: long fuword(addr)
159: void * addr;
160: {
161: long word;
162:
163: if (copyin(addr, (void *) &word, sizeof(int)))
164: return(-1);
165: return(word);
166: }
167:
168: /* suiword and fuiword are the same as suword and fuword, respectively */
169:
170: suiword(addr, word)
171: void * addr;
172: long word;
173: {
174: return (copyout((void *) &word, addr, sizeof(int)) == 0 ? 0 : -1);
175: }
176:
177: long fuiword(addr)
178: void * addr;
179: {
180: long word;
181:
182: if (copyin(addr, (void *) &word, sizeof(int)))
183: return(-1);
184: return(word);
185: }
186: #endif /* defined(sun) || BALANCE || defined(m88k) || defined(i386) */
187:
188: swapon()
189: {
190: /* Not yet implemented */
191: printf("swapon(): not supported!\n");
192: return(EPERM);
193: }
194:
195: thread_t procdup(child, parent)
196: struct proc *child, *parent;
197: {
198: thread_t thread;
199: task_t task;
200: kern_return_t result;
201: boolean_t inherit_memory = TRUE;
202:
203: if (parent->task == kernel_task)
204: inherit_memory = FALSE;
205:
206: result = task_create(parent->task, inherit_memory, &task);
207: if(result != KERN_SUCCESS)
208: printf("fork/procdup: task_create failed. result: %d\n", result);
209: child->task = task; /* retained reference */
210: task->proc = child;
211:
212: result = thread_create(task, &thread);
213: if(result != KERN_SUCCESS)
214: printf("fork/procdup: thread_create failed. Code: 0x%x\n", result);
215: thread_deallocate(thread); /* release reference */
216:
217: /*
218: * Don't need to lock thread here because it can't
219: * possibly execute and no one else knows about it.
220: */
221: compute_priority(thread, FALSE);
222:
223: return(thread);
224: }
225:
226: chgprot(_addr, prot)
227: caddr_t _addr;
228: vm_prot_t prot;
229: {
230: vm_offset_t addr = (vm_offset_t) _addr;
231:
232: return(vm_map_protect(current_task()->map,
233: trunc_page(addr),
234: round_page(addr + 1),
235: prot, FALSE) == KERN_SUCCESS);
236: }
237:
238: kern_return_t unix_pid(t, x)
239: task_t t;
240: int *x;
241: {
242: if (t == TASK_NULL) {
243: *x = -1;
244: return(KERN_FAILURE);
245: } else {
246: if (t->proc)
247: *x = t->proc->p_pid;
248: else {
249: *x = -1;
250: return(KERN_FAILURE);
251: }
252: return(KERN_SUCCESS);
253: }
254: }
255:
256: /*
257: * Routine: task_by_unix_pid
258: * Purpose:
259: * Get the task port for another "process", named by its
260: * process ID on the same host as "target_task".
261: *
262: * Only permitted to privileged processes, or processes
263: * with the same user ID.
264: */
265: kern_return_t task_by_unix_pid(target_task, pid, t)
266: task_t target_task;
267: int pid;
268: task_t *t;
269: {
270: struct proc *p;
271:
272: unix_master();
273:
274: if (
275: ((p = pfind(pid)) != (struct proc *) 0)
276: && (target_task->proc != (struct proc *) 0)
277: && ((p->p_ucred->cr_uid == target_task->proc->p_ucred->cr_uid)
278: || !(suser(target_task->proc->p_ucred, &target_task->proc->p_acflag)))
279: && (p->p_stat != SZOMB)
280: ) {
281: if (p->task != TASK_NULL)
282: task_reference(p->task);
283: *t = p->task;
284: /* compatibility with NeXT 2.0 debuggers, launchers */
285: if (suser(p->p_ucred, &p->p_acflag)) {
286: /* don't set it for anyone; could exec a careless nameserver */
287: struct proc *p = current_task()->proc;
288:
289: if (p)
290: p->p_debugger = 1;
291: }
292: unix_release();
293: return(KERN_SUCCESS);
294: }
295: *t = TASK_NULL;
296: unix_release();
297: return(KERN_FAILURE);
298: }
299:
300: /*
301: * Routine: task_by_pid
302: * Purpose:
303: * Trap form of "task_by_unix_pid"; soon to be eliminated.
304: */
305: port_t task_by_pid(pid)
306: int pid;
307: {
308: task_t self = current_task();
309: port_t t = PORT_NULL;
310: task_t result_task;
311:
312: if (task_by_unix_pid(self, pid, &result_task) == KERN_SUCCESS) {
313: t = convert_task_to_port(result_task);
314: if (t != PORT_NULL)
315: object_copyout(self, t, MSG_TYPE_PORT, &t);
316: }
317:
318: return(t);
319: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.