summaryrefslogtreecommitdiff
path: root/minix/servers/vfs/utility.c
blob: f68bf38be7b016fe3b848d3485999d0f87289549 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This file contains a few general purpose utility routines.
 *
 * The entry points into this file are
 *   copy_path:	  copy a path name from a path request from userland
 *   fetch_name:  go get a path name from user space
 *   panic:       something awful has occurred;  MINIX cannot continue
 *   in_group:    determines if group 'grp' is in rfp->fp_sgroups[]
 */

#include "fs.h"
#include <minix/callnr.h>
#include <minix/endpoint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "file.h"
#include "vmnt.h"

/*===========================================================================*
 *				copy_path				     *
 *===========================================================================*/
int copy_path(char *dest, size_t size)
{
/* Go get the path for a path request. Put the result in in 'dest', which
 * should be at least PATH_MAX in size.
 */
  vir_bytes name;
  size_t len;

  assert(size >= PATH_MAX);

  name = job_m_in.m_lc_vfs_path.name;
  len = job_m_in.m_lc_vfs_path.len;

  if (len > size) {	/* 'len' includes terminating-nul */
	err_code = ENAMETOOLONG;
	return(EGENERIC);
  }

  /* Is the string contained in the message? If not, perform a normal copy. */
  if (len > M_PATH_STRING_MAX)
	return fetch_name(name, len, dest);

  /* Just copy the path from the message */
  strncpy(dest, job_m_in.m_lc_vfs_path.buf, len);

  if (dest[len - 1] != '\0') {
	err_code = ENAMETOOLONG;
	return(EGENERIC);
  }

  return(OK);
}

/*===========================================================================*
 *				fetch_name				     *
 *===========================================================================*/
int fetch_name(vir_bytes path, size_t len, char *dest)
{
/* Go get path and put it in 'dest'.  */
  int r;

  if (len > PATH_MAX) {	/* 'len' includes terminating-nul */
	err_code = ENAMETOOLONG;
	return(EGENERIC);
  }

  /* Check name length for validity. */
  if (len > SSIZE_MAX) {
	err_code = EINVAL;
	return(EGENERIC);
  }

  /* String is not contained in the message.  Get it from user space. */
  r = sys_datacopy_wrapper(who_e, path, VFS_PROC_NR, (vir_bytes) dest, len);
  if (r != OK) {
	err_code = EINVAL;
	return(r);
  }

  if (dest[len - 1] != '\0') {
	err_code = ENAMETOOLONG;
	return(EGENERIC);
  }

  return(OK);
}

/*===========================================================================*
 *				isokendpt_f				     *
 *===========================================================================*/
int isokendpt_f(const char *file, int line, endpoint_t endpoint, int *proc,
       int fatal)
{
  int failed = 0;
  endpoint_t ke;
  *proc = _ENDPOINT_P(endpoint);
  if (endpoint == NONE) {
	printf("VFS %s:%d: endpoint is NONE\n", file, line);
	failed = 1;
  } else if (*proc < 0 || *proc >= NR_PROCS) {
	printf("VFS %s:%d: proc (%d) from endpoint (%d) out of range\n",
		file, line, *proc, endpoint);
	failed = 1;
  } else if ((ke = fproc[*proc].fp_endpoint) != endpoint) {
	if(ke == NONE) {
		assert(fproc[*proc].fp_pid == PID_FREE);
	} else {
		printf("VFS %s:%d: proc (%d) from endpoint (%d) doesn't match "
			"known endpoint (%d)\n", file, line, *proc, endpoint,
			fproc[*proc].fp_endpoint);
		assert(fproc[*proc].fp_pid != PID_FREE);
	}
	failed = 1;
  }

  if(failed && fatal)
	panic("isokendpt_f failed");

  return(failed ? EDEADEPT : OK);
}

/*===========================================================================*
 *                              in_group                                     *
 *===========================================================================*/
int in_group(struct fproc *rfp, gid_t grp)
{
  int i;

  for (i = 0; i < rfp->fp_ngroups; i++)
	if (rfp->fp_sgroups[i] == grp)
		return(OK);

  return(EINVAL);
}

/*===========================================================================*
 *                              sys_datacopy_wrapper                         *
 *===========================================================================*/
int sys_datacopy_wrapper(endpoint_t src, vir_bytes srcv,
	endpoint_t dst, vir_bytes dstv, size_t len)
{
	/* Safe function to copy data from or to a user buffer.
	 * VFS has to be a bit more careful as a regular copy
	 * might trigger VFS action needed by VM while it's
	 * blocked on the kernel call. This wrapper tries the
	 * copy, invokes VM itself asynchronously if necessary,
	 * then tries the copy again.
	 *
	 * This function assumes it's between VFS and a user process,
	 * and therefore one of the endpoints is SELF (VFS).
	 */
	int r;
	endpoint_t them = NONE;
	vir_bytes themv = -1;
	int writable = -1;

	r = sys_datacopy_try(src, srcv, dst, dstv, len);

	if(src == VFS_PROC_NR) src = SELF;
	if(dst == VFS_PROC_NR) dst = SELF;

	assert(src == SELF || dst == SELF);

	if(src == SELF) { them = dst; themv = dstv; writable = 1; }
	if(dst == SELF) { them = src; themv = srcv; writable = 0; }

	assert(writable >= 0);
	assert(them != SELF);

	if(r == EFAULT) {
		/* The copy has failed with EFAULT, this means the kernel has
		 * given up but it might not be a legitimate error. Ask VM.
		 */
		if((r=vm_vfs_procctl_handlemem(them, themv, len, writable)) != OK) {
			return r;
		}

		r = sys_datacopy_try(src, srcv, dst, dstv, len);
	}

	return r;
}