2005-05-13 10:57:08 +02:00
|
|
|
/* This file contains some utility routines for PM.
|
2005-05-03 17:35:52 +02:00
|
|
|
*
|
|
|
|
* The entry points are:
|
2005-06-17 11:10:30 +02:00
|
|
|
* find_param: look up a boot monitor parameter
|
2005-05-13 10:57:08 +02:00
|
|
|
* get_free_pid: get a free process or group id
|
|
|
|
* allowed: see if an access is permitted
|
|
|
|
* no_sys: called for invalid system call numbers
|
|
|
|
* panic: PM has run aground of a fatal error
|
|
|
|
* tell_fs: interface to FS
|
|
|
|
* get_mem_map: get memory map of given process
|
|
|
|
* get_stack_ptr: get stack pointer of given process
|
2005-07-01 19:58:29 +02:00
|
|
|
* proc_from_pid: return process pointer from pid number
|
2005-05-03 17:35:52 +02:00
|
|
|
*/
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
#include "pm.h"
|
2005-05-03 17:35:52 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).
In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)
pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.
message field names have changed.
fs drivers are endpoints.
fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)
fproc[].fp_task (indicating which driver the process is suspended on)
became an int.
PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.
More return code checks of sys_* functions have been added.
IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.
Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.
PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.
PM now sys_exit()s itself on panic(), instead of sys_abort().
RS also talks in endpoints instead of process numbers.
2006-03-03 11:20:58 +01:00
|
|
|
#include <minix/endpoint.h>
|
2005-05-03 17:35:52 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h> /* needed only because mproc.h needs it */
|
|
|
|
#include "mproc.h"
|
|
|
|
#include "param.h"
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
#include <minix/config.h>
|
|
|
|
#include <timers.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../../kernel/const.h"
|
2005-07-14 17:16:12 +02:00
|
|
|
#include "../../kernel/config.h"
|
2005-05-13 10:57:08 +02:00
|
|
|
#include "../../kernel/type.h"
|
|
|
|
#include "../../kernel/proc.h"
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_free_pid *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC pid_t get_free_pid()
|
|
|
|
{
|
|
|
|
static pid_t next_pid = INIT_PID + 1; /* next pid to be assigned */
|
|
|
|
register struct mproc *rmp; /* check process table */
|
|
|
|
int t; /* zero if pid still free */
|
|
|
|
|
|
|
|
/* Find a free pid for the child and put it in the table. */
|
|
|
|
do {
|
|
|
|
t = 0;
|
2005-06-24 18:21:21 +02:00
|
|
|
next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
|
2005-05-13 10:57:08 +02:00
|
|
|
for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
|
|
|
|
if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
|
|
|
|
t = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (t); /* 't' = 0 means pid free */
|
|
|
|
return(next_pid);
|
|
|
|
}
|
|
|
|
|
2005-05-03 17:35:52 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* allowed *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int allowed(name_buf, s_buf, mask)
|
|
|
|
char *name_buf; /* pointer to file name to be EXECed */
|
|
|
|
struct stat *s_buf; /* buffer for doing and returning stat struct*/
|
|
|
|
int mask; /* R_BIT, W_BIT, or X_BIT */
|
|
|
|
{
|
|
|
|
/* Check to see if file can be accessed. Return EACCES or ENOENT if the access
|
|
|
|
* is prohibited. If it is legal open the file and return a file descriptor.
|
|
|
|
*/
|
|
|
|
int fd;
|
|
|
|
int save_errno;
|
|
|
|
|
|
|
|
/* Use the fact that mask for access() is the same as the permissions mask.
|
|
|
|
* E.g., X_BIT in <minix/const.h> is the same as X_OK in <unistd.h> and
|
2005-05-13 10:57:08 +02:00
|
|
|
* S_IXOTH in <sys/stat.h>. tell_fs(DO_CHDIR, ...) has set PM's real ids
|
2005-05-03 17:35:52 +02:00
|
|
|
* to the user's effective ids, so access() works right for setuid programs.
|
|
|
|
*/
|
|
|
|
if (access(name_buf, mask) < 0) return(-errno);
|
|
|
|
|
|
|
|
/* The file is accessible but might not be readable. Make it readable. */
|
|
|
|
tell_fs(SETUID, PM_PROC_NR, (int) SUPER_USER, (int) SUPER_USER);
|
|
|
|
|
|
|
|
/* Open the file and fstat it. Restore the ids early to handle errors. */
|
|
|
|
fd = open(name_buf, O_RDONLY | O_NONBLOCK);
|
|
|
|
save_errno = errno; /* open might fail, e.g. from ENFILE */
|
|
|
|
tell_fs(SETUID, PM_PROC_NR, (int) mp->mp_effuid, (int) mp->mp_effuid);
|
|
|
|
if (fd < 0) return(-save_errno);
|
2005-06-01 16:31:00 +02:00
|
|
|
if (fstat(fd, s_buf) < 0) panic(__FILE__,"allowed: fstat failed", NO_NUM);
|
2005-05-03 17:35:52 +02:00
|
|
|
|
|
|
|
/* Only regular files can be executed. */
|
|
|
|
if (mask == X_BIT && (s_buf->st_mode & I_TYPE) != I_REGULAR) {
|
|
|
|
close(fd);
|
|
|
|
return(EACCES);
|
|
|
|
}
|
|
|
|
return(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* no_sys *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int no_sys()
|
|
|
|
{
|
2005-05-13 10:57:08 +02:00
|
|
|
/* A system call number not implemented by PM has been requested. */
|
2005-05-03 17:35:52 +02:00
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
return(ENOSYS);
|
2005-05-03 17:35:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* panic *
|
|
|
|
*===========================================================================*/
|
2005-06-01 16:31:00 +02:00
|
|
|
PUBLIC void panic(who, mess, num)
|
|
|
|
char *who; /* who caused the panic */
|
|
|
|
char *mess; /* panic message string */
|
|
|
|
int num; /* number to go with it */
|
2005-05-03 17:35:52 +02:00
|
|
|
{
|
2005-05-13 10:57:08 +02:00
|
|
|
/* An unrecoverable error has occurred. Panics are caused when an internal
|
2005-05-03 17:35:52 +02:00
|
|
|
* inconsistency is detected, e.g., a programming error or illegal value of a
|
2006-03-10 17:10:05 +01:00
|
|
|
* defined constant. The process manager decides to exit.
|
2005-05-03 17:35:52 +02:00
|
|
|
*/
|
2005-10-12 17:07:38 +02:00
|
|
|
message m;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/* Switch to primary console and print panic message. */
|
|
|
|
check_sig(mproc[TTY_PROC_NR].mp_pid, SIGTERM);
|
2005-06-01 16:31:00 +02:00
|
|
|
printf("PM panic (%s): %s", who, mess);
|
2005-05-13 10:57:08 +02:00
|
|
|
if (num != NO_NUM) printf(": %d",num);
|
2005-05-03 17:35:52 +02:00
|
|
|
printf("\n");
|
2006-03-10 17:10:05 +01:00
|
|
|
|
|
|
|
/* Exit PM. */
|
endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).
In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)
pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.
message field names have changed.
fs drivers are endpoints.
fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)
fproc[].fp_task (indicating which driver the process is suspended on)
became an int.
PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.
More return code checks of sys_* functions have been added.
IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.
Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.
PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.
PM now sys_exit()s itself on panic(), instead of sys_abort().
RS also talks in endpoints instead of process numbers.
2006-03-03 11:20:58 +01:00
|
|
|
sys_exit(SELF);
|
2005-05-03 17:35:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* tell_fs *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void tell_fs(what, p1, p2, p3)
|
|
|
|
int what, p1, p2, p3;
|
|
|
|
{
|
2005-05-13 10:57:08 +02:00
|
|
|
/* This routine is only used by PM to inform FS of certain events:
|
2005-05-03 17:35:52 +02:00
|
|
|
* tell_fs(CHDIR, slot, dir, 0)
|
|
|
|
* tell_fs(EXEC, proc, 0, 0)
|
|
|
|
* tell_fs(EXIT, proc, 0, 0)
|
|
|
|
* tell_fs(FORK, parent, child, pid)
|
|
|
|
* tell_fs(SETGID, proc, realgid, effgid)
|
|
|
|
* tell_fs(SETSID, proc, 0, 0)
|
|
|
|
* tell_fs(SETUID, proc, realuid, effuid)
|
|
|
|
* tell_fs(UNPAUSE, proc, signr, 0)
|
2005-05-31 11:50:51 +02:00
|
|
|
* tell_fs(STIME, time, 0, 0)
|
2005-10-12 17:07:38 +02:00
|
|
|
* Ignore this call if the FS is already dead, e.g. on shutdown.
|
2005-05-03 17:35:52 +02:00
|
|
|
*/
|
|
|
|
message m;
|
|
|
|
|
2005-10-12 17:07:38 +02:00
|
|
|
if ((mproc[FS_PROC_NR].mp_flags & (IN_USE|ZOMBIE)) != IN_USE)
|
|
|
|
return;
|
|
|
|
|
2005-05-03 17:35:52 +02:00
|
|
|
m.tell_fs_arg1 = p1;
|
|
|
|
m.tell_fs_arg2 = p2;
|
|
|
|
m.tell_fs_arg3 = p3;
|
|
|
|
_taskcall(FS_PROC_NR, what, &m);
|
|
|
|
}
|
2005-05-13 10:57:08 +02:00
|
|
|
|
2005-09-11 18:45:46 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* find_param *
|
|
|
|
*===========================================================================*/
|
2005-06-17 11:10:30 +02:00
|
|
|
PUBLIC char *find_param(name)
|
|
|
|
const char *name;
|
|
|
|
{
|
|
|
|
register const char *namep;
|
|
|
|
register char *envp;
|
|
|
|
|
|
|
|
for (envp = (char *) monitor_params; *envp != 0;) {
|
|
|
|
for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
|
|
|
|
;
|
|
|
|
if (*namep == '\0' && *envp == '=')
|
|
|
|
return(envp + 1);
|
|
|
|
while (*envp++ != 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* get_mem_map *
|
2005-05-13 10:57:08 +02:00
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int get_mem_map(proc_nr, mem_map)
|
|
|
|
int proc_nr; /* process to get map of */
|
|
|
|
struct mem_map *mem_map; /* put memory map here */
|
|
|
|
{
|
|
|
|
struct proc p;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((s=sys_getproc(&p, proc_nr)) != OK)
|
|
|
|
return(s);
|
|
|
|
memcpy(mem_map, p.p_memmap, sizeof(p.p_memmap));
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-09-11 18:45:46 +02:00
|
|
|
* get_stack_ptr *
|
2005-05-13 10:57:08 +02:00
|
|
|
*===========================================================================*/
|
endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).
In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)
pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.
message field names have changed.
fs drivers are endpoints.
fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)
fproc[].fp_task (indicating which driver the process is suspended on)
became an int.
PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.
More return code checks of sys_* functions have been added.
IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.
Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.
PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.
PM now sys_exit()s itself on panic(), instead of sys_abort().
RS also talks in endpoints instead of process numbers.
2006-03-03 11:20:58 +01:00
|
|
|
PUBLIC int get_stack_ptr(proc_nr_e, sp)
|
|
|
|
int proc_nr_e; /* process to get sp of */
|
2005-05-13 10:57:08 +02:00
|
|
|
vir_bytes *sp; /* put stack pointer here */
|
|
|
|
{
|
|
|
|
struct proc p;
|
|
|
|
int s;
|
|
|
|
|
endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).
In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)
pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.
message field names have changed.
fs drivers are endpoints.
fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)
fproc[].fp_task (indicating which driver the process is suspended on)
became an int.
PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.
More return code checks of sys_* functions have been added.
IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.
Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.
PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.
PM now sys_exit()s itself on panic(), instead of sys_abort().
RS also talks in endpoints instead of process numbers.
2006-03-03 11:20:58 +01:00
|
|
|
if ((s=sys_getproc(&p, proc_nr_e)) != OK)
|
2005-05-13 10:57:08 +02:00
|
|
|
return(s);
|
|
|
|
*sp = p.p_reg.sp;
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
2005-07-01 19:58:29 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* proc_from_pid *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int proc_from_pid(mp_pid)
|
|
|
|
pid_t mp_pid;
|
|
|
|
{
|
|
|
|
int rmp;
|
|
|
|
|
|
|
|
for (rmp = 0; rmp < NR_PROCS; rmp++)
|
|
|
|
if (mproc[rmp].mp_pid == mp_pid)
|
|
|
|
return rmp;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
endpoint-aware conversion of servers.
'who', indicating caller number in pm and fs and some other servers, has
been removed in favour of 'who_e' (endpoint) and 'who_p' (proc nr.).
In both PM and FS, isokendpt() convert endpoints to process slot
numbers, returning OK if it was a valid and consistent endpoint number.
okendpt() does the same but panic()s if it doesn't succeed. (In PM,
this is pm_isok..)
pm and fs keep their own records of process endpoints in their proc tables,
which are needed to make kernel calls about those processes.
message field names have changed.
fs drivers are endpoints.
fs now doesn't try to get out of driver deadlock, as the protocol isn't
supposed to let that happen any more. (A warning is printed if ELOCKED
is detected though.)
fproc[].fp_task (indicating which driver the process is suspended on)
became an int.
PM and FS now get endpoint numbers of initial boot processes from the
kernel. These happen to be the same as the old proc numbers, to let
user processes reach them with the old numbers, but FS and PM don't know
that. All new processes after INIT, even after the generation number
wraps around, get endpoint numbers with generation 1 and higher, so
the first instances of the boot processes are the only processes ever
to have endpoint numbers in the old proc number range.
More return code checks of sys_* functions have been added.
IS has become endpoint-aware. Ditched the 'text' and 'data' fields
in the kernel dump (which show locations, not sizes, so aren't terribly
useful) in favour of the endpoint number. Proc number is still visible.
Some other dumps (e.g. dmap, rs) show endpoint numbers now too which got
the formatting changed.
PM reading segments using rw_seg() has changed - it uses other fields
in the message now instead of encoding the segment and process number and
fd in the fd field. For that it uses _read_pm() and _write_pm() which to
_taskcall()s directly in pm/misc.c.
PM now sys_exit()s itself on panic(), instead of sys_abort().
RS also talks in endpoints instead of process numbers.
2006-03-03 11:20:58 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* pm_isokendpt *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int pm_isokendpt(int endpoint, int *proc)
|
|
|
|
{
|
|
|
|
*proc = _ENDPOINT_P(endpoint);
|
|
|
|
if(*proc < -NR_TASKS || *proc >= NR_PROCS)
|
|
|
|
return EINVAL;
|
|
|
|
if(*proc >= 0 && endpoint != mproc[*proc].mp_endpoint)
|
|
|
|
return EDEADSRCDST;
|
|
|
|
if(*proc >= 0 && !(mproc[*proc].mp_flags & IN_USE))
|
|
|
|
return EDEADSRCDST;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|