|
root / base / usr / src / lib / libpctx / common
common Plain Text 1160 lines 27.6 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
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This file contains a set of generic routines for periodically
 * sampling the state of another process, or tree of processes.
 *
 * It is built upon the infrastructure provided by libproc.
 */

#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <libproc.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <ctype.h>
#include <libintl.h>
#include <libcpc.h>
#include <sys/cpc_impl.h>

#include "libpctx.h"

struct __pctx {
	pctx_errfn_t *errfn;
	struct ps_prochandle *Pr;
	void *uarg;
	pctx_sysc_execfn_t *exec;
	pctx_sysc_forkfn_t *fork;
	pctx_sysc_exitfn_t *exit;
	pctx_sysc_lwp_createfn_t *lwp_create;
	pctx_init_lwpfn_t *init_lwp;
	pctx_fini_lwpfn_t *fini_lwp;
	pctx_sysc_lwp_exitfn_t *lwp_exit;
	int verbose;
	int created;
	int sigblocked;
	int terminate;
	sigset_t savedset;
	cpc_t *cpc;
};

static void (*pctx_cpc_callback)(cpc_t *cpc, struct __pctx *pctx);

static void
pctx_default_errfn(const char *fn, const char *fmt, va_list ap)
{
	(void) fprintf(stderr, "libpctx: pctx_%s: ", fn);
	(void) vfprintf(stderr, fmt, ap);
}

/*PRINTFLIKE3*/
static void
pctx_error(pctx_t *pctx, const char *fn, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	pctx->errfn(fn, fmt, ap);
	va_end(ap);
}

/*
 * Create a new process and bind the user args for it
 */
pctx_t *
pctx_create(
    const char *filename,
    char *const *argv,
    void *arg,
    int verbose,
    pctx_errfn_t *errfn)
{
	static const char fn[] = "create";
	int err;
	pctx_t *pctx;

	pctx = calloc(1, sizeof (*pctx));
	pctx->uarg = arg;
	pctx->verbose = verbose;
	pctx->terminate = 0;
	pctx->errfn = errfn ? errfn : pctx_default_errfn;

	if ((pctx->Pr = Pcreate(filename, argv, &err, 0, 0)) == NULL) {
		switch (err) {
		case C_PERM:
			pctx_error(pctx, fn, gettext("cannot trace set-id or "
			    "unreadable program '%s'\n"), filename);
			break;
		case C_LP64:
			pctx_error(pctx, fn, gettext("cannot control LP64 "
			    "program '%s'\n"), filename);
			break;
		case C_NOEXEC:
			pctx_error(pctx, fn, gettext("cannot execute "
			    "program '%s'\n"), filename);
			break;
		case C_NOENT:
			pctx_error(pctx, fn, gettext("cannot find"
			    "program '%s'\n"), filename);
			break;
		case C_FORK:
			pctx_error(pctx, fn, gettext("cannot fork, "
			    "program '%s'\n"), filename);
			break;
		default:
			pctx_error(pctx, fn, gettext("%s, program '%s'\n"),
			    Pcreate_error(err), filename);
			break;
		}
		free(pctx);
		return (NULL);
	}

	if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
		pctx_error(pctx, fn,
		    gettext("can't stop-on-exit() program '%s'\n"), filename);
		Prelease(pctx->Pr, PRELEASE_KILL);
		free(pctx);
		return (NULL);
	}
	/*
	 * Set kill-on-last-close so the controlled process
	 * dies if we die.
	 */
	pctx->created = 1;
	(void) Psetflags(pctx->Pr, PR_KLC);
	(void) pctx_set_events(pctx, PCTX_NULL_EVENT);

	return (pctx);
}

/*
 * Capture an existing process and bind the user args for it
 */
