1 /* This file contains some utility routines for PM. 2 * 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 11 */ 12 13 #include "pm.h" 14 #include <sys/resource.h> 15 #include <sys/stat.h> 16 #include <minix/callnr.h> 17 #include <minix/com.h> 18 #include <minix/endpoint.h> 19 #include <fcntl.h> 20 #include <signal.h> /* needed only because mproc.h needs it */ 21 #include "mproc.h" 22 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" 30 31 /*===========================================================================* 32 * get_free_pid * 33 *===========================================================================*/ 34 pid_t get_free_pid() 35 { 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 */ 39 40 /* Find a free pid for the child and put it in the table. */ 41 do { 42 t = 0; 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) { 46 t = 1; 47 break; 48 } 49 } while (t); /* 't' = 0 means pid free */ 50 return(next_pid); 51 } 52 53 /*===========================================================================* 54 * find_param * 55 *===========================================================================*/ 56 char *find_param(name) 57 const char *name; 58 { 59 register const char *namep; 60 register char *envp; 61 62 for (envp = (char *) monitor_params; *envp != 0;) { 63 for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++) 64 ; 65 if (*namep == '\0' && *envp == '=') 66 return(envp + 1); 67 while (*envp++ != 0) 68 ; 69 } 70 return(NULL); 71 } 72 73 /*===========================================================================* 74 * find_proc * 75 *===========================================================================*/ 76 struct mproc *find_proc(lpid) 77 pid_t lpid; 78 { 79 register struct mproc *rmp; 80 81 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) 82 if ((rmp->mp_flags & IN_USE) && rmp->mp_pid == lpid) 83 return(rmp); 84 85 return(NULL); 86 } 87 88 /*===========================================================================* 89 * nice_to_priority * 90 *===========================================================================*/ 91 int nice_to_priority(int nice, unsigned* new_q) 92 { 93 if (nice < PRIO_MIN || nice > PRIO_MAX) return(EINVAL); 94 95 *new_q = MAX_USER_Q + (nice-PRIO_MIN) * (MIN_USER_Q-MAX_USER_Q+1) / 96 (PRIO_MAX-PRIO_MIN+1); 97 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; 101 102 return (OK); 103 } 104 105 /*===========================================================================* 106 * pm_isokendpt * 107 *===========================================================================*/ 108 int pm_isokendpt(int endpoint, int *proc) 109 { 110 *proc = _ENDPOINT_P(endpoint); 111 if (*proc < 0 || *proc >= NR_PROCS) 112 return EINVAL; 113 if (endpoint != mproc[*proc].mp_endpoint) 114 return EDEADEPT; 115 if (!(mproc[*proc].mp_flags & IN_USE)) 116 return EDEADEPT; 117 return OK; 118 } 119 120 /*===========================================================================* 121 * tell_vfs * 122 *===========================================================================*/ 123 void tell_vfs(rmp, m_ptr) 124 struct mproc *rmp; 125 message *m_ptr; 126 { 127 /* Send a request to VFS, without blocking. 128 */ 129 int r; 130 131 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) 132 panic("tell_vfs: not idle: %d", m_ptr->m_type); 133 134 r = asynsend3(VFS_PROC_NR, m_ptr, AMF_NOREPLY); 135 if (r != OK) 136 panic("unable to send to VFS: %d", r); 137 138 rmp->mp_flags |= VFS_CALL; 139 } 140 141 /*===========================================================================* 142 * set_rusage_times * 143 *===========================================================================*/ 144 void 145 set_rusage_times(struct rusage * r_usage, clock_t user_time, clock_t sys_time) 146 { 147 u64_t usec; 148 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; 152 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; 156 } 157