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