@@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -113,21 +114,16 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
int tst_get_free_pids_(void (*cleanup_fn) (void))
{
- FILE *f;
- int rc, used_pids, max_pids, max_session_pids, max_threads;
-
- f = popen("ps -eT | wc -l", "r");
- if (!f) {
- tst_brkm(TBROK, cleanup_fn, "Could not run 'ps' to calculate used pids");
- return -1;
- }
- rc = fscanf(f, "%i", &used_pids);
- pclose(f);
-
- if (rc != 1 || used_pids < 0) {
- tst_brkm(TBROK, cleanup_fn, "Could not read output of 'ps' to calculate used pids");
- return -1;
+ DIR *f;
+ struct dirent *ent;
+ int max_pids, max_session_pids, max_threads, used_pids = 0;
+
+ f = SAFE_OPENDIR("/proc");
+ while ((ent = SAFE_READDIR(f))) {
+ if (isdigit(ent->d_name[0]))
+ ++used_pids;
}
+ SAFE_CLOSEDIR(f);
SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);