[4] | 1 | /* Miscellaneous system calls. Author: Kees J. Bot
|
---|
| 2 | * 31 Mar 2000
|
---|
| 3 | * The entry points into this file are:
|
---|
| 4 | * do_reboot: kill all processes, then reboot system
|
---|
| 5 | * do_svrctl: process manager control
|
---|
| 6 | * do_getsysinfo: request copy of PM data structure (Jorrit N. Herder)
|
---|
| 7 | * do_getprocnr: lookup process slot number (Jorrit N. Herder)
|
---|
| 8 | * do_memalloc: allocate a chunk of memory (Jorrit N. Herder)
|
---|
| 9 | * do_memfree: deallocate a chunk of memory (Jorrit N. Herder)
|
---|
| 10 | * do_getsetpriority: get/set process priority
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include "pm.h"
|
---|
| 14 | #include <minix/callnr.h>
|
---|
| 15 | #include <signal.h>
|
---|
| 16 | #include <sys/svrctl.h>
|
---|
| 17 | #include <sys/resource.h>
|
---|
| 18 | #include <minix/com.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | #include "mproc.h"
|
---|
| 21 | #include "param.h"
|
---|
| 22 |
|
---|
| 23 | /*===========================================================================*
|
---|
| 24 | * do_allocmem *
|
---|
| 25 | *===========================================================================*/
|
---|
| 26 | PUBLIC int do_allocmem()
|
---|
| 27 | {
|
---|
| 28 | vir_clicks mem_clicks;
|
---|
| 29 | phys_clicks mem_base;
|
---|
| 30 |
|
---|
| 31 | mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 32 | mem_base = alloc_mem(mem_clicks);
|
---|
| 33 | if (mem_base == NO_MEM) return(ENOMEM);
|
---|
| 34 | mp->mp_reply.membase = (phys_bytes) (mem_base << CLICK_SHIFT);
|
---|
| 35 | return(OK);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /*===========================================================================*
|
---|
| 39 | * do_freemem *
|
---|
| 40 | *===========================================================================*/
|
---|
| 41 | PUBLIC int do_freemem()
|
---|
| 42 | {
|
---|
| 43 | vir_clicks mem_clicks;
|
---|
| 44 | phys_clicks mem_base;
|
---|
| 45 |
|
---|
| 46 | mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 47 | mem_base = (m_in.membase + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 48 | free_mem(mem_base, mem_clicks);
|
---|
| 49 | return(OK);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /*===========================================================================*
|
---|
| 53 | * do_getsysinfo *
|
---|
| 54 | *===========================================================================*/
|
---|
| 55 | PUBLIC int do_getsysinfo()
|
---|
| 56 | {
|
---|
| 57 | struct mproc *proc_addr;
|
---|
| 58 | vir_bytes src_addr, dst_addr;
|
---|
| 59 | struct kinfo kinfo;
|
---|
| 60 | size_t len;
|
---|
| 61 | int s;
|
---|
| 62 |
|
---|
| 63 | switch(m_in.info_what) {
|
---|
| 64 | case SI_KINFO: /* kernel info is obtained via PM */
|
---|
| 65 | sys_getkinfo(&kinfo);
|
---|
| 66 | src_addr = (vir_bytes) &kinfo;
|
---|
| 67 | len = sizeof(struct kinfo);
|
---|
| 68 | break;
|
---|
| 69 | case SI_PROC_ADDR: /* get address of PM process table */
|
---|
| 70 | proc_addr = &mproc[0];
|
---|
| 71 | src_addr = (vir_bytes) &proc_addr;
|
---|
| 72 | len = sizeof(struct mproc *);
|
---|
| 73 | break;
|
---|
| 74 | case SI_PROC_TAB: /* copy entire process table */
|
---|
| 75 | src_addr = (vir_bytes) mproc;
|
---|
| 76 | len = sizeof(struct mproc) * NR_PROCS;
|
---|
| 77 | break;
|
---|
| 78 | default:
|
---|
| 79 | return(EINVAL);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | dst_addr = (vir_bytes) m_in.info_where;
|
---|
| 83 | if (OK != (s=sys_datacopy(SELF, src_addr, who, dst_addr, len)))
|
---|
| 84 | return(s);
|
---|
| 85 | return(OK);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /*===========================================================================*
|
---|
| 89 | * do_getprocnr *
|
---|
| 90 | *===========================================================================*/
|
---|
| 91 | PUBLIC int do_getprocnr()
|
---|
| 92 | {
|
---|
| 93 | register struct mproc *rmp;
|
---|
| 94 | static char search_key[PROC_NAME_LEN+1];
|
---|
| 95 | int key_len;
|
---|
| 96 | int s;
|
---|
| 97 |
|
---|
| 98 | if (m_in.pid >= 0) { /* lookup process by pid */
|
---|
| 99 | for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
---|
| 100 | if ((rmp->mp_flags & IN_USE) && (rmp->mp_pid==m_in.pid)) {
|
---|
| 101 | mp->mp_reply.procnr = (int) (rmp - mproc);
|
---|
| 102 | return(OK);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | return(ESRCH);
|
---|
| 106 | } else if (m_in.namelen > 0) { /* lookup process by name */
|
---|
| 107 | key_len = MIN(m_in.namelen, PROC_NAME_LEN);
|
---|
| 108 | if (OK != (s=sys_datacopy(who, (vir_bytes) m_in.addr,
|
---|
| 109 | SELF, (vir_bytes) search_key, key_len)))
|
---|
| 110 | return(s);
|
---|
| 111 | search_key[key_len] = '\0'; /* terminate for safety */
|
---|
| 112 | for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
---|
| 113 | if ((rmp->mp_flags & IN_USE) &&
|
---|
| 114 | strncmp(rmp->mp_name, search_key, key_len)==0) {
|
---|
| 115 | mp->mp_reply.procnr = (int) (rmp - mproc);
|
---|
| 116 | return(OK);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | return(ESRCH);
|
---|
| 120 | } else { /* return own process number */
|
---|
| 121 | mp->mp_reply.procnr = who;
|
---|
| 122 | }
|
---|
| 123 | return(OK);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /*===========================================================================*
|
---|
| 127 | * do_reboot *
|
---|
| 128 | *===========================================================================*/
|
---|
| 129 | #define REBOOT_CODE "delay; boot"
|
---|
| 130 | PUBLIC int do_reboot()
|
---|
| 131 | {
|
---|
| 132 | char monitor_code[32*sizeof(char *)];
|
---|
| 133 | int code_len;
|
---|
| 134 | int abort_flag;
|
---|
| 135 |
|
---|
| 136 | if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
---|
| 137 |
|
---|
| 138 | switch (m_in.reboot_flag) {
|
---|
| 139 | case RBT_HALT:
|
---|
| 140 | case RBT_PANIC:
|
---|
| 141 | case RBT_RESET:
|
---|
| 142 | abort_flag = m_in.reboot_flag;
|
---|
| 143 | break;
|
---|
| 144 | case RBT_REBOOT:
|
---|
| 145 | code_len = strlen(REBOOT_CODE) + 1;
|
---|
| 146 | strncpy(monitor_code, REBOOT_CODE, code_len);
|
---|
| 147 | abort_flag = RBT_MONITOR;
|
---|
| 148 | break;
|
---|
| 149 | case RBT_MONITOR:
|
---|
| 150 | code_len = m_in.reboot_strlen + 1;
|
---|
| 151 | if (code_len > sizeof(monitor_code)) return(EINVAL);
|
---|
| 152 | if (sys_datacopy(who, (vir_bytes) m_in.reboot_code,
|
---|
| 153 | PM_PROC_NR, (vir_bytes) monitor_code,
|
---|
| 154 | (phys_bytes) (code_len)) != OK) return(EFAULT);
|
---|
| 155 | if (monitor_code[code_len-1] != 0) return(EINVAL);
|
---|
| 156 | abort_flag = RBT_MONITOR;
|
---|
| 157 | break;
|
---|
| 158 | default:
|
---|
| 159 | return(EINVAL);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | check_sig(-1, SIGKILL); /* kill all processes except init */
|
---|
| 163 | tell_fs(REBOOT,0,0,0); /* tell FS to prepare for shutdown */
|
---|
| 164 |
|
---|
| 165 | /* Ask the kernel to abort. All system services, including the PM, will
|
---|
| 166 | * get a HARD_STOP notification. Await the notification in the main loop.
|
---|
| 167 | */
|
---|
| 168 | sys_abort(abort_flag, PM_PROC_NR, monitor_code, code_len);
|
---|
| 169 | return(SUSPEND); /* don't reply to killed process */
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*===========================================================================*
|
---|
| 173 | * do_getsetpriority *
|
---|
| 174 | *===========================================================================*/
|
---|
| 175 | PUBLIC int do_getsetpriority()
|
---|
| 176 | {
|
---|
| 177 | int arg_which, arg_who, arg_pri;
|
---|
| 178 | int rmp_nr;
|
---|
| 179 | struct mproc *rmp;
|
---|
| 180 |
|
---|
| 181 | arg_which = m_in.m1_i1;
|
---|
| 182 | arg_who = m_in.m1_i2;
|
---|
| 183 | arg_pri = m_in.m1_i3; /* for SETPRIORITY */
|
---|
| 184 |
|
---|
| 185 | /* Code common to GETPRIORITY and SETPRIORITY. */
|
---|
| 186 |
|
---|
| 187 | /* Only support PRIO_PROCESS for now. */
|
---|
| 188 | if (arg_which != PRIO_PROCESS)
|
---|
| 189 | return(EINVAL);
|
---|
| 190 |
|
---|
| 191 | if (arg_who == 0)
|
---|
| 192 | rmp_nr = who;
|
---|
| 193 | else
|
---|
| 194 | if ((rmp_nr = proc_from_pid(arg_who)) < 0)
|
---|
| 195 | return(ESRCH);
|
---|
| 196 |
|
---|
| 197 | rmp = &mproc[rmp_nr];
|
---|
| 198 |
|
---|
| 199 | if (mp->mp_effuid != SUPER_USER &&
|
---|
| 200 | mp->mp_effuid != rmp->mp_effuid && mp->mp_effuid != rmp->mp_realuid)
|
---|
| 201 | return EPERM;
|
---|
| 202 |
|
---|
| 203 | /* If GET, that's it. */
|
---|
| 204 | if (call_nr == GETPRIORITY) {
|
---|
| 205 | return(rmp->mp_nice - PRIO_MIN);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* Only root is allowed to reduce the nice level. */
|
---|
| 209 | if (rmp->mp_nice > arg_pri && mp->mp_effuid != SUPER_USER)
|
---|
| 210 | return(EACCES);
|
---|
| 211 |
|
---|
| 212 | /* We're SET, and it's allowed. Do it and tell kernel. */
|
---|
| 213 | rmp->mp_nice = arg_pri;
|
---|
| 214 | return sys_nice(rmp_nr, arg_pri);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /*===========================================================================*
|
---|
| 218 | * do_svrctl *
|
---|
| 219 | *===========================================================================*/
|
---|
| 220 | PUBLIC int do_svrctl()
|
---|
| 221 | {
|
---|
| 222 | int s, req;
|
---|
| 223 | vir_bytes ptr;
|
---|
| 224 | #define MAX_LOCAL_PARAMS 2
|
---|
| 225 | static struct {
|
---|
| 226 | char name[30];
|
---|
| 227 | char value[30];
|
---|
| 228 | } local_param_overrides[MAX_LOCAL_PARAMS];
|
---|
| 229 | static int local_params = 0;
|
---|
| 230 |
|
---|
| 231 | req = m_in.svrctl_req;
|
---|
| 232 | ptr = (vir_bytes) m_in.svrctl_argp;
|
---|
| 233 |
|
---|
| 234 | /* Is the request indeed for the MM? */
|
---|
| 235 | if (((req >> 8) & 0xFF) != 'M') return(EINVAL);
|
---|
| 236 |
|
---|
| 237 | /* Control operations local to the PM. */
|
---|
| 238 | switch(req) {
|
---|
| 239 | case MMSETPARAM:
|
---|
| 240 | case MMGETPARAM: {
|
---|
| 241 | struct sysgetenv sysgetenv;
|
---|
| 242 | char search_key[64];
|
---|
| 243 | char *val_start;
|
---|
| 244 | size_t val_len;
|
---|
| 245 | size_t copy_len;
|
---|
| 246 |
|
---|
| 247 | /* Copy sysgetenv structure to PM. */
|
---|
| 248 | if (sys_datacopy(who, ptr, SELF, (vir_bytes) &sysgetenv,
|
---|
| 249 | sizeof(sysgetenv)) != OK) return(EFAULT);
|
---|
| 250 |
|
---|
| 251 | /* Set a param override? */
|
---|
| 252 | if (req == MMSETPARAM) {
|
---|
| 253 | if (local_params >= MAX_LOCAL_PARAMS) return ENOSPC;
|
---|
| 254 | if (sysgetenv.keylen <= 0
|
---|
| 255 | || sysgetenv.keylen >=
|
---|
| 256 | sizeof(local_param_overrides[local_params].name)
|
---|
| 257 | || sysgetenv.vallen <= 0
|
---|
| 258 | || sysgetenv.vallen >=
|
---|
| 259 | sizeof(local_param_overrides[local_params].value))
|
---|
| 260 | return EINVAL;
|
---|
| 261 |
|
---|
| 262 | if ((s = sys_datacopy(who, (vir_bytes) sysgetenv.key,
|
---|
| 263 | SELF, (vir_bytes) local_param_overrides[local_params].name,
|
---|
| 264 | sysgetenv.keylen)) != OK)
|
---|
| 265 | return s;
|
---|
| 266 | if ((s = sys_datacopy(who, (vir_bytes) sysgetenv.val,
|
---|
| 267 | SELF, (vir_bytes) local_param_overrides[local_params].value,
|
---|
| 268 | sysgetenv.keylen)) != OK)
|
---|
| 269 | return s;
|
---|
| 270 | local_param_overrides[local_params].name[sysgetenv.keylen] = '\0';
|
---|
| 271 | local_param_overrides[local_params].value[sysgetenv.vallen] = '\0';
|
---|
| 272 |
|
---|
| 273 | local_params++;
|
---|
| 274 |
|
---|
| 275 | return OK;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | if (sysgetenv.keylen == 0) { /* copy all parameters */
|
---|
| 279 | val_start = monitor_params;
|
---|
| 280 | val_len = sizeof(monitor_params);
|
---|
| 281 | }
|
---|
| 282 | else { /* lookup value for key */
|
---|
| 283 | int p;
|
---|
| 284 | /* Try to get a copy of the requested key. */
|
---|
| 285 | if (sysgetenv.keylen > sizeof(search_key)) return(EINVAL);
|
---|
| 286 | if ((s = sys_datacopy(who, (vir_bytes) sysgetenv.key,
|
---|
| 287 | SELF, (vir_bytes) search_key, sysgetenv.keylen)) != OK)
|
---|
| 288 | return(s);
|
---|
| 289 |
|
---|
| 290 | /* Make sure key is null-terminated and lookup value.
|
---|
| 291 | * First check local overrides.
|
---|
| 292 | */
|
---|
| 293 | search_key[sysgetenv.keylen-1]= '\0';
|
---|
| 294 | for(p = 0; p < local_params; p++) {
|
---|
| 295 | if (!strcmp(search_key, local_param_overrides[p].name)) {
|
---|
| 296 | val_start = local_param_overrides[p].value;
|
---|
| 297 | break;
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | if (p >= local_params && (val_start = find_param(search_key)) == NULL)
|
---|
| 301 | return(ESRCH);
|
---|
| 302 | val_len = strlen(val_start) + 1;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /* See if it fits in the client's buffer. */
|
---|
| 306 | if (val_len > sysgetenv.vallen)
|
---|
| 307 | return E2BIG;
|
---|
| 308 |
|
---|
| 309 | /* Value found, make the actual copy (as far as possible). */
|
---|
| 310 | copy_len = MIN(val_len, sysgetenv.vallen);
|
---|
| 311 | if ((s=sys_datacopy(SELF, (vir_bytes) val_start,
|
---|
| 312 | who, (vir_bytes) sysgetenv.val, copy_len)) != OK)
|
---|
| 313 | return(s);
|
---|
| 314 |
|
---|
| 315 | return OK;
|
---|
| 316 | }
|
---|
| 317 | default:
|
---|
| 318 | return(EINVAL);
|
---|
| 319 | }
|
---|
| 320 | }
|
---|