|
root / base / usr / src / uts / common / contract / process.c
process.c C 1197 lines 33.7 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2019 Joyent, Inc.
 */

#include <sys/mutex.h>
#include <sys/debug.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/thread.h>
#include <sys/id_space.h>
#include <sys/avl.h>
#include <sys/list.h>
#include <sys/sysmacros.h>
#include <sys/proc.h>
#include <sys/contract.h>
#include <sys/contract_impl.h>
#include <sys/contract/process.h>
#include <sys/contract/process_impl.h>
#include <sys/cmn_err.h>
#include <sys/nvpair.h>
#include <sys/policy.h>
#include <sys/refstr.h>
#include <sys/sunddi.h>

/*
 * Process Contracts
 * -----------------
 *
 * Generally speaking, a process contract is a contract between a
 * process and a set of its descendent processes.  In some cases, when
 * the child processes outlive the author of the contract, the contract
 * may be held by (and therefore be between the child processes and) a
 * successor process which adopts the contract after the death of the
 * original author.
 *
 * The process contract adds two new concepts to the Solaris process
 * model.  The first is that a process contract forms a rigid fault
 * boundary around a set of processes.  Hardware, software, and even
 * administrator errors impacting a process in a process contract
 * generate specific events and can be requested to atomically shutdown
 * all processes in the contract.  The second is that a process
 * contract is a process collective whose leader is not a member of the
 * collective.  This means that the leader can reliably react to events
 * in the collective, and may also act upon the collective without
 * special casing itself.
 *
 * A composite outcome of these two concepts is that we can now create
 * a tree of process contracts, rooted at init(8), which represent
 * services and subservices that are reliably observed and can be
 * restarted when fatal errors occur.  The service management framework
 * (SMF) realizes this structure.
 *
 * For more details, see the "restart agreements" case, PSARC 2003/193.
 *
 * There are four sets of routines in this file: the process contract
 * standard template operations, the process contract standard contract
 * operations, a couple routines used only by the contract subsystem to
 * handle process contracts' unique role as a temporary holder of
 * abandoned contracts, and the interfaces which allow the system to
 * create and act upon process contracts.  The first two are defined by
 * the contracts framework and won't be discussed further.  As for the
 * remaining two:
 *
 * Special framework interfaces
 * ----------------------------
 *
 * contract_process_accept - determines if a process contract is a
 *   regent, i.e. if it can inherit other contracts.
 *
 * contract_process_take - tells a regent process contract to inherit
 *   an abandoned contract
 *
 * contract_process_adopt - tells a regent process contract that a
 *   contract it has inherited is being adopted by a process.
 *
 * Process contract interfaces
 * ---------------------------
 *
 * contract_process_fork - called when a process is created; adds the
 *   new process to an existing contract or to a newly created one.
 *
 * contract_process_exit - called when a process exits
 *
 * contract_process_core - called when a process would have dumped core
 *   (even if a core file wasn't generated)
 *
 * contract_process_hwerr - called when a process was killed because of
 *   an uncorrectable hardware error
 *
 * contract_process_sig - called when a process was killed by a fatal
 *   signal sent by a process in another process contract
 *
 */

ct_type_t *process_type;
ctmpl_process_t *sys_process_tmpl;
refstr_t *conp_svc_aux_default;

/*
 * Macro predicates for determining when events should be sent and how.
 */
#define	EVSENDP(ctp, flag) \
	((ctp->conp_contract.ct_ev_info | ctp->conp_contract.ct_ev_crit) & flag)

#define	EVINFOP(ctp, flag) \
	((ctp->conp_contract.ct_ev_crit & flag) == 0)

#define	EVFATALP(ctp, flag) \
	(ctp->conp_ev_fatal & flag)


/*
 * Process contract template implementation
 */

/*
 * ctmpl_process_dup
 *
 * The process contract template dup entry point.  Other than the
 * to-be-subsumed contract, which must be held, this simply copies all
 * the fields of the original.
 */
static struct ct_template *
ctmpl_process_dup(struct ct_template *template)
{
	ctmpl_process_t *new;
	ctmpl_process_t *old = template->ctmpl_data;

	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);

	ctmpl_copy(&new->ctp_ctmpl, template);
	new->ctp_ctmpl.ctmpl_data = new;

	new->ctp_subsume = old->ctp_subsume;
	if (new->ctp_subsume)
		contract_hold(new->ctp_subsume);
	new->ctp_params = old->ctp_params;
	new->ctp_ev_fatal = old->ctp_ev_fatal;
	new->ctp_svc_fmri = old->ctp_svc_fmri;
	if (new->ctp_svc_fmri != NULL) {
		refstr_hold(new->ctp_svc_fmri);
	}
	new->ctp_svc_aux = old->ctp_svc_aux;
	if (new->ctp_svc_aux != NULL) {
		refstr_hold(new->ctp_svc_aux);
	}

	return (&new->ctp_ctmpl);
}

/*
 * ctmpl_process_free
 *
 * The process contract template free entry point.  Just releases a
 * to-be-subsumed contract and frees the template.
 */
