Remove building with NOCRYPTO option
[minix.git] / minix / servers / pm / main.c
blob15eff637d291327944ea0c9e04737d69717d9145
1 /* This file contains the main program of the process manager and some related
2 * procedures. When MINIX starts up, the kernel runs for a little while,
3 * initializing itself and its tasks, and then it runs PM and VFS. Both PM
4 * and VFS initialize themselves as far as they can. PM asks the kernel for
5 * all free memory and starts serving requests.
7 * The entry points into this file are:
8 * main: starts PM running
9 * reply: send a reply to a process making a PM system call
12 #include "pm.h"
13 #include <minix/callnr.h>
14 #include <minix/com.h>
15 #include <minix/ds.h>
16 #include <minix/endpoint.h>
17 #include <minix/minlib.h>
18 #include <minix/type.h>
19 #include <minix/vm.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <sys/resource.h>
24 #include <sys/utsname.h>
25 #include <sys/wait.h>
26 #include <machine/archtypes.h>
27 #include <assert.h>
28 #include "mproc.h"
30 #include "kernel/const.h"
31 #include "kernel/config.h"
32 #include "kernel/proc.h"
34 #if ENABLE_SYSCALL_STATS
35 EXTERN unsigned long calls_stats[NR_PM_CALLS];
36 #endif
38 static int get_nice_value(int queue);
39 static void handle_vfs_reply(void);
41 /* SEF functions and variables. */
42 static void sef_local_startup(void);
43 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
45 /*===========================================================================*
46 * main *
47 *===========================================================================*/
48 int
49 main(void)
51 /* Main routine of the process manager. */
52 unsigned int call_index;
53 int ipc_status, result;
55 /* SEF local startup. */
56 sef_local_startup();
58 /* This is PM's main loop- get work and do it, forever and forever. */
59 while (TRUE) {
60 /* Wait for the next message. */
61 if (sef_receive_status(ANY, &m_in, &ipc_status) != OK)
62 panic("PM sef_receive_status error");
64 /* Check for system notifications first. Special cases. */
65 if (is_ipc_notify(ipc_status)) {
66 if (_ENDPOINT_P(m_in.m_source) == CLOCK)
67 expire_timers(m_in.m_notify.timestamp);
69 /* done, continue */
70 continue;
73 /* Extract useful information from the message. */
74 who_e = m_in.m_source; /* who sent the message */
75 if (pm_isokendpt(who_e, &who_p) != OK)
76 panic("PM got message from invalid endpoint: %d", who_e);
77 mp = &mproc[who_p]; /* process slot of caller */
78 call_nr = m_in.m_type; /* system call number */
80 /* Drop delayed calls from exiting processes. */
81 if (mp->mp_flags & EXITING)
82 continue;
84 if (IS_VFS_PM_RS(call_nr) && who_e == VFS_PROC_NR) {
85 handle_vfs_reply();
87 result = SUSPEND; /* don't reply */
88 } else if (call_nr == PROC_EVENT_REPLY) {
89 result = do_proc_event_reply();
90 } else if (IS_PM_CALL(call_nr)) {
91 /* If the system call number is valid, perform the call. */
92 call_index = (unsigned int) (call_nr - PM_BASE);
94 if (call_index < NR_PM_CALLS && call_vec[call_index] != NULL) {
95 #if ENABLE_SYSCALL_STATS
96 calls_stats[call_index]++;
97 #endif
99 result = (*call_vec[call_index])();
100 } else
101 result = ENOSYS;
102 } else
103 result = ENOSYS;
105 /* Send reply. */
106 if (result != SUSPEND) reply(who_p, result);
108 return(OK);
111 /*===========================================================================*
112 * sef_local_startup *
113 *===========================================================================*/
114 static void
115 sef_local_startup(void)
117 /* Register init callbacks. */
118 sef_setcb_init_fresh(sef_cb_init_fresh);
119 sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
121 /* Register signal callbacks. */
122 sef_setcb_signal_manager(process_ksig);
124 /* Let SEF perform startup. */
125 sef_startup();
128 /*===========================================================================*
129 * sef_cb_init_fresh *
130 *===========================================================================*/
131 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
133 /* Initialize the process manager. */
134 int s;
135 static struct boot_image image[NR_BOOT_PROCS];
136 register struct boot_image *ip;
137 static char core_sigs[] = { SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
138 SIGEMT, SIGFPE, SIGBUS, SIGSEGV };
139 static char ign_sigs[] = { SIGCHLD, SIGWINCH, SIGCONT, SIGINFO };
140 static char noign_sigs[] = { SIGILL, SIGTRAP, SIGEMT, SIGFPE,
141 SIGBUS, SIGSEGV };
142 register struct mproc *rmp;
143 register char *sig_ptr;
144 message mess;
146 /* Initialize process table, including timers. */
147 for (rmp=&mproc[0]; rmp<&mproc[NR_PROCS]; rmp++) {
148 init_timer(&rmp->mp_timer);
149 rmp->mp_magic = MP_MAGIC;
150 rmp->mp_sigact = mpsigact[rmp - mproc];
151 rmp->mp_eventsub = NO_EVENTSUB;
154 /* Build the set of signals which cause core dumps, and the set of signals
155 * that are by default ignored.
157 sigemptyset(&core_sset);
158 for (sig_ptr = core_sigs; sig_ptr < core_sigs+sizeof(core_sigs); sig_ptr++)
159 sigaddset(&core_sset, *sig_ptr);
160 sigemptyset(&ign_sset);
161 for (sig_ptr = ign_sigs; sig_ptr < ign_sigs+sizeof(ign_sigs); sig_ptr++)
162 sigaddset(&ign_sset, *sig_ptr);
163 sigemptyset(&noign_sset);
164 for (sig_ptr = noign_sigs; sig_ptr < noign_sigs+sizeof(noign_sigs); sig_ptr++)
165 sigaddset(&noign_sset, *sig_ptr);
167 /* Obtain a copy of the boot monitor parameters.
169 if ((s=sys_getmonparams(monitor_params, sizeof(monitor_params))) != OK)
170 panic("get monitor params failed: %d", s);
172 /* Initialize PM's process table. Request a copy of the system image table
173 * that is defined at the kernel level to see which slots to fill in.
175 if (OK != (s=sys_getimage(image)))
176 panic("couldn't get image table: %d", s);
177 procs_in_use = 0; /* start populating table */
178 for (ip = &image[0]; ip < &image[NR_BOOT_PROCS]; ip++) {
179 if (ip->proc_nr >= 0) { /* task have negative nrs */
180 procs_in_use += 1; /* found user process */
182 /* Set process details found in the image table. */
183 rmp = &mproc[ip->proc_nr];
184 strlcpy(rmp->mp_name, ip->proc_name, PROC_NAME_LEN);
185 (void) sigemptyset(&rmp->mp_ignore);
186 (void) sigemptyset(&rmp->mp_sigmask);
187 (void) sigemptyset(&rmp->mp_catch);
188 if (ip->proc_nr == INIT_PROC_NR) { /* user process */
189 /* INIT is root, we make it father of itself. This is
190 * not really OK, INIT should have no father, i.e.
191 * a father with pid NO_PID. But PM currently assumes
192 * that mp_parent always points to a valid slot number.
194 rmp->mp_parent = INIT_PROC_NR;
195 rmp->mp_procgrp = rmp->mp_pid = INIT_PID;
196 rmp->mp_flags |= IN_USE;
198 /* Set scheduling info */
199 rmp->mp_scheduler = KERNEL;
200 rmp->mp_nice = get_nice_value(USR_Q);
202 else { /* system process */
203 if(ip->proc_nr == RS_PROC_NR) {
204 rmp->mp_parent = INIT_PROC_NR;
206 else {
207 rmp->mp_parent = RS_PROC_NR;
209 rmp->mp_pid = get_free_pid();
210 rmp->mp_flags |= IN_USE | PRIV_PROC;
212 /* RS schedules this process */
213 rmp->mp_scheduler = NONE;
214 rmp->mp_nice = get_nice_value(SRV_Q);
217 /* Get kernel endpoint identifier. */
218 rmp->mp_endpoint = ip->endpoint;
220 /* Tell VFS about this system process. */
221 memset(&mess, 0, sizeof(mess));
222 mess.m_type = VFS_PM_INIT;
223 mess.VFS_PM_SLOT = ip->proc_nr;
224 mess.VFS_PM_PID = rmp->mp_pid;
225 mess.VFS_PM_ENDPT = rmp->mp_endpoint;
226 if (OK != (s=ipc_send(VFS_PROC_NR, &mess)))
227 panic("can't sync up with VFS: %d", s);
231 /* Tell VFS that no more system processes follow and synchronize. */
232 memset(&mess, 0, sizeof(mess));
233 mess.m_type = VFS_PM_INIT;
234 mess.VFS_PM_ENDPT = NONE;
235 if (ipc_sendrec(VFS_PROC_NR, &mess) != OK || mess.m_type != OK)
236 panic("can't sync up with VFS");
238 system_hz = sys_hz();
240 /* Initialize user-space scheduling. */
241 sched_init();
243 return(OK);
246 /*===========================================================================*
247 * reply *
248 *===========================================================================*/
249 void
250 reply(
251 int proc_nr, /* process to reply to */
252 int result /* result of call (usually OK or error #) */
255 /* Send a reply to a user process. System calls may occasionally fill in other
256 * fields, this is only for the main return value and for sending the reply.
258 struct mproc *rmp;
259 int r;
261 if(proc_nr < 0 || proc_nr >= NR_PROCS)
262 panic("reply arg out of range: %d", proc_nr);
264 rmp = &mproc[proc_nr];
265 rmp->mp_reply.m_type = result;
267 if ((r = ipc_sendnb(rmp->mp_endpoint, &rmp->mp_reply)) != OK)
268 printf("PM can't reply to %d (%s): %d\n", rmp->mp_endpoint,
269 rmp->mp_name, r);
272 /*===========================================================================*
273 * get_nice_value *
274 *===========================================================================*/
275 static int
276 get_nice_value(
277 int queue /* store mem chunks here */
280 /* Processes in the boot image have a priority assigned. The PM doesn't know
281 * about priorities, but uses 'nice' values instead. The priority is between
282 * MIN_USER_Q and MAX_USER_Q. We have to scale between PRIO_MIN and PRIO_MAX.
284 int nice_val = (queue - USER_Q) * (PRIO_MAX-PRIO_MIN+1) /
285 (MIN_USER_Q-MAX_USER_Q+1);
286 if (nice_val > PRIO_MAX) nice_val = PRIO_MAX; /* shouldn't happen */
287 if (nice_val < PRIO_MIN) nice_val = PRIO_MIN; /* shouldn't happen */
288 return nice_val;
291 /*===========================================================================*
292 * handle_vfs_reply *
293 *===========================================================================*/
294 static void
295 handle_vfs_reply(void)
297 struct mproc *rmp;
298 endpoint_t proc_e;
299 int r, proc_n, new_parent;
301 /* VFS_PM_REBOOT is the only request not associated with a process.
302 * Handle its reply first.
304 if (call_nr == VFS_PM_REBOOT_REPLY) {
305 /* Ask the kernel to abort. All system services, including
306 * the PM, will get a HARD_STOP notification. Await the
307 * notification in the main loop.
309 sys_abort(abort_flag);
311 return;
314 /* Get the process associated with this call */
315 proc_e = m_in.VFS_PM_ENDPT;
317 if (pm_isokendpt(proc_e, &proc_n) != OK) {
318 panic("handle_vfs_reply: got bad endpoint from VFS: %d", proc_e);
321 rmp = &mproc[proc_n];
323 /* Now that VFS replied, mark the process as VFS-idle again */
324 if (!(rmp->mp_flags & VFS_CALL))
325 panic("handle_vfs_reply: reply without request: %d", call_nr);
327 new_parent = rmp->mp_flags & NEW_PARENT;
328 rmp->mp_flags &= ~(VFS_CALL | NEW_PARENT);
330 if (rmp->mp_flags & UNPAUSED)
331 panic("handle_vfs_reply: UNPAUSED set on entry: %d", call_nr);
333 /* Call-specific handler code */
334 switch (call_nr) {
335 case VFS_PM_SETUID_REPLY:
336 case VFS_PM_SETGID_REPLY:
337 case VFS_PM_SETGROUPS_REPLY:
338 /* Wake up the original caller */
339 reply(rmp-mproc, OK);
341 break;
343 case VFS_PM_SETSID_REPLY:
344 /* Wake up the original caller */
345 reply(rmp-mproc, rmp->mp_procgrp);
347 break;
349 case VFS_PM_EXEC_REPLY:
350 exec_restart(rmp, m_in.VFS_PM_STATUS, (vir_bytes)m_in.VFS_PM_PC,
351 (vir_bytes)m_in.VFS_PM_NEWSP,
352 (vir_bytes)m_in.VFS_PM_NEWPS_STR);
354 break;
356 case VFS_PM_CORE_REPLY:
357 if (m_in.VFS_PM_STATUS == OK)
358 rmp->mp_sigstatus |= WCOREFLAG;
360 /* FALLTHROUGH */
361 case VFS_PM_EXIT_REPLY:
362 assert(rmp->mp_flags & EXITING);
364 /* Publish the exit event. Continue exiting the process after that. */
365 publish_event(rmp);
367 return; /* do not take the default action */
369 case VFS_PM_FORK_REPLY:
370 /* Schedule the newly created process ... */
371 r = OK;
372 if (rmp->mp_scheduler != KERNEL && rmp->mp_scheduler != NONE) {
373 r = sched_start_user(rmp->mp_scheduler, rmp);
376 /* If scheduling the process failed, we want to tear down the process
377 * and fail the fork */
378 if (r != OK) {
379 /* Tear down the newly created process */
380 rmp->mp_scheduler = NONE; /* don't try to stop scheduling */
381 exit_proc(rmp, -1, FALSE /*dump_core*/);
383 /* Wake up the parent with a failed fork (unless dead) */
384 if (!new_parent)
385 reply(rmp->mp_parent, -1);
387 else {
388 /* Wake up the child */
389 reply(proc_n, OK);
391 /* Wake up the parent, unless the parent is already dead */
392 if (!new_parent)
393 reply(rmp->mp_parent, rmp->mp_pid);
396 break;
398 case VFS_PM_SRV_FORK_REPLY:
399 /* Nothing to do */
401 break;
403 case VFS_PM_UNPAUSE_REPLY:
404 /* The target process must always be stopped while unpausing; otherwise
405 * it could just end up pausing itself on a new call afterwards.
407 assert(rmp->mp_flags & PROC_STOPPED);
409 /* Process is now unpaused */
410 rmp->mp_flags |= UNPAUSED;
412 /* Publish the signal event. Continue with signals only after that. */
413 publish_event(rmp);
415 return; /* do not take the default action */
417 default:
418 panic("handle_vfs_reply: unknown reply code: %d", call_nr);
421 /* Now that the process is idle again, look at pending signals */
422 if ((rmp->mp_flags & (IN_USE | EXITING)) == IN_USE)
423 restart_sigs(rmp);