Home | History | Annotate | Line # | Download | only in sys
t_ptrace_kill.c revision 1.2.4.2
      1  1.2.4.2  perseant /*	$NetBSD: t_ptrace_kill.c,v 1.2.4.2 2025/08/02 05:58:07 perseant Exp $	*/
      2  1.2.4.2  perseant 
      3  1.2.4.2  perseant /*-
      4  1.2.4.2  perseant  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.2.4.2  perseant  * All rights reserved.
      6  1.2.4.2  perseant  *
      7  1.2.4.2  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  perseant  * by Christos Zoulas.
      9  1.2.4.2  perseant  *
     10  1.2.4.2  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  perseant  * modification, are permitted provided that the following conditions
     12  1.2.4.2  perseant  * are met:
     13  1.2.4.2  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  perseant  *
     19  1.2.4.2  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.4.2  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.4.2  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.4.2  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.4.2  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.4.2  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.4.2  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.4.2  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.4.2  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.4.2  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.4.2  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  perseant  */
     31  1.2.4.2  perseant 
     32  1.2.4.2  perseant #include <sys/cdefs.h>
     33  1.2.4.2  perseant __RCSID("$NetBSD: t_ptrace_kill.c,v 1.2.4.2 2025/08/02 05:58:07 perseant Exp $");
     34  1.2.4.2  perseant 
     35  1.2.4.2  perseant #include <sys/types.h>
     36  1.2.4.2  perseant #include <sys/ptrace.h>
     37  1.2.4.2  perseant #include <sys/wait.h>
     38  1.2.4.2  perseant #include <pthread.h>
     39  1.2.4.2  perseant #include <stdio.h>
     40  1.2.4.2  perseant #include <stdlib.h>
     41  1.2.4.2  perseant #include <string.h>
     42  1.2.4.2  perseant #include <signal.h>
     43  1.2.4.2  perseant #include <unistd.h>
     44  1.2.4.2  perseant #include <errno.h>
     45  1.2.4.2  perseant #include <err.h>
     46  1.2.4.2  perseant #include <atf-c.h>
     47  1.2.4.2  perseant 
     48  1.2.4.2  perseant #define SYSCALL(a, b) ATF_REQUIRE_EQ_MSG(a, b, "%s got %s", #a, strerror(errno))
     49  1.2.4.2  perseant 
     50  1.2.4.2  perseant ATF_TC(pt_kill);
     51  1.2.4.2  perseant ATF_TC_HEAD(pt_kill, tc)
     52  1.2.4.2  perseant {
     53  1.2.4.2  perseant 	atf_tc_set_md_var(tc, "descr", "Test PT_KILL of a PT_STOP'ed process");
     54  1.2.4.2  perseant }
     55  1.2.4.2  perseant 
     56  1.2.4.2  perseant static void
     57  1.2.4.2  perseant child(int *fdto, int *fdfrom)
     58  1.2.4.2  perseant {
     59  1.2.4.2  perseant 	char p = '2', q;
     60  1.2.4.2  perseant 	printf("%d: born\n", getpid());
     61  1.2.4.2  perseant 	write(fdfrom[1], &p, 1);
     62  1.2.4.2  perseant 	read(fdto[0], &q, 1);
     63  1.2.4.2  perseant 	printf("%d: seppuku %c\n", getpid(), q);
     64  1.2.4.2  perseant 	write(fdfrom[1], &p, 1);
     65  1.2.4.2  perseant 	read(fdto[0], &q, 1);
     66  1.2.4.2  perseant //	*(int *)1 = 0;
     67  1.2.4.2  perseant //	kill(getpid(), SIGSEGV);
     68  1.2.4.2  perseant //	kill(getpid(), SIGSTOP);
     69  1.2.4.2  perseant 	for (;;)
     70  1.2.4.2  perseant 		sleep(1);
     71  1.2.4.2  perseant 
     72  1.2.4.2  perseant }
     73  1.2.4.2  perseant 
     74  1.2.4.2  perseant static void *
     75  1.2.4.2  perseant waitthread(void *pidp)
     76  1.2.4.2  perseant {
     77  1.2.4.2  perseant 	int status = 0;
     78  1.2.4.2  perseant 	pid_t rpid, pid;
     79  1.2.4.2  perseant 
     80  1.2.4.2  perseant 	pid = *(pid_t *)pidp;
     81  1.2.4.2  perseant 	printf("waiting for %d\n", pid);
     82  1.2.4.2  perseant 	while ((rpid = waitpid(pid, &status, 0)) != pid) {
     83  1.2.4.2  perseant 		printf("waitpid %d = %d status = %#x", pid, rpid, status);
     84  1.2.4.2  perseant 	}
     85  1.2.4.2  perseant 	printf("done waitpid %d = %d status = %#x", pid, rpid, status);
     86  1.2.4.2  perseant 	return NULL;
     87  1.2.4.2  perseant }
     88  1.2.4.2  perseant 
     89  1.2.4.2  perseant ATF_TC_BODY(pt_kill, tc)
     90  1.2.4.2  perseant {
     91  1.2.4.2  perseant 	pid_t pid;
     92  1.2.4.2  perseant 	int fdto[2], fdfrom[2];
     93  1.2.4.2  perseant 	char p = '1', q;
     94  1.2.4.2  perseant 	int status;
     95  1.2.4.2  perseant 	pthread_t thread;
     96  1.2.4.2  perseant 
     97  1.2.4.2  perseant 	SYSCALL(pipe(fdto), 0);
     98  1.2.4.2  perseant 	SYSCALL(pipe(fdfrom), 0);
     99  1.2.4.2  perseant 	switch (pid = fork()) {
    100  1.2.4.2  perseant 	case 0:
    101  1.2.4.2  perseant 		child(fdto, fdfrom);
    102  1.2.4.2  perseant 		break;
    103  1.2.4.2  perseant 	case -1:
    104  1.2.4.2  perseant 		err(EXIT_FAILURE, "fork failed");
    105  1.2.4.2  perseant 	default:
    106  1.2.4.2  perseant 		SYSCALL(pthread_create(&thread, NULL, waitthread, &pid), 0);
    107  1.2.4.2  perseant 		sleep(1); // XXX: too lazy to sync properly
    108  1.2.4.2  perseant 		SYSCALL(read(fdfrom[0], &q, 1), 1);
    109  1.2.4.2  perseant 		printf("%d: read %c\n", pid, q);
    110  1.2.4.2  perseant 		SYSCALL(ptrace(PT_ATTACH, pid, NULL, 0), 0);
    111  1.2.4.2  perseant 		printf("%d: attached\n", pid);
    112  1.2.4.2  perseant 		SYSCALL(write(fdto[1], &p, 1), 1);
    113  1.2.4.2  perseant 		waitpid(pid, NULL, WNOHANG);
    114  1.2.4.2  perseant 		printf("%d: sent\n", pid);
    115  1.2.4.2  perseant 		SYSCALL(ptrace(PT_CONTINUE, pid, (void *)1, 0), 0);
    116  1.2.4.2  perseant 		SYSCALL(read(fdfrom[0], &p, 1), 1);
    117  1.2.4.2  perseant 		printf("%d: received\n", pid);
    118  1.2.4.2  perseant 		SYSCALL(ptrace(PT_STOP, pid, NULL, 0), 0);
    119  1.2.4.2  perseant 		SYSCALL(ptrace(PT_KILL, pid, NULL, 0), 0);
    120  1.2.4.2  perseant 		SYSCALL(waitpid(pid, &status, 0), pid);
    121  1.2.4.2  perseant 		ATF_REQUIRE(status == 9);
    122  1.2.4.2  perseant 		break;
    123  1.2.4.2  perseant 	}
    124  1.2.4.2  perseant }
    125  1.2.4.2  perseant 
    126  1.2.4.2  perseant ATF_TP_ADD_TCS(tp)
    127  1.2.4.2  perseant {
    128  1.2.4.2  perseant 	ATF_TP_ADD_TC(tp, pt_kill);
    129  1.2.4.2  perseant 
    130  1.2.4.2  perseant 	return atf_no_error();
    131  1.2.4.2  perseant }
    132