static void
ctmpl_process_free(struct ct_template *template)
{
	ctmpl_process_t *ctp = template->ctmpl_data;

	if (ctp->ctp_subsume)
		contract_rele(ctp->ctp_subsume);
	if (ctp->ctp_svc_fmri != NULL) {
		refstr_rele(ctp->ctp_svc_fmri);
	}
	if (ctp->ctp_svc_aux != NULL) {
		refstr_rele(ctp->ctp_svc_aux);
	}
	kmem_free(template, sizeof (ctmpl_process_t));
}

/*
 * SAFE_EV is the set of events which a non-privileged process is
 * allowed to make critical but not fatal or if the PGRPONLY parameter
 * is set.  EXCESS tells us if "value", a critical event set, requires
 * additional privilege given the template "ctp".
 */
#define	SAFE_EV			(CT_PR_EV_EMPTY)
#define	EXCESS(ctp, value)	\
	(((value) & ~((ctp)->ctp_ev_fatal | SAFE_EV)) || \
	(((value) & ~SAFE_EV) && (ctp->ctp_params & CT_PR_PGRPONLY)))

/*
 * ctmpl_process_set
 *
 * The process contract template set entry point.  None of the terms
 * may be unconditionally set, and setting the parameters or fatal
 * event set may result in events being implicitly removed from to the
 * critical event set and added to the informative event set.  The
 * (admittedly subtle) reason we implicitly change the critical event
 * set when the parameter or fatal event set is modified but not the
 * other way around is because a change to the critical event set only
 * affects the contract's owner, whereas a change to the parameter set
 * and fatal set can affect the execution of the application running in
 * the contract (and should therefore be only made explicitly).  We
 * allow implicit changes at all so that setting contract terms doesn't
 * become a complex dance dependent on the template's initial state and
 * the desired terms.
 */
static int
ctmpl_process_set(struct ct_template *tmpl, ct_kparam_t *kparam,
    const cred_t *cr)
{
	ctmpl_process_t *ctp = tmpl->ctmpl_data;
	ct_param_t *param = &kparam->param;
	contract_t *ct;
	int error;
	uint64_t param_value = 0;
	char *str_value;

	if ((param->ctpm_id == CTPP_SVC_FMRI) ||
	    (param->ctpm_id == CTPP_CREATOR_AUX)) {
		str_value = (char *)kparam->ctpm_kbuf;
		str_value[param->ctpm_size - 1] = '\0';
	} else {
		if (param->ctpm_size < sizeof (uint64_t))
			return (EINVAL);
		param_value = *(uint64_t *)kparam->ctpm_kbuf;
		/*
		 * No process contract parameters are > 32 bits.
		 * Unless it is a string.
		 */
		if (param_value & ~UINT32_MAX)
			return (EINVAL);
	}

	switch (param->ctpm_id) {
	case CTPP_SUBSUME:
		if (param_value != 0) {
			/*
			 * Ensure that the contract exists, that we
			 * hold the contract, and that the contract is
			 * empty.
			 */
			ct = contract_type_ptr(process_type, param_value,
			    curproc->p_zone->zone_uniqid);
			if (ct == NULL)
				return (ESRCH);
			if (ct->ct_owner != curproc) {
				contract_rele(ct);
				return (EACCES);
			}
			if (((cont_process_t *)ct->ct_data)->conp_nmembers) {
				contract_rele(ct);
				return (ENOTEMPTY);
			}
		} else {
			ct = NULL;
		}
		if (ctp->ctp_subsume)
			contract_rele(ctp->ctp_subsume);
		ctp->ctp_subsume = ct;
		break;
	case CTPP_PARAMS:
		if (param_value & ~CT_PR_ALLPARAM)
			return (EINVAL);
		ctp->ctp_params = param_value;
		/*
		 * If an unprivileged process requests that
		 * CT_PR_PGRPONLY be set, remove any unsafe events from
		 * the critical event set and add them to the
		 * informative event set.
		 */
		if ((ctp->ctp_params & CT_PR_PGRPONLY) &&
		    EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
		    !secpolicy_contract_event_choice(cr)) {
			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~SAFE_EV);
			tmpl->ctmpl_ev_crit &= SAFE_EV;
		}

		break;
	case CTPP_SVC_FMRI:
		if (error = secpolicy_contract_identity(cr))
			return (error);
		if (ctp->ctp_svc_fmri != NULL)
			refstr_rele(ctp->ctp_svc_fmri);
		if (strcmp(CT_PR_SVC_DEFAULT, str_value) == 0)
			ctp->ctp_svc_fmri = NULL;
		else
			ctp->ctp_svc_fmri =
			    refstr_alloc(str_value);
		break;
	case CTPP_CREATOR_AUX:
		if (ctp->ctp_svc_aux != NULL)
			refstr_rele(ctp->ctp_svc_aux);
		if (param->ctpm_size == 1) /* empty string */
			ctp->ctp_svc_aux = NULL;
		else
			ctp->ctp_svc_aux =
			    refstr_alloc(str_value);
		break;
	case CTP_EV_CRITICAL:
		/*
		 * We simply don't allow adding events to the critical
		 * event set which aren't permitted by our policy or by
		 * privilege.
		 */
		if (EXCESS(ctp, param_value) &&
		    (error = secpolicy_contract_event(cr)) != 0)
			return (error);
		tmpl->ctmpl_ev_crit = param_value;
		break;
	case CTPP_EV_FATAL:
		if (param_value & ~CT_PR_ALLFATAL)
			return (EINVAL);
		ctp->ctp_ev_fatal = param_value;
		/*
		 * Check to see if an unprivileged process is
		 * requesting that events be removed from the fatal
		 * event set which are still in the critical event set.
		 */
		if (EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
		    !secpolicy_contract_event_choice(cr)) {
			int allowed =
			    SAFE_EV | (ctp->ctp_params & CT_PR_PGRPONLY) ?
			    0 : ctp->ctp_ev_fatal;
			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~allowed);
			tmpl->ctmpl_ev_crit &= allowed;
		}
		break;
	default:
		return (EINVAL);
	}

	return (0);
}

