summaryrefslogtreecommitdiff
path: root/proc/mgt.c
blob: 76cebaa9c89805e3aacb38837925cc8cb65c320c (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
/* Process management
   Copyright (C) 1992,93,94,95,96,99,2000,01,02,13,14,21
     Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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 the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include <mach.h>
#include <mach/task_notify.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <hurd/hurd_types.h>
#include <stdlib.h>
#include <string.h>
#include <mach/notify.h>
#include <sys/wait.h>
#include <mach/mig_errors.h>
#include <sys/resource.h>
#include <hurd/auth.h>
#include <assert-backtrace.h>
#include <pids.h>

#include "proc.h"
#include "process_S.h"
#include "mutated_ourmsg_U.h"
#include "proc_exc_S.h"
#include "proc_exc_U.h"
#include "task_notify_S.h"
#include <hurd/signal.h>

extern mach_msg_return_t
mach_msg_receive (mach_msg_header_t *msg);

/* Create a new id structure with the given genuine uids. */
static inline struct ids *
make_ids (const uid_t *eff_uids, size_t n_eff_uids,
          const uid_t *avail_uids, size_t n_avail_uids)
{
  struct ids *i;

  i = malloc (sizeof (struct ids) + sizeof (uid_t) * (n_eff_uids + n_avail_uids));
  if (! i)
    return NULL;

  i->i_nuids = n_eff_uids + n_avail_uids;
  i->i_refcnt = 1;

  memcpy (&i->i_uids[0], eff_uids, sizeof (uid_t) * n_eff_uids);
  memcpy (&i->i_uids[n_eff_uids], avail_uids, sizeof (uid_t) * n_avail_uids);
  return i;
}

static inline void
ids_ref (struct ids *i)
{
  i->i_refcnt ++;
}

/* Free an id structure. */
static inline void
ids_rele (struct ids *i)
{
  i->i_refcnt --;
  if (i->i_refcnt == 0)
    free (i);
}

/* Tell if process P has uid UID, or has root.  */
int
check_uid (struct proc *p, uid_t uid)
{
  int i;
  for (i = 0; i < p->p_id->i_nuids; i++)
    if (p->p_id->i_uids[i] == uid || p->p_id->i_uids[i] == 0)
      return 1;
  return 0;
}

/* Implement proc_reathenticate as described in <hurd/process.defs>. */
kern_return_t
S_proc_reauthenticate (struct proc *p, mach_port_t rendport)
{
  error_t err;
  mach_port_t new_proc_port, prev;
  mach_msg_header_t msg;
  struct ids *new_ids;
  uid_t gubuf[50], aubuf[50], ggbuf[50], agbuf[50];
  uid_t *gen_uids, *aux_uids, *gen_gids, *aux_gids;
  mach_msg_type_number_t ngen_uids, naux_uids, ngen_gids, naux_gids;

  if (!p)
    return EOPNOTSUPP;

  gen_uids = gubuf;
  aux_uids = aubuf;
  gen_gids = ggbuf;
  aux_gids = agbuf;

  ngen_uids = sizeof (gubuf) / sizeof (uid_t);
  naux_uids = sizeof (aubuf) / sizeof (uid_t);
  ngen_gids = sizeof (ggbuf) / sizeof (uid_t);
  naux_gids = sizeof (agbuf) / sizeof (uid_t);

  new_proc_port = mach_reply_port ();
  /* Ask to be told if the new port dies.  We receive the notification
     on the same port, and as it's not a proc_reauthenticate_complete ()
     call, consider it an error.  */
  err = mach_port_request_notification (mach_task_self (), new_proc_port,
                                        MACH_NOTIFY_NO_SENDERS, 1,
                                        new_proc_port,
                                        MACH_MSG_TYPE_MAKE_SEND_ONCE,
                                        &prev);
  assert_perror_backtrace (err);
  assert_backtrace (prev == MACH_PORT_NULL);

  /* Release the global lock while blocking on the auth server and client.  */
  pthread_mutex_unlock (&global_lock);

  do
    err = auth_server_authenticate (authserver,
                                    rendport, MACH_MSG_TYPE_COPY_SEND,
                                    new_proc_port, MACH_MSG_TYPE_MAKE_SEND,
                                    &gen_uids, &ngen_uids,
                                    &aux_uids, &naux_uids,
                                    &gen_gids, &ngen_gids,
                                    &aux_gids, &naux_gids);
  while (err == EINTR);

  /* Wait for a proc_reauthenticate_complete () call
     on the new port before proceeding.  */
  if (!err)
    {
      msg.msgh_size = sizeof (mach_msg_header_t);
      msg.msgh_local_port = new_proc_port;
      err = mach_msg_receive (&msg);
    }

  pthread_mutex_lock (&global_lock);

  if (err)
    goto out;

  /* Check if what we have received was indeed a
     proc_reauthenticate_complete () call.  */
  if (msg.msgh_id != 24064)
    err = EINVAL;
  mach_msg_destroy (&msg);
  if (err)
    goto out;

  if (p->p_dead)
    {
      /* The process died while we had the lock released.
         Its p_id field is no longer valid and we shouldn't touch it.  */
      err = EAGAIN;
      goto out;
    }

  new_ids = make_ids (gen_uids, ngen_uids, aux_uids, naux_uids);
  if (!new_ids)
    {
      err = errno;
      goto out;
    }

  /* From here on, nothing can go wrong.  */

  mach_port_deallocate (mach_task_self (), rendport);

  ids_rele (p->p_id);
  p->p_id = new_ids;

  ports_reallocate_from_external (p, new_proc_port);

 out:
  if (gen_uids != gubuf)
    munmap (gen_uids, ngen_uids * sizeof (uid_t));
  if (aux_uids != aubuf)
    munmap (aux_uids, naux_uids * sizeof (uid_t));
  if (gen_gids != ggbuf)
    munmap (gen_gids, ngen_gids * sizeof (uid_t));
  if (aux_gids != agbuf)
    munmap (aux_gids, naux_gids * sizeof (uid_t));

  if (err)
    mach_port_mod_refs (mach_task_self (), new_proc_port,
                        MACH_PORT_RIGHT_RECEIVE, -1);
  return err;
}

/* Implement proc_child as described in <hurd/process.defs>. */
kern_return_t
S_proc_child (struct proc *parentp,
	      task_t childt)
{
  struct proc *childp;

  if (!parentp)
    return EOPNOTSUPP;

  childp = task_find (childt);
  if (!childp)
    return ESRCH;

  if (childp->p_parentset)
    return EBUSY;

  mach_port_deallocate (mach_task_self (), childt);

  /* Process identification.
     Leave p_task and p_pid alone; all the rest comes from the
     new parent. */

  if (!--childp->p_login->l_refcnt)
    free (childp->p_login);
  childp->p_login = parentp->p_login;
  childp->p_login->l_refcnt++;

  ids_rele (childp->p_id);
  ids_ref (parentp->p_id);
  childp->p_id = parentp->p_id;

  /* Process hierarchy.  Remove from our current location
     and place us under our new parent.  Sanity check to make sure
     parent is currently init. */
  assert_backtrace (childp->p_parent == init_proc);
  if (childp->p_sib)
    childp->p_sib->p_prevsib = childp->p_prevsib;
  *childp->p_prevsib = childp->p_sib;

  childp->p_parent = parentp;
  childp->p_sib = parentp->p_ochild;
  childp->p_prevsib = &parentp->p_ochild;
  if (parentp->p_ochild)
    parentp->p_ochild->p_prevsib = &childp->p_sib;
  parentp->p_ochild = childp;

  /* Process group structure. */
  if (childp->p_pgrp != parentp->p_pgrp)
    {
      leave_pgrp (childp);
      childp->p_pgrp = parentp->p_pgrp;
      join_pgrp (childp);
      /* Not necessary to call newids ourself because join_pgrp does
	 it for us. */
    }
  else if (childp->p_msgport != MACH_PORT_NULL)
    nowait_msg_proc_newids (childp->p_msgport, childp->p_task,
			    childp->p_parent->p_pid, childp->p_pgrp->pg_pgid,
			    !childp->p_pgrp->pg_orphcnt);
  childp->p_parentset = 1;

  /* If these are not set in the child, it was probably fork(2)ed.  If
     so, it inherits the values of its parent.  */
  if (! childp->start_code && ! childp->end_code)
    {
      childp->start_code = parentp->start_code;
      childp->end_code = parentp->end_code;
    }
  if (! childp->exe && parentp->exe)
    childp->exe = strdup (parentp->exe);

  if (MACH_PORT_VALID (parentp->p_task_namespace))
    {
      mach_port_mod_refs (mach_task_self (), parentp->p_task_namespace,
			  MACH_PORT_RIGHT_SEND, +1);
      childp->p_task_namespace = parentp->p_task_namespace;
    }

  return 0;
}

/* Implement proc_reassign as described in <hurd/process.defs>. */
kern_return_t
S_proc_reassign (struct proc *p,
		 task_t newt)
{
  struct proc *stubp;

  if (!p)
    return EOPNOTSUPP;

  stubp = task_find (newt);
  if (!stubp)
    return ESRCH;

  if (stubp == p)
    return EINVAL;

  mach_port_deallocate (mach_task_self (), newt);

  remove_proc_from_hash (p);

  task_terminate (p->p_task);
  mach_port_deallocate (mach_task_self (), p->p_task);
  p->p_task = stubp->p_task;

  /* For security, we need to use the request port from STUBP */
  ports_transfer_right (p, stubp);

  /* Enqueued messages might refer to the old task port, so
     destroy them. */
  if (p->p_msgport != MACH_PORT_NULL)
    {
      mach_port_deallocate (mach_task_self (), p->p_msgport);
      p->p_msgport = MACH_PORT_NULL;
      p->p_deadmsg = 1;
    }

  /* These two are image dependent. */
  p->p_argv = stubp->p_argv;
  p->p_envp = stubp->p_envp;

  /* Destroy stubp */
  stubp->p_task = MACH_PORT_NULL;/* block deallocation */
  process_has_exited (stubp);
  stubp->p_waited = 1;		/* fake out complete_exit */
  complete_exit (stubp);

  add_proc_to_hash (p);

  return 0;
}

/* Implement proc_reauthenticate_reassign
   as described in <hurd/process.defs>.  */
kern_return_t
S_proc_reauthenticate_reassign (struct proc *p,
                                mach_port_t rendezvous,
                                task_t new_task)
{
  error_t err;
  mach_port_t new_proc_port, prev;
  mach_msg_header_t msg;
  struct proc *stubp;
  struct ids *new_ids;
  uid_t gubuf[50], aubuf[50], ggbuf[50], agbuf[50];
  uid_t *gen_uids, *aux_uids, *gen_gids, *aux_gids;
  mach_msg_type_number_t ngen_uids, naux_uids, ngen_gids, naux_gids;

  if (!p)
    return EOPNOTSUPP;

  if (!MACH_PORT_VALID (rendezvous))
    return EINVAL;

  stubp = task_find (new_task);
  if (!stubp)
    return ESRCH;
  if (stubp == p)
    return EINVAL;
  ports_port_ref (stubp);

  gen_uids = gubuf;
  aux_uids = aubuf;
  gen_gids = ggbuf;
  aux_gids = agbuf;

  ngen_uids = sizeof (gubuf) / sizeof (uid_t);
  naux_uids = sizeof (aubuf) / sizeof (uid_t);
  ngen_gids = sizeof (ggbuf) / sizeof (uid_t);
  naux_gids = sizeof (agbuf) / sizeof (uid_t);

  new_proc_port = mach_reply_port ();
  /* Ask to be told if the new port dies.  We receive the notification
     on the same port, and as it's not a proc_reauthenticate_complete ()
     call, consider it an error.  */
  err = mach_port_request_notification (mach_task_self (), new_proc_port,
                                        MACH_NOTIFY_NO_SENDERS, 1,
                                        new_proc_port,
                                        MACH_MSG_TYPE_MAKE_SEND_ONCE,
                                        &prev);
  assert_perror_backtrace (err);
  assert_backtrace (prev == MACH_PORT_NULL);

  /* Communicate with the auth server before doing
     *any changes* to these two processes.  */

  /* Release the lock while talking to the auth server.  */
  pthread_mutex_unlock (&global_lock);

  do
    err = auth_server_authenticate (authserver,
                                    rendezvous,
                                    MACH_MSG_TYPE_COPY_SEND,
                                    new_proc_port,
                                    MACH_MSG_TYPE_MAKE_SEND,
                                    &gen_uids, &ngen_uids,
                                    &aux_uids, &naux_uids,
                                    &gen_gids, &ngen_gids,
                                    &aux_gids, &naux_gids);
  while (err == EINTR);

  /* Wait for a proc_reauthenticate_complete () call
     on the new port before proceeding.  */
  if (!err)
    {
      msg.msgh_size = sizeof (mach_msg_header_t);
      msg.msgh_local_port = new_proc_port;
      err = mach_msg_receive (&msg);
    }

  pthread_mutex_lock (&global_lock);

  if (err)
    goto out;

  /* Check if what we have received was indeed a
     proc_reauthenticate_complete () call.  */
  if (msg.msgh_id != 24064)
    err = EINVAL;
  mach_msg_destroy (&msg);
  if (err)
    goto out;

  /* If any task has died, or the process referring to the new task
     has itself been reassigned in the meantime, abort.  */
  if (p->p_dead || stubp->p_dead || stubp->p_task != new_task)
    {
      err = EAGAIN;
      goto out;
    }

  /* Copy in the new ids.  */
  new_ids = make_ids (gen_uids, ngen_uids, aux_uids, naux_uids);
  if (!new_ids)
    {
      err = errno;
      goto out;
    }
  ids_rele (p->p_id);
  p->p_id = new_ids;

  /* From here on, nothing can go wrong.  */

  mach_port_deallocate (mach_task_self (), new_task);
  mach_port_deallocate (mach_task_self (), rendezvous);

  remove_proc_from_hash (p);

  task_terminate (p->p_task);
  mach_port_deallocate (mach_task_self (), p->p_task);

  p->p_task = stubp->p_task;
  stubp->p_task = MACH_PORT_NULL;
  ports_destroy_right (stubp);
  ports_reallocate_from_external (p, new_proc_port);

  if (MACH_PORT_VALID (p->p_msgport))
    {
      mach_port_deallocate (mach_task_self (), p->p_msgport);
      p->p_msgport = MACH_PORT_NULL;
      p->p_deadmsg = 1;
    }

  /* These two are image dependent.  */
  p->p_argv = stubp->p_argv;
  p->p_envp = stubp->p_envp;

  /* Destroy stubp.  */
  process_has_exited (stubp);
  stubp->p_waited = 1; /* fake out complete_exit */
  complete_exit (stubp);

  add_proc_to_hash (p);
  err = 0;

 out:
  if (gen_uids != gubuf)
    munmap (gen_uids, ngen_uids * sizeof (uid_t));
  if (aux_uids != aubuf)
    munmap (aux_uids, naux_uids * sizeof (uid_t));
  if (gen_gids != ggbuf)
    munmap (gen_gids, ngen_gids * sizeof (uid_t));
  if (aux_gids != agbuf)
    munmap (aux_gids, naux_gids * sizeof (uid_t));

  if (err)
    mach_port_mod_refs (mach_task_self (), new_proc_port,
                        MACH_PORT_RIGHT_RECEIVE, -1);

  ports_port_deref (stubp);

  return err;
}

kern_return_t
S_proc_setowner (struct proc *p,
                 uid_t owner,
                 int clear)
{
  /* No longer does anything, kept for compatibility.  */
  return EOPNOTSUPP;
}

kern_return_t
S_proc_reauthenticate_complete (struct proc *p)
{
  /* Regular calls of this routine always fail.  */
  return EOPNOTSUPP;
}

/* Implement proc_getpids as described in <hurd/process.defs>. */
kern_return_t
S_proc_getpids (struct proc *p,
		pid_t *pid,
		pid_t *ppid,
		int *orphaned)
{
  if (!p)
    return EOPNOTSUPP;
  *pid = p->p_pid;
  *ppid = p->p_parent->p_pid;
  *orphaned = !p->p_pgrp->pg_orphcnt;
  return 0;
}

/* Implement proc_set_arg_locations as described in <hurd/process.defs>. */
kern_return_t
S_proc_set_arg_locations (struct proc *p,
			  vm_address_t argv,
			  vm_address_t envp)
{
  if (!p)
    return EOPNOTSUPP;
  p->p_argv = argv;
  p->p_envp = envp;
  return 0;
}

/* Implement proc_get_arg_locations as described in <hurd/process.defs>. */
kern_return_t
S_proc_get_arg_locations (struct proc *p,
			  vm_address_t *argv,
			  vm_address_t *envp)
{
  *argv = p->p_argv;
  *envp = p->p_envp;
  return 0;
}

/* Implement proc_set_entry as described in <hurd/process.defs>. */
kern_return_t
S_proc_set_entry (struct proc *p, vm_address_t entry)
{
  if (!p)
    return EOPNOTSUPP;
  p->p_entry = entry;
  return 0;
}

/* Implement proc_get_entry as described in <hurd/process.defs>. */
kern_return_t
S_proc_get_entry (struct proc *p, vm_address_t *entry)
{
  *entry = p->p_entry;
  return 0;
}

/* Implement proc_dostop as described in <hurd/process.defs>. */
kern_return_t
S_proc_dostop (struct proc *p,
	       thread_t contthread)
{
  thread_t threadbuf[2], *threads = threadbuf;
  mach_msg_type_number_t nthreads = sizeof (threadbuf) / sizeof (thread_t);
  int i;
  error_t err;

  if (!p)
    return EOPNOTSUPP;

  err = task_suspend (p->p_task);
  if (err)
    return err;
  err = task_threads (p->p_task, &threads, &nthreads);
  if (err)
    {
      task_resume (p->p_task);
      return err;
    }
  /* We can not compare the thread ports with CONTTHREAD, as CONTTHREAD
     might be a proxy port (for example in rpctrace).  For this reason
     we suspend all threads and then resume  CONTTHREAD.  */
  for (i = 0; i < nthreads; i++)
    {
      if (threads[i] != contthread)
	thread_suspend (threads[i]);
      mach_port_deallocate (mach_task_self (), threads[i]);
    }
  if (threads != threadbuf)
    munmap (threads, nthreads * sizeof (thread_t));
  err = task_resume (p->p_task);
  if (err)
    return err;

  mach_port_deallocate (mach_task_self (), contthread);
  return 0;
}

/* Clean state of E before it is deallocated */
void
exc_clean (void *arg)
{
  struct exc *e = arg;
  mach_port_deallocate (mach_task_self (), e->forwardport);
}

/* Implement proc_handle_exceptions as described in <hurd/process.defs>. */
kern_return_t
S_proc_handle_exceptions (struct proc *p,
			  mach_port_t msgport,
			  mach_port_t forwardport,
			  int flavor,
			  thread_state_t new_state,
			  mach_msg_type_number_t statecnt)
{
  struct exc *e;
  error_t err;

  /* No need to check P here; we don't use it. */

  err = ports_import_port (exc_class, proc_bucket, msgport,
			   (sizeof (struct exc)
			    + (statecnt * sizeof (natural_t))), &e);
  if (err)
    return err;

  e->forwardport = forwardport;
  e->flavor = flavor;
  e->statecnt = statecnt;
  memcpy (e->thread_state, new_state, statecnt * sizeof (natural_t));
  ports_port_deref (e);
  return 0;
}

/* Called on exception ports provided to proc_handle_exceptions.  Do
   the thread_set_state requested by proc_handle_exceptions and then
   send an exception_raise message as requested. */
kern_return_t
S_proc_exception_raise (struct exc *e,
			mach_port_t reply,
			mach_msg_type_name_t reply_type,
			mach_port_t thread,
			mach_port_t task,
			integer_t exception,
			integer_t code,
			long_integer_t subcode)
{
  error_t err;
  struct proc *p;
  if (!e || e->pi.bucket != proc_bucket || e->pi.class != exc_class)
    return EOPNOTSUPP;

  p = task_find (task);
  if (! p)
    {
      /* Bogus RPC.  */
      return EINVAL;
    }

  /* Try to forward the message.  */
  err = proc_exception_raise (e->forwardport,
			      reply, reply_type, MACH_SEND_NOTIFY,
			      thread, task, exception, code, subcode);

  switch (err)
    {
      struct hurd_signal_detail hsd;
      int signo;

    case 0:
      /* We have successfully forwarded the exception message.  Now reset
	 the faulting thread's state to run its recovery code, which should
	 dequeue that message.  */
      err = thread_set_state (thread, e->flavor, e->thread_state, e->statecnt);
      if (err)
	return err;
      mach_port_deallocate (mach_task_self (), thread);
      mach_port_deallocate (mach_task_self (), task);
      return MIG_NO_REPLY;

    default:
      /* Some unexpected error in forwarding the message.  */
      /* FALLTHROUGH */

    case MACH_SEND_NOTIFY_IN_PROGRESS:
      /* The port's queue is full; this means the thread didn't receive
	 the exception message we forwarded last time it faulted.
	 Declare that signal thread hopeless and the task crashed.  */

      /* Translate the exception code into a signal number
	 and mark the process as having died that way.  */
      hsd.exc = exception;
      hsd.exc_code = code;
      hsd.exc_subcode = subcode;
      _hurd_exception2signal (&hsd, &signo);
      p->p_exiting = 1;
      p->p_status = W_EXITCODE (0, signo);
      p->p_sigcode = hsd.code;

      /* Nuke the task; we will get a notification message and report that
	 it died with SIGNO.  */
      task_terminate (task);

      /* In the MACH_SEND_NOTIFY_IN_PROGRESS case, the kernel did a
	 pseudo-receive of the RPC request message that may have added user
	 refs to these send rights.  But we have lost track because the MiG
	 stub did not save the message buffer that was modified by the
	 pseudo-receive.

	 Fortunately, we can be sure that we don't need the THREAD send
	 right for anything since this task is now dead; there would be a
	 potential race here with another exception_raise message arriving
	 with the same thread, but we expect that this won't happen since
	 the thread will still be waiting for our reply.  XXX We have no
	 secure knowledge that this is really from the kernel, so a
	 malicious user could confuse us and induce a race where we clobber
	 another port put on the THREAD name after the destroy; also, a
	 user just doing thread_set_state et al could arrange that we get a
	 second legitimate exception_raise for the same thread and have the
	 first race mentioned above!

	 There are all manner of race problems if we destroy the TASK
	 right.  Fortunately, since we've terminated the task we know that
	 we will shortly be getting a dead-name notifiction and that will
	 call mach_port_destroy in TASK when it is safe to do so.  */

      mach_port_destroy (mach_task_self (), thread);

      return MIG_NO_REPLY;
    }

}

/* This function is used as callback in S_proc_getallpids.  */
static void
count_up (struct proc *p, void *counter)
{
  ++*(int *)counter;
}

/* This function is used as callback in S_proc_getallpids.  */
static void
store_pid (struct proc *p, void *loc)
{
  *(*(pid_t **)loc)++ = p->p_pid;
}

/* Implement proc_getallpids as described in <hurd/process.defs>. */
kern_return_t
S_proc_getallpids (struct proc *p,
		   pid_t **pids,
		   mach_msg_type_number_t *pidslen)
{
  int nprocs;
  pid_t *loc;

  /* No need to check P here; we don't use it. */

  add_tasks (0);

  nprocs = 0;
  prociterate (count_up, &nprocs);

  if (nprocs > *pidslen)
    {
      *pids = mmap (0, nprocs * sizeof (pid_t), PROT_READ|PROT_WRITE,
		    MAP_ANON, 0, 0);
      if (*pids == MAP_FAILED)
        return ENOMEM;
    }

  loc = *pids;
  prociterate (store_pid, &loc);

  *pidslen = nprocs;
  return 0;
}

/* Create a process for TASK, which is not otherwise known to us.
   The PID/parentage/job-control fields are not yet filled in,
   and the proc is not entered into any hash table.  */
struct proc *
allocate_proc (task_t task)
{
  error_t err;
  struct proc *p;

  /* Pid 0 is us; pid 1 is init.  We handle those here specially;
     all other processes inherit from init here (though proc_child
     will move them to their actual parent usually).  */

  err = ports_create_port (proc_class, proc_bucket, sizeof (struct proc), &p);
  if (err)
    return NULL;

  memset (&p->p_pi + 1, 0, sizeof *p - sizeof p->p_pi);
  p->p_task = task;
  p->p_task_namespace = MACH_PORT_NULL;
  p->p_msgport = MACH_PORT_NULL;

  pthread_cond_init (&p->p_wakeup, NULL);

  return p;
}

/* Allocate and initialize the proc structure for init (PID 1),
   the original parent of all other procs.  */
struct proc *
create_init_proc (void)
{
  static const uid_t zero;
  struct proc *p;
  const char *rootsname = "root";

  p = allocate_proc (MACH_PORT_NULL);
  assert_backtrace (p);

  p->p_pid = HURD_PID_INIT;

  p->p_parent = p;
  p->p_sib = 0;
  p->p_prevsib = &p->p_ochild;
  p->p_ochild = p;
  p->p_parentset = 1;

  p->p_deadmsg = 1;		/* Force initial "re-"fetch of msgport.  */

  p->p_important = 1;

  p->p_id = make_ids (&zero, 1, &zero, 0);
  assert_backtrace (p->p_id);

  p->p_loginleader = 1;
  p->p_login = malloc (sizeof (struct login) + strlen (rootsname) + 1);
  assert_backtrace (p->p_login);

  p->p_login->l_refcnt = 1;
  strcpy (p->p_login->l_name, rootsname);

  boot_setsid (p);

  return p;
}

/* Request a dead-name notification for P's task port.  */

void
proc_death_notify (struct proc *p)
{
  error_t err;

  err = ports_request_dead_name_notification (p, p->p_task, NULL);
  assert_perror_backtrace (err);
}

/* Complete a new process that has been allocated but not entirely initialized.
   This gets called for every process except init_proc (PID 1).  */
void
complete_proc (struct proc *p, pid_t pid)
{
  /* Because these have a reference count of one before starting,
     they can never be freed, so we're safe. */
  static struct login *nulllogin;
  static struct ids nullids = { i_refcnt : 1, i_nuids : 0};
  const char nullsname [] = "<none>";

  if (!nulllogin)
    {
      nulllogin = malloc (sizeof (struct login) + sizeof (nullsname) + 1);
      nulllogin->l_refcnt = 1;
      strcpy (nulllogin->l_name, nullsname);
    }

  p->p_pid = pid;

  if (pid == HURD_PID_STARTUP)
    {
      /* Equip HURD_PID_STARTUP with the same credentials as
         HURD_PID_INIT.  */
      static const uid_t zero;
      p->p_id = make_ids (&zero, 1, &zero, 0);
      assert_backtrace (p->p_id);
    }
  else
    {
      ids_ref (&nullids);
      p->p_id = &nullids;
    }

  p->p_login = nulllogin;
  p->p_login->l_refcnt++;

  /* Our parent is init for now.  */
  p->p_parent = init_proc;

  p->p_sib = init_proc->p_ochild;
  p->p_prevsib = &init_proc->p_ochild;
  if (p->p_sib)
    p->p_sib->p_prevsib = &p->p_sib;
  init_proc->p_ochild = p;
  p->p_loginleader = 0;
  p->p_ochild = 0;
  p->p_parentset = 0;

  p->p_pgrp = init_proc->p_pgrp;

  /* At this point, we do not know the task of the startup process,
     defer registering death notifications and adding it to the hash
     tables.  */
  if (pid != HURD_PID_STARTUP)
    add_proc_to_hash (p);
  join_pgrp (p);
  if (pid != HURD_PID_STARTUP)
    proc_death_notify (p);
}


/* Create a process for TASK, which is not otherwise known to us
   and initialize it in the usual ways.  */
static struct proc *
new_proc (task_t task)
{
  struct proc *p;

  p = allocate_proc (task);
  if (p)
    complete_proc (p, genpid ());
  return p;
}



/* Task namespace support.  */

/* Check if a given process is part of a task namespace but is not the
   root.  The root is managed by us, while all other tasks are managed
   by the root itself.  */
int
namespace_is_subprocess (struct proc *p)
{
  return (p
          && MACH_PORT_VALID (p->p_task_namespace)
          && p->p_parent
          && MACH_PORT_VALID (p->p_parent->p_task_namespace));
}

/* Translate PIDs valid in NAMESPACE into PIDs valid in our own
   process space.

   Conditions: global_lock is unlocked before calling, and is locked
   afterwards.  */
error_t
namespace_translate_pids (mach_port_t namespace, pid_t *pids, size_t pids_len)
{
  size_t i;
  task_t *tasks;

  tasks = calloc (pids_len, sizeof *tasks);
  if (tasks == NULL)
    {
      pthread_mutex_lock (&global_lock);
      return ENOMEM;
    }

  for (i = 0; i < pids_len; i++)
    /* We handle errors by checking each returned task.  */
    proc_pid2task (namespace, pids[i], &tasks[i]);

  pthread_mutex_lock (&global_lock);

  for (i = 0; i < pids_len; i++)
    if (MACH_PORT_VALID (tasks[i]))
      {
        struct proc *p = task_find_nocreate (tasks[i]);
        mach_port_deallocate (mach_task_self (), tasks[i]);
        pids[i] = p ? p->p_pid : (pid_t) -1;
      }
    else
      pids[i] = (pid_t) -1;

  free (tasks);
  return 0;
}

/* Find the creator of the task namespace that P is in.  */
struct proc *
namespace_find_root (struct proc *p)
{
  for (;
       MACH_PORT_VALID (p->p_parent->p_task_namespace);
       p = p->p_parent)
    {
      /* Walk up the process hierarchy until we find the creator of
         the task namespace.  The last process we encounter that has a
         valid task_namespace must be the creator.  */
    }

  return p;
}

/* Used with prociterate to terminate all tasks in a task
   namespace.  */
static void
namespace_terminate (struct proc *p, void *cookie)
{
  mach_port_t *namespacep = cookie;
  if (p->p_task_namespace == *namespacep)
    task_terminate (p->p_task);
}



/* The task associated with process P has died.  Drop most state,
   and then record us as dead.  Our parent will eventually complete the
   deallocation. */
void
process_has_exited (struct proc *p)
{
  /* We have already died; this can happen since both proc_reassign
     and dead-name notifications could result in two calls to this
     routine for the same process.  */
  if (p->p_dead)
    return;

  p->p_waited = 0;
  if (p->p_task != MACH_PORT_NULL)
    alert_parent (p);

  if (p->p_msgport)
    mach_port_deallocate (mach_task_self (), p->p_msgport);
  p->p_msgport = MACH_PORT_NULL;

  prociterate ((void (*) (struct proc *, void *))check_message_dying, p);

  /* Nuke external send rights and the (possible) associated reference.  */
  ports_destroy_right (p);

  if (!--p->p_login->l_refcnt)
    free (p->p_login);
  free (p->exe);
  p->exe = NULL;

  ids_rele (p->p_id);

  /* Reparent our children to init by attaching the head and tail of
     our list onto init's.  If the process is part of a task
     namespace, reparent to the process that created the namespace
     instead.  */
  if (p->p_ochild)
    {
      struct proc *reparent_to = init_proc;
      struct proc *tp;		/* will point to the last one.  */
      int isdead = 0;

      if (MACH_PORT_VALID (p->p_task_namespace))
	{
          tp = namespace_find_root (p);
	  if (p == tp)
	    {
	      /* The creator of the task namespace died.  Terminate
		 all tasks.  */
	      prociterate (namespace_terminate, &p->p_task_namespace);

	      mach_port_deallocate (mach_task_self (), p->p_task_namespace);
	      p->p_task_namespace = MACH_PORT_NULL;
	    }
	  else
	    reparent_to = tp;
	}

      /* first tell them their parent is changing */
      for (tp = p->p_ochild; tp->p_sib; tp = tp->p_sib)
	{
	  if (tp->p_msgport != MACH_PORT_NULL)
	    nowait_msg_proc_newids (tp->p_msgport, tp->p_task,
				    1, tp->p_pgrp->pg_pgid,
				    !tp->p_pgrp->pg_orphcnt);
	  tp->p_parent = reparent_to;
	  if (tp->p_dead)
	    isdead = 1;
	}
      if (tp->p_msgport != MACH_PORT_NULL)
	nowait_msg_proc_newids (tp->p_msgport, tp->p_task,
				1, tp->p_pgrp->pg_pgid,
				!tp->p_pgrp->pg_orphcnt);
      tp->p_parent = reparent_to;

      /* And now append the lists. */
      tp->p_sib = reparent_to->p_ochild;
      if (tp->p_sib)
	tp->p_sib->p_prevsib = &tp->p_sib;
      reparent_to->p_ochild = p->p_ochild;
      p->p_ochild->p_prevsib = &reparent_to->p_ochild;

      if (isdead)
	alert_parent (reparent_to);
    }

  /* If an operation is in progress for this process, cause it
     to wakeup and return now. */
  if (p->p_waiting || p->p_msgportwait)
    pthread_cond_broadcast (&p->p_wakeup);

  p->p_dead = 1;

  /* Cancel any outstanding RPCs done on behalf of the dying process.  */
  ports_interrupt_rpcs (p);

  /* No one is going to wait for processes in a task namespace.  */
  if (MACH_PORT_VALID (p->p_task_namespace))
    {
      mach_port_deallocate (mach_task_self (), p->p_task_namespace);
      p->p_waited = 1;
      complete_exit (p);
    }
}

void
complete_exit (struct proc *p)
{
  assert_backtrace (p->p_dead);
  assert_backtrace (p->p_waited);

  remove_proc_from_hash (p);
  if (p->p_task != MACH_PORT_NULL)
    {
      mach_port_deallocate (mach_task_self (), p->p_task);
      p->p_task = MACH_PORT_NULL;
    }

  /* Remove us from our parent's list of children. */
  if (p->p_sib)
    p->p_sib->p_prevsib = p->p_prevsib;
  *p->p_prevsib = p->p_sib;

  leave_pgrp (p);

  /* Drop the reference we created long ago in new_proc.  The only
     other references that ever show up are those for RPC args, which
     will shortly vanish (because we are p_dead, those routines do
     nothing).  */
  ports_port_deref (p);
}

/* Get the list of all tasks from the kernel and start adding them.
   If we encounter TASK, then don't do any more and return its proc.
   If TASK is null or we never find it, then return 0. */
struct proc *
add_tasks (task_t task)
{
  mach_port_t *psets;
  mach_msg_type_number_t npsets;
  int i;
  struct proc *foundp = 0;

  host_processor_sets (mach_host_self (), &psets, &npsets);
  for (i = 0; i < npsets; i++)
    {
      mach_port_t psetpriv;
      mach_port_t *tasks;
      mach_msg_type_number_t ntasks;
      int j;

      if (!foundp)
	{
	  host_processor_set_priv (_hurd_host_priv, psets[i], &psetpriv);
	  processor_set_tasks (psetpriv, &tasks, &ntasks);
	  for (j = 0; j < ntasks; j++)
	    {
	      int set = 0;

	      /* The kernel can deliver us an array with null slots in the
		 middle, e.g. if a task died during the call.  */
	      if (! MACH_PORT_VALID (tasks[j]))
		continue;

	      if (!foundp)
		{
		  struct proc *p = task_find_nocreate (tasks[j]);
		  if (!p)
		    {
		      p = new_proc (tasks[j]);
		      if (p)
			set = 1;
		    }
		  if (!foundp && tasks[j] == task)
		    foundp = p;
		}
	      if (!set)
		mach_port_deallocate (mach_task_self (), tasks[j]);
	    }
	  munmap (tasks, ntasks * sizeof (task_t));
	  mach_port_deallocate (mach_task_self (), psetpriv);
	}
      mach_port_deallocate (mach_task_self (), psets[i]);
    }
  munmap (psets, npsets * sizeof (mach_port_t));
  return foundp;
}

/* Allocate a new unused PID.
   (Unused means it is neither the pid nor pgrp of any relevant data.) */
int
genpid (void)
{
#define WRAP_AROUND 30000
#define START_OVER 100
  static int nextpid = 1;
  static int wrap = WRAP_AROUND;

  while (nextpid < wrap && !pidfree (nextpid))
    ++nextpid;
  if (nextpid >= wrap)
    {
      nextpid = START_OVER;
      while (!pidfree (nextpid))
	nextpid++;

      while (nextpid > wrap)
	wrap *= 2;
    }

  return nextpid++;
}



/* Support for making sysvinit PID 1.  */

/* We reserve PID 1 for sysvinit.  However, proc may pick up the task
   when it is created and reserve an entry in the process table for
   it.  When startup tells us the task that it created for sysvinit,
   we need to locate this preliminary entry and remove it.  Otherwise,
   we end up with two entries for sysvinit with the same task.  */

/* XXX: This is a mess.  It would be nicer if startup gave us the
   ports (e.g. sysvinit's task, the kernel task...) before starting
   us, communicating the names using command line options.  */

/* Implement proc_set_init_task as described in <hurd/process.defs>.  */
kern_return_t
S_proc_set_init_task(struct proc *callerp,
		     task_t task)
{
  struct proc *shadow;

  if (! callerp)
    return EOPNOTSUPP;

  if (callerp != startup_proc)
    return EPERM;

  /* Check if TASK already made it into the process table, and if so
     remove it.  */
  shadow = task_find_nocreate (task);
  if (shadow)
    {
      /* Cheat a little so we can use complete_exit.  */
      shadow->p_dead = 1;
      shadow->p_waited = 1;
      mach_port_deallocate (mach_task_self (), shadow->p_task);
      shadow->p_task = MACH_PORT_NULL;
      complete_exit (shadow);
    }

  init_proc->p_task = task;
  add_proc_to_hash (init_proc);
  proc_death_notify (init_proc);

  return 0;
}



/* Implement proc_mark_important as described in <hurd/process.defs>. */
kern_return_t
S_proc_mark_important (struct proc *p)
{
  if (!p)
    return EOPNOTSUPP;

  /* Only root may use this interface.  Any children of startup_proc
     exempt from this restriction, as startup_proc calls this on their
     behalf.  The kernel process is a notable example of an process
     that needs this exemption.  That is not an problem however, since
     all children of /hurd/startup are important and we mark them as
     such anyway.  */
  if (! check_uid (p, 0) && p->p_parent != startup_proc)
    return EPERM;

  p->p_important = 1;
  return 0;
}

/* Implement proc_is_important as described in <hurd/process.defs>.  */
kern_return_t
S_proc_is_important (struct proc *callerp,
		     boolean_t *essential)
{
  if (!callerp)
    return EOPNOTSUPP;

  *essential = callerp->p_important;

  return 0;
}

/* Implement proc_set_code as described in <hurd/process.defs>.  */
kern_return_t
S_proc_set_code (struct proc *callerp,
		 vm_address_t start_code,
		 vm_address_t end_code)
{
  if (!callerp)
    return EOPNOTSUPP;

  callerp->start_code = start_code;
  callerp->end_code = end_code;

  return 0;
}

/* Implement proc_get_code as described in <hurd/process.defs>.  */
kern_return_t
S_proc_get_code (struct proc *callerp,
		 vm_address_t *start_code,
		 vm_address_t *end_code)
{
  if (!callerp)
    return EOPNOTSUPP;

  *start_code = callerp->start_code;
  *end_code = callerp->end_code;

  return 0;
}

/* Handle new task notifications from the kernel.  */
kern_return_t
S_mach_notify_new_task (struct port_info *notify,
			mach_port_t task,
			mach_port_t parent)
{
  struct proc *parentp, *childp;

  if (! notify
      || (kernel_proc == NULL && notify->class != generic_port_class)
      || (kernel_proc != NULL && notify != (struct port_info *) kernel_proc))
    return EOPNOTSUPP;

  if (task == MACH_PORT_DEAD)
    return ESRCH;

  parentp = task_find_nocreate (parent);
  if (! parentp)
    return ESRCH;

  childp = task_find_nocreate (task);
  if (! childp)
    {
      mach_port_mod_refs (mach_task_self (), task, MACH_PORT_RIGHT_SEND, +1);
      childp = new_proc (task);
    }

  if (MACH_PORT_VALID (parentp->p_task_namespace))
    {
      error_t err;
      /* Tasks in a task namespace are not expected to call
	 proc_child, so we do it on their behalf.  */
      mach_port_mod_refs (mach_task_self (), task, MACH_PORT_RIGHT_SEND, +1);
      err = S_proc_child (parentp, task);
      assert_perror_backtrace (err);

      /* Relay the notification.  This consumes task and parent.  */
      return mach_notify_new_task (childp->p_task_namespace, task, parent);
    }

  mach_port_deallocate (mach_task_self (), task);
  mach_port_deallocate (mach_task_self (), parent);
  return 0;
}

/* Implement proc_make_task_namespace as described in
   <hurd/process.defs>.  */
kern_return_t
S_proc_make_task_namespace (struct proc *callerp,
			    mach_port_t notify)
{
  if (! callerp)
    return EOPNOTSUPP;

  if (! MACH_PORT_VALID (notify))
    return EINVAL;

  if (MACH_PORT_VALID (callerp->p_task_namespace))
    return EBUSY;

  callerp->p_task_namespace = notify;

  return 0;
}