source: branches/minix3-book/servers/pm/utility.c@ 4

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

Importazione sorgenti libro

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