/*
 * ctmpl_process_get
 *
 * The process contract template get entry point.  Simply fetches and
 * returns the requested term.
 */
static int
ctmpl_process_get(struct ct_template *template, ct_kparam_t *kparam)
{
	ctmpl_process_t *ctp = template->ctmpl_data;
	ct_param_t *param = &kparam->param;
	uint64_t *param_value = kparam->ctpm_kbuf;

	if (param->ctpm_id == CTPP_SUBSUME ||
	    param->ctpm_id == CTPP_PARAMS ||
	    param->ctpm_id == CTPP_EV_FATAL) {
		if (param->ctpm_size < sizeof (uint64_t))
			return (EINVAL);
		kparam->ret_size = sizeof (uint64_t);
	}

	switch (param->ctpm_id) {
	case CTPP_SUBSUME:
		*param_value = ctp->ctp_subsume ?
		    ctp->ctp_subsume->ct_id : 0;
		break;
	case CTPP_PARAMS:
		*param_value = ctp->ctp_params;
		break;
	case CTPP_SVC_FMRI:
		if (ctp->ctp_svc_fmri == NULL) {
			kparam->ret_size =
			    strlcpy((char *)kparam->ctpm_kbuf,
			    CT_PR_SVC_DEFAULT, param->ctpm_size);
		} else {
			kparam->ret_size =
			    strlcpy((char *)kparam->ctpm_kbuf,
			    refstr_value(ctp->ctp_svc_fmri), param->ctpm_size);
		}
		kparam->ret_size++;
		break;
	case CTPP_CREATOR_AUX:
		if (ctp->ctp_svc_aux == NULL) {
			kparam->ret_size =
			    strlcpy((char *)kparam->ctpm_kbuf,
			    refstr_value(conp_svc_aux_default),
			    param->ctpm_size);
		} else {
			kparam->ret_size =
			    strlcpy((char *)kparam->ctpm_kbuf,
			    refstr_value(ctp->ctp_svc_aux), param->ctpm_size);
		}
		kparam->ret_size++;
		break;
	case CTPP_EV_FATAL:
		*param_value = ctp->ctp_ev_fatal;
		break;
	default:
		return (EINVAL);
	}

	return (0);
}

static ctmplops_t ctmpl_process_ops = {
	ctmpl_process_dup,		/* ctop_dup */
	ctmpl_process_free,		/* ctop_free */
	ctmpl_process_set,		/* ctop_set */
	ctmpl_process_get,		/* ctop_get */
	ctmpl_create_inval,		/* ctop_create */
	CT_PR_ALLEVENT
};


/*
 * Process contract implementation
 */

/*
 * ctmpl_process_default
 *
 * The process contract default template entry point.  Creates a
 * process contract template with no parameters set, with informative
 * core and signal events, critical empty and hwerr events, and fatal
 * hwerr events.
 */
static ct_template_t *
contract_process_default(void)
{
	ctmpl_process_t *new;

	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
	ctmpl_init(&new->ctp_ctmpl, &ctmpl_process_ops, process_type, new);

	new->ctp_subsume = NULL;
	new->ctp_params = 0;
	new->ctp_ctmpl.ctmpl_ev_info = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
	new->ctp_ctmpl.ctmpl_ev_crit = CT_PR_EV_EMPTY | CT_PR_EV_HWERR;
	new->ctp_ev_fatal = CT_PR_EV_HWERR;
	new->ctp_svc_fmri = NULL;
	new->ctp_svc_aux = NULL;

	return (&new->ctp_ctmpl);
}

/*
 * contract_process_free
 *
 * The process contract free entry point.
 */
