summaryrefslogtreecommitdiff
path: root/hurd/hurdmsg.c
blob: 63e5e81b8772955560283db9da999a3fc05b32f6 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* Copyright (C) 1992-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

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

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <hurd.h>
#include <hurd/msg_server.h>
#include <hurd/fd.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <argz.h>


#define AUTHCHECK \
  if (auth != mach_task_self () && ! __USEPORT (AUTH, port == auth)) \
    return EPERM


/* Snarfing and frobbing the init ports.  */

kern_return_t
  _S_msg_get_init_port (mach_port_t msgport, mach_port_t auth, int which,
			mach_port_t *result, mach_msg_type_name_t *result_type)
{
  AUTHCHECK;
  *result_type = MACH_MSG_TYPE_MOVE_SEND;
  /* This function adds a new user reference for the *RESULT it gives back.
     Our reply message uses a move-send right that consumes this reference.  */
  return _hurd_ports_get (which, result);
}

kern_return_t
_S_msg_set_init_port (mach_port_t msgport, mach_port_t auth,
		      int which, mach_port_t port)
{
  error_t err;

  AUTHCHECK;

  err = _hurd_ports_set (which, port);
  if (err == 0)
    __mach_port_deallocate (__mach_task_self (), port);

  return 0;
}

kern_return_t
_S_msg_get_init_ports (mach_port_t msgport, mach_port_t auth,
		       mach_port_t **ports,
		       mach_msg_type_name_t *ports_type,
		       mach_msg_type_number_t *nports)
{
  mach_msg_type_number_t i;
  error_t err;

  AUTHCHECK;

  if (err = __vm_allocate (__mach_task_self (), (vm_address_t *) ports,
			   _hurd_nports * sizeof (mach_port_t), 1))
    return err;
  *nports = _hurd_nports;

  for (i = 0; i < _hurd_nports; ++i)
    /* This function adds a new user ref for the *RESULT it gives back.
       Our reply message uses move-send rights that consumes this ref.  */
    if (err = _hurd_ports_get (i, &(*ports)[i]))
      {
	/* Died part way through.  Deallocate the ports already fetched.  */
	while (i-- > 0)
	  __mach_port_deallocate (__mach_task_self (), (*ports)[i]);
	__vm_deallocate (__mach_task_self (),
			 (vm_address_t) *ports,
			 *nports * sizeof (mach_port_t));
	return err;
      }

  *ports_type = MACH_MSG_TYPE_MOVE_SEND;
  return 0;
}

kern_return_t
_S_msg_set_init_ports (mach_port_t msgport, mach_port_t auth,
		       mach_port_t *ports, mach_msg_type_number_t nports)
{
  mach_msg_type_number_t i;
  error_t err;

  AUTHCHECK;

  for (i = 0; i < _hurd_nports; ++i)
    {
      if (err = _hurd_ports_set (i, ports[i]))
	return err;
      else
	__mach_port_deallocate (__mach_task_self (), ports[i]);
    }

  return 0;
}

/* Snarfing and frobbing the init ints.  */

static kern_return_t
get_int (int which, int *value)
{
  switch (which)
    {
    case INIT_UMASK:
      *value = _hurd_umask;
      return 0;
    case INIT_SIGMASK:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	*value = ss->blocked;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGPENDING:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	*value = ss->pending;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGIGN:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	sigset_t ign;
	int sig;
	__spin_lock (&ss->lock);
	__sigemptyset (&ign);
	for (sig = 1; sig < NSIG; ++sig)
	  if (ss->actions[sig].sa_handler == SIG_IGN)
	    __sigaddset (&ign, sig);
	__spin_unlock (&ss->lock);
	*value = ign;
	return 0;
      }
    default:
      return EINVAL;
    }
}

kern_return_t
_S_msg_get_init_int (mach_port_t msgport, mach_port_t auth,
		     int which, int *value)
{
  AUTHCHECK;

  return get_int (which, value);
}

kern_return_t
_S_msg_get_init_ints (mach_port_t msgport, mach_port_t auth,
		      int **values, mach_msg_type_number_t *nvalues)
{
  error_t err;
  mach_msg_type_number_t i;

  AUTHCHECK;

  if (err = __vm_allocate (__mach_task_self (), (vm_address_t *) values,
			   INIT_INT_MAX * sizeof (int), 1))
    return err;
  *nvalues = INIT_INT_MAX;

  for (i = 0; i < INIT_INT_MAX; ++i)
    switch (err = get_int (i, &(*values)[i]))
      {
      case 0:			/* Success.  */
	break;
      case EINVAL:		/* Unknown index.  */
	(*values)[i] = 0;
	break;
      default:			/* Lossage.  */
	__vm_deallocate (__mach_task_self (),
			 (vm_address_t) *values, INIT_INT_MAX * sizeof (int));
	return err;
      }

  return 0;
}