pctx_t *
pctx_capture(pid_t pid, void *arg, int verbose, pctx_errfn_t *errfn)
{
	static const char fn[] = "capture";
	int err;
	pctx_t *pctx;

	pctx = calloc(1, sizeof (*pctx));
	pctx->uarg = arg;
	pctx->verbose = verbose;
	pctx->errfn = errfn ? errfn : pctx_default_errfn;

	if ((pctx->Pr = Pgrab(pid, 0, &err)) == NULL) {
		switch (err) {
		case G_NOPROC:
			pctx_error(pctx, fn,
			    gettext("pid %d doesn't exist\n"), (int)pid);
			break;
		case G_ZOMB:
			pctx_error(pctx, fn,
			    gettext("pid %d is a zombie\n"), (int)pid);
			break;
		case G_PERM:
			pctx_error(pctx, fn,
			    gettext("pid %d: permission denied\n"), (int)pid);
			break;
		case G_BUSY:
			pctx_error(pctx, fn,
			    gettext("pid %d is already being traced\n"),
			    (int)pid);
			break;
		case G_SYS:
			pctx_error(pctx, fn,
			    gettext("pid %d is a system process\n"), (int)pid);
			break;
		case G_SELF:
			pctx_error(pctx, fn,
			    gettext("cannot capture self!\n"));
			break;
		case G_LP64:
			pctx_error(pctx, fn, gettext("cannot control LP64 "
			    "process, pid %d\n"), (int)pid);
			break;
		default:
			pctx_error(pctx, fn, gettext("%s: pid %d\n"),
			    Pgrab_error(err), (int)pid);
			break;
		}
		free(pctx);
		return (NULL);
	}

	if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
		pctx_error(pctx, fn,
		    gettext("can't stop-on-exit() pid %d\n"), (int)pid);
		Prelease(pctx->Pr, PRELEASE_CLEAR);
		free(pctx);
		return (NULL);
	}

	/*
	 * Set run-on-last-close so the controlled process
	 * runs even if we die on a signal.  This is because
	 * we grabbed an existing process - it would be impolite
	 * to cause it to die if we exit prematurely.
	 */
	pctx->created = 0;
	(void) Psetflags(pctx->Pr, PR_RLC);
	(void) pctx_set_events(pctx, PCTX_NULL_EVENT);

	return (pctx);
}

/*ARGSUSED*/
static void
default_void(pctx_t *pctx)
{}

/*ARGSUSED*/
static int
default_int(pctx_t *pctx)
{
	return (0);
}