static void
contract_process_free(contract_t *ct)
{
	cont_process_t *ctp = ct->ct_data;
	crfree(ctp->conp_cred);
	list_destroy(&ctp->conp_members);
	list_destroy(&ctp->conp_inherited);
	if (ctp->conp_svc_fmri != NULL) {
		refstr_rele(ctp->conp_svc_fmri);
	}
	if (ctp->conp_svc_aux != NULL) {
		refstr_rele(ctp->conp_svc_aux);
	}
	if (ctp->conp_svc_creator != NULL) {
		refstr_rele(ctp->conp_svc_creator);
	}
	kmem_free(ctp, sizeof (cont_process_t));
}

/*
 * contract_process_cankill
 *
 * Determine if the contract author had or if the process generating
 * the event, sp, has adequate privileges to kill process tp.
 */
static int
contract_process_cankill(proc_t *tp, proc_t *sp, cont_process_t *ctp)
{
	int cankill;

	mutex_enter(&tp->p_crlock);
	cankill = hasprocperm(tp->p_cred, ctp->conp_cred);
	mutex_exit(&tp->p_crlock);
	if (cankill || (sp && prochasprocperm(tp, sp, CRED())))
		return (1);

	return (0);
}

/*
 * contract_process_kill
 *
 * Kills all processes in a contract, or all processes in the
 * intersection of a contract and ex's process group (if ex is non-NULL
 * and the contract's PGRPONLY parameter is set).  If checkpriv is
 * true, only those processes which may be signaled by the contract
 * author or ex are killed.
 */
static void
contract_process_kill(contract_t *ct, proc_t *ex, int checkpriv)
{
	cont_process_t *ctp = ct->ct_data;
	proc_t *p;
	pid_t pgrp = -1;

	ASSERT(MUTEX_HELD(&ct->ct_lock));

	if (ex && (ctp->conp_params & CT_PR_PGRPONLY)) {
		pgrp = ex->p_pgrp;
		mutex_enter(&pidlock);
	}

	for (p = list_head(&ctp->conp_members); p != NULL;
	    p = list_next(&ctp->conp_members, p)) {
		if ((p == ex) ||
		    (pgrp != -1 && (p->p_stat == SIDL || p->p_pgrp != pgrp)) ||
		    (checkpriv && !contract_process_cankill(p, ex, ctp)))
			continue;

		psignal(p, SIGKILL);
	}

	if (pgrp != -1)
		mutex_exit(&pidlock);
}


/*
 * contract_process_accept
 *
 * Tests if the process contract is willing to act as a regent for
 * inherited contracts.  Though brief and only called from one place,
 * this functionality is kept here to avoid including knowledge of
 * process contract implementation in the generic contract code.
 */
int
contract_process_accept(contract_t *parent)
{
	cont_process_t *ctp = parent->ct_data;

	ASSERT(parent->ct_type == process_type);

	return (ctp->conp_params & CT_PR_REGENT);
}

/*
 * contract_process_take
 *
 * Executes the process contract side of inheriting a contract.
 */
void
contract_process_take(contract_t *parent, contract_t *child)
{
	cont_process_t *ctp = parent->ct_data;

	ASSERT(MUTEX_HELD(&parent->ct_lock));
	ASSERT(MUTEX_HELD(&child->ct_lock));
	ASSERT(parent->ct_type == process_type);
	ASSERT(ctp->conp_params & CT_PR_REGENT);

	list_insert_head(&ctp->conp_inherited, child);
	ctp->conp_ninherited++;
}

/*
 * contract_process_adopt
 *
 * Executes the process contract side of adopting a contract.
 */
void
contract_process_adopt(contract_t *ct, proc_t *p)
{
	cont_process_t *parent = p->p_ct_process;

	ASSERT(MUTEX_HELD(&parent->conp_contract.ct_lock));
	ASSERT(MUTEX_HELD(&ct->ct_lock));

	list_remove(&parent->conp_inherited, ct);
	parent->conp_ninherited--;

	/*
	 * We drop the parent lock first because a) we are passing the
	 * contract reference to the child, and b) contract_adopt
	 * expects us to return with the contract lock held.
	 */
	mutex_exit(&parent->conp_contract.ct_lock);
}

/*
 * contract_process_abandon
 *
 * The process contract abandon entry point.
 */
static void
contract_process_abandon(contract_t *ct)
{
	cont_process_t *ctp = ct->ct_data;

	ASSERT(MUTEX_HELD(&ct->ct_lock));

	/*
	 * Shall we stay or shall we go?
	 */
	if (list_head(&ctp->conp_members) == NULL) {
		contract_destroy(ct);
	} else {
		/*
		 * Strictly speaking, we actually do orphan the contract.
		 * Assuming our credentials allow us to kill all
		 * processes in the contract, this is only temporary.
		 */
		if (ctp->conp_params & CT_PR_NOORPHAN)
			contract_process_kill(ct, NULL, B_TRUE);
		contract_orphan(ct);
		mutex_exit(&ct->ct_lock);
		contract_rele(ct);
	}
}

/*
 * contract_process_destroy
 *
 * The process contract destroy entry point.
 */
