summaryrefslogtreecommitdiff
path: root/trans/proxy-defpager.c
blob: cda795920f9bedbc913e893e9eb17de16fb41927 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* A translator for providing access to Mach default_pager.defs control calls

   Copyright (C) 2002, 2007 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <hurd/trivfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <argp.h>
#include <error.h>
#include <version.h>
#include <hurd/paths.h>

#include "default_pager_S.h"
#include "default_pager_U.h"

static mach_port_t real_defpager, dev_master;

static error_t
allowed (mach_port_t port, int mode)
{
  struct trivfs_protid *cred = ports_lookup_port
    (0, port, trivfs_protid_portclasses[0]);
  if (!cred)
    return MIG_BAD_ID;
  error_t result = (cred->po->openmodes & mode) ? 0 : EACCES;
  ports_port_deref (cred);
  return result;
}

kern_return_t
S_default_pager_object_create (mach_port_t default_pager,
			       memory_object_t *memory_object,
			       vm_size_t object_size)
{
  return allowed (default_pager, O_EXEC)
    ?: default_pager_object_create (real_defpager, memory_object, object_size);
}

kern_return_t
S_default_pager_info (mach_port_t default_pager, default_pager_info_t *info)
{
  return allowed (default_pager, O_READ)
    ?: default_pager_info (real_defpager, info);
}

kern_return_t
S_default_pager_objects (mach_port_t default_pager,
			 default_pager_object_array_t *objects,
			 mach_msg_type_number_t *objectsCnt,
			 mach_port_array_t *ports,
			 mach_msg_type_number_t *portsCnt)
{
  return allowed (default_pager, O_WRITE)
    ?: default_pager_objects (real_defpager,
			      objects, objectsCnt, ports, portsCnt);
}

kern_return_t
S_default_pager_object_pages (mach_port_t default_pager,
			      mach_port_t memory_object,
			      default_pager_page_array_t *pages,
			      mach_msg_type_number_t *pagesCnt)
{
  return allowed (default_pager, O_WRITE)
    ?: default_pager_object_pages (real_defpager, memory_object,
				   pages, pagesCnt);
}


kern_return_t
S_default_pager_paging_file (mach_port_t default_pager,
			     mach_port_t master_device_port,
			     default_pager_filename_t filename,
			     boolean_t add)
{
  return allowed (default_pager, O_WRITE)
    ?: default_pager_paging_file (real_defpager, dev_master, filename, add)
    ?: mach_port_deallocate (mach_task_self (), master_device_port);
}

kern_return_t
S_default_pager_paging_storage (mach_port_t default_pager,
				mach_port_t device,
				recnum_t *runs, mach_msg_type_number_t nruns,
				default_pager_filename_t name,
				boolean_t add)
{
  return allowed (default_pager, O_WRITE)
    ?: default_pager_paging_storage (real_defpager, dev_master,
				     runs, nruns, name, add)
    ?: mach_port_deallocate (mach_task_self (), device);
}

kern_return_t
S_default_pager_object_set_size (mach_port_t memory_object,
				 mach_port_t reply_port,
				 mach_port_seqno_t seqno,
				 vm_size_t object_size_limit)
{
  /* This is sent to an object, not the control port.  */
  return MIG_BAD_ID;
}


/* Trivfs hooks  */

int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid;

int trivfs_support_read = 1;
int trivfs_support_write = 1;
int trivfs_support_exec = 1;

int trivfs_allow_open = O_READ | O_WRITE | O_EXEC;

void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
  st->st_blksize = vm_page_size * 256; /* Make transfers LARRRRRGE */

  st->st_size = 0;
  st->st_blocks = 0;

  st->st_mode &= ~S_IFMT;
  st->st_mode |= S_IFCHR;
}


error_t
trivfs_goaway (struct trivfs_control *fsys, int flags)
{
  exit (0);
}

kern_return_t
trivfs_S_io_read (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t replytype,
		  char **data,
		  mach_msg_type_number_t *datalen,
		  loff_t offs,
		  mach_msg_type_number_t amt)
{
  if (!cred)
    return EOPNOTSUPP;
  return EIO;
}

kern_return_t
trivfs_S_io_write (struct trivfs_protid *cred,
		   mach_port_t reply, mach_msg_type_name_t replytype,
		   char *data, mach_msg_type_number_t datalen,
		   loff_t offs, mach_msg_type_number_t *amt)
{
  if (!cred)
    return EOPNOTSUPP;
  return EIO;
}

kern_return_t
trivfs_S_io_get_openmodes (struct trivfs_protid *cred,
			   mach_port_t reply, mach_msg_type_name_t replytype,
			   int *bits)
{
  if (!cred)
    return EOPNOTSUPP;
  else
    {
      *bits = cred->po->openmodes;
      return 0;
    }
}

error_t
trivfs_S_io_set_all_openmodes (struct trivfs_protid *cred,
			       mach_port_t reply,
			       mach_msg_type_name_t replytype,
			       int mode)
{
  if (!cred)
    return EOPNOTSUPP;
  else
    return 0;
}

kern_return_t
trivfs_S_io_set_some_openmodes (struct trivfs_protid *cred,
				mach_port_t reply,
				mach_msg_type_name_t replytype,
				int bits)
{
  if (!cred)
    return EOPNOTSUPP;
  else
    return 0;
}

kern_return_t
trivfs_S_io_clear_some_openmodes (struct trivfs_protid *cred,
				  mach_port_t reply,
				  mach_msg_type_name_t replytype,
				  int bits)
{
  if (!cred)
    return EOPNOTSUPP;
  else
    return 0;
}

const char *argp_program_version = STANDARD_HURD_VERSION (proxy-defpager);

static const struct argp argp =
{doc: "\
Access to control interfaces of Mach default pager.\n\
This translator should normally be set on " _SERVERS_DEFPAGER "."};

int
proxy_defpager_demuxer (mach_msg_header_t *inp,
			mach_msg_header_t *outp)
{
  extern int default_pager_server (mach_msg_header_t *, mach_msg_header_t *);

  return default_pager_server (inp, outp)
    || trivfs_demuxer (inp, outp);
}

int
main (int argc, char **argv)
{
  error_t err;
  mach_port_t bootstrap;
  struct trivfs_control *fsys;
  mach_port_t host_priv;

  argp_parse (&argp, argc, argv, 0, 0, 0);

  err = get_privileged_ports (&host_priv, &dev_master);
  if (err)
    error (2, err, "cannot get privileged ports");
  real_defpager = MACH_PORT_NULL;
  err = vm_set_default_memory_manager (host_priv, &real_defpager);
  mach_port_deallocate (mach_task_self (), host_priv);
  if (err)
    error (3, err, "vm_set_default_memory_manager");
  if (real_defpager == MACH_PORT_NULL)
    error (1, 0, "no default memory manager set!");

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap == MACH_PORT_NULL)
    error (1, 0, "Must be started as a translator");

  trivfs_fsid = getpid ();

  /* Reply to our parent.  */
  err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
  mach_port_deallocate (mach_task_self (), bootstrap);
  if (err)
    error (4, err, "Contacting parent");

  /* Launch. */
  ports_manage_port_operations_multithread (fsys->pi.bucket,
					    proxy_defpager_demuxer,
					    2 * 60 * 1000, 0, 0);

  return 0;
}