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 * find_param: look up a boot monitor parameter
6 * find_proc: return process pointer from pid number
7 * nice_to_priority convert nice level to priority queue
8 * pm_isokendpt: check the validity of an endpoint
9 * tell_vfs: send a request to VFS on behalf of a process
10 * set_rusage_times: store user and system times in rusage structure
14 #include <sys/resource.h>
16 #include <minix/callnr.h>
17 #include <minix/com.h>
18 #include <minix/endpoint.h>
20 #include <signal.h> /* needed only because mproc.h needs it */
23 #include <minix/config.h>
24 #include <minix/timers.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 /*===========================================================================*
33 *===========================================================================*/
36 static pid_t next_pid
= INIT_PID
+ 1; /* next pid to be assigned */
37 register struct mproc
*rmp
; /* check process table */
38 int t
; /* zero if pid still free */
40 /* Find a free pid for the child and put it in the table. */
43 next_pid
= (next_pid
< NR_PIDS
? next_pid
+ 1 : INIT_PID
+ 1);
44 for (rmp
= &mproc
[0]; rmp
< &mproc
[NR_PROCS
]; rmp
++)
45 if (rmp
->mp_pid
== next_pid
|| rmp
->mp_procgrp
== next_pid
) {
49 } while (t
); /* 't' = 0 means pid free */
53 /*===========================================================================*
55 *===========================================================================*/
57 find_param(const char *name
)
59 register const char *namep
;
62 for (envp
= (char *) monitor_params
; *envp
!= 0;) {
63 for (namep
= name
; *namep
!= 0 && *namep
== *envp
; namep
++, envp
++)
65 if (*namep
== '\0' && *envp
== '=')
73 /*===========================================================================*
75 *===========================================================================*/
76 struct mproc
*find_proc(lpid
)
79 register struct mproc
*rmp
;
81 for (rmp
= &mproc
[0]; rmp
< &mproc
[NR_PROCS
]; rmp
++)
82 if ((rmp
->mp_flags
& IN_USE
) && rmp
->mp_pid
== lpid
)
88 /*===========================================================================*
90 *===========================================================================*/
91 int nice_to_priority(int nice
, unsigned* new_q
)
93 if (nice
< PRIO_MIN
|| nice
> PRIO_MAX
) return(EINVAL
);
95 *new_q
= MAX_USER_Q
+ (nice
-PRIO_MIN
) * (MIN_USER_Q
-MAX_USER_Q
+1) /
96 (PRIO_MAX
-PRIO_MIN
+1);
98 /* Neither of these should ever happen. */
99 if ((signed) *new_q
< MAX_USER_Q
) *new_q
= MAX_USER_Q
;
100 if (*new_q
> MIN_USER_Q
) *new_q
= MIN_USER_Q
;
105 /*===========================================================================*
107 *===========================================================================*/
108 int pm_isokendpt(int endpoint
, int *proc
)
110 *proc
= _ENDPOINT_P(endpoint
);
111 if (*proc
< 0 || *proc
>= NR_PROCS
)
113 if (endpoint
!= mproc
[*proc
].mp_endpoint
)
115 if (!(mproc
[*proc
].mp_flags
& IN_USE
))
120 /*===========================================================================*
122 *===========================================================================*/
123 void tell_vfs(rmp
, m_ptr
)
127 /* Send a request to VFS, without blocking.
131 if (rmp
->mp_flags
& (VFS_CALL
| EVENT_CALL
))
132 panic("tell_vfs: not idle: %d", m_ptr
->m_type
);
134 r
= asynsend3(VFS_PROC_NR
, m_ptr
, AMF_NOREPLY
);
136 panic("unable to send to VFS: %d", r
);
138 rmp
->mp_flags
|= VFS_CALL
;
141 /*===========================================================================*
143 *===========================================================================*/
145 set_rusage_times(struct rusage
* r_usage
, clock_t user_time
, clock_t sys_time
)
149 usec
= user_time
* 1000000 / sys_hz();
150 r_usage
->ru_utime
.tv_sec
= usec
/ 1000000;
151 r_usage
->ru_utime
.tv_usec
= usec
% 1000000;
153 usec
= sys_time
* 1000000 / sys_hz();
154 r_usage
->ru_stime
.tv_sec
= usec
/ 1000000;
155 r_usage
->ru_stime
.tv_usec
= usec
% 1000000;