static void
contract_process_destroy(contract_t *ct)
{
	cont_process_t *ctp = ct->ct_data;
	contract_t *cct;

	ASSERT(MUTEX_HELD(&ct->ct_lock));

	/*
	 * contract_destroy all empty children, kill or orphan the rest
	 */
	while (cct = list_head(&ctp->conp_inherited)) {
		mutex_enter(&cct->ct_lock);

		ASSERT(cct->ct_state == CTS_INHERITED);

		list_remove(&ctp->conp_inherited, cct);
		ctp->conp_ninherited--;
		cct->ct_regent = NULL;
		cct->ct_type->ct_type_ops->contop_abandon(cct);
	}
}

/*
 * contract_process_status
 *
 * The process contract status entry point.
 */
static void
contract_process_status(contract_t *ct, zone_t *zone, int detail, nvlist_t *nvl,
    void *status, model_t model)
{
	cont_process_t *ctp = ct->ct_data;
	uint32_t *pids, *ctids;
	uint_t npids, nctids;
	uint_t spids, sctids;
	ctid_t local_svc_zone_enter;

	if (detail == CTD_FIXED) {
		mutex_enter(&ct->ct_lock);
		contract_status_common(ct, zone, status, model);
		local_svc_zone_enter = ctp->conp_svc_zone_enter;
		mutex_exit(&ct->ct_lock);
		pids = NULL;
		ctids = NULL;
	} else {
		contract_t *cnext;
		proc_t *pnext;
		uint_t loc;

		ASSERT(detail == CTD_ALL);
		mutex_enter(&ct->ct_lock);
		for (;;) {
			spids = ctp->conp_nmembers + 5;
			sctids = ctp->conp_ninherited + 5;
			mutex_exit(&ct->ct_lock);

			pids = kmem_alloc(spids * sizeof (uint32_t), KM_SLEEP);
			ctids = kmem_alloc(sctids * sizeof (uint32_t),
			    KM_SLEEP);

			mutex_enter(&ct->ct_lock);
			npids = ctp->conp_nmembers;
			nctids = ctp->conp_ninherited;
			if (spids >= npids && sctids >= nctids)
				break;

			kmem_free(pids, spids * sizeof (uint32_t));
			kmem_free(ctids, sctids * sizeof (uint32_t));
		}
		contract_status_common(ct, zone, status, model);
		for (loc = 0, cnext = list_head(&ctp->conp_inherited); cnext;
		    cnext = list_next(&ctp->conp_inherited, cnext))
			ctids[loc++] = cnext->ct_id;
		ASSERT(loc == nctids);
		for (loc = 0, pnext = list_head(&ctp->conp_members); pnext;
		    pnext = list_next(&ctp->conp_members, pnext))
			pids[loc++] = pnext->p_pid;
		ASSERT(loc == npids);
		local_svc_zone_enter = ctp->conp_svc_zone_enter;
		mutex_exit(&ct->ct_lock);
	}

	/*
	 * Contract terms are static; there's no need to hold the
	 * contract lock while accessing them.
	 */
	VERIFY(nvlist_add_uint32(nvl, CTPS_PARAMS, ctp->conp_params) == 0);
	VERIFY(nvlist_add_uint32(nvl, CTPS_EV_FATAL, ctp->conp_ev_fatal) == 0);
	if (detail == CTD_ALL) {
		VERIFY(nvlist_add_uint32_array(nvl, CTPS_MEMBERS, pids,
		    npids) == 0);
		VERIFY(nvlist_add_uint32_array(nvl, CTPS_CONTRACTS, ctids,
		    nctids) == 0);
		VERIFY(nvlist_add_string(nvl, CTPS_CREATOR_AUX,
		    refstr_value(ctp->conp_svc_aux)) == 0);
		VERIFY(nvlist_add_string(nvl, CTPS_SVC_CREATOR,
		    refstr_value(ctp->conp_svc_creator)) == 0);
	}
	if (ctids != NULL)
		kmem_free(ctids, sctids * sizeof (uint32_t));
	if (pids != NULL)
		kmem_free(pids, spids * sizeof (uint32_t));

	/*
	 * if we are in a local zone and svc_fmri was inherited from
	 * the global zone, we provide fake svc_fmri and svc_ctid
	 */
	if (local_svc_zone_enter == 0 ||
	    zone->zone_uniqid == GLOBAL_ZONEUNIQID) {
		if (detail > CTD_COMMON) {
			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
			    ctp->conp_svc_ctid) == 0);
			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
			    refstr_value(ctp->conp_svc_fmri)) == 0);
		}
	} else {
		if (detail > CTD_COMMON) {
			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
			    local_svc_zone_enter) == 0);
			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
			    CT_PR_SVC_FMRI_ZONE_ENTER) == 0);
		}
	}
}

/*ARGSUSED*/
static int
contract_process_newct(contract_t *ct)
{
	return (0);
}

/* process contracts don't negotiate */
static contops_t contract_process_ops = {
	contract_process_free,		/* contop_free */
	contract_process_abandon,	/* contop_abandon */
	contract_process_destroy,	/* contop_destroy */
	contract_process_status,	/* contop_status */
	contract_ack_inval,		/* contop_ack */
	contract_ack_inval,		/* contop_nack */
	contract_qack_inval,		/* contop_qack */
	contract_process_newct		/* contop_newct */
};

