2005-05-03 17:35:52 +02:00
|
|
|
/* Miscellaneous system calls. Author: Kees J. Bot
|
|
|
|
* 31 Mar 2000
|
|
|
|
* The entry points into this file are:
|
|
|
|
* do_reboot: kill all processes, then reboot system
|
2005-05-13 10:57:08 +02:00
|
|
|
* do_svrctl: process manager control
|
2005-06-02 14:43:21 +02:00
|
|
|
* do_getsysinfo: request copy of PM data structure (Jorrit N. Herder)
|
|
|
|
* do_getprocnr: lookup process slot number (Jorrit N. Herder)
|
|
|
|
* do_memalloc: allocate a chunk of memory (Jorrit N. Herder)
|
|
|
|
* do_memfree: deallocate a chunk of memory (Jorrit N. Herder)
|
2005-07-01 19:58:29 +02:00
|
|
|
* do_getsetpriority: get/set process priority
|
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 <minix/callnr.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/svrctl.h>
|
2005-07-01 19:58:29 +02:00
|
|
|
#include <sys/resource.h>
|
2005-05-03 17:35:52 +02:00
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/utils.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mproc.h"
|
|
|
|
#include "param.h"
|
|
|
|
|
|
|
|
|
2005-06-03 15:55:06 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_allocmem *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_allocmem()
|
2005-06-02 14:43:21 +02:00
|
|
|
{
|
|
|
|
vir_clicks mem_clicks;
|
|
|
|
phys_clicks mem_base;
|
|
|
|
|
|
|
|
mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
|
|
|
mem_base = alloc_mem(mem_clicks);
|
|
|
|
if (mem_base == NO_MEM) return(ENOMEM);
|
|
|
|
mp->mp_reply.membase = (phys_bytes) (mem_base << CLICK_SHIFT);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
2005-06-03 15:55:06 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_freemem *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int do_freemem()
|
2005-06-02 14:43:21 +02:00
|
|
|
{
|
2005-06-03 15:55:06 +02:00
|
|
|
vir_clicks mem_clicks;
|
|
|
|
phys_clicks mem_base;
|
|
|
|
|
|
|
|
mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
|
|
|
mem_base = (m_in.membase + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
|
|
|
free_mem(mem_base, mem_clicks);
|
2005-06-02 14:43:21 +02:00
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
2005-06-06 15:51:50 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_getsysinfo *
|
|
|
|
*===========================================================================*/
|
2005-05-03 17:35:52 +02:00
|
|
|
PUBLIC int do_getsysinfo()
|
|
|
|
{
|
2005-06-06 15:51:50 +02:00
|
|
|
struct mproc *proc_addr;
|
|
|
|
vir_bytes src_addr, dst_addr;
|
|
|
|
struct kinfo kinfo;
|
|
|
|
size_t len;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
switch(m_in.info_what) {
|
|
|
|
case SI_KINFO: /* kernel info is obtained via PM */
|
|
|
|
sys_getkinfo(&kinfo);
|
|
|
|
src_addr = (vir_bytes) &kinfo;
|
|
|
|
len = sizeof(struct kinfo);
|
|
|
|
break;
|
|
|
|
case SI_PROC_ADDR: /* get address of PM process table */
|
|
|
|
proc_addr = &mproc[0];
|
|
|
|
src_addr = (vir_bytes) &proc_addr;
|
|
|
|
len = sizeof(struct mproc *);
|
|
|
|
break;
|
2005-06-07 16:43:35 +02:00
|
|
|
case SI_PROC_TAB: /* copy entire process table */
|
|
|
|
src_addr = (vir_bytes) mproc;
|
|
|
|
len = sizeof(struct mproc) * NR_PROCS;
|
|
|
|
break;
|
2005-06-06 15:51:50 +02:00
|
|
|
default:
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_addr = (vir_bytes) m_in.info_where;
|
|
|
|
if (OK != (s=sys_datacopy(SELF, src_addr, who, dst_addr, len)))
|
|
|
|
return(s);
|
2005-05-03 17:35:52 +02:00
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-06 15:51:50 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_getprocnr *
|
|
|
|
*===========================================================================*/
|
2005-05-19 11:38:29 +02:00
|
|
|
PUBLIC int do_getprocnr()
|
|
|
|
{
|
2005-06-02 14:43:21 +02:00
|
|
|
register struct mproc *rmp;
|
2005-06-20 11:35:23 +02:00
|
|
|
static char search_key[PROC_NAME_LEN+1];
|
2005-06-02 14:43:21 +02:00
|
|
|
int key_len;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (m_in.namelen > 0) { /* lookup process by name */
|
2005-06-20 11:35:23 +02:00
|
|
|
key_len = MIN(m_in.namelen, PROC_NAME_LEN);
|
2005-06-02 14:43:21 +02:00
|
|
|
if (OK != (s=sys_datacopy(who, (vir_bytes) m_in.addr,
|
|
|
|
SELF, (vir_bytes) search_key, key_len)))
|
|
|
|
return(s);
|
|
|
|
search_key[key_len] = '\0'; /* terminate for safety */
|
|
|
|
for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
2005-06-20 11:35:23 +02:00
|
|
|
if ((rmp->mp_flags & IN_USE) &&
|
2005-06-02 14:43:21 +02:00
|
|
|
strncmp(rmp->mp_name, search_key, key_len)==0) {
|
|
|
|
mp->mp_reply.procnr = (int) (rmp - mproc);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(ESRCH);
|
|
|
|
}
|
|
|
|
else { /* return own process number */
|
|
|
|
mp->mp_reply.procnr = who;
|
|
|
|
}
|
2005-05-19 11:38:29 +02:00
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-03 17:35:52 +02:00
|
|
|
/*=====================================================================*
|
|
|
|
* do_reboot *
|
|
|
|
*=====================================================================*/
|
2005-06-24 18:21:21 +02:00
|
|
|
#define REBOOT_CODE "delay; boot"
|
2005-05-03 17:35:52 +02:00
|
|
|
PUBLIC int do_reboot()
|
|
|
|
{
|
2005-06-24 18:21:21 +02:00
|
|
|
char monitor_code[32*sizeof(char *)];
|
|
|
|
int code_len;
|
|
|
|
int abort_flag;
|
2005-05-03 17:35:52 +02:00
|
|
|
|
2005-06-24 18:21:21 +02:00
|
|
|
if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
2005-05-03 17:35:52 +02:00
|
|
|
|
|
|
|
switch (m_in.reboot_flag) {
|
|
|
|
case RBT_HALT:
|
|
|
|
case RBT_PANIC:
|
|
|
|
case RBT_RESET:
|
2005-06-24 18:21:21 +02:00
|
|
|
abort_flag = m_in.reboot_flag;
|
|
|
|
break;
|
|
|
|
case RBT_REBOOT:
|
|
|
|
code_len = strlen(REBOOT_CODE) + 1;
|
|
|
|
strncpy(monitor_code, REBOOT_CODE, code_len);
|
|
|
|
abort_flag = RBT_MONITOR;
|
2005-05-03 17:35:52 +02:00
|
|
|
break;
|
|
|
|
case RBT_MONITOR:
|
2005-06-24 18:21:21 +02:00
|
|
|
code_len = m_in.reboot_strlen + 1;
|
|
|
|
if (code_len > sizeof(monitor_code)) return(EINVAL);
|
2005-05-03 17:35:52 +02:00
|
|
|
if (sys_datacopy(who, (vir_bytes) m_in.reboot_code,
|
|
|
|
PM_PROC_NR, (vir_bytes) monitor_code,
|
2005-06-24 18:21:21 +02:00
|
|
|
(phys_bytes) (code_len)) != OK) return(EFAULT);
|
|
|
|
if (monitor_code[code_len-1] != 0) return(EINVAL);
|
|
|
|
abort_flag = RBT_MONITOR;
|
2005-05-03 17:35:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tell_fs(REBOOT,0,0,0); /* tell FS to prepare for shutdown */
|
2005-05-13 10:57:08 +02:00
|
|
|
check_sig(-1, SIGKILL); /* kill all processes except init */
|
2005-05-03 17:35:52 +02:00
|
|
|
|
2005-05-27 15:10:58 +02:00
|
|
|
/* Ask the kernel to abort. All system services, including the PM, will
|
|
|
|
* get a HARD_STOP notification. Await the notification in the main loop.
|
|
|
|
*/
|
2005-06-24 18:21:21 +02:00
|
|
|
sys_abort(abort_flag, PM_PROC_NR, monitor_code, code_len);
|
2005-05-30 11:29:48 +02:00
|
|
|
return(SUSPEND); /* don't reply to killed process */
|
2005-05-03 17:35:52 +02:00
|
|
|
}
|
|
|
|
|
2005-07-01 19:58:29 +02:00
|
|
|
/*=====================================================================*
|
|
|
|
* do_getsetpriority *
|
|
|
|
*=====================================================================*/
|
|
|
|
PUBLIC int do_getsetpriority()
|
|
|
|
{
|
|
|
|
int arg_which, arg_who, arg_pri;
|
|
|
|
int rmp_nr;
|
|
|
|
struct mproc *rmp;
|
|
|
|
|
|
|
|
arg_which = m_in.m1_i1;
|
|
|
|
arg_who = m_in.m1_i2;
|
|
|
|
arg_pri = m_in.m1_i3; /* for SETPRIORITY */
|
|
|
|
|
|
|
|
/* Code common to GETPRIORITY and SETPRIORITY. */
|
|
|
|
|
|
|
|
/* Only support PRIO_PROCESS for now. */
|
|
|
|
if(arg_which != PRIO_PROCESS)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if(arg_who == 0)
|
|
|
|
rmp_nr = who;
|
|
|
|
else
|
|
|
|
if((rmp_nr = proc_from_pid(arg_who)) < 0)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
rmp = &mproc[rmp_nr];
|
|
|
|
|
|
|
|
if(mp->mp_effuid != SUPER_USER &&
|
|
|
|
mp->mp_effuid != rmp->mp_effuid && mp->mp_effuid != rmp->mp_realuid)
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
/* If GET, that's it. */
|
|
|
|
|
|
|
|
if(call_nr == GETPRIORITY) {
|
|
|
|
return rmp->mp_nice - PRIO_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only root is allowed to reduce the nice level. */
|
|
|
|
if(rmp->mp_nice > arg_pri && mp->mp_effuid != SUPER_USER)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
/* We're SET, and it's allowed. Do it and tell kernel. */
|
|
|
|
rmp->mp_nice = arg_pri;
|
|
|
|
return sys_setpriority(rmp_nr, arg_pri);
|
|
|
|
}
|
|
|
|
|
2005-05-03 17:35:52 +02:00
|
|
|
/*=====================================================================*
|
|
|
|
* do_svrctl *
|
|
|
|
*=====================================================================*/
|
|
|
|
PUBLIC int do_svrctl()
|
|
|
|
{
|
|
|
|
int s, req;
|
|
|
|
vir_bytes ptr;
|
|
|
|
req = m_in.svrctl_req;
|
|
|
|
ptr = (vir_bytes) m_in.svrctl_argp;
|
|
|
|
|
2005-06-17 11:10:30 +02:00
|
|
|
/* Is the request for the kernel? Forward it, except for SYSGETENV. */
|
|
|
|
if (((req >> 8) & 0xFF) == 'S') {
|
2005-05-03 17:35:52 +02:00
|
|
|
|
2005-06-17 11:10:30 +02:00
|
|
|
/* Binary compatibility check. */
|
|
|
|
if (req == SYSGETENV) {
|
2005-05-03 17:35:52 +02:00
|
|
|
printf("SYSGETENV by %d (fix!)\n", who);
|
|
|
|
req = MMGETPARAM;
|
2005-06-17 11:10:30 +02:00
|
|
|
}
|
|
|
|
else
|
2005-05-03 17:35:52 +02:00
|
|
|
|
|
|
|
/* Simply forward call to the SYSTEM task. */
|
|
|
|
return(sys_svrctl(who, req, mp->mp_effuid == SUPER_USER, ptr));
|
|
|
|
}
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
/* Control operations local to the PM. */
|
2005-05-03 17:35:52 +02:00
|
|
|
switch(req) {
|
|
|
|
case MMGETPARAM: {
|
|
|
|
struct sysgetenv sysgetenv;
|
|
|
|
char search_key[64];
|
|
|
|
char *val_start;
|
|
|
|
size_t val_len;
|
|
|
|
size_t copy_len;
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
/* Copy sysgetenv structure to PM. */
|
2005-05-03 17:35:52 +02:00
|
|
|
if (sys_datacopy(who, ptr, SELF, (vir_bytes) &sysgetenv,
|
|
|
|
sizeof(sysgetenv)) != OK) return(EFAULT);
|
|
|
|
|
|
|
|
if (sysgetenv.keylen == 0) { /* copy all parameters */
|
|
|
|
val_start = monitor_params;
|
|
|
|
val_len = sizeof(monitor_params);
|
|
|
|
}
|
|
|
|
else { /* lookup value for key */
|
|
|
|
/* Try to get a copy of the requested key. */
|
|
|
|
if (sysgetenv.keylen > sizeof(search_key)) return(EINVAL);
|
|
|
|
if ((s = sys_datacopy(who, (vir_bytes) sysgetenv.key,
|
|
|
|
SELF, (vir_bytes) search_key, sysgetenv.keylen)) != OK)
|
|
|
|
return(s);
|
|
|
|
|
|
|
|
/* Make sure key is null-terminated and lookup value. */
|
|
|
|
search_key[sysgetenv.keylen-1]= '\0';
|
2005-06-17 11:10:30 +02:00
|
|
|
if ((val_start = find_param(search_key)) == NULL)
|
2005-05-03 17:35:52 +02:00
|
|
|
return(ESRCH);
|
|
|
|
val_len = strlen(val_start) + 1;
|
|
|
|
}
|
|
|
|
|
2005-06-20 11:35:23 +02:00
|
|
|
/* See if it fits in the client's buffer. */
|
|
|
|
if (val_len > sysgetenv.vallen)
|
|
|
|
return E2BIG;
|
|
|
|
|
2005-05-03 17:35:52 +02:00
|
|
|
/* Value found, make the actual copy (as far as possible). */
|
2005-06-20 11:35:23 +02:00
|
|
|
copy_len = MIN(val_len, sysgetenv.vallen);
|
2005-05-03 17:35:52 +02:00
|
|
|
if ((s=sys_datacopy(SELF, (vir_bytes) val_start,
|
|
|
|
who, (vir_bytes) sysgetenv.val, copy_len)) != OK)
|
|
|
|
return(s);
|
|
|
|
|
2005-06-20 11:35:23 +02:00
|
|
|
return OK;
|
2005-05-03 17:35:52 +02:00
|
|
|
}
|
|
|
|
case MMSIGNON: {
|
|
|
|
/* A user process becomes a task. Simulate an exit by
|
|
|
|
* releasing a waiting parent and disinheriting children.
|
|
|
|
*/
|
|
|
|
struct mproc *rmp;
|
|
|
|
pid_t pidarg;
|
|
|
|
|
|
|
|
if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
|
|
|
|
|
|
|
rmp = &mproc[mp->mp_parent];
|
|
|
|
tell_fs(EXIT, who, 0, 0);
|
|
|
|
|
|
|
|
pidarg = rmp->mp_wpid;
|
|
|
|
if ((rmp->mp_flags & WAITING) && (pidarg == -1
|
|
|
|
|| pidarg == mp->mp_pid || -pidarg == mp->mp_procgrp))
|
|
|
|
{
|
|
|
|
/* Wake up the parent. */
|
|
|
|
rmp->mp_reply.reply_res2 = 0;
|
|
|
|
setreply(mp->mp_parent, mp->mp_pid);
|
|
|
|
rmp->mp_flags &= ~WAITING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disinherit children. */
|
|
|
|
for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
2005-06-20 11:35:23 +02:00
|
|
|
if ((rmp->mp_flags & IN_USE) && rmp->mp_parent == who) {
|
2005-05-03 17:35:52 +02:00
|
|
|
rmp->mp_parent = INIT_PROC_NR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-13 10:57:08 +02:00
|
|
|
/* Become like PM and FS. */
|
2005-05-03 17:35:52 +02:00
|
|
|
mp->mp_pid = mp->mp_procgrp = 0;
|
|
|
|
mp->mp_parent = 0;
|
|
|
|
return(OK); }
|
|
|
|
|
|
|
|
#if ENABLE_SWAP
|
|
|
|
case MMSWAPON: {
|
|
|
|
struct mmswapon swapon;
|
|
|
|
|
|
|
|
if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
|
|
|
|
|
|
|
if (sys_datacopy(who, (phys_bytes) ptr,
|
|
|
|
PM_PROC_NR, (phys_bytes) &swapon,
|
|
|
|
(phys_bytes) sizeof(swapon)) != OK) return(EFAULT);
|
|
|
|
|
|
|
|
return(swap_on(swapon.file, swapon.offset, swapon.size)); }
|
|
|
|
|
|
|
|
case MMSWAPOFF: {
|
|
|
|
if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
|
|
|
|
|
|
|
return(swap_off()); }
|
|
|
|
#endif /* SWAP */
|
|
|
|
|
|
|
|
default:
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|