xref: /minix3/minix/servers/pm/utility.c (revision 637f688f0d4ccff0fa8a6ddafcd1d89fdfbc462d)
1433d6423SLionel Sambuc /* This file contains some utility routines for PM.
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * The entry points are:
4433d6423SLionel Sambuc  *   get_free_pid:	get a free process or group id
5433d6423SLionel Sambuc  *   find_param:	look up a boot monitor parameter
6433d6423SLionel Sambuc  *   find_proc:		return process pointer from pid number
7433d6423SLionel Sambuc  *   nice_to_priority	convert nice level to priority queue
8433d6423SLionel Sambuc  *   pm_isokendpt:	check the validity of an endpoint
9433d6423SLionel Sambuc  *   tell_vfs:		send a request to VFS on behalf of a process
1029346ab0SDavid van Moolenbroek  *   set_rusage_times:	store user and system times in rusage structure
11433d6423SLionel Sambuc  */
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc #include "pm.h"
14433d6423SLionel Sambuc #include <sys/resource.h>
15433d6423SLionel Sambuc #include <sys/stat.h>
16433d6423SLionel Sambuc #include <minix/callnr.h>
17433d6423SLionel Sambuc #include <minix/com.h>
18433d6423SLionel Sambuc #include <minix/endpoint.h>
19433d6423SLionel Sambuc #include <fcntl.h>
20433d6423SLionel Sambuc #include <signal.h>		/* needed only because mproc.h needs it */
21433d6423SLionel Sambuc #include "mproc.h"
22433d6423SLionel Sambuc 
23433d6423SLionel Sambuc #include <minix/config.h>
24433d6423SLionel Sambuc #include <minix/timers.h>
25433d6423SLionel Sambuc #include <machine/archtypes.h>
26433d6423SLionel Sambuc #include "kernel/const.h"
27433d6423SLionel Sambuc #include "kernel/config.h"
28433d6423SLionel Sambuc #include "kernel/type.h"
29433d6423SLionel Sambuc #include "kernel/proc.h"
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc /*===========================================================================*
32433d6423SLionel Sambuc  *				get_free_pid				     *
33433d6423SLionel Sambuc  *===========================================================================*/
get_free_pid()34433d6423SLionel Sambuc pid_t get_free_pid()
35433d6423SLionel Sambuc {
36433d6423SLionel Sambuc   static pid_t next_pid = INIT_PID + 1;		/* next pid to be assigned */
37433d6423SLionel Sambuc   register struct mproc *rmp;			/* check process table */
38433d6423SLionel Sambuc   int t;					/* zero if pid still free */
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc   /* Find a free pid for the child and put it in the table. */
41433d6423SLionel Sambuc   do {
42433d6423SLionel Sambuc 	t = 0;
43433d6423SLionel Sambuc 	next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
44433d6423SLionel Sambuc 	for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
45433d6423SLionel Sambuc 		if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
46433d6423SLionel Sambuc 			t = 1;
47433d6423SLionel Sambuc 			break;
48433d6423SLionel Sambuc 		}
49433d6423SLionel Sambuc   } while (t);					/* 't' = 0 means pid free */
50433d6423SLionel Sambuc   return(next_pid);
51433d6423SLionel Sambuc }
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc /*===========================================================================*
54433d6423SLionel Sambuc  *				find_param				     *
55433d6423SLionel Sambuc  *===========================================================================*/
56*637f688fSRichard Sailer char *
find_param(const char * name)57*637f688fSRichard Sailer find_param(const char *name)
58433d6423SLionel Sambuc {
59433d6423SLionel Sambuc   register const char *namep;
60433d6423SLionel Sambuc   register char *envp;
61433d6423SLionel Sambuc 
62433d6423SLionel Sambuc   for (envp = (char *) monitor_params; *envp != 0;) {
63433d6423SLionel Sambuc 	for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
64433d6423SLionel Sambuc 		;
65433d6423SLionel Sambuc 	if (*namep == '\0' && *envp == '=')
66433d6423SLionel Sambuc 		return(envp + 1);
67433d6423SLionel Sambuc 	while (*envp++ != 0)
68433d6423SLionel Sambuc 		;
69433d6423SLionel Sambuc   }
70433d6423SLionel Sambuc   return(NULL);
71433d6423SLionel Sambuc }
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc /*===========================================================================*
74433d6423SLionel Sambuc  *				find_proc  				     *
75433d6423SLionel Sambuc  *===========================================================================*/
find_proc(lpid)76433d6423SLionel Sambuc struct mproc *find_proc(lpid)
77433d6423SLionel Sambuc pid_t lpid;
78433d6423SLionel Sambuc {
79433d6423SLionel Sambuc   register struct mproc *rmp;
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc   for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
82433d6423SLionel Sambuc 	if ((rmp->mp_flags & IN_USE) && rmp->mp_pid == lpid)
83433d6423SLionel Sambuc 		return(rmp);
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc   return(NULL);
86433d6423SLionel Sambuc }
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc /*===========================================================================*
89433d6423SLionel Sambuc  *				nice_to_priority			     *
90433d6423SLionel Sambuc  *===========================================================================*/
nice_to_priority(int nice,unsigned * new_q)91433d6423SLionel Sambuc int nice_to_priority(int nice, unsigned* new_q)
92433d6423SLionel Sambuc {
93433d6423SLionel Sambuc 	if (nice < PRIO_MIN || nice > PRIO_MAX) return(EINVAL);
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc 	*new_q = MAX_USER_Q + (nice-PRIO_MIN) * (MIN_USER_Q-MAX_USER_Q+1) /
96433d6423SLionel Sambuc 	    (PRIO_MAX-PRIO_MIN+1);
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc 	/* Neither of these should ever happen. */
99433d6423SLionel Sambuc 	if ((signed) *new_q < MAX_USER_Q) *new_q = MAX_USER_Q;
100433d6423SLionel Sambuc 	if (*new_q > MIN_USER_Q) *new_q = MIN_USER_Q;
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc 	return (OK);
103433d6423SLionel Sambuc }
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc /*===========================================================================*
106433d6423SLionel Sambuc  *				pm_isokendpt			 	     *
107433d6423SLionel Sambuc  *===========================================================================*/
pm_isokendpt(int endpoint,int * proc)108433d6423SLionel Sambuc int pm_isokendpt(int endpoint, int *proc)
109433d6423SLionel Sambuc {
110433d6423SLionel Sambuc 	*proc = _ENDPOINT_P(endpoint);
111433d6423SLionel Sambuc 	if (*proc < 0 || *proc >= NR_PROCS)
112433d6423SLionel Sambuc 		return EINVAL;
113433d6423SLionel Sambuc 	if (endpoint != mproc[*proc].mp_endpoint)
114433d6423SLionel Sambuc 		return EDEADEPT;
115433d6423SLionel Sambuc 	if (!(mproc[*proc].mp_flags & IN_USE))
116433d6423SLionel Sambuc 		return EDEADEPT;
117433d6423SLionel Sambuc 	return OK;
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc /*===========================================================================*
121433d6423SLionel Sambuc  *				tell_vfs			 	     *
122433d6423SLionel Sambuc  *===========================================================================*/
tell_vfs(rmp,m_ptr)123433d6423SLionel Sambuc void tell_vfs(rmp, m_ptr)
124433d6423SLionel Sambuc struct mproc *rmp;
125433d6423SLionel Sambuc message *m_ptr;
126433d6423SLionel Sambuc {
127433d6423SLionel Sambuc /* Send a request to VFS, without blocking.
128433d6423SLionel Sambuc  */
129433d6423SLionel Sambuc   int r;
130433d6423SLionel Sambuc 
131910831cbSDavid van Moolenbroek   if (rmp->mp_flags & (VFS_CALL | EVENT_CALL))
132433d6423SLionel Sambuc 	panic("tell_vfs: not idle: %d", m_ptr->m_type);
133433d6423SLionel Sambuc 
134433d6423SLionel Sambuc   r = asynsend3(VFS_PROC_NR, m_ptr, AMF_NOREPLY);
135433d6423SLionel Sambuc   if (r != OK)
136433d6423SLionel Sambuc   	panic("unable to send to VFS: %d", r);
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc   rmp->mp_flags |= VFS_CALL;
139433d6423SLionel Sambuc }
14029346ab0SDavid van Moolenbroek 
14129346ab0SDavid van Moolenbroek /*===========================================================================*
14229346ab0SDavid van Moolenbroek  *				set_rusage_times		 	     *
14329346ab0SDavid van Moolenbroek  *===========================================================================*/
14429346ab0SDavid van Moolenbroek void
set_rusage_times(struct rusage * r_usage,clock_t user_time,clock_t sys_time)14529346ab0SDavid van Moolenbroek set_rusage_times(struct rusage * r_usage, clock_t user_time, clock_t sys_time)
14629346ab0SDavid van Moolenbroek {
14729346ab0SDavid van Moolenbroek 	u64_t usec;
14829346ab0SDavid van Moolenbroek 
14929346ab0SDavid van Moolenbroek 	usec = user_time * 1000000 / sys_hz();
15029346ab0SDavid van Moolenbroek 	r_usage->ru_utime.tv_sec = usec / 1000000;
15129346ab0SDavid van Moolenbroek 	r_usage->ru_utime.tv_usec = usec % 1000000;
15229346ab0SDavid van Moolenbroek 
15329346ab0SDavid van Moolenbroek 	usec = sys_time * 1000000 / sys_hz();
15429346ab0SDavid van Moolenbroek 	r_usage->ru_stime.tv_sec = usec / 1000000;
15529346ab0SDavid van Moolenbroek 	r_usage->ru_stime.tv_usec = usec % 1000000;
15629346ab0SDavid van Moolenbroek }
157