1a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis/* 2a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz> 3a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * 4a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * Licensed under the GNU GPLv2 or later. 5a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * This program is free software; you can redistribute it and/or modify 6a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * it under the terms of the GNU General Public License as published by 7a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * the Free Software Foundation; either version 2 of the License, or 8a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * (at your option) any later version. 9a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * 10a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * This program is distributed in the hope that it will be useful, 11a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * the GNU General Public License for more details. 14a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * 15a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * You should have received a copy of the GNU General Public License 16a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * along with this program; if not, write to the Free Software Foundation, 17a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis */ 19a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 20a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis#ifndef FUTEX_UTILS_H__ 21a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis#define FUTEX_UTILS_H__ 22a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 23a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis/* 24a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis * Wait for nr_threads to be sleeping 25a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis */ 26a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubisstatic int wait_for_threads(unsigned int nr_threads) 27a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis{ 28a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis char thread_state, name[1024]; 29a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis DIR *dir; 30a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis struct dirent *dent; 31a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis unsigned int cnt = 0; 32a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 33a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis snprintf(name, sizeof(name), "/proc/%i/task/", getpid()); 34a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 35a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis dir = SAFE_OPENDIR(NULL, name); 36a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 37a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis while ((dent = SAFE_READDIR(NULL, dir))) { 38a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis /* skip ".", ".." and the main thread */ 39a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis if (atoi(dent->d_name) == getpid() || atoi(dent->d_name) == 0) 40a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis continue; 41a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 42a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis snprintf(name, sizeof(name), "/proc/%i/task/%s/stat", 43a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis getpid(), dent->d_name); 44a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 45a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis SAFE_FILE_SCANF(NULL, name, "%*i %*s %c", &thread_state); 46a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 47a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis if (thread_state != 'S') { 48a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis tst_resm(TINFO, "Thread %s not sleeping yet", dent->d_name); 49a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis SAFE_CLOSEDIR(NULL, dir); 50a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis return 1; 51a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis } 52a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis cnt++; 53a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis } 54a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 55a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis SAFE_CLOSEDIR(NULL, dir); 56a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 57a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis if (cnt != nr_threads) { 58a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis tst_resm(TINFO, "%u threads sleeping, expected %u", 59a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis cnt, nr_threads); 60a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis } 61a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 62a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis return 0; 63a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis} 64a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis 65a5a1806859468d9dabd5fe15c429192a0680bd84Cyril Hrubis#endif /* FUTEX_UTILS_H__ */ 66