Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / lib / system_specific_process_info.c
blob05e1e8aad48f0c7b64bf96c82d6db55f37183f1f
1 /*
3 * Copyright (c) International Business Machines Corp., 2009
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * DESCRIPTION
22 * get_max_pids(): Return the maximum number of pids for this system by
23 * reading /proc/sys/kernel/pid_max
25 * get_free_pids(): Return number of free pids by subtracting the number
26 * of pids currently used ('ps -eT') from max_pids
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <sys/types.h>
33 #include "test.h"
35 #define BUFSIZE 512
37 int get_max_pids(void)
39 #ifdef __linux__
41 FILE *f;
42 char buf[BUFSIZE];
44 f = fopen("/proc/sys/kernel/pid_max", "r");
45 if (!f) {
46 tst_resm(TBROK, "Could not open /proc/sys/kernel/pid_max");
47 return -1;
49 if (!fgets(buf, BUFSIZE, f)) {
50 fclose(f);
51 tst_resm(TBROK, "Could not read /proc/sys/kernel/pid_max");
52 return -1;
54 fclose(f);
55 return atoi(buf);
56 #else
57 return SHRT_MAX;
58 #endif
62 int get_free_pids(void)
64 FILE *f;
65 int rc, used_pids, max_pids;
67 f = popen("ps -eT | wc -l", "r");
68 if (!f) {
69 tst_resm(TBROK, "Could not run 'ps' to calculate used "
70 "pids");
71 return -1;
73 rc = fscanf(f, "%i", &used_pids);
74 pclose(f);
76 if (rc != 1 || used_pids < 0) {
77 tst_resm(TBROK, "Could not read output of 'ps' to "
78 "calculate used pids");
79 return -1;
82 max_pids = get_max_pids();
84 if (max_pids < 0)
85 return -1;
87 return max_pids - used_pids;