[9] | 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_procstat: request process status (Jorrit N. Herder)
|
---|
| 6 | * do_getsysinfo: request copy of PM data structure (Jorrit N. Herder)
|
---|
| 7 | * do_getprocnr: lookup process slot number (Jorrit N. Herder)
|
---|
| 8 | * do_allocmem: allocate a chunk of memory (Jorrit N. Herder)
|
---|
| 9 | * do_freemem: deallocate a chunk of memory (Jorrit N. Herder)
|
---|
| 10 | * do_getsetpriority: get/set process priority
|
---|
| 11 | * do_svrctl: process manager control
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | #include "pm.h"
|
---|
| 15 | #include <minix/callnr.h>
|
---|
| 16 | #include <signal.h>
|
---|
| 17 | #include <sys/svrctl.h>
|
---|
| 18 | #include <sys/resource.h>
|
---|
| 19 | #include <minix/com.h>
|
---|
| 20 | #include <minix/config.h>
|
---|
| 21 | #include <minix/type.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <lib.h>
|
---|
| 24 | #include "mproc.h"
|
---|
| 25 | #include "param.h"
|
---|
| 26 | #include "../../kernel/proc.h"
|
---|
| 27 |
|
---|
| 28 | /*===========================================================================*
|
---|
| 29 | * do_allocmem *
|
---|
| 30 | *===========================================================================*/
|
---|
| 31 | PUBLIC int do_allocmem()
|
---|
| 32 | {
|
---|
| 33 | vir_clicks mem_clicks;
|
---|
| 34 | phys_clicks mem_base;
|
---|
| 35 |
|
---|
| 36 | mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 37 | mem_base = alloc_mem(mem_clicks);
|
---|
| 38 | if (mem_base == NO_MEM) return(ENOMEM);
|
---|
| 39 | mp->mp_reply.membase = (phys_bytes) (mem_base << CLICK_SHIFT);
|
---|
| 40 | return(OK);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /*===========================================================================*
|
---|
| 44 | * do_freemem *
|
---|
| 45 | *===========================================================================*/
|
---|
| 46 | PUBLIC int do_freemem()
|
---|
| 47 | {
|
---|
| 48 | vir_clicks mem_clicks;
|
---|
| 49 | phys_clicks mem_base;
|
---|
| 50 |
|
---|
| 51 | mem_clicks = (m_in.memsize + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 52 | mem_base = (m_in.membase + CLICK_SIZE -1 ) >> CLICK_SHIFT;
|
---|
| 53 | free_mem(mem_base, mem_clicks);
|
---|
| 54 | return(OK);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /*===========================================================================*
|
---|
| 58 | * do_procstat *
|
---|
| 59 | *===========================================================================*/
|
---|
| 60 | PUBLIC int do_procstat()
|
---|
| 61 | {
|
---|
| 62 | /* For the moment, this is only used to return pending signals to
|
---|
| 63 | * system processes that request the PM for their own status.
|
---|
| 64 | *
|
---|
| 65 | * Future use might include the FS requesting for process status of
|
---|
| 66 | * any user process.
|
---|
| 67 | */
|
---|
| 68 | if (m_in.stat_nr == SELF) {
|
---|
| 69 | mp->mp_reply.sig_set = mp->mp_sigpending;
|
---|
| 70 | sigemptyset(&mp->mp_sigpending);
|
---|
| 71 | }
|
---|
| 72 | else {
|
---|
| 73 | return(ENOSYS);
|
---|
| 74 | }
|
---|
| 75 | return(OK);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /*===========================================================================*
|
---|
| 79 | * do_getsysinfo *
|
---|
| 80 | *===========================================================================*/
|
---|
| 81 | PUBLIC int do_getsysinfo()
|
---|
| 82 | {
|
---|
| 83 | struct mproc *proc_addr;
|
---|
| 84 | vir_bytes src_addr, dst_addr;
|
---|
| 85 | struct kinfo kinfo;
|
---|
| 86 | struct loadinfo loadinfo;
|
---|
| 87 | static struct proc proctab[NR_PROCS+NR_TASKS];
|
---|
| 88 | size_t len;
|
---|
| 89 | static struct pm_mem_info pmi;
|
---|
| 90 | int s, r;
|
---|
| 91 | size_t holesize;
|
---|
| 92 |
|
---|
| 93 | switch(m_in.info_what) {
|
---|
| 94 | case SI_KINFO: /* kernel info is obtained via PM */
|
---|
| 95 | sys_getkinfo(&kinfo);
|
---|
| 96 | src_addr = (vir_bytes) &kinfo;
|
---|
| 97 | len = sizeof(struct kinfo);
|
---|
| 98 | break;
|
---|
| 99 | case SI_PROC_ADDR: /* get address of PM process table */
|
---|
| 100 | proc_addr = &mproc[0];
|
---|
| 101 | src_addr = (vir_bytes) &proc_addr;
|
---|
| 102 | len = sizeof(struct mproc *);
|
---|
| 103 | break;
|
---|
| 104 | case SI_PROC_TAB: /* copy entire process table */
|
---|
| 105 | src_addr = (vir_bytes) mproc;
|
---|
| 106 | len = sizeof(struct mproc) * NR_PROCS;
|
---|
| 107 | break;
|
---|
| 108 | case SI_KPROC_TAB: /* copy entire process table */
|
---|
| 109 | if((r=sys_getproctab(proctab)) != OK)
|
---|
| 110 | return r;
|
---|
| 111 | src_addr = (vir_bytes) proctab;
|
---|
| 112 | len = sizeof(proctab);
|
---|
| 113 | break;
|
---|
| 114 | case SI_MEM_ALLOC:
|
---|
| 115 | holesize = sizeof(pmi.pmi_holes);
|
---|
| 116 | if((r=mem_holes_copy(pmi.pmi_holes, &holesize,
|
---|
| 117 | &pmi.pmi_hi_watermark)) != OK)
|
---|
| 118 | return r;
|
---|
| 119 | src_addr = (vir_bytes) &pmi;
|
---|
| 120 | len = sizeof(pmi);
|
---|
| 121 | break;
|
---|
| 122 | case SI_LOADINFO: /* loadinfo is obtained via PM */
|
---|
| 123 | sys_getloadinfo(&loadinfo);
|
---|
| 124 | src_addr = (vir_bytes) &loadinfo;
|
---|
| 125 | len = sizeof(struct loadinfo);
|
---|
| 126 | break;
|
---|
| 127 | default:
|
---|
| 128 | return(EINVAL);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | dst_addr = (vir_bytes) m_in.info_where;
|
---|
| 132 | if (OK != (s=sys_datacopy(SELF, src_addr, who_e, dst_addr, len)))
|
---|
| 133 | return(s);
|
---|
| 134 | return(OK);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*===========================================================================*
|
---|
| 138 | * do_getprocnr *
|
---|
| 139 | *===========================================================================*/
|
---|
| 140 | PUBLIC int do_getprocnr()
|
---|
| 141 | {
|
---|
| 142 | register struct mproc *rmp;
|
---|
| 143 | static char search_key[PROC_NAME_LEN+1];
|
---|
| 144 | int key_len;
|
---|
| 145 | int s;
|
---|
| 146 |
|
---|
| 147 | if (m_in.pid >= 0) { /* lookup process by pid */
|
---|
| 148 | for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
---|
| 149 | if ((rmp->mp_flags & IN_USE) && (rmp->mp_pid==m_in.pid)) {
|
---|
| 150 | mp->mp_reply.endpt = rmp->mp_endpoint;
|
---|
| 151 | return(OK);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | return(ESRCH);
|
---|
| 155 | } else if (m_in.namelen > 0) { /* lookup process by name */
|
---|
| 156 | key_len = MIN(m_in.namelen, PROC_NAME_LEN);
|
---|
| 157 | if (OK != (s=sys_datacopy(who_e, (vir_bytes) m_in.addr,
|
---|
| 158 | SELF, (vir_bytes) search_key, key_len)))
|
---|
| 159 | return(s);
|
---|
| 160 | search_key[key_len] = '\0'; /* terminate for safety */
|
---|
| 161 | for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
|
---|
| 162 | if (((rmp->mp_flags & (IN_USE | ZOMBIE)) == IN_USE) &&
|
---|
| 163 | strncmp(rmp->mp_name, search_key, key_len)==0) {
|
---|
| 164 | mp->mp_reply.endpt = rmp->mp_endpoint;
|
---|
| 165 | return(OK);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | return(ESRCH);
|
---|
| 169 | } else { /* return own/parent process number */
|
---|
| 170 | mp->mp_reply.endpt = who_e;
|
---|
| 171 | mp->mp_reply.pendpt = mproc[mp->mp_parent].mp_endpoint;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | return(OK);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /*===========================================================================*
|
---|
| 178 | * do_reboot *
|
---|
| 179 | *===========================================================================*/
|
---|
| 180 | PUBLIC int do_reboot()
|
---|
| 181 | {
|
---|
| 182 | char monitor_code[256];
|
---|
| 183 | vir_bytes code_addr;
|
---|
| 184 | int code_size;
|
---|
| 185 | int abort_flag;
|
---|
| 186 |
|
---|
| 187 | /* Check permission to abort the system. */
|
---|
| 188 | if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
---|
| 189 |
|
---|
| 190 | /* See how the system should be aborted. */
|
---|
| 191 | abort_flag = (unsigned) m_in.reboot_flag;
|
---|
| 192 | if (abort_flag >= RBT_INVALID) return(EINVAL);
|
---|
| 193 | if (RBT_MONITOR == abort_flag) {
|
---|
| 194 | int r;
|
---|
| 195 | if(m_in.reboot_strlen >= sizeof(monitor_code))
|
---|
| 196 | return EINVAL;
|
---|
| 197 | if((r = sys_datacopy(who_e, (vir_bytes) m_in.reboot_code,
|
---|
| 198 | SELF, (vir_bytes) monitor_code, m_in.reboot_strlen)) != OK)
|
---|
| 199 | return r;
|
---|
| 200 | code_addr = (vir_bytes) monitor_code;
|
---|
| 201 | monitor_code[m_in.reboot_strlen] = '\0';
|
---|
| 202 | code_size = m_in.reboot_strlen + 1;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /* Order matters here. When FS is told to reboot, it exits all its
|
---|
| 206 | * processes, and then would be confused if they're exited again by
|
---|
| 207 | * SIGKILL. So first kill, then reboot.
|
---|
| 208 | */
|
---|
| 209 |
|
---|
| 210 | check_sig(-1, SIGKILL); /* kill all users except init */
|
---|
| 211 | sys_nice(INIT_PROC_NR, PRIO_STOP); /* stop init, but keep it around */
|
---|
| 212 | tell_fs(REBOOT, 0, 0, 0); /* tell FS to synchronize */
|
---|
| 213 |
|
---|
| 214 | /* Ask the kernel to abort. All system services, including the PM, will
|
---|
| 215 | * get a HARD_STOP notification. Await the notification in the main loop.
|
---|
| 216 | */
|
---|
| 217 | sys_abort(abort_flag, PM_PROC_NR, code_addr, code_size);
|
---|
| 218 | return(SUSPEND); /* don't reply to caller */
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /*===========================================================================*
|
---|
| 222 | * do_getsetpriority *
|
---|
| 223 | *===========================================================================*/
|
---|
| 224 | PUBLIC int do_getsetpriority()
|
---|
| 225 | {
|
---|
| 226 | int arg_which, arg_who, arg_pri;
|
---|
| 227 | int rmp_nr;
|
---|
| 228 | struct mproc *rmp;
|
---|
| 229 |
|
---|
| 230 | arg_which = m_in.m1_i1;
|
---|
| 231 | arg_who = m_in.m1_i2;
|
---|
| 232 | arg_pri = m_in.m1_i3; /* for SETPRIORITY */
|
---|
| 233 |
|
---|
| 234 | /* Code common to GETPRIORITY and SETPRIORITY. */
|
---|
| 235 |
|
---|
| 236 | /* Only support PRIO_PROCESS for now. */
|
---|
| 237 | if (arg_which != PRIO_PROCESS)
|
---|
| 238 | return(EINVAL);
|
---|
| 239 |
|
---|
| 240 | if (arg_who == 0)
|
---|
| 241 | rmp_nr = who_p;
|
---|
| 242 | else
|
---|
| 243 | if ((rmp_nr = proc_from_pid(arg_who)) < 0)
|
---|
| 244 | return(ESRCH);
|
---|
| 245 |
|
---|
| 246 | rmp = &mproc[rmp_nr];
|
---|
| 247 |
|
---|
| 248 | if (mp->mp_effuid != SUPER_USER &&
|
---|
| 249 | mp->mp_effuid != rmp->mp_effuid && mp->mp_effuid != rmp->mp_realuid)
|
---|
| 250 | return EPERM;
|
---|
| 251 |
|
---|
| 252 | /* If GET, that's it. */
|
---|
| 253 | if (call_nr == GETPRIORITY) {
|
---|
| 254 | return(rmp->mp_nice - PRIO_MIN);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /* Only root is allowed to reduce the nice level. */
|
---|
| 258 | if (rmp->mp_nice > arg_pri && mp->mp_effuid != SUPER_USER)
|
---|
| 259 | return(EACCES);
|
---|
| 260 |
|
---|
| 261 | /* We're SET, and it's allowed. Do it and tell kernel. */
|
---|
| 262 | rmp->mp_nice = arg_pri;
|
---|
| 263 | return sys_nice(rmp->mp_endpoint, arg_pri);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /*===========================================================================*
|
---|
| 267 | * do_svrctl *
|
---|
| 268 | *===========================================================================*/
|
---|
| 269 | PUBLIC int do_svrctl()
|
---|
| 270 | {
|
---|
| 271 | int s, req;
|
---|
| 272 | vir_bytes ptr;
|
---|
| 273 | #define MAX_LOCAL_PARAMS 2
|
---|
| 274 | static struct {
|
---|
| 275 | char name[30];
|
---|
| 276 | char value[30];
|
---|
| 277 | } local_param_overrides[MAX_LOCAL_PARAMS];
|
---|
| 278 | static int local_params = 0;
|
---|
| 279 |
|
---|
| 280 | req = m_in.svrctl_req;
|
---|
| 281 | ptr = (vir_bytes) m_in.svrctl_argp;
|
---|
| 282 |
|
---|
| 283 | /* Is the request indeed for the MM? */
|
---|
| 284 | if (((req >> 8) & 0xFF) != 'M') return(EINVAL);
|
---|
| 285 |
|
---|
| 286 | /* Control operations local to the PM. */
|
---|
| 287 | switch(req) {
|
---|
| 288 | case MMSETPARAM:
|
---|
| 289 | case MMGETPARAM: {
|
---|
| 290 | struct sysgetenv sysgetenv;
|
---|
| 291 | char search_key[64];
|
---|
| 292 | char *val_start;
|
---|
| 293 | size_t val_len;
|
---|
| 294 | size_t copy_len;
|
---|
| 295 |
|
---|
| 296 | /* Copy sysgetenv structure to PM. */
|
---|
| 297 | if (sys_datacopy(who_e, ptr, SELF, (vir_bytes) &sysgetenv,
|
---|
| 298 | sizeof(sysgetenv)) != OK) return(EFAULT);
|
---|
| 299 |
|
---|
| 300 | /* Set a param override? */
|
---|
| 301 | if (req == MMSETPARAM) {
|
---|
| 302 | if (local_params >= MAX_LOCAL_PARAMS) return ENOSPC;
|
---|
| 303 | if (sysgetenv.keylen <= 0
|
---|
| 304 | || sysgetenv.keylen >=
|
---|
| 305 | sizeof(local_param_overrides[local_params].name)
|
---|
| 306 | || sysgetenv.vallen <= 0
|
---|
| 307 | || sysgetenv.vallen >=
|
---|
| 308 | sizeof(local_param_overrides[local_params].value))
|
---|
| 309 | return EINVAL;
|
---|
| 310 |
|
---|
| 311 | if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.key,
|
---|
| 312 | SELF, (vir_bytes) local_param_overrides[local_params].name,
|
---|
| 313 | sysgetenv.keylen)) != OK)
|
---|
| 314 | return s;
|
---|
| 315 | if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.val,
|
---|
| 316 | SELF, (vir_bytes) local_param_overrides[local_params].value,
|
---|
| 317 | sysgetenv.keylen)) != OK)
|
---|
| 318 | return s;
|
---|
| 319 | local_param_overrides[local_params].name[sysgetenv.keylen] = '\0';
|
---|
| 320 | local_param_overrides[local_params].value[sysgetenv.vallen] = '\0';
|
---|
| 321 |
|
---|
| 322 | local_params++;
|
---|
| 323 |
|
---|
| 324 | return OK;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | if (sysgetenv.keylen == 0) { /* copy all parameters */
|
---|
| 328 | val_start = monitor_params;
|
---|
| 329 | val_len = sizeof(monitor_params);
|
---|
| 330 | }
|
---|
| 331 | else { /* lookup value for key */
|
---|
| 332 | int p;
|
---|
| 333 | /* Try to get a copy of the requested key. */
|
---|
| 334 | if (sysgetenv.keylen > sizeof(search_key)) return(EINVAL);
|
---|
| 335 | if ((s = sys_datacopy(who_e, (vir_bytes) sysgetenv.key,
|
---|
| 336 | SELF, (vir_bytes) search_key, sysgetenv.keylen)) != OK)
|
---|
| 337 | return(s);
|
---|
| 338 |
|
---|
| 339 | /* Make sure key is null-terminated and lookup value.
|
---|
| 340 | * First check local overrides.
|
---|
| 341 | */
|
---|
| 342 | search_key[sysgetenv.keylen-1]= '\0';
|
---|
| 343 | for(p = 0; p < local_params; p++) {
|
---|
| 344 | if (!strcmp(search_key, local_param_overrides[p].name)) {
|
---|
| 345 | val_start = local_param_overrides[p].value;
|
---|
| 346 | break;
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 | if (p >= local_params && (val_start = find_param(search_key)) == NULL)
|
---|
| 350 | return(ESRCH);
|
---|
| 351 | val_len = strlen(val_start) + 1;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /* See if it fits in the client's buffer. */
|
---|
| 355 | if (val_len > sysgetenv.vallen)
|
---|
| 356 | return E2BIG;
|
---|
| 357 |
|
---|
| 358 | /* Value found, make the actual copy (as far as possible). */
|
---|
| 359 | copy_len = MIN(val_len, sysgetenv.vallen);
|
---|
| 360 | if ((s=sys_datacopy(SELF, (vir_bytes) val_start,
|
---|
| 361 | who_e, (vir_bytes) sysgetenv.val, copy_len)) != OK)
|
---|
| 362 | return(s);
|
---|
| 363 |
|
---|
| 364 | return OK;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | #if ENABLE_SWAP
|
---|
| 368 | case MMSWAPON: {
|
---|
| 369 | struct mmswapon swapon;
|
---|
| 370 |
|
---|
| 371 | if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
---|
| 372 |
|
---|
| 373 | if (sys_datacopy(who_e, (phys_bytes) ptr,
|
---|
| 374 | PM_PROC_NR, (phys_bytes) &swapon,
|
---|
| 375 | (phys_bytes) sizeof(swapon)) != OK) return(EFAULT);
|
---|
| 376 |
|
---|
| 377 | return(swap_on(swapon.file, swapon.offset, swapon.size)); }
|
---|
| 378 |
|
---|
| 379 | case MMSWAPOFF: {
|
---|
| 380 | if (mp->mp_effuid != SUPER_USER) return(EPERM);
|
---|
| 381 |
|
---|
| 382 | return(swap_off()); }
|
---|
| 383 | #endif /* SWAP */
|
---|
| 384 |
|
---|
| 385 | default:
|
---|
| 386 | return(EINVAL);
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | /*===========================================================================*
|
---|
| 391 | * _read_pm *
|
---|
| 392 | *===========================================================================*/
|
---|
| 393 | PUBLIC ssize_t _read_pm(fd, buffer, nbytes, seg, ep)
|
---|
| 394 | int fd;
|
---|
| 395 | void *buffer;
|
---|
| 396 | size_t nbytes;
|
---|
| 397 | int seg;
|
---|
| 398 | int ep;
|
---|
| 399 | {
|
---|
| 400 | message m;
|
---|
| 401 |
|
---|
| 402 | m.m1_i1 = _PM_SEG_FLAG | fd;
|
---|
| 403 | m.m1_i2 = nbytes;
|
---|
| 404 | m.m1_p1 = (char *) buffer;
|
---|
| 405 | m.m1_p2 = (char *) seg;
|
---|
| 406 | m.m1_p3 = (char *) ep;
|
---|
| 407 | return(_syscall(FS_PROC_NR, READ, &m));
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /*===========================================================================*
|
---|
| 411 | * _write_pm *
|
---|
| 412 | *===========================================================================*/
|
---|
| 413 | PUBLIC ssize_t _write_pm(fd, buffer, nbytes, seg, ep)
|
---|
| 414 | int fd;
|
---|
| 415 | void *buffer;
|
---|
| 416 | size_t nbytes;
|
---|
| 417 | int seg;
|
---|
| 418 | int ep;
|
---|
| 419 | {
|
---|
| 420 | message m;
|
---|
| 421 |
|
---|
| 422 | m.m1_i1 = _PM_SEG_FLAG | fd;
|
---|
| 423 | m.m1_i2 = nbytes;
|
---|
| 424 | m.m1_p1 = (char *) buffer;
|
---|
| 425 | m.m1_p2 = (char *) seg;
|
---|
| 426 | m.m1_p3 = (char *) ep;
|
---|
| 427 | return(_syscall(FS_PROC_NR, WRITE, &m));
|
---|
| 428 | }
|
---|
| 429 |
|
---|