Annotation of prex-old/usr/server/proc/pid.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005-2007, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: /*
31: * pid.c - service for process id.
32: */
33:
34: #include <prex/prex.h>
35: #include <server/proc.h>
36: #include <server/stdmsg.h>
37: #include <sys/list.h>
38:
39: #include <unistd.h>
40: #include <errno.h>
41: #include <stdlib.h>
42:
43: #include "proc.h"
44:
45: /*
46: * PID previously allocated.
47: *
48: * Note: The following pids are reserved by default.
49: * pid = 0: Process server
50: * pid = 1: Init process
51: */
52: static pid_t last_pid = 1;
53:
54: /*
55: * Assign new pid.
56: * Returns pid on sucess, or 0 on failure.
57: */
58: pid_t
59: pid_assign(void)
60: {
61: pid_t pid;
62:
63: pid = last_pid + 1;
64: if (pid >= PID_MAX)
65: pid = 1;
66:
67: while (pid != last_pid) {
68: if (!proc_find(pid))
69: break;
70: if (++pid >= PID_MAX)
71: pid = 1;
72: }
73: if (pid == last_pid)
74: return 0;
75: last_pid = pid;
76: return pid;
77: }
78:
79: /*
80: * Get pid for specified task
81: */
82: int
83: proc_getpid(struct msg *msg)
84: {
85:
86: if (curproc == NULL)
87: return ESRCH;
88:
89: msg->data[0] = (int)curproc->pid;
90: return 0;
91: }
92:
93: /*
94: * Get ppid for specified task
95: */
96: int
97: proc_getppid(struct msg *msg)
98: {
99:
100: if (curproc == NULL)
101: return ESRCH;
102:
103: msg->data[0] = (int)curproc->parent->pid;
104: return 0;
105: }
106:
107: /*
108: * Get pgid for specified process.
109: */
110: int
111: proc_getpgid(struct msg *msg)
112: {
113: pid_t pid;
114: struct proc *p;
115:
116: pid = (pid_t)msg->data[0];
117: if ((p = proc_find(pid)) == NULL)
118: return ESRCH;
119:
120: msg->data[0] = (int)p->pgrp->pgid;
121: dprintf("proc getpgid=%x\n", msg->data[0]);
122: return 0;
123: }
124:
125: /*
126: * Set process group for specified process.
127: */
128: int
129: proc_setpgid(struct msg *msg)
130: {
131: pid_t pid, pgid;
132: struct proc *p;
133: struct pgrp *pgrp;
134:
135: pid = (pid_t)msg->data[0];
136: pgid = (pid_t)msg->data[1];
137:
138: if (pgid < 0)
139: return EINVAL;
140:
141: if ((p = proc_find(pid)) == NULL)
142: return ESRCH;
143:
144: if (p->pgrp->pgid == pgid)
145: return 0;
146:
147: pgrp = pgrp_find(pgid);
148: if (pgrp == NULL) {
149: /*
150: * Create new process group
151: */
152: pgrp = malloc(sizeof(struct pgrp));
153: if (pgrp == NULL)
154: return ENOMEM;
155: list_init(&pgrp->members);
156: pgrp->pgid = pgid;
157: pgrp_add(pgrp);
158: }
159: else {
160: /*
161: * Remove from old process group
162: */
163: list_remove(&p->pgrp_link);
164: }
165: list_insert(&pgrp->members, &p->pgrp_link);
166: p->pgrp = pgrp;
167: return 0;
168: }
CVSweb