1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci ****************************************************************************** 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * ptrace05 - an app which ptraces itself as per arbitrarily specified signals, 5f08c3bdfSopenharmony_ci * over a user specified range. 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * Copyright (C) 2009, Ngie Cooper 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 10f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 11f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 12f08c3bdfSopenharmony_ci * (at your option) any later version. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 15f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 16f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17f08c3bdfSopenharmony_ci * GNU General Public License for more details. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along 20f08c3bdfSopenharmony_ci * with this program; if not, write to the Free Software Foundation, Inc., 21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci ****************************************************************************** 24f08c3bdfSopenharmony_ci */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#include <sys/types.h> 27f08c3bdfSopenharmony_ci#include <sys/wait.h> 28f08c3bdfSopenharmony_ci#include <signal.h> 29f08c3bdfSopenharmony_ci#include <errno.h> 30f08c3bdfSopenharmony_ci#include <libgen.h> 31f08c3bdfSopenharmony_ci#include <math.h> 32f08c3bdfSopenharmony_ci#include <stdlib.h> 33f08c3bdfSopenharmony_ci#include <stdio.h> 34f08c3bdfSopenharmony_ci#include <string.h> 35f08c3bdfSopenharmony_ci#include <unistd.h> 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#include <config.h> 38f08c3bdfSopenharmony_ci#include "ptrace.h" 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_ci#include "test.h" 41f08c3bdfSopenharmony_ci#include "lapi/signal.h" 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_cichar *TCID = "ptrace05"; 44f08c3bdfSopenharmony_ciint TST_TOTAL = 0; 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ciint usage(const char *); 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ciint usage(const char *argv0) 49f08c3bdfSopenharmony_ci{ 50f08c3bdfSopenharmony_ci fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0); 51f08c3bdfSopenharmony_ci return 1; 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ciint main(int argc, char **argv) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci int end_signum = -1; 58f08c3bdfSopenharmony_ci int signum; 59f08c3bdfSopenharmony_ci int start_signum = -1; 60f08c3bdfSopenharmony_ci int status; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci pid_t child; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci if (start_signum == -1) { 67f08c3bdfSopenharmony_ci start_signum = 0; 68f08c3bdfSopenharmony_ci } 69f08c3bdfSopenharmony_ci if (end_signum == -1) { 70f08c3bdfSopenharmony_ci end_signum = SIGRTMAX; 71f08c3bdfSopenharmony_ci } 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci for (signum = start_signum; signum <= end_signum; signum++) { 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci if (signum >= __SIGRTMIN && signum < SIGRTMIN) 76f08c3bdfSopenharmony_ci continue; 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci switch (child = fork()) { 79f08c3bdfSopenharmony_ci case -1: 80f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, NULL, "fork() failed"); 81f08c3bdfSopenharmony_ci case 0: 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) { 84f08c3bdfSopenharmony_ci tst_resm(TINFO, "[child] Sending kill(.., %d)", 85f08c3bdfSopenharmony_ci signum); 86f08c3bdfSopenharmony_ci if (kill(getpid(), signum) < 0) { 87f08c3bdfSopenharmony_ci tst_resm(TINFO | TERRNO, 88f08c3bdfSopenharmony_ci "[child] kill(.., %d) failed.", 89f08c3bdfSopenharmony_ci signum); 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci } else { 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci /* 94f08c3bdfSopenharmony_ci * This won't increment the TST_COUNT var. 95f08c3bdfSopenharmony_ci * properly, but it'll show up as a failure 96f08c3bdfSopenharmony_ci * nonetheless. 97f08c3bdfSopenharmony_ci */ 98f08c3bdfSopenharmony_ci tst_resm(TFAIL | TERRNO, 99f08c3bdfSopenharmony_ci "Failed to ptrace(PTRACE_TRACEME, ...) " 100f08c3bdfSopenharmony_ci "properly"); 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci } 103f08c3bdfSopenharmony_ci /* Shouldn't get here if signum == 0. */ 104f08c3bdfSopenharmony_ci exit((signum == 0 ? 0 : 2)); 105f08c3bdfSopenharmony_ci break; 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci default: 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci waitpid(child, &status, 0); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci switch (signum) { 112f08c3bdfSopenharmony_ci case 0: 113f08c3bdfSopenharmony_ci if (WIFEXITED(status) 114f08c3bdfSopenharmony_ci && WEXITSTATUS(status) == 0) { 115f08c3bdfSopenharmony_ci tst_resm(TPASS, 116f08c3bdfSopenharmony_ci "kill(.., 0) exited " 117f08c3bdfSopenharmony_ci "with 0, as expected."); 118f08c3bdfSopenharmony_ci } else { 119f08c3bdfSopenharmony_ci tst_resm(TFAIL, 120f08c3bdfSopenharmony_ci "kill(.., 0) didn't exit " 121f08c3bdfSopenharmony_ci "with 0."); 122f08c3bdfSopenharmony_ci } 123f08c3bdfSopenharmony_ci break; 124f08c3bdfSopenharmony_ci case SIGKILL: 125f08c3bdfSopenharmony_ci if (WIFSIGNALED(status)) { 126f08c3bdfSopenharmony_ci /* SIGKILL must be uncatchable. */ 127f08c3bdfSopenharmony_ci if (WTERMSIG(status) == SIGKILL) { 128f08c3bdfSopenharmony_ci tst_resm(TPASS, 129f08c3bdfSopenharmony_ci "Killed with SIGKILL, " 130f08c3bdfSopenharmony_ci "as expected."); 131f08c3bdfSopenharmony_ci } else { 132f08c3bdfSopenharmony_ci tst_resm(TPASS, 133f08c3bdfSopenharmony_ci "Didn't die with " 134f08c3bdfSopenharmony_ci "SIGKILL (?!) "); 135f08c3bdfSopenharmony_ci } 136f08c3bdfSopenharmony_ci } else if (WIFEXITED(status)) { 137f08c3bdfSopenharmony_ci tst_resm(TFAIL, 138f08c3bdfSopenharmony_ci "Exited unexpectedly instead " 139f08c3bdfSopenharmony_ci "of dying with SIGKILL."); 140f08c3bdfSopenharmony_ci } else if (WIFSTOPPED(status)) { 141f08c3bdfSopenharmony_ci tst_resm(TFAIL, 142f08c3bdfSopenharmony_ci "Stopped instead of dying " 143f08c3bdfSopenharmony_ci "with SIGKILL."); 144f08c3bdfSopenharmony_ci } 145f08c3bdfSopenharmony_ci break; 146f08c3bdfSopenharmony_ci /* All other processes should be stopped. */ 147f08c3bdfSopenharmony_ci default: 148f08c3bdfSopenharmony_ci if (WIFSTOPPED(status)) { 149f08c3bdfSopenharmony_ci tst_resm(TPASS, "Stopped as expected"); 150f08c3bdfSopenharmony_ci } else { 151f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Didn't stop as " 152f08c3bdfSopenharmony_ci "expected."); 153f08c3bdfSopenharmony_ci if (kill(child, 0)) { 154f08c3bdfSopenharmony_ci tst_resm(TINFO, 155f08c3bdfSopenharmony_ci "Is still alive!?"); 156f08c3bdfSopenharmony_ci } else if (WIFEXITED(status)) { 157f08c3bdfSopenharmony_ci tst_resm(TINFO, 158f08c3bdfSopenharmony_ci "Exited normally"); 159f08c3bdfSopenharmony_ci } else if (WIFSIGNALED(status)) { 160f08c3bdfSopenharmony_ci tst_resm(TINFO, 161f08c3bdfSopenharmony_ci "Was signaled with " 162f08c3bdfSopenharmony_ci "signum=%d", 163f08c3bdfSopenharmony_ci WTERMSIG(status)); 164f08c3bdfSopenharmony_ci } 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_ci } 167f08c3bdfSopenharmony_ci 168f08c3bdfSopenharmony_ci break; 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_ci } 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci } 173f08c3bdfSopenharmony_ci /* Make sure the child dies a quick and painless death ... */ 174f08c3bdfSopenharmony_ci kill(child, 9); 175f08c3bdfSopenharmony_ci 176f08c3bdfSopenharmony_ci } 177f08c3bdfSopenharmony_ci 178f08c3bdfSopenharmony_ci tst_exit(); 179f08c3bdfSopenharmony_ci 180f08c3bdfSopenharmony_ci} 181