0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #define _GNU_SOURCE
0023 #include <sys/eventfd.h>
0024 #include <sys/wait.h>
0025 #include <sys/types.h>
0026 #include <sched.h>
0027 #include <sys/prctl.h>
0028 #include <unistd.h>
0029 #include <time.h>
0030 #include <stdio.h>
0031 #include <stdlib.h>
0032 #include <string.h>
0033
0034 #if __GLIBC_PREREQ(2, 30) == 0
0035 #include <sys/syscall.h>
0036 static pid_t gettid(void)
0037 {
0038 return syscall(SYS_gettid);
0039 }
0040 #endif
0041
0042 #ifndef PR_SCHED_CORE
0043 #define PR_SCHED_CORE 62
0044 # define PR_SCHED_CORE_GET 0
0045 # define PR_SCHED_CORE_CREATE 1
0046 # define PR_SCHED_CORE_SHARE_TO 2
0047 # define PR_SCHED_CORE_SHARE_FROM 3
0048 # define PR_SCHED_CORE_MAX 4
0049 #endif
0050
0051 #define MAX_PROCESSES 128
0052 #define MAX_THREADS 128
0053
0054 static const char USAGE[] = "cs_prctl_test [options]\n"
0055 " options:\n"
0056 " -P : number of processes to create.\n"
0057 " -T : number of threads per process to create.\n"
0058 " -d : delay time to keep tasks alive.\n"
0059 " -k : keep tasks alive until keypress.\n";
0060
0061 enum pid_type {PIDTYPE_PID = 0, PIDTYPE_TGID, PIDTYPE_PGID};
0062
0063 const int THREAD_CLONE_FLAGS = CLONE_THREAD | CLONE_SIGHAND | CLONE_FS | CLONE_VM | CLONE_FILES;
0064
0065 struct child_args {
0066 int num_threads;
0067 int pfd[2];
0068 int cpid;
0069 int thr_tids[MAX_THREADS];
0070 };
0071
0072 static struct child_args procs[MAX_PROCESSES];
0073 static int num_processes = 2;
0074 static int need_cleanup = 0;
0075
0076 static int _prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
0077 unsigned long arg5)
0078 {
0079 int res;
0080
0081 res = prctl(option, arg2, arg3, arg4, arg5);
0082 printf("%d = prctl(%d, %ld, %ld, %ld, %lx)\n", res, option, (long)arg2, (long)arg3,
0083 (long)arg4, arg5);
0084 return res;
0085 }
0086
0087 #define STACK_SIZE (1024 * 1024)
0088
0089 #define handle_error(msg) __handle_error(__FILE__, __LINE__, msg)
0090 static void __handle_error(char *fn, int ln, char *msg)
0091 {
0092 int pidx;
0093 printf("(%s:%d) - ", fn, ln);
0094 perror(msg);
0095 if (need_cleanup) {
0096 for (pidx = 0; pidx < num_processes; ++pidx)
0097 kill(procs[pidx].cpid, 15);
0098 need_cleanup = 0;
0099 }
0100 exit(EXIT_FAILURE);
0101 }
0102
0103 static void handle_usage(int rc, char *msg)
0104 {
0105 puts(USAGE);
0106 puts(msg);
0107 putchar('\n');
0108 exit(rc);
0109 }
0110
0111 static unsigned long get_cs_cookie(int pid)
0112 {
0113 unsigned long long cookie;
0114 int ret;
0115
0116 ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid, PIDTYPE_PID,
0117 (unsigned long)&cookie);
0118 if (ret) {
0119 printf("Not a core sched system\n");
0120 return -1UL;
0121 }
0122
0123 return cookie;
0124 }
0125
0126 static int child_func_thread(void __attribute__((unused))*arg)
0127 {
0128 while (1)
0129 usleep(20000);
0130 return 0;
0131 }
0132
0133 static void create_threads(int num_threads, int thr_tids[])
0134 {
0135 void *child_stack;
0136 pid_t tid;
0137 int i;
0138
0139 for (i = 0; i < num_threads; ++i) {
0140 child_stack = malloc(STACK_SIZE);
0141 if (!child_stack)
0142 handle_error("child stack allocate");
0143
0144 tid = clone(child_func_thread, child_stack + STACK_SIZE, THREAD_CLONE_FLAGS, NULL);
0145 if (tid == -1)
0146 handle_error("clone thread");
0147 thr_tids[i] = tid;
0148 }
0149 }
0150
0151 static int child_func_process(void *arg)
0152 {
0153 struct child_args *ca = (struct child_args *)arg;
0154
0155 close(ca->pfd[0]);
0156
0157 create_threads(ca->num_threads, ca->thr_tids);
0158
0159 write(ca->pfd[1], &ca->thr_tids, sizeof(int) * ca->num_threads);
0160 close(ca->pfd[1]);
0161
0162 while (1)
0163 usleep(20000);
0164 return 0;
0165 }
0166
0167 static unsigned char child_func_process_stack[STACK_SIZE];
0168
0169 void create_processes(int num_processes, int num_threads, struct child_args proc[])
0170 {
0171 pid_t cpid;
0172 int i;
0173
0174 for (i = 0; i < num_processes; ++i) {
0175 proc[i].num_threads = num_threads;
0176
0177 if (pipe(proc[i].pfd) == -1)
0178 handle_error("pipe() failed");
0179
0180 cpid = clone(child_func_process, child_func_process_stack + STACK_SIZE,
0181 SIGCHLD, &proc[i]);
0182 proc[i].cpid = cpid;
0183 close(proc[i].pfd[1]);
0184 }
0185
0186 for (i = 0; i < num_processes; ++i) {
0187 read(proc[i].pfd[0], &proc[i].thr_tids, sizeof(int) * proc[i].num_threads);
0188 close(proc[i].pfd[0]);
0189 }
0190 }
0191
0192 void disp_processes(int num_processes, struct child_args proc[])
0193 {
0194 int i, j;
0195
0196 printf("tid=%d, / tgid=%d / pgid=%d: %lx\n", gettid(), getpid(), getpgid(0),
0197 get_cs_cookie(getpid()));
0198
0199 for (i = 0; i < num_processes; ++i) {
0200 printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].cpid, proc[i].cpid,
0201 getpgid(proc[i].cpid), get_cs_cookie(proc[i].cpid));
0202 for (j = 0; j < proc[i].num_threads; ++j) {
0203 printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].thr_tids[j],
0204 proc[i].cpid, getpgid(0), get_cs_cookie(proc[i].thr_tids[j]));
0205 }
0206 }
0207 puts("\n");
0208 }
0209
0210 static int errors;
0211
0212 #define validate(v) _validate(__LINE__, v, #v)
0213 void _validate(int line, int val, char *msg)
0214 {
0215 if (!val) {
0216 ++errors;
0217 printf("(%d) FAILED: %s\n", line, msg);
0218 } else {
0219 printf("(%d) PASSED: %s\n", line, msg);
0220 }
0221 }
0222
0223 int main(int argc, char *argv[])
0224 {
0225 int keypress = 0;
0226 int num_threads = 3;
0227 int delay = 0;
0228 int res = 0;
0229 int pidx;
0230 int pid;
0231 int opt;
0232
0233 while ((opt = getopt(argc, argv, ":hkT:P:d:")) != -1) {
0234 switch (opt) {
0235 case 'P':
0236 num_processes = (int)strtol(optarg, NULL, 10);
0237 break;
0238 case 'T':
0239 num_threads = (int)strtoul(optarg, NULL, 10);
0240 break;
0241 case 'd':
0242 delay = (int)strtol(optarg, NULL, 10);
0243 break;
0244 case 'k':
0245 keypress = 1;
0246 break;
0247 case 'h':
0248 printf(USAGE);
0249 exit(EXIT_SUCCESS);
0250 default:
0251 handle_usage(20, "unknown option");
0252 }
0253 }
0254
0255 if (num_processes < 1 || num_processes > MAX_PROCESSES)
0256 handle_usage(1, "Bad processes value");
0257
0258 if (num_threads < 1 || num_threads > MAX_THREADS)
0259 handle_usage(2, "Bad thread value");
0260
0261 if (keypress)
0262 delay = -1;
0263
0264 srand(time(NULL));
0265
0266
0267 if (setpgid(0, 0) != 0)
0268 handle_error("process group");
0269
0270 printf("\n## Create a thread/process/process group hiearchy\n");
0271 create_processes(num_processes, num_threads, procs);
0272 need_cleanup = 1;
0273 disp_processes(num_processes, procs);
0274 validate(get_cs_cookie(0) == 0);
0275
0276 printf("\n## Set a cookie on entire process group\n");
0277 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PGID, 0) < 0)
0278 handle_error("core_sched create failed -- PGID");
0279 disp_processes(num_processes, procs);
0280
0281 validate(get_cs_cookie(0) != 0);
0282
0283
0284 pidx = rand() % num_processes;
0285 pid = procs[pidx].cpid;
0286
0287 validate(get_cs_cookie(0) == get_cs_cookie(pid));
0288 validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
0289
0290 printf("\n## Set a new cookie on entire process/TGID [%d]\n", pid);
0291 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid, PIDTYPE_TGID, 0) < 0)
0292 handle_error("core_sched create failed -- TGID");
0293 disp_processes(num_processes, procs);
0294
0295 validate(get_cs_cookie(0) != get_cs_cookie(pid));
0296 validate(get_cs_cookie(pid) != 0);
0297 validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
0298
0299 printf("\n## Copy the cookie of current/PGID[%d], to pid [%d] as PIDTYPE_PID\n",
0300 getpid(), pid);
0301 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, pid, PIDTYPE_PID, 0) < 0)
0302 handle_error("core_sched share to itself failed -- PID");
0303 disp_processes(num_processes, procs);
0304
0305 validate(get_cs_cookie(0) == get_cs_cookie(pid));
0306 validate(get_cs_cookie(pid) != 0);
0307 validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
0308
0309 printf("\n## Copy cookie from a thread [%d] to current/PGID [%d] as PIDTYPE_PID\n",
0310 procs[pidx].thr_tids[0], getpid());
0311 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, procs[pidx].thr_tids[0],
0312 PIDTYPE_PID, 0) < 0)
0313 handle_error("core_sched share from thread failed -- PID");
0314 disp_processes(num_processes, procs);
0315
0316 validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
0317 validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
0318
0319 printf("\n## Copy cookie from current [%d] to current as pidtype PGID\n", getpid());
0320 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, 0, PIDTYPE_PGID, 0) < 0)
0321 handle_error("core_sched share to self failed -- PGID");
0322 disp_processes(num_processes, procs);
0323
0324 validate(get_cs_cookie(0) == get_cs_cookie(pid));
0325 validate(get_cs_cookie(pid) != 0);
0326 validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
0327
0328 if (errors) {
0329 printf("TESTS FAILED. errors: %d\n", errors);
0330 res = 10;
0331 } else {
0332 printf("SUCCESS !!!\n");
0333 }
0334
0335 if (keypress)
0336 getchar();
0337 else
0338 sleep(delay);
0339
0340 for (pidx = 0; pidx < num_processes; ++pidx)
0341 kill(procs[pidx].cpid, 15);
0342
0343 return res;
0344 }