int
pctx_set_events(pctx_t *pctx, ...)
{
	static const char fn[] = "set_events";
	va_list pvar;
	int error = 0;
	pctx_event_t event;

	va_start(pvar, pctx);
	do {
		switch (event = (pctx_event_t)va_arg(pvar, pctx_event_t)) {
		case PCTX_NULL_EVENT:
			break;
		case PCTX_SYSC_EXEC_EVENT:
			pctx->exec = (pctx_sysc_execfn_t *)
			    va_arg(pvar, pctx_sysc_execfn_t *);
			break;
		case PCTX_SYSC_FORK_EVENT:
			pctx->fork = (pctx_sysc_forkfn_t *)
			    va_arg(pvar, pctx_sysc_forkfn_t *);
			break;
		case PCTX_SYSC_EXIT_EVENT:	/* always intercepted */
			pctx->exit = (pctx_sysc_exitfn_t *)
			    va_arg(pvar, pctx_sysc_exitfn_t *);
			break;
		case PCTX_SYSC_LWP_CREATE_EVENT:
			pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)
			    va_arg(pvar, pctx_sysc_lwp_createfn_t *);
			break;
		case PCTX_INIT_LWP_EVENT:
			pctx->init_lwp = (pctx_init_lwpfn_t *)
			    va_arg(pvar, pctx_init_lwpfn_t *);
			break;
		case PCTX_FINI_LWP_EVENT:
			pctx->fini_lwp = (pctx_fini_lwpfn_t *)
			    va_arg(pvar, pctx_fini_lwpfn_t *);
			break;
		case PCTX_SYSC_LWP_EXIT_EVENT:
			pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)
			    va_arg(pvar, pctx_sysc_lwp_exitfn_t *);
			break;
		default:
			pctx_error(pctx, fn,
			    gettext("unknown event type %x\n"), event);
			error = -1;
			break;
		}
	} while (event != PCTX_NULL_EVENT && error == 0);
	va_end(pvar);

	if (error != 0)
		return (error);

	if (pctx->exec == NULL)
		pctx->exec = (pctx_sysc_execfn_t *)(uintptr_t)default_int;
	if (pctx->fork == NULL)
		pctx->fork = (pctx_sysc_forkfn_t *)(uintptr_t)default_void;
	if (pctx->exit == NULL)
		pctx->exit = (pctx_sysc_exitfn_t *)(uintptr_t)default_void;
	if (pctx->lwp_create == NULL)
		pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)
		    (uintptr_t)default_int;
	if (pctx->init_lwp == NULL)
		pctx->init_lwp = (pctx_init_lwpfn_t *)(uintptr_t)default_int;
	if (pctx->fini_lwp == NULL)
		pctx->fini_lwp = (pctx_fini_lwpfn_t *)(uintptr_t)default_int;
	if (pctx->lwp_exit == NULL)
		pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)
		    (uintptr_t)default_int;

	if ((uintptr_t)pctx->fork != (uintptr_t)default_void) {
		(void) Psysexit(pctx->Pr, SYS_vfork, 1);
		(void) Psysexit(pctx->Pr, SYS_forksys, 1);
		if (Psetflags(pctx->Pr, PR_FORK) == -1)
			error = -1;
	} else {
		(void) Psysexit(pctx->Pr, SYS_vfork, 0);
		(void) Psysexit(pctx->Pr, SYS_forksys, 0);
		if (Punsetflags(pctx->Pr, PR_FORK) == -1)
			error = -1;
	}

	/*
	 * exec causes termination of all but the exec-ing lwp,
	 * and resets the lwpid to one in the new address space.
	 */
	if ((uintptr_t)pctx->exec != (uintptr_t)default_int ||
	    (uintptr_t)pctx->fini_lwp != (uintptr_t)default_int ||
	    (uintptr_t)pctx->init_lwp != (uintptr_t)default_int) {
		(void) Psysexit(pctx->Pr, SYS_execve, 1);
		(void) Psysentry(pctx->Pr, SYS_execve, 1);
	} else {
		(void) Psysexit(pctx->Pr, SYS_execve, 0);
		(void) Psysentry(pctx->Pr, SYS_execve, 0);
	}

	(void) Psysexit(pctx->Pr, SYS_lwp_create,
	    (uintptr_t)pctx->lwp_create != (uintptr_t)default_int ||
	    (uintptr_t)pctx->init_lwp != (uintptr_t)default_int);

	(void) Psysentry(pctx->Pr, SYS_lwp_exit,
	    (uintptr_t)pctx->lwp_exit != (uintptr_t)default_int ||
	    (uintptr_t)pctx->fini_lwp != (uintptr_t)default_int);

	return (0);
}

static sigset_t termsig;

static void
__libpctx_init(void)
{
	/*
	 * Initialize the signal set used to shield ourselves from
	 * death-by-terminal-signal while the agent lwp is running.
	 */
	(void) sigemptyset(&termsig);
	(void) sigaddset(&termsig, SIGHUP);
	(void) sigaddset(&termsig, SIGTERM);
	(void) sigaddset(&termsig, SIGINT);
	(void) sigaddset(&termsig, SIGQUIT);
}

#pragma init(__libpctx_init)

static void
pctx_begin_syscalls(pctx_t *pctx)
{
	if (pctx->Pr == NULL)
		return;
	if (pctx->sigblocked++ == 0) {
		(void) sigprocmask(SIG_BLOCK, &termsig, &pctx->savedset);
		(void) Pcreate_agent(pctx->Pr);
	}
}

static void
pctx_end_syscalls(pctx_t *pctx)
{
	if (pctx->Pr == NULL)
		return;
	if (--pctx->sigblocked == 0) {
		(void) Pdestroy_agent(pctx->Pr);
		(void) sigprocmask(SIG_SETMASK, &pctx->savedset, NULL);
	}
}

/*
 * Iterate over the valid lwpids in the process, invoking the
 * action function on each one.
 */