/*
 * contract_process_init
 *
 * Initializes the process contract type.  Also creates a template for
 * use by newproc() when it creates user processes.
 */
void
contract_process_init(void)
{
	process_type = contract_type_init(CTT_PROCESS, "process",
	    &contract_process_ops, contract_process_default);

	/*
	 * Create a template for use with init(8) and other
	 * kernel-started processes.
	 */
	sys_process_tmpl = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
	ctmpl_init(&sys_process_tmpl->ctp_ctmpl, &ctmpl_process_ops,
	    process_type, sys_process_tmpl);
	sys_process_tmpl->ctp_subsume = NULL;
	sys_process_tmpl->ctp_params = CT_PR_NOORPHAN;
	sys_process_tmpl->ctp_ev_fatal = CT_PR_EV_HWERR;
	sys_process_tmpl->ctp_svc_fmri =
	    refstr_alloc("svc:/system/init:default");
	sys_process_tmpl->ctp_svc_aux = refstr_alloc("");
	conp_svc_aux_default = sys_process_tmpl->ctp_svc_aux;
	refstr_hold(conp_svc_aux_default);
}

/*
 * contract_process_create
 *
 * create a process contract given template "tmpl" and parent process
 * "parent".  May fail and return NULL if project.max-contracts would
 * have been exceeded.
 */
static cont_process_t *
contract_process_create(ctmpl_process_t *tmpl, proc_t *parent, int canfail)
{
	cont_process_t *ctp;

	ASSERT(tmpl != NULL);

	(void) contract_type_pbundle(process_type, parent);

	ctp = kmem_zalloc(sizeof (cont_process_t), KM_SLEEP);

	list_create(&ctp->conp_members, sizeof (proc_t),
	    offsetof(proc_t, p_ct_member));
	list_create(&ctp->conp_inherited, sizeof (contract_t),
	    offsetof(contract_t, ct_ctlist));
	mutex_enter(&tmpl->ctp_ctmpl.ctmpl_lock);
	ctp->conp_params = tmpl->ctp_params;
	ctp->conp_ev_fatal = tmpl->ctp_ev_fatal;
	crhold(ctp->conp_cred = CRED());

	if (contract_ctor(&ctp->conp_contract, process_type, &tmpl->ctp_ctmpl,
	    ctp, (ctp->conp_params & CT_PR_INHERIT) ? CTF_INHERIT : 0,
	    parent, canfail)) {
		mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
		contract_process_free(&ctp->conp_contract);
		return (NULL);
	}

	/*
	 * inherit svc_fmri if not defined by consumer. In this case, inherit
	 * also svc_ctid to keep track of the contract id where
	 * svc_fmri was set
	 */
	if (tmpl->ctp_svc_fmri == NULL) {
		ctp->conp_svc_fmri = parent->p_ct_process->conp_svc_fmri;
		ctp->conp_svc_ctid = parent->p_ct_process->conp_svc_ctid;
		ctp->conp_svc_zone_enter =
		    parent->p_ct_process->conp_svc_zone_enter;
	} else {
		ctp->conp_svc_fmri = tmpl->ctp_svc_fmri;
		ctp->conp_svc_ctid = ctp->conp_contract.ct_id;
		/* make svc_zone_enter flag false when svc_fmri is set */
		ctp->conp_svc_zone_enter = 0;
	}
	refstr_hold(ctp->conp_svc_fmri);
	/* set svc_aux to default value if not defined in template */
	if (tmpl->ctp_svc_aux == NULL) {
		ctp->conp_svc_aux = conp_svc_aux_default;
	} else {
		ctp->conp_svc_aux = tmpl->ctp_svc_aux;
	}
	refstr_hold(ctp->conp_svc_aux);
	/*
	 * set svc_creator to execname
	 * We special case pid0 because when newproc() creates
	 * the init process, the p_user.u_comm field of sched's proc_t
	 * has not been populated yet.
	 */
	if (parent->p_pidp == &pid0) /* if the kernel is the creator */
		ctp->conp_svc_creator = refstr_alloc("sched");
	else
		ctp->conp_svc_creator = refstr_alloc(parent->p_user.u_comm);

	/*
	 * Transfer subcontracts only after new contract is visible.
	 * Also, only transfer contracts if the parent matches -- we
	 * don't want to create a cycle in the tree of contracts.
	 */
	if (tmpl->ctp_subsume && tmpl->ctp_subsume->ct_owner == parent) {
		cont_process_t *sct = tmpl->ctp_subsume->ct_data;
		contract_t *ct;

		mutex_enter(&tmpl->ctp_subsume->ct_lock);
		mutex_enter(&ctp->conp_contract.ct_lock);
		while (ct = list_head(&sct->conp_inherited)) {
			mutex_enter(&ct->ct_lock);
			list_remove(&sct->conp_inherited, ct);
			list_insert_tail(&ctp->conp_inherited, ct);
			ct->ct_regent = &ctp->conp_contract;
			mutex_exit(&ct->ct_lock);
		}
		ctp->conp_ninherited += sct->conp_ninherited;
		sct->conp_ninherited = 0;
		mutex_exit(&ctp->conp_contract.ct_lock);
		mutex_exit(&tmpl->ctp_subsume->ct_lock);

		/*
		 * Automatically abandon the contract.
		 */
		(void) contract_abandon(tmpl->ctp_subsume, parent, 1);
	}

	mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);

	return (ctp);
}

