source: trunk/minix/servers/pm/utility.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 7.5 KB
Line 
1/* This file contains some utility routines for PM.
2 *
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 * tell_fs: interface to FS
10 * get_mem_map: get memory map of given process
11 * get_stack_ptr: get stack pointer of given process
12 * proc_from_pid: return process pointer from pid number
13 */
14
15#include "pm.h"
16#include <sys/stat.h>
17#include <minix/callnr.h>
18#include <minix/com.h>
19#include <minix/endpoint.h>
20#include <fcntl.h>
21#include <signal.h> /* needed only because mproc.h needs it */
22#include "mproc.h"
23#include "param.h"
24
25#include <minix/config.h>
26#include <timers.h>
27#include <string.h>
28#include "../../kernel/const.h"
29#include "../../kernel/config.h"
30#include "../../kernel/type.h"
31#include "../../kernel/proc.h"
32
33/*===========================================================================*
34 * get_free_pid *
35 *===========================================================================*/
36PUBLIC pid_t get_free_pid()
37{
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 */
41
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;
50 }
51 } while (t); /* 't' = 0 means pid free */
52 return(next_pid);
53}
54
55/*===========================================================================*
56 * allowed *
57 *===========================================================================*/
58PUBLIC int allowed(name_buf, s_buf, mask)
59char *name_buf; /* pointer to file name to be EXECed */
60struct stat *s_buf; /* buffer for doing and returning stat struct*/
61int mask; /* R_BIT, W_BIT, or X_BIT */
62{
63/* Check to see if file can be accessed. Return EACCES or ENOENT if the access
64 * is prohibited. If it is legal open the file and return a file descriptor.
65 */
66 int fd;
67 int save_errno;
68
69 /* Use the fact that mask for access() is the same as the permissions mask.
70 * E.g., X_BIT in <minix/const.h> is the same as X_OK in <unistd.h> and
71 * S_IXOTH in <sys/stat.h>. tell_fs(DO_CHDIR, ...) has set PM's real ids
72 * to the user's effective ids, so access() works right for setuid programs.
73 */
74 if (access(name_buf, mask) < 0) return(-errno);
75
76 /* The file is accessible but might not be readable. Make it readable. */
77 tell_fs(SETUID, PM_PROC_NR, (int) SUPER_USER, (int) SUPER_USER);
78
79 /* Open the file and fstat it. Restore the ids early to handle errors. */
80 fd = open(name_buf, O_RDONLY | O_NONBLOCK);
81 save_errno = errno; /* open might fail, e.g. from ENFILE */
82 tell_fs(SETUID, PM_PROC_NR, (int) mp->mp_effuid, (int) mp->mp_effuid);
83 if (fd < 0) return(-save_errno);
84 if (fstat(fd, s_buf) < 0) panic(__FILE__,"allowed: fstat failed", NO_NUM);
85
86 /* Only regular files can be executed. */
87 if (mask == X_BIT && (s_buf->st_mode & I_TYPE) != I_REGULAR) {
88 close(fd);
89 return(EACCES);
90 }
91 return(fd);
92}
93
94/*===========================================================================*
95 * no_sys *
96 *===========================================================================*/
97PUBLIC int no_sys()
98{
99/* A system call number not implemented by PM has been requested. */
100
101 return(ENOSYS);
102}
103
104/*===========================================================================*
105 * panic *
106 *===========================================================================*/
107PUBLIC void panic(who, mess, num)
108char *who; /* who caused the panic */
109char *mess; /* panic message string */
110int num; /* number to go with it */
111{
112/* An unrecoverable error has occurred. Panics are caused when an internal
113 * inconsistency is detected, e.g., a programming error or illegal value of a
114 * defined constant. The process manager decides to exit.
115 */
116 message m;
117 int s;
118
119 /* Switch to primary console and print panic message. */
120 check_sig(mproc[TTY_PROC_NR].mp_pid, SIGTERM);
121 printf("PM panic (%s): %s", who, mess);
122 if (num != NO_NUM) printf(": %d",num);
123 printf("\n");
124
125 /* Exit PM. */
126 sys_exit(SELF);
127}
128
129/*===========================================================================*
130 * tell_fs *
131 *===========================================================================*/
132PUBLIC void tell_fs(what, p1, p2, p3)
133int what, p1, p2, p3;
134{
135/* This routine is only used by PM to inform FS of certain events:
136 * tell_fs(CHDIR, slot, dir, 0)
137 * tell_fs(EXEC, proc, 0, 0)
138 * tell_fs(EXIT, proc, 0, 0)
139 * tell_fs(FORK, parent, child, pid)
140 * tell_fs(SETGID, proc, realgid, effgid)
141 * tell_fs(SETSID, proc, 0, 0)
142 * tell_fs(SETUID, proc, realuid, effuid)
143 * tell_fs(UNPAUSE, proc, signr, 0)
144 * tell_fs(STIME, time, 0, 0)
145 * Ignore this call if the FS is already dead, e.g. on shutdown.
146 */
147 message m;
148
149 if ((mproc[FS_PROC_NR].mp_flags & (IN_USE|ZOMBIE)) != IN_USE)
150 return;
151
152 m.tell_fs_arg1 = p1;
153 m.tell_fs_arg2 = p2;
154 m.tell_fs_arg3 = p3;
155 _taskcall(FS_PROC_NR, what, &m);
156}
157
158/*===========================================================================*
159 * find_param *
160 *===========================================================================*/
161PUBLIC char *find_param(name)
162const char *name;
163{
164 register const char *namep;
165 register char *envp;
166
167 for (envp = (char *) monitor_params; *envp != 0;) {
168 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
169 ;
170 if (*namep == '\0' && *envp == '=')
171 return(envp + 1);
172 while (*envp++ != 0)
173 ;
174 }
175 return(NULL);
176}
177
178/*===========================================================================*
179 * get_mem_map *
180 *===========================================================================*/
181PUBLIC int get_mem_map(proc_nr, mem_map)
182int proc_nr; /* process to get map of */
183struct mem_map *mem_map; /* put memory map here */
184{
185 struct proc p;
186 int s;
187
188 if ((s=sys_getproc(&p, proc_nr)) != OK)
189 return(s);
190 memcpy(mem_map, p.p_memmap, sizeof(p.p_memmap));
191 return(OK);
192}
193
194/*===========================================================================*
195 * get_stack_ptr *
196 *===========================================================================*/
197PUBLIC int get_stack_ptr(proc_nr_e, sp)
198int proc_nr_e; /* process to get sp of */
199vir_bytes *sp; /* put stack pointer here */
200{
201 struct proc p;
202 int s;
203
204 if ((s=sys_getproc(&p, proc_nr_e)) != OK)
205 return(s);
206 *sp = p.p_reg.sp;
207 return(OK);
208}
209
210/*===========================================================================*
211 * proc_from_pid *
212 *===========================================================================*/
213PUBLIC int proc_from_pid(mp_pid)
214pid_t mp_pid;
215{
216 int rmp;
217
218 for (rmp = 0; rmp < NR_PROCS; rmp++)
219 if (mproc[rmp].mp_pid == mp_pid)
220 return rmp;
221
222 return -1;
223}
224
225/*===========================================================================*
226 * pm_isokendpt *
227 *===========================================================================*/
228PUBLIC int pm_isokendpt(int endpoint, int *proc)
229{
230 *proc = _ENDPOINT_P(endpoint);
231 if(*proc < -NR_TASKS || *proc >= NR_PROCS)
232 return EINVAL;
233 if(*proc >= 0 && endpoint != mproc[*proc].mp_endpoint)
234 return EDEADSRCDST;
235 if(*proc >= 0 && !(mproc[*proc].mp_flags & IN_USE))
236 return EDEADSRCDST;
237 return OK;
238}
239
Note: See TracBrowser for help on using the repository browser.