static int
pctx_lwpiterate(pctx_t *pctx, int (*action)(pctx_t *, pid_t, id_t, void *))
{
	const pstatus_t *pstatus;
	char lstatus[64];
	struct stat statb;
	lwpstatus_t *lwps;
	prheader_t *prh;
	int fd, nlwp;
	int ret = 0;

	if ((uintptr_t)action == (uintptr_t)default_int)
		return (0);

	pstatus = Pstatus(pctx->Pr);
	if (pstatus->pr_nlwp <= 1) {
		pctx_begin_syscalls(pctx);
		ret = action(pctx, pstatus->pr_pid, 1, pctx->uarg);
		pctx_end_syscalls(pctx);
		return (ret);
	}

	(void) snprintf(lstatus, sizeof (lstatus),
	    "/proc/%d/lstatus", (int)pstatus->pr_pid);

	if ((fd = open(lstatus, O_RDONLY)) < 0 ||
	    fstat(fd, &statb) != 0) {
		if (fd >= 0)
			(void) close(fd);
		return (-1);
	}

	prh = malloc(statb.st_size);
	if (read(fd, prh, statb.st_size) <
	    sizeof (prheader_t) + sizeof (lwpstatus_t)) {
		(void) close(fd);
		free(prh);
		return (-1);
	}
	(void) close(fd);

	/* LINTED pointer cast may result in improper alignment */
	lwps = (lwpstatus_t *)(prh + 1);
	pctx_begin_syscalls(pctx);
	for (nlwp = prh->pr_nent; nlwp > 0; nlwp--) {
		if (action(pctx,
		    pstatus->pr_pid, lwps->pr_lwpid, pctx->uarg) != 0)
			ret = -1;
		/* LINTED pointer cast may result in improper alignment */
		lwps = (lwpstatus_t *)((char *)lwps + prh->pr_entsize);
	}
	pctx_end_syscalls(pctx);
	free(prh);
	return (ret);
}

/*
 * Free any associated state, but leave the process stopped if it
 * is still under our control.  (If it isn't under our control,
 * it should just run to completion when we do our last close)
 */
static void
pctx_free(pctx_t *pctx)
{
	if (pctx->cpc != NULL && pctx_cpc_callback != NULL)
		(*pctx_cpc_callback)(pctx->cpc, pctx);
	if (pctx->Pr) {
		Pfree(pctx->Pr);
		pctx->Pr = NULL;
	}
	pctx->errfn = pctx_default_errfn;
}

/*
 * Completely release the process from our control and discard all our state
 */
void
pctx_release(pctx_t *pctx)
{
	if (pctx->Pr) {
		Prelease(pctx->Pr, PRELEASE_CLEAR);
		pctx->Pr = NULL;
	}

	pctx_free(pctx);
	bzero(pctx, sizeof (*pctx));
	free(pctx);
}

static void
msincr(struct timeval *tv, uint_t msec)
{
	tv->tv_sec += msec / MILLISEC;
	tv->tv_usec += (msec % MILLISEC) * MILLISEC;
	if (tv->tv_usec > MICROSEC) {
		tv->tv_sec++;
		tv->tv_usec -= MICROSEC;
	}
}

static uint_t
msdiff(struct timeval *tva, struct timeval *tvb)
{
	time_t sdiff = tva->tv_sec - tvb->tv_sec;
	suseconds_t udiff = tva->tv_usec - tvb->tv_usec;

	if (sdiff < 0)
		return (0);
	if (udiff < 0) {
		udiff += MICROSEC;
		sdiff--;
	}
	if (sdiff < 0)
		return (0);
	if (sdiff >= (INT_MAX / MILLISEC))
		return ((uint_t)INT_MAX);
	return ((uint_t)(sdiff * MILLISEC + udiff / MILLISEC));
}

