. service tells you which device it couldn't stat
[minix3.git] / servers / pm / utility.c
blob23adbdcfe718cf081d63578496b7e04e1d5379f2
1 /* This file contains some utility routines for PM.
3 * The entry points are:
4 * find_param: look up a boot monitor parameter
5 * get_free_pid: get a free process or group id
6 * allowed: see if an access is permitted
7 * no_sys: called for invalid system call numbers
8 * panic: PM has run aground of a fatal error
9 * get_mem_map: get memory map of given process
10 * get_stack_ptr: get stack pointer of given process
11 * proc_from_pid: return process pointer from pid number
14 #include "pm.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 <archconst.h>
28 #include <archtypes.h>
29 #include "../../kernel/const.h"
30 #include "../../kernel/config.h"
31 #include "../../kernel/type.h"
32 #include "../../kernel/proc.h"
34 /*===========================================================================*
35 * get_free_pid *
36 *===========================================================================*/
37 PUBLIC pid_t get_free_pid()
39 static pid_t next_pid = INIT_PID + 1; /* next pid to be assigned */
40 register struct mproc *rmp; /* check process table */
41 int t; /* zero if pid still free */
43 /* Find a free pid for the child and put it in the table. */
44 do {
45 t = 0;
46 next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
47 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
48 if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
49 t = 1;
50 break;
52 } while (t); /* 't' = 0 means pid free */
53 return(next_pid);
57 /*===========================================================================*
58 * no_sys *
59 *===========================================================================*/
60 PUBLIC int no_sys()
62 /* A system call number not implemented by PM has been requested. */
63 printf("PM: in no_sys, call nr %d from %d\n", call_nr, who_e);
64 return(ENOSYS);
67 /*===========================================================================*
68 * panic *
69 *===========================================================================*/
70 PUBLIC void panic(who, mess, num)
71 char *who; /* who caused the panic */
72 char *mess; /* panic message string */
73 int num; /* number to go with it */
75 /* An unrecoverable error has occurred. Panics are caused when an internal
76 * inconsistency is detected, e.g., a programming error or illegal value of a
77 * defined constant. The process manager decides to exit.
79 printf("PM panic (%s): %s", who, mess);
80 if (num != NO_NUM) printf(": %d",num);
81 printf("\n");
83 /* Exit PM. */
84 sys_exit(SELF);
88 /*===========================================================================*
89 * find_param *
90 *===========================================================================*/
91 PUBLIC char *find_param(name)
92 const char *name;
94 register const char *namep;
95 register char *envp;
97 for (envp = (char *) monitor_params; *envp != 0;) {
98 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
100 if (*namep == '\0' && *envp == '=')
101 return(envp + 1);
102 while (*envp++ != 0)
105 return(NULL);
108 /*===========================================================================*
109 * get_mem_map *
110 *===========================================================================*/
111 PUBLIC int get_mem_map(proc_nr, mem_map)
112 int proc_nr; /* process to get map of */
113 struct mem_map *mem_map; /* put memory map here */
115 struct proc p;
116 int s;
118 if ((s=sys_getproc(&p, proc_nr)) != OK)
119 return(s);
120 memcpy(mem_map, p.p_memmap, sizeof(p.p_memmap));
121 return(OK);
124 /*===========================================================================*
125 * get_stack_ptr *
126 *===========================================================================*/
127 PUBLIC int get_stack_ptr(proc_nr_e, sp)
128 int proc_nr_e; /* process to get sp of */
129 vir_bytes *sp; /* put stack pointer here */
131 struct proc p;
132 int s;
134 if ((s=sys_getproc(&p, proc_nr_e)) != OK)
135 return(s);
136 *sp = p.p_reg.sp;
137 return(OK);
140 /*===========================================================================*
141 * proc_from_pid *
142 *===========================================================================*/
143 PUBLIC int proc_from_pid(mp_pid)
144 pid_t mp_pid;
146 int rmp;
148 for (rmp = 0; rmp < NR_PROCS; rmp++)
149 if (mproc[rmp].mp_pid == mp_pid)
150 return rmp;
152 return -1;
155 /*===========================================================================*
156 * pm_isokendpt *
157 *===========================================================================*/
158 PUBLIC int pm_isokendpt(int endpoint, int *proc)
160 *proc = _ENDPOINT_P(endpoint);
161 if(*proc < -NR_TASKS || *proc >= NR_PROCS)
162 return EINVAL;
163 if(*proc >= 0 && endpoint != mproc[*proc].mp_endpoint)
164 return EDEADSRCDST;
165 if(*proc >= 0 && !(mproc[*proc].mp_flags & IN_USE))
166 return EDEADSRCDST;
167 return OK;