static kern_return_t
set_int (int which, int value)
{
  switch (which)
    {
    case INIT_UMASK:
      _hurd_umask = value;
      return 0;

      /* These are pretty odd things to do.  But you asked for it.  */
    case INIT_SIGMASK:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	ss->blocked = value;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGPENDING:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	__spin_lock (&ss->lock);
	ss->pending = value;
	__spin_unlock (&ss->lock);
	return 0;
      }
    case INIT_SIGIGN:
      {
	struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
	int sig;
	const sigset_t ign = value;
	__spin_lock (&ss->lock);
	for (sig = 1; sig < NSIG; ++sig)
	  {
	    if (__sigismember (&ign, sig))
	      ss->actions[sig].sa_handler = SIG_IGN;
	    else if (ss->actions[sig].sa_handler == SIG_IGN)
	      ss->actions[sig].sa_handler = SIG_DFL;
	  }
	__spin_unlock (&ss->lock);
	return 0;

      case INIT_TRACEMASK:
	_hurdsig_traced = value;
	return 0;
      }
    default:
      return EINVAL;
    }
}

kern_return_t
_S_msg_set_init_int (mach_port_t msgport, mach_port_t auth,
		     int which, int value)
{
  AUTHCHECK;

  return set_int (which, value);
}

kern_return_t
_S_msg_set_init_ints (mach_port_t msgport, mach_port_t auth,
		      int *values, mach_msg_type_number_t nvalues)
{
  error_t err;
  mach_msg_type_number_t i;

  AUTHCHECK;

  for (i = 0; i < INIT_INT_MAX; ++i)
    switch (err = set_int (i, values[i]))
      {
      case 0:			/* Success.  */
	break;
      case EINVAL:		/* Unknown index.  */
	break;
      default:			/* Lossage.  */
	return err;
      }

  return 0;
}


kern_return_t
_S_msg_get_fd (mach_port_t msgport, mach_port_t auth, int which,
	       mach_port_t *result, mach_msg_type_name_t *result_type)
{
  AUTHCHECK;

  /* This creates a new user reference for the send right.
     Our reply message will move that reference to the caller.  */
  *result = __getdport (which);
  if (*result == MACH_PORT_NULL)
    return errno;
  *result_type = MACH_MSG_TYPE_MOVE_SEND;

  return 0;
}

kern_return_t
_S_msg_set_fd (mach_port_t msgport, mach_port_t auth,
	       int which, mach_port_t port)
{
  AUTHCHECK;

  /* We consume the reference if successful.  */
  return HURD_FD_USE (which, (_hurd_port2fd (descriptor, port, 0), 0));
}

/* Snarfing and frobbing environment variables.  */

kern_return_t
_S_msg_get_env_variable (mach_port_t msgport,
			 char *variable,
			 char **data, mach_msg_type_number_t *datalen)
{
  error_t err;
  mach_msg_type_number_t valuelen;
  const char *value = getenv (variable);

  if (value == NULL)
    return ENOENT;

  valuelen = strlen (value);
  if (valuelen > *datalen)
    {
      if (err = __vm_allocate (__mach_task_self (),
			       (vm_address_t *) data, valuelen, 1))
	return err;
    }

  memcpy (*data, value, valuelen);
  *datalen = valuelen;

  return 0;
}


kern_return_t
_S_msg_set_env_variable (mach_port_t msgport, mach_port_t auth,
			 char *variable,
			 char *value,
			 int replace)
{
  AUTHCHECK;

  if (__setenv (variable, value, replace)) /* XXX name space */
    return errno;
  return 0;
}

kern_return_t
_S_msg_get_environment (mach_port_t msgport,
			char **data, mach_msg_type_number_t *datalen)
{
  /* Pack the environment into an array with nulls separating elements.  */
  if (__environ != NULL)
    {
      char *ap, **p;
      size_t envlen = 0;

      for (p = __environ; *p != NULL; ++p)
	envlen += strlen (*p) + 1;

      if (envlen > *datalen)
	{
	  if (__vm_allocate (__mach_task_self (),
			     (vm_address_t *) data, envlen, 1))
	    return ENOMEM;
	}

      ap = *data;
      for (p = __environ; *p != NULL; ++p)
	ap = __memccpy (ap, *p, '\0', ULONG_MAX);

      *datalen = envlen;
    }
  else
    *datalen = 0;

  return 0;
}

kern_return_t
_S_msg_set_environment (mach_port_t msgport, mach_port_t auth,
			char *data, mach_msg_type_number_t datalen)
{
  int _hurd_split_args (char *, mach_msg_type_number_t, char **);
  int envc;
  char **envp;

  AUTHCHECK;

  envc = __argz_count (data, datalen);
  envp = malloc ((envc + 1) * sizeof (char *));
  if (envp == NULL)
    return errno;
  __argz_extract (data, datalen, envp);
  __environ = envp;		/* XXX cooperate with loadenv et al */
  return 0;
}


/* XXX */

kern_return_t
_S_msg_get_dtable (mach_port_t process,
		   mach_port_t refport,
		   portarray_t *dtable,
		   mach_msg_type_name_t *dtablePoly,
		   mach_msg_type_number_t *dtableCnt)
{ return EOPNOTSUPP; }

kern_return_t
_S_msg_set_dtable (mach_port_t process,
		   mach_port_t refport,
		   portarray_t dtable,
		   mach_msg_type_number_t dtableCnt)
{ return EOPNOTSUPP; }