int
pctx_run(
	pctx_t *pctx,
	uint_t msec,
	uint_t nsamples,
	int (*tick)(pctx_t *, pid_t, id_t, void *))
{
	static const char fn[] = "run";
	struct timeval tvgoal, tvnow;
	uint_t mswait = 0;
	int running = 1;
	const pstatus_t *pstatus;
	psinfo_t psinfo;
	void (*sigsaved)();
	id_t lwpid;
	pid_t pid = Pstatus(pctx->Pr)->pr_pid;
	int pstate;

	if (msec == 0)
		nsamples = 0;
	if (nsamples == 0)
		nsamples = UINT_MAX;

	/*
	 * Casually discard any knowledge of the children we create
	 */
	sigsaved = signal(SIGCHLD, SIG_IGN);

	/*
	 * Since we've just "discovered" this process which might have
	 * been running for weeks, deliver some init_lwp events so
	 * that our caller gets a handle on the process.
	 */
	if (pctx_lwpiterate(pctx, pctx->init_lwp) != 0) {
		if (pctx->verbose)
			pctx_error(pctx, fn,
			    gettext("%d: lwp discovery failed\n"), (int)pid);
		goto bailout;
	}

	if (msec != 0) {
		/*
		 * tvgoal represents the time at which the sample
		 * should next be taken.
		 */
		(void) gettimeofday(&tvgoal, 0);
		msincr(&tvgoal, msec);
	}

	/*
	 * The event handling loop continues while running is 1.
	 * running becomes 0 when either the controlled process has
	 * exited successfully or the number of time samples has expired.
	 * Otherwise, if an error has occurred, running becomes -1.
	 */
	while (running == 1 && !pctx->terminate) {

		if (Psetrun(pctx->Pr, 0, 0) != 0) {
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("%d: Psetrun\n"), (int)pid);
			break;
		}

		if (msec != 0) {
			/*
			 * This timing loop attempts to estimate the number
			 * of milliseconds between our "goal" time (when
			 * we should stop the process and run the tick
			 * routine) and the current time.
			 *
			 * If we ever find ourselves running behind i.e. we
			 * missed our goal, then we skip ahead to the next
			 * goal instead.
			 */
			do {
				(void) gettimeofday(&tvnow, 0);
				if ((mswait = msdiff(&tvgoal, &tvnow)) == 0) {
					msincr(&tvgoal, msec);
					/*
					 * Skip ahead to the next goal, unless
					 * there is only one more sample left
					 * to take.
					 */
					if (nsamples != 1)
						nsamples--;
				}
			} while (mswait == 0 && !pctx->terminate);
		}

		if (pctx->terminate)
			goto bailout;
		else
			(void) Pwait(pctx->Pr, mswait);

checkstate:
		switch (pstate = Pstate(pctx->Pr)) {
		case PS_RUN:
			/*
			 * Try again, but wait for up to 5 seconds.
			 */
			if (Pstop(pctx->Pr, 5 * MILLISEC) == -1 ||
			    (pstate = Pstate(pctx->Pr)) != PS_STOP) {
				pctx_error(pctx, fn,
				    gettext("%d: won't stop\n"), (int)pid);
			}
			break;
		case PS_STOP:
			break;
		case PS_LOST:
			/*
			 * Lost control - probably execed a setuid/setgid
			 * executable.  Try and get control back again,
			 * else bail ..
			 */
			(void) Preopen(pctx->Pr);
			if ((pstate = Pstate(pctx->Pr)) != PS_LOST)
				goto checkstate;
			pctx_error(pctx, fn,
			    gettext("%d: execed a program that cannot "
			    "be tracked\n"), (int)pid);
			running = -1;
			break;
		case PS_UNDEAD:
		case PS_DEAD:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("%d: process terminated\n"),
				    (int)pid);
			running = -1;
			break;
		default:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("%d: process state 0x%x?\n"),
				    (int)pid, pstate);
			break;
		}

		if (pstate != PS_STOP)
			break;

		pstatus = Pstatus(pctx->Pr);
		lwpid = pstatus->pr_lwp.pr_lwpid;
		switch (pstatus->pr_lwp.pr_why) {
		case PR_REQUESTED:
			msincr(&tvgoal, msec);
			if (pstatus->pr_flags & PR_VFORKP) {
				/*
				 * The process is in a vfork stupor until
				 * its child releases it via an exec.
				 * Don't sample it while it's in this state
				 * - we won't be able to create the agent.
				 */
				break;
			}
			if (pctx_lwpiterate(pctx, tick) != 0)
				running = -1;
			if (running == 1 && --nsamples == 0)
				running = 0;
			break;
		case PR_SYSENTRY:
			switch (pstatus->pr_lwp.pr_what) {
			case SYS_lwp_exit:
				pctx_begin_syscalls(pctx);
				(void) pctx->fini_lwp(pctx,
				    pid, lwpid, pctx->uarg);
				(void) pctx->lwp_exit(pctx,
				    pid, lwpid, pctx->uarg);
				pctx_end_syscalls(pctx);
				break;
			case SYS_exit:
				if (pctx_lwpiterate(pctx, pctx->fini_lwp)
				    != 0)
					running = -1;
				pctx->exit(pctx, pid, lwpid,
				    (int)pstatus->pr_lwp.pr_sysarg[0],
				    pctx->uarg);
				if (running == 1)
					running = 0;
				break;
			case SYS_execve:
				(void) pctx_lwpiterate(pctx, pctx->fini_lwp);
				break;
			default:
				pctx_error(pctx, fn,
				    "warning - pid %d sysentry(%d)\n",
				    (int)pid, pstatus->pr_lwp.pr_what);
				break;
			}
			break;
		case PR_SYSEXIT:
			switch (pstatus->pr_lwp.pr_what) {
			case SYS_execve:
				if (pstatus->pr_lwp.pr_errno) {
					/*
					 * The exec failed completely.
					 * Reinstate the lwps we fini'd
					 * at exec entrance
					 */
					if (pctx_lwpiterate(pctx,
					    pctx->init_lwp) == 0)
						running = 1;
					else
						running = -1;
					break;
				}
				if ((uintptr_t)pctx->exec ==
				    (uintptr_t)default_int) {
					running = 0;
					break;
				}
				(void) memcpy(&psinfo,
				    Ppsinfo(pctx->Pr), sizeof (psinfo));
				proc_unctrl_psinfo(&psinfo);
				pctx_begin_syscalls(pctx);
				if (pctx->exec(pctx, pid, lwpid,
				    psinfo.pr_psargs, pctx->uarg) != 0)
					running = -1;
				if (running == 1 && pctx->init_lwp(pctx,
				    pid, 1, pctx->uarg) != 0)
					running = -1;
				pctx_end_syscalls(pctx);
				break;
			case SYS_lwp_create:
				if (pstatus->pr_lwp.pr_errno ||
				    pstatus->pr_lwp.pr_rval1)
					break;
				pctx_begin_syscalls(pctx);
				if (pctx->init_lwp(pctx, pid, lwpid,
				    pctx->uarg) != 0)
					running = -1;
				if (running == 1 && pctx->lwp_create(pctx,
				    pid, lwpid, pctx->uarg) != 0)
					running = -1;
				pctx_end_syscalls(pctx);
				break;
			case SYS_vfork:
			case SYS_forksys:
				if (pstatus->pr_lwp.pr_errno)
					break;
				(void) fflush(NULL);
				switch (fork1()) {
					pid_t ppid;
					int wascreated;
					pctx_sysc_forkfn_t *forkfn;
				case 0:
					ppid = pid;
					pid = pstatus->pr_lwp.pr_rval1;
					wascreated = pctx->created;
					forkfn = pctx->fork;
					pctx_free(pctx);
					pctx = pctx_capture(pid, pctx->uarg,
					    pctx->verbose, pctx->errfn);
					if (pctx != NULL) {
						if (wascreated) {
							/*
							 * Set kill on last
							 * close so -all-
							 * children die.
							 */
							pctx->created = 1;
							(void) Psetflags(
							    pctx->Pr, PR_KLC);
						}
						(*forkfn)(pctx, ppid, pid,
						    lwpid, pctx->uarg);
						pctx_release(pctx);
						_exit(0);
					} else {
						_exit(1);
					}
					/*NOTREACHED*/
				case -1:
					pctx_error(pctx, fn,
					    "cannot follow pid %d: %s\n",
					    (int)pstatus->pr_lwp.pr_rval1,
					    strerror(errno));
					break;
				default:
					break;
				}
				break;
			default:
				pctx_error(pctx, fn, gettext(
				    "warning - pid %d sysexit(%d)\n"),
				    (int)pid, pstatus->pr_lwp.pr_what);
				break;
			}
			break;
		case PR_SIGNALLED:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - signalled\n"), (int)pid);
			break;
		case PR_JOBCONTROL:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - job control stop\n"),
				    (int)pid);
			running = -1;
			break;
		case PR_FAULTED:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - faulted\n"), (int)pid);
			break;
		case PR_SUSPENDED:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - suspended\n"), (int)pid);
			break;
		case PR_CHECKPOINT:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - checkpoint\n"),
				    (int)pid);
			break;
		default:
			if (pctx->verbose)
				pctx_error(pctx, fn,
				    gettext("pid %d - reason %d\n"),
				    (int)pid, pstatus->pr_lwp.pr_why);
			running = -1;
			break;
		}
	}

