1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2021 Cyril Hrubis <chrubis@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci#include <stdio.h> 7f08c3bdfSopenharmony_ci#include <signal.h> 8f08c3bdfSopenharmony_ci#include <stdlib.h> 9f08c3bdfSopenharmony_ci#include <unistd.h> 10f08c3bdfSopenharmony_ci#include <errno.h> 11f08c3bdfSopenharmony_ci#include <string.h> 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_cistatic void print_help(const char *name) 14f08c3bdfSopenharmony_ci{ 15f08c3bdfSopenharmony_ci fprintf(stderr, "usage: %s timeout pid\n", name); 16f08c3bdfSopenharmony_ci} 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#define print_msg(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 21f08c3bdfSopenharmony_ci{ 22f08c3bdfSopenharmony_ci int timeout, pid, ret, i; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci if (argc != 3) { 25f08c3bdfSopenharmony_ci print_help(argv[0]); 26f08c3bdfSopenharmony_ci return 1; 27f08c3bdfSopenharmony_ci } 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci timeout = atoi(argv[1]); 30f08c3bdfSopenharmony_ci pid = atoi(argv[2]); 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci if (timeout < 0) { 33f08c3bdfSopenharmony_ci fprintf(stderr, "Invalid timeout '%s'\n", argv[1]); 34f08c3bdfSopenharmony_ci print_help(argv[0]); 35f08c3bdfSopenharmony_ci return 1; 36f08c3bdfSopenharmony_ci } 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci if (pid <= 1) { 39f08c3bdfSopenharmony_ci fprintf(stderr, "Invalid pid '%s'\n", argv[2]); 40f08c3bdfSopenharmony_ci print_help(argv[0]); 41f08c3bdfSopenharmony_ci return 1; 42f08c3bdfSopenharmony_ci } 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci ret = setpgid(0, 0); 45f08c3bdfSopenharmony_ci if (ret) 46f08c3bdfSopenharmony_ci print_msg("setpgid() failed: %s", strerror(errno)); 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci if (timeout) 49f08c3bdfSopenharmony_ci sleep(timeout); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci print_msg("Test timed out, sending SIGTERM!"); 52f08c3bdfSopenharmony_ci print_msg("If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1"); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci ret = kill(-pid, SIGTERM); 55f08c3bdfSopenharmony_ci if (ret) { 56f08c3bdfSopenharmony_ci print_msg("kill(%i) failed: %s", -pid, strerror(errno)); 57f08c3bdfSopenharmony_ci return 1; 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci usleep(100000); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci i = 10; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci while (!kill(-pid, 0) && i-- > 0) { 65f08c3bdfSopenharmony_ci print_msg("Test is still running... %i", i + 1); 66f08c3bdfSopenharmony_ci sleep(1); 67f08c3bdfSopenharmony_ci } 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci if (!kill(-pid, 0)) { 70f08c3bdfSopenharmony_ci print_msg("Test is still running, sending SIGKILL"); 71f08c3bdfSopenharmony_ci ret = kill(-pid, SIGKILL); 72f08c3bdfSopenharmony_ci if (ret) { 73f08c3bdfSopenharmony_ci print_msg("kill(%i) failed: %s", -pid, strerror(errno)); 74f08c3bdfSopenharmony_ci return 1; 75f08c3bdfSopenharmony_ci } 76f08c3bdfSopenharmony_ci } 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci return 0; 79f08c3bdfSopenharmony_ci} 80