/*
 * contract_process_exit
 *
 * Called on process exit.  Removes process p from process contract
 * ctp.  Generates an exit event, if requested.  Generates an empty
 * event, if p is the last member of the the process contract and empty
 * events were requested.
 */
void
contract_process_exit(cont_process_t *ctp, proc_t *p, int exitstatus)
{
	contract_t *ct = &ctp->conp_contract;
	ct_kevent_t *event;
	int empty;

	/*
	 * Remove self from process contract.
	 */
	mutex_enter(&ct->ct_lock);
	list_remove(&ctp->conp_members, p);
	ctp->conp_nmembers--;
	mutex_enter(&p->p_lock);	/* in case /proc is watching */
	p->p_ct_process = NULL;
	mutex_exit(&p->p_lock);

	/*
	 * We check for emptiness before dropping the contract lock to
	 * send the exit event, otherwise we could end up with two
	 * empty events.
	 */
	empty = (list_head(&ctp->conp_members) == NULL);
	if (EVSENDP(ctp, CT_PR_EV_EXIT)) {
		nvlist_t *nvl;

		mutex_exit(&ct->ct_lock);
		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
		VERIFY(nvlist_add_int32(nvl, CTPE_EXITSTATUS, exitstatus) == 0);

		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
		event->cte_flags = EVINFOP(ctp, CT_PR_EV_EXIT) ? CTE_INFO : 0;
		event->cte_type = CT_PR_EV_EXIT;
		(void) cte_publish_all(ct, event, nvl, NULL);
		mutex_enter(&ct->ct_lock);
	}
	if (empty) {
		/*
		 * Send EMPTY message.
		 */
		if (EVSENDP(ctp, CT_PR_EV_EMPTY)) {
			nvlist_t *nvl;

			mutex_exit(&ct->ct_lock);
			VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
			    KM_SLEEP) == 0);
			VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);

			event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
			event->cte_flags = EVINFOP(ctp, CT_PR_EV_EMPTY) ?
			    CTE_INFO : 0;
			event->cte_type = CT_PR_EV_EMPTY;
			(void) cte_publish_all(ct, event, nvl, NULL);
			mutex_enter(&ct->ct_lock);
		}

		/*
		 * The last one to leave an orphaned contract turns out
		 * the lights.
		 */
		if (ct->ct_state == CTS_ORPHAN) {
			contract_destroy(ct);
			return;
		}
	}
	mutex_exit(&ct->ct_lock);
	contract_rele(ct);
}

/*
 * contract_process_fork
 *
 * Called on process fork.  If the current lwp has a active process
 * contract template, we attempt to create a new process contract.
 * Failure to create a process contract when required is a failure in
 * fork so, in such an event, we return NULL.
 *
 * Assuming we succeeded or skipped the previous step, we add the child
 * process to the new contract (success) or to the parent's process
 * contract (skip).  If requested, we also send a fork event to that
 * contract.
 *
 * Because contract_process_fork() may fail, and because we would
 * prefer that process contracts not be created for processes which
 * don't complete forking, this should be the last function called
 * before the "all clear" point in cfork.
 */
cont_process_t *
contract_process_fork(ctmpl_process_t *rtmpl, proc_t *cp, proc_t *pp,
    int canfail)
{
	contract_t *ct;
	cont_process_t *ctp;
	ct_kevent_t *event;
	ct_template_t *tmpl;

	if (rtmpl == NULL && (tmpl = ttolwp(curthread)->lwp_ct_active[
	    process_type->ct_type_index]) != NULL)
		rtmpl = tmpl->ctmpl_data;

	if (rtmpl == NULL)
		ctp = curproc->p_ct_process;
	else if ((ctp = contract_process_create(rtmpl, pp, canfail)) == NULL)
		return (NULL);

	ct = &ctp->conp_contract;
	/*
	 * Prevent contract_process_kill() from missing forked children
	 * by failing forks by parents that have just been killed.
	 * It's not worth hoisting the ctp test since contract creation
	 * is by no means the common case.
	 */
	mutex_enter(&ct->ct_lock);
	mutex_enter(&pp->p_lock);
	if (ctp == curproc->p_ct_process && (pp->p_flag & SKILLED) != 0 &&
	    canfail) {
		mutex_exit(&pp->p_lock);
		mutex_exit(&ct->ct_lock);
		return (NULL);
	}
	cp->p_ct_process = ctp;
	mutex_exit(&pp->p_lock);
	contract_hold(ct);
	list_insert_head(&ctp->conp_members, cp);
	ctp->conp_nmembers++;
	mutex_exit(&ct->ct_lock);
	if (EVSENDP(ctp, CT_PR_EV_FORK)) {
		nvlist_t *nvl;

		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, cp->p_pid) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PPID, pp->p_pid) == 0);

		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
		event->cte_flags = EVINFOP(ctp, CT_PR_EV_FORK) ? CTE_INFO : 0;
		event->cte_type = CT_PR_EV_FORK;
		(void) cte_publish_all(ct, event, nvl, NULL);
	}
	return (ctp);
}

