1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2009
4 * Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include "test.h"
29 #include "tst_pid.h"
30 #include "old_safe_file_ops.h"
31 #include "tst_safe_macros.h"
32
33 #define PID_MAX_PATH "/proc/sys/kernel/pid_max"
34 #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
35 #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
36 /* Leave some available processes for the OS */
37 #define PIDS_RESERVE 50
38
tst_get_unused_pid_(void (* cleanup_fn)(void))39 pid_t tst_get_unused_pid_(void (*cleanup_fn) (void))
40 {
41 pid_t pid;
42
43 SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &pid);
44
45 return pid;
46 }
47
48 /*
49 * Get the effective session UID - either one invoking current test via sudo
50 * or the real UID.
51 */
get_session_uid(void)52 static unsigned int get_session_uid(void)
53 {
54 const char *sudo_uid;
55
56 sudo_uid = getenv("SUDO_UID");
57 if (sudo_uid) {
58 unsigned int real_uid;
59 int ret;
60
61 ret = sscanf(sudo_uid, "%u", &real_uid);
62 if (ret == 1)
63 return real_uid;
64 }
65
66 return getuid();
67 }
68
read_session_pids_limit(const char * path_fmt,int uid,void (* cleanup_fn)(void))69 static int read_session_pids_limit(const char *path_fmt, int uid,
70 void (*cleanup_fn) (void))
71 {
72 int max_pids, ret;
73 char max_pid_value[100];
74 char path[PATH_MAX];
75
76 ret = snprintf(path, sizeof(path), path_fmt, uid);
77 if (ret < 0 || (size_t)ret >= sizeof(path))
78 return -1;
79
80 if (access(path, R_OK) != 0) {
81 tst_resm(TINFO, "Cannot read session user limits from '%s'", path);
82 return -1;
83 }
84
85 SAFE_FILE_SCANF(cleanup_fn, path, "%s", max_pid_value);
86 if (!strcmp(max_pid_value, "max")) {
87 SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
88 tst_resm(TINFO, "Found limit of processes %d (from %s=max)", max_pids, path);
89 } else {
90 max_pids = SAFE_STRTOL(max_pid_value, 0, INT_MAX);
91 tst_resm(TINFO, "Found limit of processes %d (from %s)", max_pids, path);
92 }
93
94 return max_pids;
95 }
96
get_session_pids_limit(void (* cleanup_fn)(void))97 static int get_session_pids_limit(void (*cleanup_fn) (void))
98 {
99 int max_pids, uid;
100
101 uid = get_session_uid();
102 max_pids = read_session_pids_limit(CGROUPS_V2_SLICE_FMT, uid, cleanup_fn);
103 if (max_pids < 0)
104 max_pids = read_session_pids_limit(CGROUPS_V1_SLICE_FMT, uid,
105 cleanup_fn);
106
107 if (max_pids < 0)
108 return -1;
109
110 return max_pids;
111 }
112
tst_get_free_pids_(void (* cleanup_fn)(void))113 int tst_get_free_pids_(void (*cleanup_fn) (void))
114 {
115 FILE *f;
116 int rc, used_pids, max_pids, max_session_pids;
117
118 f = popen("ps -eT | wc -l", "r");
119 if (!f) {
120 tst_brkm(TBROK, cleanup_fn, "Could not run 'ps' to calculate used pids");
121 return -1;
122 }
123 rc = fscanf(f, "%i", &used_pids);
124 pclose(f);
125
126 if (rc != 1 || used_pids < 0) {
127 tst_brkm(TBROK, cleanup_fn, "Could not read output of 'ps' to calculate used pids");
128 return -1;
129 }
130
131 SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
132
133 max_session_pids = get_session_pids_limit(cleanup_fn);
134 if ((max_session_pids > 0) && (max_session_pids < max_pids))
135 max_pids = max_session_pids;
136
137 if (max_pids > PIDS_RESERVE)
138 max_pids -= PIDS_RESERVE;
139 else
140 max_pids = 0;
141
142 /* max_pids contains the maximum PID + 1,
143 * used_pids contains used PIDs + 1,
144 * so this additional '1' is eliminated by the substraction */
145 if (used_pids >= max_pids) {
146 tst_brkm(TBROK, cleanup_fn, "No free pids");
147 return 0;
148 }
149 return max_pids - used_pids;
150 }
151