tty: don't use custom kputc; this fixes tty printf()s.
[minix.git] / servers / pm / utility.c
blob66ee1fac0a81d4540f85f94e88388215add57931
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 * pm_isokendpt: check the validity of an endpoint
9 * tell_fs: send a request to FS on behalf of a process
12 #include "pm.h"
13 #include <sys/stat.h>
14 #include <minix/callnr.h>
15 #include <minix/com.h>
16 #include <minix/endpoint.h>
17 #include <fcntl.h>
18 #include <signal.h> /* needed only because mproc.h needs it */
19 #include "mproc.h"
20 #include "param.h"
22 #include <minix/config.h>
23 #include <timers.h>
24 #include <string.h>
25 #include <machine/archtypes.h>
26 #include "kernel/const.h"
27 #include "kernel/config.h"
28 #include "kernel/type.h"
29 #include "kernel/proc.h"
31 #define munmap _munmap
32 #define munmap_text _munmap_text
33 #include <sys/mman.h>
34 #undef munmap
35 #undef munmap_text
37 /*===========================================================================*
38 * get_free_pid *
39 *===========================================================================*/
40 PUBLIC pid_t get_free_pid()
42 static pid_t next_pid = INIT_PID + 1; /* next pid to be assigned */
43 register struct mproc *rmp; /* check process table */
44 int t; /* zero if pid still free */
46 /* Find a free pid for the child and put it in the table. */
47 do {
48 t = 0;
49 next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
50 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
51 if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
52 t = 1;
53 break;
55 } while (t); /* 't' = 0 means pid free */
56 return(next_pid);
60 /*===========================================================================*
61 * no_sys *
62 *===========================================================================*/
63 PUBLIC int no_sys()
65 /* A system call number not implemented by PM has been requested. */
66 printf("PM: in no_sys, call nr %d from %d\n", call_nr, who_e);
67 return(ENOSYS);
70 /*===========================================================================*
71 * find_param *
72 *===========================================================================*/
73 PUBLIC char *find_param(name)
74 const char *name;
76 register const char *namep;
77 register char *envp;
79 for (envp = (char *) monitor_params; *envp != 0;) {
80 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
82 if (*namep == '\0' && *envp == '=')
83 return(envp + 1);
84 while (*envp++ != 0)
87 return(NULL);
90 /*===========================================================================*
91 * find_proc *
92 *===========================================================================*/
93 PUBLIC struct mproc *find_proc(lpid)
94 pid_t lpid;
96 register struct mproc *rmp;
98 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
99 if ((rmp->mp_flags & IN_USE) && rmp->mp_pid == lpid)
100 return(rmp);
102 return(NIL_MPROC);
105 /*===========================================================================*
106 * pm_isokendpt *
107 *===========================================================================*/
108 PUBLIC int pm_isokendpt(int endpoint, int *proc)
110 *proc = _ENDPOINT_P(endpoint);
111 if(*proc < -NR_TASKS || *proc >= NR_PROCS)
112 return EINVAL;
113 if(*proc >= 0 && endpoint != mproc[*proc].mp_endpoint)
114 return EDEADSRCDST;
115 if(*proc >= 0 && !(mproc[*proc].mp_flags & IN_USE))
116 return EDEADSRCDST;
117 return OK;
120 /*===========================================================================*
121 * tell_fs *
122 *===========================================================================*/
123 PUBLIC void tell_fs(rmp, m_ptr)
124 struct mproc *rmp;
125 message *m_ptr;
127 /* Send a request to VFS, without blocking.
129 int r;
131 if (rmp->mp_flags & FS_CALL)
132 panic("tell_fs: not idle: %d", m_ptr->m_type);
134 r = asynsend3(FS_PROC_NR, m_ptr, AMF_NOREPLY);
135 if (r != OK)
136 panic("unable to send to FS: %d", r);
138 rmp->mp_flags |= FS_CALL;
141 int unmap_ok = 0;
143 PUBLIC int munmap(void *addrstart, vir_bytes len)
145 if(!unmap_ok)
146 return ENOSYS;
148 return _munmap(addrstart, len);
151 PUBLIC int munmap_text(void *addrstart, vir_bytes len)
153 if(!unmap_ok)
154 return ENOSYS;
156 return _munmap_text(addrstart, len);