subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2009 |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * DESCRIPTION |
| 22 | * get_max_pids(): Return the maximum number of pids for this system by |
| 23 | * reading /proc/sys/kernel/pid_max |
| 24 | * |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * get_free_pids(): Return number of free pids by subtracting the number |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 26 | * of pids currently used ('ps -eT') from max_pids |
| 27 | */ |
| 28 | |
| 29 | |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
subrata_modak | fc3529d | 2009-07-07 14:32:03 +0000 | [diff] [blame] | 31 | #include <limits.h> |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
subrata_modak | fc3529d | 2009-07-07 14:32:03 +0000 | [diff] [blame] | 33 | #include "test.h" |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 34 | |
| 35 | #define BUFSIZE 512 |
| 36 | |
| 37 | int get_max_pids(void) |
| 38 | { |
| 39 | #ifdef __linux__ |
| 40 | |
| 41 | FILE *f; |
| 42 | char buf[BUFSIZE]; |
| 43 | |
| 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; |
| 48 | } |
| 49 | if (!fgets(buf, BUFSIZE, f)) { |
| 50 | fclose(f); |
| 51 | tst_resm(TBROK, "Could not read /proc/sys/kernel/pid_max"); |
| 52 | return -1; |
| 53 | } |
| 54 | fclose(f); |
| 55 | return atoi(buf); |
| 56 | #else |
| 57 | return SHRT_MAX; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int get_free_pids(void) |
| 63 | { |
| 64 | FILE *f; |
yaberauneya | 4263e49 | 2009-11-09 05:52:26 +0000 | [diff] [blame] | 65 | int rc, used_pids, max_pids; |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 66 | |
| 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; |
| 72 | } |
yaberauneya | 4263e49 | 2009-11-09 05:52:26 +0000 | [diff] [blame] | 73 | rc = fscanf(f, "%i", &used_pids); |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 74 | pclose(f); |
| 75 | |
yaberauneya | 4263e49 | 2009-11-09 05:52:26 +0000 | [diff] [blame] | 76 | if (rc != 1 || used_pids < 0) { |
subrata_modak | 8bc4d77 | 2009-01-15 10:31:45 +0000 | [diff] [blame] | 77 | tst_resm(TBROK, "Could not read output of 'ps' to " |
| 78 | "calculate used pids"); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | max_pids = get_max_pids(); |
| 83 | |
| 84 | if (max_pids < 0) |
| 85 | return -1; |
| 86 | |
| 87 | return max_pids - used_pids; |
| 88 | } |
| 89 | |