1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) 2017 Linux Test Project 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 5f08c3bdfSopenharmony_ci * it under the terms in version 2 of the GNU General Public License as 6f08c3bdfSopenharmony_ci * published by the Free Software Foundation. 7f08c3bdfSopenharmony_ci * 8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 9f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 11f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 12f08c3bdfSopenharmony_ci */ 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ci#include <time.h> 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "posixtest.h" 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#ifdef __linux__ 19f08c3bdfSopenharmony_ci# include <errno.h> 20f08c3bdfSopenharmony_ci# include <string.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ciint tst_process_state_wait3(pid_t pid, const char state, 23f08c3bdfSopenharmony_ci long maxwait_s) 24f08c3bdfSopenharmony_ci{ 25f08c3bdfSopenharmony_ci char proc_path[128], cur_state; 26f08c3bdfSopenharmony_ci int wait_step_ms = 10; 27f08c3bdfSopenharmony_ci struct timespec wait_step_ts; 28f08c3bdfSopenharmony_ci long iteration, max_iterations = (maxwait_s * 1000) / wait_step_ms; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci wait_step_ts.tv_sec = 0; 31f08c3bdfSopenharmony_ci wait_step_ts.tv_nsec = wait_step_ms * 1000000; 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci snprintf(proc_path, sizeof(proc_path), "/proc/%i/stat", pid); 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci for (iteration = 0; iteration < max_iterations; iteration++) { 36f08c3bdfSopenharmony_ci FILE *f = fopen(proc_path, "r"); 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci if (!f) { 39f08c3bdfSopenharmony_ci fprintf(stderr, "Failed to open '%s': %s\n", 40f08c3bdfSopenharmony_ci proc_path, strerror(errno)); 41f08c3bdfSopenharmony_ci return 1; 42f08c3bdfSopenharmony_ci } 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci if (fscanf(f, "%*i %*s %c", &cur_state) != 1) { 45f08c3bdfSopenharmony_ci fclose(f); 46f08c3bdfSopenharmony_ci fprintf(stderr, "Failed to read '%s': %s\n", 47f08c3bdfSopenharmony_ci proc_path, strerror(errno)); 48f08c3bdfSopenharmony_ci return 1; 49f08c3bdfSopenharmony_ci } 50f08c3bdfSopenharmony_ci fclose(f); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci if (state == cur_state) 53f08c3bdfSopenharmony_ci return 0; 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci nanosleep(&wait_step_ts, NULL); 56f08c3bdfSopenharmony_ci } 57f08c3bdfSopenharmony_ci fprintf(stderr, "Reached max wait time\n"); 58f08c3bdfSopenharmony_ci return 1; 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci#else 61f08c3bdfSopenharmony_ciint tst_process_state_wait3(pid_t pid PTS_ATTRIBUTE_UNUSED, 62f08c3bdfSopenharmony_ci const char state PTS_ATTRIBUTE_UNUSED, long maxwait_s) 63f08c3bdfSopenharmony_ci{ 64f08c3bdfSopenharmony_ci struct timespec maxwait_ts; 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci maxwait_ts.tv_sec = maxwait_s; 67f08c3bdfSopenharmony_ci maxwait_ts.tv_nsec = 0; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci nanosleep(&maxwait_ts, NULL); 70f08c3bdfSopenharmony_ci return 0; 71f08c3bdfSopenharmony_ci} 72f08c3bdfSopenharmony_ci#endif 73