bailout:
	(void) signal(SIGCHLD, sigsaved);

	if (pctx->terminate)
		return (0);

	switch (running) {
	case 0:
		return (0);
	case -1:
		return (-1);
	default:
		pctx_error(pctx, fn, gettext("lost control of pid %d\n"),
		    (int)pid);
		pctx_free(pctx);
		return (-1);
	}
}

/*
 * Execute the private 'cpc' system call in the context of the
 * controlled process.
 */
int
__pctx_cpc(pctx_t *pctx, cpc_t *cpc,
    int cmd, id_t lwpid, void *data1, void *data2, void *data3, int bufsize)
{
	sysret_t rval;
	argdes_t argd[5];
	argdes_t *adp = &argd[0];
	int error;

	/*
	 * Keep track of the relationship between cpc_t and pctx_t here.
	 * We store the last cpc_t used by libpctx, so that when this pctx is
	 * destroyed, libpctx can notify libcpc.
	 */

	if (pctx->cpc != NULL && pctx->cpc != cpc && pctx_cpc_callback != NULL)
		(*pctx_cpc_callback)(pctx->cpc, pctx);
	pctx->cpc = cpc;

	/*
	 * cmd and lwpid are passed in by value no matter what the command is.
	 */
	adp->arg_value = cmd;
	adp->arg_object = NULL;
	adp->arg_type = AT_BYVAL;
	adp->arg_inout = AI_INPUT;
	adp->arg_size = 0;
	adp++;

	adp->arg_value = lwpid;
	adp->arg_object = NULL;
	adp->arg_type = AT_BYVAL;
	adp->arg_inout = AI_INPUT;
	adp->arg_size = 0;
	adp++;

	switch (cmd) {
	case CPC_BIND:
		adp->arg_value = 0;
		adp->arg_object = data1;
		adp->arg_type = AT_BYREF;
		adp->arg_inout = AI_INPUT;
		adp->arg_size = (size_t)data2;
		adp++;

		adp->arg_value = (size_t)data2;
		adp->arg_object = NULL;
		adp->arg_type = AT_BYVAL;
		adp->arg_inout = AI_INPUT;
		adp->arg_size = 0;
		adp++;

		adp->arg_value = 0;
		adp->arg_object = data3;
		adp->arg_type = AT_BYREF;
		adp->arg_inout = AI_INOUT;
		adp->arg_size = sizeof (int);

		break;
	case CPC_SAMPLE:
		adp->arg_value = 0;
		adp->arg_object = data1;
		adp->arg_type = AT_BYREF;
		adp->arg_inout = AI_OUTPUT;
		adp->arg_size = bufsize;
		adp++;

		adp->arg_value = 0;
		adp->arg_object = data2;
		adp->arg_type = AT_BYREF;
		adp->arg_inout = AI_OUTPUT;
		adp->arg_size = sizeof (hrtime_t);
		adp++;

		adp->arg_value = 0;
		adp->arg_object = data3;
		adp->arg_type = AT_BYREF;
		adp->arg_inout = AI_OUTPUT;
		adp->arg_size = sizeof (uint64_t);

		break;
	default:
		adp->arg_value = 0;
		adp->arg_object = 0;
		adp->arg_type = AT_BYVAL;
		adp->arg_inout = AI_INPUT;
		adp->arg_size = 0;
		adp++;

		adp->arg_value = 0;
		adp->arg_object = 0;
		adp->arg_type = AT_BYVAL;
		adp->arg_inout = AI_INPUT;
		adp->arg_size = 0;
		adp++;

		adp->arg_value = 0;
		adp->arg_object = 0;
		adp->arg_type = AT_BYVAL;
		adp->arg_inout = AI_INPUT;
		adp->arg_size = 0;

		break;
	}

	error = Psyscall(pctx->Pr, &rval, SYS_cpc, 5, &argd[0]);

	if (error) {
		errno = error > 0 ? error : ENOSYS;
		return (-1);
	}
	return (rval.sys_rval1);
}

