version 1.1, 2008/06/03 10:38:51 |
version 1.1.1.1.2.1, 2008/08/13 17:12:45 |
|
|
pid = last_pid + 1; |
pid = last_pid + 1; |
if (pid >= PID_MAX) |
if (pid >= PID_MAX) |
pid = 1; |
pid = 1; |
|
|
while (pid != last_pid) { |
while (pid != last_pid) { |
if (!proc_find(pid)) |
if (proc_find(pid) == NULL) |
break; |
break; |
if (++pid >= PID_MAX) |
if (++pid >= PID_MAX) |
pid = 1; |
pid = 1; |
|
|
} |
} |
|
|
/* |
/* |
* Get pid for specified task |
* getpid - get the process ID. |
*/ |
*/ |
int |
int |
proc_getpid(struct msg *msg) |
proc_getpid(struct msg *msg) |
|
|
if (curproc == NULL) |
if (curproc == NULL) |
return ESRCH; |
return ESRCH; |
|
|
msg->data[0] = (int)curproc->pid; |
msg->data[0] = (int)curproc->p_pid; |
return 0; |
return 0; |
} |
} |
|
|
/* |
/* |
* Get ppid for specified task |
* getppid - get the parent process ID. |
*/ |
*/ |
int |
int |
proc_getppid(struct msg *msg) |
proc_getppid(struct msg *msg) |
|
|
if (curproc == NULL) |
if (curproc == NULL) |
return ESRCH; |
return ESRCH; |
|
|
msg->data[0] = (int)curproc->parent->pid; |
msg->data[0] = (int)curproc->p_parent->p_pid; |
return 0; |
return 0; |
} |
} |
|
|
/* |
/* |
* Get pgid for specified process. |
* getpgid - get the process group ID for a process. |
|
* |
|
* If the specified pid is equal to 0, it returns the process |
|
* group ID of the calling process. |
*/ |
*/ |
int |
int |
proc_getpgid(struct msg *msg) |
proc_getpgid(struct msg *msg) |
|
|
pid_t pid; |
pid_t pid; |
struct proc *p; |
struct proc *p; |
|
|
pid = (pid_t)msg->data[0]; |
if (curproc == NULL) |
if ((p = proc_find(pid)) == NULL) |
|
return ESRCH; |
return ESRCH; |
|
|
msg->data[0] = (int)p->pgrp->pgid; |
pid = (pid_t)msg->data[0]; |
dprintf("proc getpgid=%x\n", msg->data[0]); |
if (pid == 0) |
|
p = curproc; |
|
else { |
|
if ((p = proc_find(pid)) == NULL) |
|
return ESRCH; |
|
} |
|
msg->data[0] = (int)p->p_pgrp->pg_pgid; |
|
DPRINTF(("proc: getpgid pgid=%d\n", msg->data[0])); |
return 0; |
return 0; |
} |
} |
|
|
/* |
/* |
* Set process group for specified process. |
* setpgid - set process group ID for job control. |
|
* |
|
* If the specified pid is equal to 0, it the process ID of |
|
* the calling process is used. Also, if pgid is 0, the process |
|
* ID of the indicated process is used. |
*/ |
*/ |
int |
int |
proc_setpgid(struct msg *msg) |
proc_setpgid(struct msg *msg) |
|
|
struct proc *p; |
struct proc *p; |
struct pgrp *pgrp; |
struct pgrp *pgrp; |
|
|
|
DPRINTF(("proc: setpgid pid=%d pgid=%d\n", msg->data[0], |
|
msg->data[1])); |
|
|
|
if (curproc == NULL) |
|
return ESRCH; |
|
|
pid = (pid_t)msg->data[0]; |
pid = (pid_t)msg->data[0]; |
|
if (pid == 0) |
|
p = curproc; |
|
else { |
|
if ((p = proc_find(pid)) == NULL) |
|
return ESRCH; |
|
} |
pgid = (pid_t)msg->data[1]; |
pgid = (pid_t)msg->data[1]; |
|
|
if (pgid < 0) |
if (pgid < 0) |
return EINVAL; |
return EINVAL; |
|
if (pgid == 0) |
if ((p = proc_find(pid)) == NULL) |
pgid = p->p_pid; |
return ESRCH; |
if (p->p_pgrp->pg_pgid == pgid) /* already leader */ |
|
|
if (p->pgrp->pgid == pgid) |
|
return 0; |
return 0; |
|
|
pgrp = pgrp_find(pgid); |
if ((pgrp = pgrp_find(pgid)) == NULL) { |
if (pgrp == NULL) { |
|
/* |
/* |
* Create new process group |
* Create a new process group. |
*/ |
*/ |
pgrp = malloc(sizeof(struct pgrp)); |
if ((pgrp = malloc(sizeof(struct pgrp))) == NULL) |
if (pgrp == NULL) |
|
return ENOMEM; |
return ENOMEM; |
list_init(&pgrp->members); |
list_init(&pgrp->pg_members); |
pgrp->pgid = pgid; |
pgrp->pg_pgid = pgid; |
pgrp_add(pgrp); |
pgrp_add(pgrp); |
} |
} else { |
else { |
|
/* |
/* |
* Remove from old process group |
* Join an existing process group. |
|
* Remove from the old process group. |
*/ |
*/ |
list_remove(&p->pgrp_link); |
list_remove(&p->p_pgrp_link); |
} |
} |
list_insert(&pgrp->members, &p->pgrp_link); |
list_insert(&pgrp->pg_members, &p->p_pgrp_link); |
p->pgrp = pgrp; |
p->p_pgrp = pgrp; |
|
return 0; |
|
} |
|
|
|
/* |
|
* getsid - get the process group ID of a session leader. |
|
*/ |
|
int |
|
proc_getsid(struct msg *msg) |
|
{ |
|
pid_t pid; |
|
struct proc *p, *leader; |
|
|
|
if (curproc == NULL) |
|
return ESRCH; |
|
|
|
pid = (pid_t)msg->data[0]; |
|
if (pid == 0) |
|
p = curproc; |
|
else { |
|
if ((p = proc_find(pid)) == NULL) |
|
return ESRCH; |
|
} |
|
leader = p->p_pgrp->pg_session->s_leader; |
|
msg->data[0] = (int)leader->p_pid; |
|
DPRINTF(("proc: getsid sid=%d\n", msg->data[0])); |
|
return 0; |
|
} |
|
|
|
/* |
|
* setsid - create session and set process group ID. |
|
*/ |
|
int |
|
proc_setsid(struct msg *msg) |
|
{ |
|
struct proc *p; |
|
struct pgrp *pgrp; |
|
struct session *sess; |
|
|
|
if (curproc == NULL) |
|
return ESRCH; |
|
p = curproc; |
|
if (p->p_pid == p->p_pgrp->pg_pgid) /* already leader */ |
|
return EPERM; |
|
|
|
if ((sess = malloc(sizeof(struct session))) == NULL) |
|
return ENOMEM; |
|
|
|
/* |
|
* Create a new process group. |
|
*/ |
|
if ((pgrp = malloc(sizeof(struct pgrp))) == NULL) { |
|
free(sess); |
|
return ENOMEM; |
|
} |
|
list_init(&pgrp->pg_members); |
|
pgrp->pg_pgid = p->p_pid; |
|
pgrp_add(pgrp); |
|
list_remove(&p->p_pgrp_link); |
|
list_insert(&pgrp->pg_members, &p->p_pgrp_link); |
|
p->p_pgrp = pgrp; |
|
|
|
/* |
|
* Create a new session. |
|
*/ |
|
sess->s_refcnt = 1; |
|
sess->s_leader = p; |
|
sess->s_ttyhold = 0; |
|
pgrp->pg_session = sess; |
|
|
|
msg->data[0] = (int)p->p_pid; |
|
DPRINTF(("proc: setsid sid=%d\n", msg->data[0])); |
return 0; |
return 0; |
} |
} |