/*
 * contract_process_core
 *
 * Called on core file generation attempts.  Generates a core event, if
 * requested, containing the names of the process, global, and
 * system-global ("zone") core files.  If dumping core is in the fatal
 * event set, calls contract_process_kill().
 */
void
contract_process_core(cont_process_t *ctp, proc_t *p, int sig,
    const char *process, const char *global, const char *zone)
{
	contract_t *ct = &ctp->conp_contract;

	if (EVSENDP(ctp, CT_PR_EV_CORE)) {
		ct_kevent_t *event;
		nvlist_t *nvl, *gnvl = NULL;

		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
		if (process)
			VERIFY(nvlist_add_string(nvl, CTPE_PCOREFILE,
			    (char *)process) == 0);
		if (global)
			VERIFY(nvlist_add_string(nvl, CTPE_GCOREFILE,
			    (char *)global) == 0);

		if (zone) {
			/*
			 * Only the global zone is informed of the
			 * local-zone generated global-zone core.
			 */
			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
			    KM_SLEEP) == 0);
			VERIFY(nvlist_add_string(gnvl, CTPE_ZCOREFILE,
			    (char *)zone) == 0);
		}

		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
		event->cte_flags = EVINFOP(ctp, CT_PR_EV_CORE) ? CTE_INFO : 0;
		event->cte_type = CT_PR_EV_CORE;
		(void) cte_publish_all(ct, event, nvl, gnvl);
	}

	if (EVFATALP(ctp, CT_PR_EV_CORE)) {
		mutex_enter(&ct->ct_lock);
		contract_process_kill(ct, p, B_TRUE);
		mutex_exit(&ct->ct_lock);
	}
}

/*
 * contract_process_hwerr
 *
 * Called when a process is killed by an unrecoverable hardware error.
 * Generates an hwerr event, if requested.  If hardware errors are in
 * the fatal event set, calls contract_process_kill().
 */
void
contract_process_hwerr(cont_process_t *ctp, proc_t *p)
{
	contract_t *ct = &ctp->conp_contract;

	if (EVSENDP(ctp, CT_PR_EV_HWERR)) {
		ct_kevent_t *event;
		nvlist_t *nvl;

		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);

		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
		event->cte_flags = EVINFOP(ctp, CT_PR_EV_HWERR) ? CTE_INFO : 0;
		event->cte_type = CT_PR_EV_HWERR;
		(void) cte_publish_all(ct, event, nvl, NULL);
	}

	if (EVFATALP(ctp, CT_PR_EV_HWERR)) {
		mutex_enter(&ct->ct_lock);
		contract_process_kill(ct, p, B_FALSE);
		mutex_exit(&ct->ct_lock);
	}
}

/*
 * contract_process_sig
 *
 * Called when a process is killed by a signal originating from a
 * process outside of its process contract or its process contract's
 * holder.  Generates an signal event, if requested, containing the
 * signal number, and the sender's pid and contract id (if available).
 * If signals are in the fatal event set, calls
 * contract_process_kill().
 */
void
contract_process_sig(cont_process_t *ctp, proc_t *p, int sig, pid_t pid,
    ctid_t ctid, zoneid_t zoneid)
{
	contract_t *ct = &ctp->conp_contract;

	if (EVSENDP(ctp, CT_PR_EV_SIGNAL)) {
		ct_kevent_t *event;
		nvlist_t *dest, *nvl, *gnvl = NULL;

		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);

		if (zoneid >= 0 && p->p_zone->zone_id != zoneid) {
			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
			    KM_SLEEP) == 0);
			dest = gnvl;
		} else {
			dest = nvl;
		}

		if (pid != -1)
			VERIFY(nvlist_add_uint32(dest, CTPE_SENDER, pid) == 0);
		if (ctid != 0)
			VERIFY(nvlist_add_uint32(dest, CTPE_SENDCT, ctid) == 0);

		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
		event->cte_flags = EVINFOP(ctp, CT_PR_EV_SIGNAL) ? CTE_INFO : 0;
		event->cte_type = CT_PR_EV_SIGNAL;
		(void) cte_publish_all(ct, event, nvl, gnvl);
	}

	if (EVFATALP(ctp, CT_PR_EV_SIGNAL)) {
		mutex_enter(&ct->ct_lock);
		contract_process_kill(ct, p, B_TRUE);
		mutex_exit(&ct->ct_lock);
	}
}