/*
 * libcpc-private hook used to register a callback. The callback is used to
 * notify libcpc when a pctx handle is invalidated.
 */
void
__pctx_cpc_register_callback(void (*arg)(struct __cpc *, struct __pctx *))
{
	pctx_cpc_callback = arg;
}

/*
 * Tell pctx_run to bail out immediately
 */
void
pctx_terminate(struct __pctx *pctx)
{
	pctx->terminate = 1;
}
/*
 * 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.
 */

#ifndef	_LIBPCTX_H
#define	_LIBPCTX_H

#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>

/*
 * The process context library allows callers to use the facilities
 * of /proc to control processes in a simplified way by managing
 * the process via an event loop.  The controlling process expresses
 * interest in various events which are handled as callbacks by the
 * library.
 */

#ifdef	__cplusplus
extern "C" {
#endif

typedef struct __pctx pctx_t;

/*
 * A vprintf-like error handling routine can be passed in for use
 * by more sophisticated callers.  If specified as NULL, errors
 * are written to stderr.
 */
typedef void (pctx_errfn_t)(const char *fn, const char *fmt, va_list ap);

extern pctx_t *pctx_create(const char *filename, char *const *argv,
    void *arg, int verbose, pctx_errfn_t *errfn);
extern pctx_t *pctx_capture(pid_t pid,
    void *arg, int verbose, pctx_errfn_t *errfn);

typedef int pctx_sysc_execfn_t(pctx_t *, pid_t, id_t, char *, void *);
typedef void pctx_sysc_forkfn_t(pctx_t *, pid_t, id_t, pid_t, void *);
typedef void pctx_sysc_exitfn_t(pctx_t *, pid_t, id_t, int, void *);
typedef int pctx_sysc_lwp_createfn_t(pctx_t *, pid_t, id_t, void *);
typedef int pctx_init_lwpfn_t(pctx_t *, pid_t, id_t, void *);
typedef int pctx_fini_lwpfn_t(pctx_t *, pid_t, id_t, void *);
typedef int pctx_sysc_lwp_exitfn_t(pctx_t *, pid_t, id_t, void *);

extern void pctx_terminate(pctx_t *);

typedef	enum {
	PCTX_NULL_EVENT = 0,
	PCTX_SYSC_EXEC_EVENT,
	PCTX_SYSC_FORK_EVENT,
	PCTX_SYSC_EXIT_EVENT,
	PCTX_SYSC_LWP_CREATE_EVENT,
	PCTX_INIT_LWP_EVENT,
	PCTX_FINI_LWP_EVENT,
	PCTX_SYSC_LWP_EXIT_EVENT
} pctx_event_t;

extern int pctx_set_events(pctx_t *pctx, ...);

extern int pctx_run(pctx_t *pctx, uint_t msec, uint_t nsamples,
    int (*tick)(pctx_t *, pid_t, id_t, void *));

extern void pctx_release(pctx_t *pctx);

/*
 * Implementation-private interfaces used by libcpc.
 */
struct __cpc;
extern int __pctx_cpc(pctx_t *, struct __cpc *, int, id_t,
	void *, void *, void *, int);
extern void __pctx_cpc_register_callback(void (*)(struct __cpc *,
	struct __pctx *));

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBPCTX_H */
#
# 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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNW_1.1 {
    global:
	pctx_capture;
	pctx_create;
	pctx_release;
	pctx_run;
	pctx_set_events;
};

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	__pctx_cpc;
	__pctx_cpc_register_callback;
	pctx_terminate;
    local:
	*;
};