VM: simplify slab allocator
[minix.git] / servers / pm / utility.c
blob0ebe296e19b588f81d8fe12afea60121f5c1f677
1 /* This file contains some utility routines for PM.
3 * The entry points are:
4 * get_free_pid: get a free process or group id
5 * no_sys: called for invalid system call numbers
6 * find_param: look up a boot monitor parameter
7 * find_proc: return process pointer from pid number
8 * nice_to_priority convert nice level to priority queue
9 * pm_isokendpt: check the validity of an endpoint
10 * tell_vfs: send a request to VFS on behalf of a process
13 #include "pm.h"
14 #include <sys/resource.h>
15 #include <sys/stat.h>
16 #include <minix/callnr.h>
17 #include <minix/com.h>
18 #include <minix/endpoint.h>
19 #include <fcntl.h>
20 #include <signal.h> /* needed only because mproc.h needs it */
21 #include "mproc.h"
22 #include "param.h"
24 #include <minix/config.h>
25 #include <timers.h>
26 #include <string.h>
27 #include <machine/archtypes.h>
28 #include "kernel/const.h"
29 #include "kernel/config.h"
30 #include "kernel/type.h"
31 #include "kernel/proc.h"
33 /*===========================================================================*
34 * get_free_pid *
35 *===========================================================================*/
36 pid_t get_free_pid()
38 static pid_t next_pid = INIT_PID + 1; /* next pid to be assigned */
39 register struct mproc *rmp; /* check process table */
40 int t; /* zero if pid still free */
42 /* Find a free pid for the child and put it in the table. */
43 do {
44 t = 0;
45 next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
46 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
47 if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
48 t = 1;
49 break;
51 } while (t); /* 't' = 0 means pid free */
52 return(next_pid);
56 /*===========================================================================*
57 * no_sys *
58 *===========================================================================*/
59 int no_sys()
61 /* A system call number not implemented by PM has been requested. */
62 return(ENOSYS);
65 /*===========================================================================*
66 * find_param *
67 *===========================================================================*/
68 char *find_param(name)
69 const char *name;
71 register const char *namep;
72 register char *envp;
74 for (envp = (char *) monitor_params; *envp != 0;) {
75 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
77 if (*namep == '\0' && *envp == '=')
78 return(envp + 1);
79 while (*envp++ != 0)
82 return(NULL);
85 /*===========================================================================*
86 * find_proc *
87 *===========================================================================*/
88 struct mproc *find_proc(lpid)
89 pid_t lpid;
91 register struct mproc *rmp;
93 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
94 if ((rmp->mp_flags & IN_USE) && rmp->mp_pid == lpid)
95 return(rmp);
97 return(NULL);
100 /*===========================================================================*
101 * nice_to_priority *
102 *===========================================================================*/
103 int nice_to_priority(int nice, unsigned* new_q)
105 if (nice < PRIO_MIN || nice > PRIO_MAX) return(EINVAL);
107 *new_q = MAX_USER_Q + (nice-PRIO_MIN) * (MIN_USER_Q-MAX_USER_Q+1) /
108 (PRIO_MAX-PRIO_MIN+1);
110 /* Neither of these should ever happen. */
111 if ((signed) *new_q < MAX_USER_Q) *new_q = MAX_USER_Q;
112 if (*new_q > MIN_USER_Q) *new_q = MIN_USER_Q;
114 return (OK);
117 /*===========================================================================*
118 * pm_isokendpt *
119 *===========================================================================*/
120 int pm_isokendpt(int endpoint, int *proc)
122 *proc = _ENDPOINT_P(endpoint);
123 if(*proc < -NR_TASKS || *proc >= NR_PROCS)
124 return EINVAL;
125 if(*proc >= 0 && endpoint != mproc[*proc].mp_endpoint)
126 return EDEADEPT;
127 if(*proc >= 0 && !(mproc[*proc].mp_flags & IN_USE))
128 return EDEADEPT;
129 return OK;
132 /*===========================================================================*
133 * tell_vfs *
134 *===========================================================================*/
135 void tell_vfs(rmp, m_ptr)
136 struct mproc *rmp;
137 message *m_ptr;
139 /* Send a request to VFS, without blocking.
141 int r;
143 if (rmp->mp_flags & VFS_CALL)
144 panic("tell_vfs: not idle: %d", m_ptr->m_type);
146 r = asynsend3(VFS_PROC_NR, m_ptr, AMF_NOREPLY);
147 if (r != OK)
148 panic("unable to send to VFS: %d", r);
150 rmp->mp_flags |= VFS_CALL;