1*433d6423SLionel Sambuc /* This file contains a few general purpose utility routines. 2*433d6423SLionel Sambuc * 3*433d6423SLionel Sambuc * The entry points into this file are 4*433d6423SLionel Sambuc * clock_timespec: ask the clock task for the real time 5*433d6423SLionel Sambuc * copy_path: copy a path name from a path request from userland 6*433d6423SLionel Sambuc * fetch_name: go get a path name from user space 7*433d6423SLionel Sambuc * panic: something awful has occurred; MINIX cannot continue 8*433d6423SLionel Sambuc * in_group: determines if group 'grp' is in rfp->fp_sgroups[] 9*433d6423SLionel Sambuc */ 10*433d6423SLionel Sambuc 11*433d6423SLionel Sambuc #include "fs.h" 12*433d6423SLionel Sambuc #include <minix/callnr.h> 13*433d6423SLionel Sambuc #include <minix/endpoint.h> 14*433d6423SLionel Sambuc #include <unistd.h> 15*433d6423SLionel Sambuc #include <stdlib.h> 16*433d6423SLionel Sambuc #include <string.h> 17*433d6423SLionel Sambuc #include <assert.h> 18*433d6423SLionel Sambuc #include <time.h> 19*433d6423SLionel Sambuc #include "file.h" 20*433d6423SLionel Sambuc #include "vmnt.h" 21*433d6423SLionel Sambuc 22*433d6423SLionel Sambuc /*===========================================================================* 23*433d6423SLionel Sambuc * copy_path * 24*433d6423SLionel Sambuc *===========================================================================*/ 25*433d6423SLionel Sambuc int copy_path(char *dest, size_t size) 26*433d6423SLionel Sambuc { 27*433d6423SLionel Sambuc /* Go get the path for a path request. Put the result in in 'dest', which 28*433d6423SLionel Sambuc * should be at least PATH_MAX in size. 29*433d6423SLionel Sambuc */ 30*433d6423SLionel Sambuc vir_bytes name; 31*433d6423SLionel Sambuc size_t len; 32*433d6423SLionel Sambuc 33*433d6423SLionel Sambuc assert(size >= PATH_MAX); 34*433d6423SLionel Sambuc 35*433d6423SLionel Sambuc name = job_m_in.m_lc_vfs_path.name; 36*433d6423SLionel Sambuc len = job_m_in.m_lc_vfs_path.len; 37*433d6423SLionel Sambuc 38*433d6423SLionel Sambuc if (len > size) { /* 'len' includes terminating-nul */ 39*433d6423SLionel Sambuc err_code = ENAMETOOLONG; 40*433d6423SLionel Sambuc return(EGENERIC); 41*433d6423SLionel Sambuc } 42*433d6423SLionel Sambuc 43*433d6423SLionel Sambuc /* Is the string contained in the message? If not, perform a normal copy. */ 44*433d6423SLionel Sambuc if (len > M_PATH_STRING_MAX) 45*433d6423SLionel Sambuc return fetch_name(name, len, dest); 46*433d6423SLionel Sambuc 47*433d6423SLionel Sambuc /* Just copy the path from the message */ 48*433d6423SLionel Sambuc strncpy(dest, job_m_in.m_lc_vfs_path.buf, len); 49*433d6423SLionel Sambuc 50*433d6423SLionel Sambuc if (dest[len - 1] != '\0') { 51*433d6423SLionel Sambuc err_code = ENAMETOOLONG; 52*433d6423SLionel Sambuc return(EGENERIC); 53*433d6423SLionel Sambuc } 54*433d6423SLionel Sambuc 55*433d6423SLionel Sambuc return(OK); 56*433d6423SLionel Sambuc } 57*433d6423SLionel Sambuc 58*433d6423SLionel Sambuc /*===========================================================================* 59*433d6423SLionel Sambuc * fetch_name * 60*433d6423SLionel Sambuc *===========================================================================*/ 61*433d6423SLionel Sambuc int fetch_name(vir_bytes path, size_t len, char *dest) 62*433d6423SLionel Sambuc { 63*433d6423SLionel Sambuc /* Go get path and put it in 'dest'. */ 64*433d6423SLionel Sambuc int r; 65*433d6423SLionel Sambuc 66*433d6423SLionel Sambuc if (len > PATH_MAX) { /* 'len' includes terminating-nul */ 67*433d6423SLionel Sambuc err_code = ENAMETOOLONG; 68*433d6423SLionel Sambuc return(EGENERIC); 69*433d6423SLionel Sambuc } 70*433d6423SLionel Sambuc 71*433d6423SLionel Sambuc /* Check name length for validity. */ 72*433d6423SLionel Sambuc if (len > SSIZE_MAX) { 73*433d6423SLionel Sambuc err_code = EINVAL; 74*433d6423SLionel Sambuc return(EGENERIC); 75*433d6423SLionel Sambuc } 76*433d6423SLionel Sambuc 77*433d6423SLionel Sambuc /* String is not contained in the message. Get it from user space. */ 78*433d6423SLionel Sambuc r = sys_datacopy_wrapper(who_e, path, VFS_PROC_NR, (vir_bytes) dest, len); 79*433d6423SLionel Sambuc if (r != OK) { 80*433d6423SLionel Sambuc err_code = EINVAL; 81*433d6423SLionel Sambuc return(r); 82*433d6423SLionel Sambuc } 83*433d6423SLionel Sambuc 84*433d6423SLionel Sambuc if (dest[len - 1] != '\0') { 85*433d6423SLionel Sambuc err_code = ENAMETOOLONG; 86*433d6423SLionel Sambuc return(EGENERIC); 87*433d6423SLionel Sambuc } 88*433d6423SLionel Sambuc 89*433d6423SLionel Sambuc return(OK); 90*433d6423SLionel Sambuc } 91*433d6423SLionel Sambuc 92*433d6423SLionel Sambuc /*===========================================================================* 93*433d6423SLionel Sambuc * isokendpt_f * 94*433d6423SLionel Sambuc *===========================================================================*/ 95*433d6423SLionel Sambuc int isokendpt_f(const char *file, int line, endpoint_t endpoint, int *proc, 96*433d6423SLionel Sambuc int fatal) 97*433d6423SLionel Sambuc { 98*433d6423SLionel Sambuc int failed = 0; 99*433d6423SLionel Sambuc endpoint_t ke; 100*433d6423SLionel Sambuc *proc = _ENDPOINT_P(endpoint); 101*433d6423SLionel Sambuc if (endpoint == NONE) { 102*433d6423SLionel Sambuc printf("VFS %s:%d: endpoint is NONE\n", file, line); 103*433d6423SLionel Sambuc failed = 1; 104*433d6423SLionel Sambuc } else if (*proc < 0 || *proc >= NR_PROCS) { 105*433d6423SLionel Sambuc printf("VFS %s:%d: proc (%d) from endpoint (%d) out of range\n", 106*433d6423SLionel Sambuc file, line, *proc, endpoint); 107*433d6423SLionel Sambuc failed = 1; 108*433d6423SLionel Sambuc } else if ((ke = fproc[*proc].fp_endpoint) != endpoint) { 109*433d6423SLionel Sambuc if(ke == NONE) { 110*433d6423SLionel Sambuc assert(fproc[*proc].fp_pid == PID_FREE); 111*433d6423SLionel Sambuc } else { 112*433d6423SLionel Sambuc printf("VFS %s:%d: proc (%d) from endpoint (%d) doesn't match " 113*433d6423SLionel Sambuc "known endpoint (%d)\n", file, line, *proc, endpoint, 114*433d6423SLionel Sambuc fproc[*proc].fp_endpoint); 115*433d6423SLionel Sambuc assert(fproc[*proc].fp_pid != PID_FREE); 116*433d6423SLionel Sambuc } 117*433d6423SLionel Sambuc failed = 1; 118*433d6423SLionel Sambuc } 119*433d6423SLionel Sambuc 120*433d6423SLionel Sambuc if(failed && fatal) 121*433d6423SLionel Sambuc panic("isokendpt_f failed"); 122*433d6423SLionel Sambuc 123*433d6423SLionel Sambuc return(failed ? EDEADEPT : OK); 124*433d6423SLionel Sambuc } 125*433d6423SLionel Sambuc 126*433d6423SLionel Sambuc 127*433d6423SLionel Sambuc /*===========================================================================* 128*433d6423SLionel Sambuc * clock_timespec * 129*433d6423SLionel Sambuc *===========================================================================*/ 130*433d6423SLionel Sambuc struct timespec clock_timespec(void) 131*433d6423SLionel Sambuc { 132*433d6423SLionel Sambuc /* This routine returns the time in seconds since 1.1.1970. MINIX is an 133*433d6423SLionel Sambuc * astrophysically naive system that assumes the earth rotates at a constant 134*433d6423SLionel Sambuc * rate and that such things as leap seconds do not exist. 135*433d6423SLionel Sambuc */ 136*433d6423SLionel Sambuc 137*433d6423SLionel Sambuc register int r; 138*433d6423SLionel Sambuc struct timespec tv; 139*433d6423SLionel Sambuc clock_t uptime; 140*433d6423SLionel Sambuc clock_t realtime; 141*433d6423SLionel Sambuc time_t boottime; 142*433d6423SLionel Sambuc 143*433d6423SLionel Sambuc r = getuptime(&uptime, &realtime, &boottime); 144*433d6423SLionel Sambuc if (r != OK) 145*433d6423SLionel Sambuc panic("clock_timespec err: %d", r); 146*433d6423SLionel Sambuc 147*433d6423SLionel Sambuc tv.tv_sec = boottime + (realtime/system_hz); 148*433d6423SLionel Sambuc /* We do not want to overflow, and system_hz can be as high as 50kHz */ 149*433d6423SLionel Sambuc assert(system_hz < LONG_MAX/40000); 150*433d6423SLionel Sambuc tv.tv_nsec = (realtime%system_hz) * 40000 / system_hz * 25000; 151*433d6423SLionel Sambuc return tv; 152*433d6423SLionel Sambuc } 153*433d6423SLionel Sambuc 154*433d6423SLionel Sambuc /*===========================================================================* 155*433d6423SLionel Sambuc * in_group * 156*433d6423SLionel Sambuc *===========================================================================*/ 157*433d6423SLionel Sambuc int in_group(struct fproc *rfp, gid_t grp) 158*433d6423SLionel Sambuc { 159*433d6423SLionel Sambuc int i; 160*433d6423SLionel Sambuc 161*433d6423SLionel Sambuc for (i = 0; i < rfp->fp_ngroups; i++) 162*433d6423SLionel Sambuc if (rfp->fp_sgroups[i] == grp) 163*433d6423SLionel Sambuc return(OK); 164*433d6423SLionel Sambuc 165*433d6423SLionel Sambuc return(EINVAL); 166*433d6423SLionel Sambuc } 167*433d6423SLionel Sambuc 168*433d6423SLionel Sambuc /*===========================================================================* 169*433d6423SLionel Sambuc * sys_datacopy_wrapper * 170*433d6423SLionel Sambuc *===========================================================================*/ 171*433d6423SLionel Sambuc int sys_datacopy_wrapper(endpoint_t src, vir_bytes srcv, 172*433d6423SLionel Sambuc endpoint_t dst, vir_bytes dstv, size_t len) 173*433d6423SLionel Sambuc { 174*433d6423SLionel Sambuc /* Safe function to copy data from or to a user buffer. 175*433d6423SLionel Sambuc * VFS has to be a bit more careful as a regular copy 176*433d6423SLionel Sambuc * might trigger VFS action needed by VM while it's 177*433d6423SLionel Sambuc * blocked on the kernel call. This wrapper tries the 178*433d6423SLionel Sambuc * copy, invokes VM itself asynchronously if necessary, 179*433d6423SLionel Sambuc * then tries the copy again. 180*433d6423SLionel Sambuc * 181*433d6423SLionel Sambuc * This function assumes it's between VFS and a user process, 182*433d6423SLionel Sambuc * and therefore one of the endpoints is SELF (VFS). 183*433d6423SLionel Sambuc */ 184*433d6423SLionel Sambuc int r; 185*433d6423SLionel Sambuc endpoint_t them = NONE; 186*433d6423SLionel Sambuc vir_bytes themv = -1; 187*433d6423SLionel Sambuc int writable = -1; 188*433d6423SLionel Sambuc 189*433d6423SLionel Sambuc r = sys_datacopy_try(src, srcv, dst, dstv, len); 190*433d6423SLionel Sambuc 191*433d6423SLionel Sambuc if(src == VFS_PROC_NR) src = SELF; 192*433d6423SLionel Sambuc if(dst == VFS_PROC_NR) dst = SELF; 193*433d6423SLionel Sambuc 194*433d6423SLionel Sambuc assert(src == SELF || dst == SELF); 195*433d6423SLionel Sambuc 196*433d6423SLionel Sambuc if(src == SELF) { them = dst; themv = dstv; writable = 1; } 197*433d6423SLionel Sambuc if(dst == SELF) { them = src; themv = srcv; writable = 0; } 198*433d6423SLionel Sambuc 199*433d6423SLionel Sambuc assert(writable >= 0); 200*433d6423SLionel Sambuc assert(them != SELF); 201*433d6423SLionel Sambuc 202*433d6423SLionel Sambuc if(r == EFAULT) { 203*433d6423SLionel Sambuc /* The copy has failed with EFAULT, this means the kernel has 204*433d6423SLionel Sambuc * given up but it might not be a legitimate error. Ask VM. 205*433d6423SLionel Sambuc */ 206*433d6423SLionel Sambuc if((r=vm_vfs_procctl_handlemem(them, themv, len, writable)) != OK) { 207*433d6423SLionel Sambuc return r; 208*433d6423SLionel Sambuc } 209*433d6423SLionel Sambuc 210*433d6423SLionel Sambuc r = sys_datacopy_try(src, srcv, dst, dstv, len); 211*433d6423SLionel Sambuc } 212*433d6423SLionel Sambuc 213*433d6423SLionel Sambuc return r; 214*433d6423SLionel Sambuc } 215*433d6423SLionel Sambuc 216